Exemplo n.º 1
0
def test_variables_pickups_externals():
    """
    Check whether pickups are also working for strings
    """

    sum_fo = FunctionObject("f = lambda p, q: p + q", ["f"])

    p = OptimizableVariable("Variable", value="glass1")
    q = OptimizableVariable("Variable", value="glass2")
    r = OptimizableVariable("Pickup",
                            functionobject=(sum_fo, "f"),
                            args=(p, q))
    s = OptimizableVariable("External",
                            functionobject=(sum_fo, "f"),
                            args=(1.0, 6.0))

    assert p() == "glass1"
    assert q() == "glass2"
    assert r() == "glass1glass2"
    assert s() == 7.0

    p.setvalue("glass5")
    q.setvalue("glass6")

    assert r() == "glass5glass6"

    r.parameters["functionobject"] = (FunctionObject(
        "f = lambda x, y: x*3 + y*4", ["f"]), "f")

    assert r() == "glass5glass5glass5glass6glass6glass6glass6"
Exemplo n.º 2
0
    def __init__(self, name, initialsrc, doc, group):
        self.Document = doc  # e.g. ActiveDocument
        self.Group = group  # functions group
        mylabel = self.returnStructureLabel(name)
        self.CoreFunctionObject = FunctionObject(initialsrc, name=mylabel)

        self.Object = doc.addObject("App::FeaturePython", mylabel)
        self.Group.addObject(self.Object)
        self.Object.addProperty("App::PropertyStringList", "functions",
                                "FunctionObject",
                                "functions in object").functions = []
        self.Object.addProperty("App::PropertyBool", "trusted",
                                "FunctionObject", "trusted?").trusted = True

        self.Object.Proxy = self
Exemplo n.º 3
0
 def __init__(self):
     super(ExampleOS, self).__init__()
     self.X = OptimizableVariable(VariableState(3.0), name="X")
     self.Y = OptimizableVariable(VariableState(20.0), name="Y")
     self.Z = OptimizableVariable(PickupState(
         (FunctionObject("f = lambda x, y: x**2 + y**2", ["f"]), "f"),
         (self.X, self.Y)),
                                  name="Z")
Exemplo n.º 4
0
class FunctionsObject(NotSerializable):
    def __init__(self, name, initialsrc, doc, group):
        self.Document = doc  # e.g. ActiveDocument
        self.Group = group  # functions group
        mylabel = self.returnStructureLabel(name)
        self.CoreFunctionObject = FunctionObject(initialsrc, name=mylabel)

        self.Object = doc.addObject("App::FeaturePython", mylabel)
        self.Group.addObject(self.Object)
        self.Object.addProperty("App::PropertyStringList", "functions",
                                "FunctionObject",
                                "functions in object").functions = []
        self.Object.addProperty("App::PropertyBool", "trusted",
                                "FunctionObject", "trusted?").trusted = True

        self.Object.Proxy = self

    def returnFunctionObjects(self):
        self.CoreFunctionObject.generate_functions_from_source(
            self.Object.functions)
        return self.CoreFunctionObject.functions

    def returnSingleFunctionObject(self, name):
        self.CoreFunctionObject.generate_functions_from_source([name])
        result = self.CoreFunctionObject.functions.get(name, None)
        # if dictionary is not filled (due to security issues), return None

        return result

    def trust(self):
        self.CoreFunctionObject.sourcecode_security_checked = True
        self.CoreFunctionObject.globals_security_checked = True

    def untrust(self):
        self.CoreFunctionObject.sourcecode_security_checked = False
        self.CoreFunctionObject.globals_security_checked = False

    def returnStructureLabel(self, name):
        return "function_" + name

    def onChanged(self, fp, prop):
        if prop == "trusted":
            if self.Object.trusted:
                self.trust()
            else:
                self.untrust()
Exemplo n.º 5
0
 def __init__(self):
     super(ExampleOS, self).__init__()
     self.X = OptimizableVariable(name="X",
                                  variable_type="variable",
                                  value=3.0)
     self.Y = OptimizableVariable(name="Y",
                                  variable_type="variable",
                                  value=20.0)
     self.Z = OptimizableVariable(name="Z",
                                  variable_type="pickup",
                                  functionobject=(FunctionObject(
                                      "f = lambda x, y: x**2 + y**2",
                                      ["f"]), "f"),
                                  args=(self.X, self.Y))
Exemplo n.º 6
0
def test_variables_pickups_externals():
    """
    Check whether pickups are also working for strings
    """

    sum_fo = FunctionObject("f = lambda p, q: p + q", ["f"])

    p = OptimizableVariable(VariableState("glass1"))
    q = OptimizableVariable(VariableState("glass2"))
    r = OptimizableVariable(PickupState((sum_fo, "f"), (p, q)))

    assert p() == "glass1"
    assert q() == "glass2"
    assert r() == "glass1glass2"

    p.set_value("glass5")
    q.set_value("glass6")

    assert r() == "glass5glass6"

    r._state.parameters["functionobject"] = (FunctionObject(
        "f = lambda x, y: x*3 + y*4", ["f"]), "f")

    assert r() == "glass5glass5glass5glass6glass6glass6glass6"
Exemplo n.º 7
0
    return merit


def updatefunction_allsteps(my_s):
    """
    Update coordinate systems of optical system.
    """
    my_s.rootcoordinatesystem.update()


# optimize
s.elements["stdelem"].surfaces["lens4front"].shape.curvature.to_variable()
s.elements["stdelem"].surfaces["lens4rear"].shape.curvature.to_variable()
s.elements["stdelem"].surfaces["lens4rear"].shape.curvature.\
    to_pickup((FunctionObject("f = lambda x: -x"), "f"),
              (s.elements["stdelem"].surfaces["lens4front"].shape.curvature,))


listOptimizableVariables(s, max_line_width=80)

optimi = Optimizer(s, meritfunction_step2, backend=ScipyBackend(),
                   updatefunction=updatefunction_allsteps, name="step2")
optimi.meritparameters["initialbundle"] = initialbundle_step1
s = optimi.run()

# draw system with last lens biconvex
s.draw2d(axarr[1], color="grey", vertices=50, plane_normal=pn, up=up)
r2 = s.seqtrace(bundle_step1(nrays=9), seq)  # trace again
for r in r2:
    r.draw2d(axarr[1], color="blue", plane_normal=pn, up=up)
Exemplo n.º 8
0
def updatefunction_allsteps(my_s):
    """
    Update coordinate systems of optical system.
    """
    my_s.rootcoordinatesystem.update()


# optimize
s.elements["stdelem"].surfaces["lens4front"].shape.curvature.\
    changetype("variable")
s.elements["stdelem"].surfaces["lens4rear"].shape.curvature.\
    changetype("variable")
s.elements["stdelem"].surfaces["lens4rear"].shape.curvature.\
    changetype("pickup",
               functionobject=(FunctionObject("f = lambda x: -x"), "f"),
               args=(s.elements["stdelem"].surfaces["lens4front"].shape.curvature,))

listOptimizableVariables(s, max_line_width=80)

optimi = Optimizer(s,
                   meritfunction_step2,
                   backend=ScipyBackend(),
                   updatefunction=updatefunction_allsteps,
                   name="step2")
s = optimi.run()

# draw system with last lens biconvex
s.draw2d(axarr[1], color="grey", vertices=50, plane_normal=pn, up=up)
r2 = s.seqtrace(bundle_step1(nrays=9), seq)  # trace again
for r in r2: