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

OptimizationProblem([batching])

Represents an optimization problem.

class OptimizationProblem(batching: bool = True)[source]

Represents an optimization problem.

operands

List of operands in the merit function.

Type:

list

variables

List of variables in the merit function.

Type:

list

initial_value

Initial value of the merit function.

Type:

float

add_operand()[source]

Add an operand to the merit function.

add_variable()[source]

Add a variable to the merit function.

fun_array()[source]

Array of operand weighted deltas squared, where the delta is the difference between the current and target value.

sum_squared()[source]

Sum of squared operand weighted deltas.

rss()[source]

Root Sum of Squares (RSS) of the current merit function.

operand_info()[source]

Print information about the operands in the merit function.

variable_info()[source]

Print information about the variables in the merit function.

info()[source]

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

property batching_enabled: bool

Whether batched evaluation is currently active.

clear_operands()[source]

Clear all operands from the merit function

clear_variables()[source]

Clear all variables from the merit function

disable_batching()[source]

Disable batched ray evaluation and use standard per-operand evaluation.

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’s input_data. Operands with an effective weight of zero are excluded from the result.

When batching is enabled, delegates to the BatchedRayEvaluator which 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

info()[source]

Print information about the optimization problem.

merit_info()[source]

Print information about the merit function.

operand_info()[source]

Print information about the operands in the merit function

residual_vector()[source]

Vector of weighted operand deltas (unsquared).

Returns a 1-D array whose i-th element is weight_i * delta_i for 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 equals sum(residual_vector() ** 2).

When batching is enabled, delegates to the BatchedRayEvaluator which minimises redundant ray traces.

Returns:

A 1-D array of length len(self.operands).

Return type:

be.ndarray

rss()[source]

RSS of current merit function

sum_squared()[source]

Calculate the sum of squared operand weighted deltas.

When batching is enabled, delegates to the BatchedRayEvaluator which minimises redundant ray traces.

update_optics()[source]

Update all optics considered in the optimization problem

variable_info()[source]

Print information about the variables in the merit function.

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 from input_data (or None).

  • wavelength: The wavelength index or value from input_data (or None).

  • operand_weight (float): The user-set Operand.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.

Returns:

One dict per operand in self.operands.

Return type:

list[dict]