Ejemplo n.º 1
0
def test_optimization():
    class ExampleOS(ClassWithOptimizableVariables):
        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))

    def testmerit(s):
        # let x, y being on a circle of radius 5
        return (s.X()**2 + s.Y()**2 - 5.**2)**2

    def testmerit2(s):
        # let x**2 + y**2 + z**2 be minimized with
        # the pickup constraint z = x**2 + y**2
        return s.X()**2 + s.Y()**2 + s.Z()**2

    os = ExampleOS()

    optimi = Optimizer(os,
                       testmerit,
                       backend=ScipyBackend(method="Nelder-Mead",
                                            options={
                                                "maxiter": 1000,
                                                "disp": False
                                            },
                                            name="scipybackend"),
                       name="optimizer")
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, 25.0)
    optimi.meritfunction = testmerit2
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, os.Z())
    optimi.backend = Newton1DBackend(dx=1e-6, iterations=500)
    optimi.meritfunction = testmerit
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, 25.0)
    optimi.meritfunction = testmerit2
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, os.Z())
Ejemplo n.º 2
0
def test_optimization():

    class ExampleOS(ClassWithOptimizableVariables):
        def __init__(self):
            super(ExampleOS, self).__init__()
            self.X = OptimizableVariable(name="X", status="Variable", value=3.0)
            self.Y = OptimizableVariable(name="Y", status="Variable", value=20.0)
            self.Z = OptimizableVariable(name="Z", status="Pickup", \
                                             function=lambda x, y: x**2 + y**2, \
                                             args=(self.X, self.Y))
    
    
    def testmerit(s):
        # let x, y being on a circle of radius 5
        return (s.X()**2 + s.Y()**2 - 5.**2)**2
        
            

    def testmerit2(s):
        # let x**2 + y**2 + z**2 be minimized with the pickup constraint z = x**2 + y**2
        return s.X()**2 + s.Y()**2 + s.Z()**2
        

    os = ExampleOS()

    optimi = Optimizer(os, testmerit, backend=ScipyBackend(method='Nelder-Mead', options={'maxiter':1000, 'disp':False}))
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, 25.0)
    optimi.meritfunction = testmerit2
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, os.Z())
    optimi.backend = Newton1DBackend(dx=1e-6, iterations=500)
    optimi.meritfunction = testmerit
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, 25.0)
    optimi.meritfunction = testmerit2
    optimi.run()
    assert np.isclose(os.X()**2 + os.Y()**2, os.Z())
Ejemplo n.º 3
0
    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)


# Step 3: colour, field, more variables
########################################

Ejemplo n.º 4
0
    rpaths = my_s.seqtrace(my_initialbundle, sysseq)

    x = rpaths[0].raybundles[-1].x[-1, 0, :]
    y = rpaths[0].raybundles[-1].x[-1, 1, :]

    xmean = np.mean(x)
    ymean = np.mean(y)

    res = np.sum((x - xmean)**2 + (y - ymean)**2) + 10. * math.exp(-len(x))

    return res


opt_backend = ScipyBackend(method='Nelder-Mead',
                           options={
                               'maxiter': 1000,
                               'disp': True
                           },
                           tol=1e-8)
if len(sys.argv) > 1:
    first_arg = sys.argv[1]

    if first_arg == "--help":
        print("p ... Particle Swarm (c1, c2)")
        print("s ... Simulated Annealing (Nt, Tt)")
        print("n ... Newtonian 1D (dx, iterations)")
        print("m ... Nelder-Mead")
        sys.exit(0)
    elif first_arg == "p":
        c1 = 2.2
        c2 = 2.1
        if len(sys.argv) == 4:
Ejemplo n.º 5
0
    """
    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)

# Step 3: colour, field, more variables
########################################

Ejemplo n.º 6
0
    rpaths = my_s.seqtrace(initialbundle_local, sysseq)
    # other constructions lead to fill up of initial bundle with intersection
    # values

    # for glassy asphere only one path necessary
    x = rpaths[0].raybundles[-1].x[-1, 0, :]
    y = rpaths[0].raybundles[-1].x[-1, 1, :]

    res = np.sum(x**2 + y**2)

    return res


backsurf = s.elements["stdelem"].surfaces["back"]
backsurf.shape.params["curv"].to_variable()
backsurf.shape.params["cc"].to_variable()
# A2 not variable
backsurf.shape.params["A4"].to_variable()
backsurf.shape.params["A6"].to_variable()

opt_backend = ScipyBackend(method='Nelder-Mead', tol=1e-9)
optimi = Optimizer(s,
                   meritfunctionrms,
                   opt_backend,
                   name="Nelder-Mead Optimizer")
s = optimi.run()

r2 = s.seqtrace(initialbundle, sysseq)

draw(s, r2)