Optimize

The Merit Function Editor, the Optimization Wizard, and local optimization.

Merit Function Editor

Merit Function Editor tools.

calculate_merit_function(ctx: Context) → dict[str, object]

Calculate the merit function and return its value and the updated operands.

Returns:

  • merit – Merit function value. None when OpticStudio does not return a finite value.

  • operands – One entry per operand, in editor order, after the calculation. The fields of each operand are the same as those returned by get_merit_function(). value and contribution are the calculated numbers.

Return type:

dict[str, object]

Raises:

ValueError – The merit function cannot be calculated.

get_merit_function(ctx: Context, operand: int | None = None) → dict[str, object]

Return the Merit Function Editor operands for the lens open in the OpticStudio window.

Pass operand to read one operand. Omit it to read every operand.

Parameters:

operand – Operand number. The first operand is 1. None reads every operand.

Returns:

Without operand, operands. With operand, that operand.

  • operands – One entry per operand, in editor order. Present when operand is omitted.

  • number – Operand number. The first operand is 1. This member is read-only.

  • active – True when the operand is active. This member is read-only.

  • type – Operand type, such as EFFL.

  • parameters – Data columns used by type. Empty when the type uses none.

  • parameters[].column – Column name, such as Param1.

  • parameters[].header – Column title from the Merit Function Editor. This member is read-only.

  • parameters[].value – Cell value. A number, or a string when the cell stores text.

  • target – Target value. Omitted when the operand does not use it.

  • weight – Operand weight. Omitted when the operand does not use it.

  • value – Operand value from the last calculation. 0 until calculate_merit_function() runs. This member is read-only. Omitted when the operand does not use it.

  • contribution – Contribution from the last calculation. 0 until calculate_merit_function() runs. This member is read-only. Omitted when the operand does not use it.

  • comment – Operand comment. Empty when the operand has no comment.

Return type:

dict[str, object]

Raises:

ValueError – operand is outside the editor.

set_merit_rows(ctx: Context, action: 'change' | 'add' | 'remove', number: int, values: dict[str, str | float | bool | int | dict[str, str | float | int]] | None = None) → dict[str, object]

Change, insert, or remove one Merit Function Editor operand and return the editor.

Parameters:
  • action – change, add, or remove.

  • number – Operand number. The first operand is 1. add inserts at this number, and following operands move up. The inserted operand keeps the editor default until values sets type. Pass number one past the last operand to append.

  • values – Keys to write. The fields are the same as those of one operand returned by get_merit_function(). Omit values to insert a default operand or to remove an operand. number, active, value, and contribution are read-only. type is an operand type name, such as EFFL. parameters maps column names such as Param1 to a number or a string; only listed columns are written, and header is read-only. type is applied before parameters. target and weight are rejected when the operand does not use them.

Returns:

The operands after the change.

Return type:

dict[str, object]

Raises:

ValueError – The action, number, or values cannot be applied.

Optimization Wizard

Optimization Wizard tool.

apply_optimization_wizard(ctx: Context, values: OptimizationWizardSettings | None = None) → dict[str, object]

Apply the Optimization Wizard and return the settings and operands.

The wizard is reset to its dialog defaults. Keys in values then replace those defaults. Omit values to keep every default. Operands from start_at through the end of the editor are replaced.

Parameters:

values – Wizard controls to change. Omit it to use the dialog defaults. A glass or air thickness turns that boundary on when its checkbox is omitted. configuration selects one configuration, and field selects one field, when the matching “all” checkbox is omitted.

Returns:

  • settings – Wizard controls that were applied, including defaults that values did not change.

  • settings.criterion – Wavefront, Contrast, Spot for spot radius, or Angular for angular radius.

  • settings.type – RMS or PTV.

  • settings.reference – Centroid, ChiefRay, or Unreferenced.

  • settings.spatial_frequency – Spatial frequency used when criterion is Contrast. The unit follows the lens MTF units.

  • settings.xs_weight – XS contrast weight.

  • settings.yt_weight – YT contrast weight.

  • settings.pupil_integration – GaussianQuadrature or RectangularArray. This selects the pupil sampling. Setting one turns the other off.

  • settings.rings – Ring count for Gaussian quadrature. An integer of at least 1.

  • settings.arms – Arm count for Gaussian quadrature: 6, 8, 10, or 12.

  • settings.obscuration – Pupil obscuration for Gaussian quadrature, as shown in the wizard.

  • settings.grid_size – Points along one side of the rectangular sampling grid. An integer of at least 1. Used when pupil_integration is RectangularArray.

  • settings.delete_vignetted – True when vignetted rays are deleted.

  • settings.use_glass_boundaries – True when glass boundary operands are added.

  • settings.glass_min – Minimum glass thickness, in lens units.

  • settings.glass_max – Maximum glass thickness, in lens units.

  • settings.glass_edge – Minimum glass edge thickness, in lens units.

  • settings.use_air_boundaries – True when air boundary operands are added.

  • settings.air_min – Minimum air thickness, in lens units.

  • settings.air_max – Maximum air thickness, in lens units.

  • settings.air_edge – Minimum air edge thickness, in lens units.

  • settings.start_at – 1-based operand row where the new operands begin.

  • settings.overall_weight – Weight applied to the generated operands.

  • settings.use_all_configurations – True when operands are built for every configuration.

  • settings.configuration – 1-based configuration number used when use_all_configurations is false.

  • settings.use_all_fields – True when operands are built for every field.

  • settings.field – 1-based field number used when use_all_fields is false.

  • settings.assume_axial_symmetry – True when the wizard assumes axial symmetry.

  • settings.ignore_lateral_color – True when lateral color is ignored.

  • settings.add_favorite_operands – True when favorite operands are added.

  • settings.optimize_for_best_nominal_performance – True when the wizard optimizes the nominal design.

  • settings.optimize_for_manufacturing_yield – True when manufacturing yield is included.

  • settings.manufacturing_yield_weight – Weight of the manufacturing-yield target.

  • settings.use_maximum_distortion – True when distortion is limited.

  • settings.maximum_distortion – Maximum distortion, in percent.

  • operands – One entry per operand after the wizard runs. The fields of each operand are the same as those returned by get_merit_function(). value and contribution are not calculated.

Return type:

dict[str, object]

Raises:

ValueError – A wizard setting cannot be applied.

Local optimization

Local optimization tool.

run_local_optimization(ctx: Context, algorithm: 'DampedLeastSquares' | 'OrthogonalDescent' = 'DampedLeastSquares', cycles: 'Automatic' | 'Fixed_1_Cycle' | 'Fixed_5_Cycles' | 'Fixed_10_Cycles' | 'Fixed_50_Cycles' = 'Automatic') → dict[str, object]

Run local optimization on the lens open in the OpticStudio window.

The call waits until the run finishes. The core count stays at the value already set in the Optimize dialog.

Parameters:
  • algorithm – DampedLeastSquares or OrthogonalDescent.

  • cycles – Automatic, Fixed_1_Cycle, Fixed_5_Cycles, Fixed_10_Cycles, or Fixed_50_Cycles.

Returns:

  • algorithm – Algorithm that ran.

  • cycles – Cycle count that ran.

  • variables – Number of variables, as shown in the Optimize dialog.

  • targets – Number of targets, as shown in the Optimize dialog.

  • initial_merit – Merit function value before the run. None when OpticStudio does not return a finite value.

  • merit – Merit function value after the run. None when OpticStudio does not return a finite value.

Return type:

dict[str, object]

Raises:

ValueError – The algorithm or cycles cannot be used, the lens has no variable or no target, or the run does not finish.