Constrained Optimization#

[1]:
import numpy as np

from optiland import optic, optimization

Define a starting lens:

[2]:
lens = optic.Optic()

# add surfaces
lens.surfaces.add(index=0, thickness=np.inf)
lens.surfaces.add(index=1, thickness=7, radius=50, material="N-KF9", is_stop=True)
lens.surfaces.add(index=2, thickness=30, radius=-1000)
lens.surfaces.add(index=3)

# set aperture
lens.set_aperture(aperture_type="EPD", value=15)

# add field
lens.fields.set_type(field_type="angle")
lens.fields.add(y=0)
lens.fields.add(y=5)

# add wavelength
lens.wavelengths.add(value=0.55, is_primary=True)

# draw lens
lens.draw(num_rays=5)
../../_images/gallery_optimization_constrained_3_0.png
[2]:
(<Figure size 1000x400 with 1 Axes>, <Axes: xlabel='Z [mm]', ylabel='Y [mm]'>)

Define optimization problem:

[3]:
problem = optimization.OptimizationProblem()

Add operands (targets for optimization):

[4]:
# 1. add focal length operand
input_data = {"optic": lens}
problem.add_operand(operand_type="f2", target=50, weight=1, input_data=input_data)

# 2. add seidel coefficient 1 operand
input_data = {"optic": lens, "seidel_number": 1}
problem.add_operand(operand_type="seidel", target=0, weight=1, input_data=input_data)

# 3. add RMS spot size operand
input_data = {
    "optic": lens,
    "surface_number": -1,
    "Hx": 0,
    "Hy": 0,
    "num_rays": 5,
    "wavelength": 0.55,
    "distribution": "hexapolar",
}

problem.add_operand(
    operand_type="rms_spot_size",
    target=0,
    weight=1,
    input_data=input_data,
)

Define variables - constrain first radius:

[5]:
problem.add_variable(lens, "radius", surface_number=1, min_val=25, max_val=100)
problem.add_variable(lens, "radius", surface_number=2)

Let thickness to image surface vary:

[6]:
problem.add_variable(lens, "thickness", surface_number=2)

Check initial merit function value and system properties:

[7]:
problem.info()
╒════╤════════════════════════╤═══════════════════╕
│    │   Merit Function Value │   Improvement (%) │
╞════╪════════════════════════╪═══════════════════╡
│  0 │                1677.84 │                 0 │
╘════╧════════════════════════╧═══════════════════╛
╒════╤════════════════╤══════════╤══════════════╤══════════════╤══════════╤═══════════════╤═════════╤═════════╤════════════════╕
│    │ Operand Type   │   Target │ Min. Bound   │ Max. Bound   │   Weight │   Eff. Weight │   Value │   Delta │   Contrib. [%] │
╞════╪════════════════╪══════════╪══════════════╪══════════════╪══════════╪═══════════════╪═════════╪═════════╪════════════════╡
│  0 │ f2             │       50 │              │              │        1 │             1 │  90.804 │  40.804 │          99.23 │
│  1 │ seidel         │        0 │              │              │        1 │             1 │  -0.009 │  -0.009 │           0    │
│  2 │ rms spot size  │        0 │              │              │        1 │             1 │   3.589 │   3.589 │           0.77 │
╘════╧════════════════╧══════════╧══════════════╧══════════════╧══════════╧═══════════════╧═════════╧═════════╧════════════════╛
╒════╤═════════════════╤═══════════╤═════════╤══════════════╤══════════════╕
│    │ Variable Type   │   Surface │   Value │   Min. Bound │   Max. Bound │
╞════╪═════════════════╪═══════════╪═════════╪══════════════╪══════════════╡
│  0 │ radius          │         1 │      50 │           25 │          100 │
│  1 │ radius          │         2 │   -1000 │          nan │          nan │
│  2 │ thickness       │         2 │      30 │          nan │          nan │
╘════╧═════════════════╧═══════════╧═════════╧══════════════╧══════════════╛

Define optimizer:

[8]:
optimizer = optimization.OptimizerGeneric(problem)

Run optimization:

[9]:
optimizer.optimize(tol=1e-6)
[9]:
  message: CONVERGENCE: RELATIVE REDUCTION OF F <= FACTR*EPSMCH
  success: True
   status: 0
      fun: 0.004351740952554544
        x: [-7.417e-01  1.271e+01  3.440e+00]
      nit: 82
      jac: [ 4.890e-01 -1.761e-04  7.788e-03]
     nfev: 408
     njev: 102
 hess_inv: <3x3 LbfgsInvHessProduct with dtype=float64>

Print merit function value and system properties after optimization:

[10]:
problem.info()
╒════╤════════════════════════╤═══════════════════╕
│    │   Merit Function Value │   Improvement (%) │
╞════╪════════════════════════╪═══════════════════╡
│  0 │             0.00435174 │           99.9997 │
╘════╧════════════════════════╧═══════════════════╛
╒════╤════════════════╤══════════╤══════════════╤══════════════╤══════════╤═══════════════╤═════════╤═════════╤════════════════╕
│    │ Operand Type   │   Target │ Min. Bound   │ Max. Bound   │   Weight │   Eff. Weight │   Value │   Delta │   Contrib. [%] │
╞════╪════════════════╪══════════╪══════════════╪══════════════╪══════════╪═══════════════╪═════════╪═════════╪════════════════╡
│  0 │ f2             │       50 │              │              │        1 │             1 │  50.002 │   0.002 │           0.08 │
│  1 │ seidel         │        0 │              │              │        1 │             1 │  -0.054 │  -0.054 │          65.83 │
│  2 │ rms spot size  │        0 │              │              │        1 │             1 │   0.039 │   0.039 │          34.09 │
╘════╧════════════════╧══════════╧══════════════╧══════════════╧══════════╧═══════════════╧═════════╧═════════╧════════════════╛
╒════╤═════════════════╤═══════════╤═══════════╤══════════════╤══════════════╕
│    │ Variable Type   │   Surface │     Value │   Min. Bound │   Max. Bound │
╞════╪═════════════════╪═══════════╪═══════════╪══════════════╪══════════════╡
│  0 │ radius          │         1 │   25.833  │           25 │          100 │
│  1 │ radius          │         2 │ 1370.51   │          nan │          nan │
│  2 │ thickness       │         2 │   44.3981 │          nan │          nan │
╘════╧═════════════════╧═══════════╧═══════════╧══════════════╧══════════════╛

Draw final lens:

[11]:
lens.draw(num_rays=5)
../../_images/gallery_optimization_constrained_21_0.png
[11]:
(<Figure size 1000x400 with 1 Axes>, <Axes: xlabel='Z [mm]', ylabel='Y [mm]'>)

Hard constraints#

Above, the radius of the first surface is bounded, and bounds are handled by the optimizer. For other conditions, such as a focal length that must be met exactly, or a minimum thickness, a hard constraint is stronger than a merit-function term: the optimizer must satisfy it, and it is not traded off against image quality. Only the native "dls" optimizer enforces hard constraints. See Default Merit and Constraints for details.

We rebuild the starting lens and require an exact focal length of 50 mm, minimize the RMS spot size, and keep the glass between 5 and 12 mm thick.

[12]:
lens = optic.Optic()

lens.surfaces.add(index=0, thickness=np.inf)
lens.surfaces.add(index=1, thickness=7, radius=50, material="N-KF9", is_stop=True)
lens.surfaces.add(index=2, thickness=30, radius=-1000)
lens.surfaces.add(index=3)

lens.set_aperture(aperture_type="EPD", value=15)
lens.fields.set_type(field_type="angle")
lens.fields.add(y=0)
lens.fields.add(y=5)
lens.wavelengths.add(value=0.55, is_primary=True)

problem = optimization.OptimizationProblem()

# merit: RMS spot size on a few quadrature rays for each field
problem.add_default_merit(lens, kind="spot", rings=3, arms=6)

# hard constraints: they are not part of the merit
problem.add_constraint("f2", target=50.0, input_data={"optic": lens})
problem.add_constraint(
    "center_thickness", min_val=5.0, max_val=12.0, input_data={"optic": lens, "surface_number": 1}
)

problem.add_variable(lens, "radius", surface_number=1, min_val=25, max_val=100)
problem.add_variable(lens, "radius", surface_number=2)
problem.add_variable(lens, "thickness", surface_number=1)
problem.add_variable(lens, "thickness", surface_number=2)

problem.constraint_info()
╒════╤═══════════════════════════╤════════════╤═══════════════╤════════════╤═════════════╤════════════╕
│    │ Constraint                │ Kind       │   Eff. Weight │   Residual │   Violation │ Feasible   │
╞════╪═══════════════════════════╪════════════╪═══════════════╪════════════╪═════════════╪════════════╡
│  0 │ #0 f2 == 50               │ equality   │             1 │       40.8 │        40.8 │ False      │
│  1 │ #1 center_thickness >= 5  │ inequality │             1 │       -2   │         0   │ True       │
│  2 │ #2 center_thickness <= 12 │ inequality │             1 │       -5   │         0   │ True       │
╘════╧═══════════════════════════╧════════════╧═══════════════╧════════════╧═════════════╧════════════╛

Run the native damped least squares. minimize changes the lens in place.

[13]:
result = optimization.minimize(problem, "dls")

print(result.stop_reason)
print(
    f"success: {result.success}, "
    f"max constraint violation: {result.max_constraint_violation:.1e}"
)
problem.constraint_info()
`ftol` termination condition is satisfied.
success: True, max constraint violation: 9.6e-09
╒════╤═══════════════════════════╤════════════╤═══════════════╤════════════╤═════════════╤════════════╕
│    │ Constraint                │ Kind       │   Eff. Weight │   Residual │   Violation │ Feasible   │
╞════╪═══════════════════════════╪════════════╪═══════════════╪════════════╪═════════════╪════════════╡
│  0 │ #0 f2 == 50               │ equality   │             1 │  9.614e-09 │   9.614e-09 │ True       │
│  1 │ #1 center_thickness >= 5  │ inequality │             1 │ -7         │   0         │ True       │
│  2 │ #2 center_thickness <= 12 │ inequality │             1 │  1.776e-15 │   1.776e-15 │ True       │
╘════╧═══════════════════════════╧════════════╧═══════════════╧════════════╧═════════════╧════════════╛

The focal length is met to numerical precision and the lens thickness is within its limits; the optimizer used the remaining freedom to reduce the spot size.

[14]:
lens.draw(num_rays=5)
../../_images/gallery_optimization_constrained_27_0.png
[14]:
(<Figure size 1000x400 with 1 Axes>, <Axes: xlabel='Z [mm]', ylabel='Y [mm]'>)