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.
Nonewhen 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().valueandcontributionare the calculated numbers.
- Return type:
- 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
operandto read one operand. Omit it to read every operand.- Parameters:
operand – Operand number. The first operand is 1.
Nonereads every operand.- Returns:
Without
operand,operands. Withoperand, that operand.operands – One entry per operand, in editor order. Present when
operandis 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:
- Raises:
ValueError –
operandis 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, orremove.number – Operand number. The first operand is 1.
addinserts at this number, and following operands move up. The inserted operand keeps the editor default untilvaluessetstype. Passnumberone 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(). Omitvaluesto insert a default operand or to remove an operand.number,active,value, andcontributionare read-only.typeis an operand type name, such asEFFL.parametersmaps column names such asParam1to a number or a string; only listed columns are written, andheaderis read-only.typeis applied beforeparameters.targetandweightare rejected when the operand does not use them.
- Returns:
The operands after the change.
- Return type:
- 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
valuesthen replace those defaults. Omitvaluesto keep every default. Operands fromstart_atthrough 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.
configurationselects one configuration, andfieldselects one field, when the matching “all” checkbox is omitted.- Returns:
settings – Wizard controls that were applied, including defaults that
valuesdid not change.settings.criterion –
Wavefront,Contrast,Spotfor spot radius, orAngularfor angular radius.settings.type –
RMSorPTV.settings.reference –
Centroid,ChiefRay, orUnreferenced.settings.spatial_frequency – Spatial frequency used when
criterionisContrast. The unit follows the lens MTF units.settings.xs_weight – XS contrast weight.
settings.yt_weight – YT contrast weight.
settings.pupil_integration –
GaussianQuadratureorRectangularArray. 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, or12.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_integrationisRectangularArray.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_configurationsis false.settings.use_all_fields – True when operands are built for every field.
settings.field – 1-based field number used when
use_all_fieldsis 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().valueandcontributionare not calculated.
- Return type:
- 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 –
DampedLeastSquaresorOrthogonalDescent.cycles –
Automatic,Fixed_1_Cycle,Fixed_5_Cycles,Fixed_10_Cycles, orFixed_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.
Nonewhen OpticStudio does not return a finite value.merit – Merit function value after the run.
Nonewhen OpticStudio does not return a finite value.
- Return type:
- Raises:
ValueError – The algorithm or cycles cannot be used, the lens has no variable or no target, or the run does not finish.