optimization.problem#
Optimization Problem Module
This module contains the OptimizationProblem class, which represents an optimization problem. The class allows for the addition of operands and variables to the merit function, and provides methods to evaluate the merit function and print information about the optimization problem.
Kramer Harrison, 2025
Classes
|
Represents an optimization problem. |
- class OptimizationProblem(batching: bool = True)[source]#
Represents an optimization problem.
- Variables:
- Methods:
add_operand – Add an operand to the merit function.
add_variable – Add a variable to the merit function.
fun_array – Array of operand weighted deltas squared, where the delta is the difference between the current and target value.
sum_squared – Sum of squared operand weighted deltas.
rss – Root Sum of Squares (RSS) of the current merit function.
operand_info – Print information about the operands in the merit function.
variable_info – Print information about the variables in the merit function.
info – Print information about the merit function, including operand and variable info.
- add_operand(operand_type=None, target=None, min_val=None, max_val=None, weight=1, input_data=None)[source]#
Add an operand to the merit function
- add_variable(optic, variable_type, scaler: Scaler = None, **kwargs)[source]#
Add a variable to the merit function
- enable_batching()[source]#
Enable batched ray evaluation for faster optimization.
When batching is enabled, operands that require ray tracing are grouped by optic and wavelength so that redundant traces are eliminated. This can dramatically speed up merit-function evaluation for problems with many ray operands.
The evaluator is re-created whenever this method is called, so it always reflects the current set of operands.
- fun_array()[source]#
Array of operand contribution terms for the merit function.
Each term is computed as:
(effective_weight(op) * op.delta()) ** 2
where
effective_weight = operand.weight * field_weight * wl_weight. Field and wavelength weights are read from the optic stored in each operand’sinput_data. Operands with an effective weight of zero are excluded from the result.When batching is enabled, delegates to the
BatchedRayEvaluatorwhich minimises redundant ray traces.- Returns:
1-D array of per-operand contribution values. Returns
[0.0]when there are no active operands.- Return type:
be.ndarray
- residual_vector()[source]#
Vector of weighted operand deltas (unsquared).
Returns a 1-D array whose i-th element is
weight_i * delta_ifor each operand. This is the residual vector r needed by least-squares algorithms such as the Damped Least-Squares (Levenberg-Marquardt) optimizer.Unlike
fun_array(), the values are not squared, so the merit function equalssum(residual_vector() ** 2).When batching is enabled, delegates to the
BatchedRayEvaluatorwhich minimises redundant ray traces.- Returns:
A 1-D array of length
len(self.operands).- Return type:
be.ndarray
- sum_squared()[source]#
Calculate the sum of squared operand weighted deltas.
When batching is enabled, delegates to the
BatchedRayEvaluatorwhich minimises redundant ray traces.
- weight_breakdown() list[dict][source]#
Return a list of dicts describing each operand’s effective weight.
The effective weight is the product of the operand’s own weight, the field weight (looked up from the optic via the operand’s
input_data), and the wavelength weight. The formula used in the merit function is:(effective_weight × delta) ** 2
Each returned dict contains:
operand_type(str): The operand type string.field: The field index or coordinate frominput_data(or None).wavelength: The wavelength index or value frominput_data(or None).operand_weight(float): The user-setOperand.weight.field_weight(float): The field’s weight from the optic (1.0 if not resolvable).wl_weight(float): The wavelength’s weight from the optic (1.0 if not resolvable).effective_weight(float): Product of the three weights above.