Optimizer

class aido.optimizer.Optimizer(parameter_dict: SimulationParameterDictionary, device: str | None = None)[source]

Bases: Module

The optimizer uses the surrogate model to optimise the detector parameters in batches. It is also linked to a generator object, to check if the parameters are still in bounds using the function is_local(parameters) of the generator.

Once the parameters are not local anymore, the optimizer will return the last parameters that were local and stop. For this purpose, the surrogate model will need to be applied using fixed weights. Then the reconstruction model loss will be applied based on the surrogate model output. The gradient w.r.t. the detector parameters will be calculated and the parameters will be updated.

__init__(parameter_dict: SimulationParameterDictionary, device: str | None = None)[source]

Initializes the optimizer with the given surrogate model and parameters. :Parameters: * starting_parameter_dict (Dict) – A dictionary containing the initial parameters.

  • device (str) – Defaults to ‘cuda’

property boosted_parameter_dict: SimulationParameterDictionary

Compute a new set of parameters by taking the current parameter dict and boosting it along the direction of change between the previous and the current values (only continuous parameters).

Formula:

[ p_{n+1} = p_{opt} + frac{1}{2} left( p_{opt} - p_n right) ]

Where: - ( p_{n+1} ) is the updated parameter dict. - ( p_{opt} ) is the current (optimized) parameter dict. - ( p_n ) is the starting parameter dict.

property boundaries: Tensor

Adds penalties for parameters that are outside of the boundaries spaned by ‘self.parameter_box’. This ensures that the optimizer does not propose new values that are outside of the scope of the Surrogate and therefore largely unknown to the current iteration.

Returns:

torch.Tensor

check_parameters_are_local(updated_parameters: Tensor, scale=1.0) bool[source]

Assure that the predicted parameters by the optimizer are within the bounds of the covariance matrix spanned by the ‘sigma’ of each parameter.

optimize(surrogate_model: Surrogate, dataset: SurrogateDataset, batch_size: int, n_epochs: int, reconstruction_loss: Callable[[Tensor, Tensor], Tensor], additional_constraints: None | Callable[[SimulationParameterDictionary, Dict], Tensor] = None, parameter_optimizer_savepath: str | PathLike | None = None, device: str | None = None, lr: float = 0.01, wandb_logger: WandbLogger | None = None) Tuple[SimulationParameterDictionary, bool][source]

Perform the optimization step.

  1. The ParameterModule().forward() method generates new parameters.

  2. The Surrogate Model computes the corresponding Reconstruction Loss (based on its interpolation).

  3. The Optimizer Loss is the Sum of the Reconstruction Loss, user-defined Parameter Loss

    (e.g. cost constraints) and the Parameter Box Loss (which ensures that the Parameters stay within acceptable boundaries during training).

  4. The optimizer applies backprogation and updates the current ParameterDict

Returns:

SimulationParameterDictionary bool

other_constraints(constraints_func: None | Callable[[SimulationParameterDictionary, Dict], Tensor], parameter_dict_as_tensor: Dict[str, Tensor | Parameter]) Tensor[source]

Adds user-defined constraints defined in ‘interface.py:AIDOUserInterface.constraints()’. If no constraints were added manually, this method defaults to calculating constraints based on the cost per parameter specified in ParameterDict. Returns a float or torch.Tensor which can be considered as a penalty loss.

to(device: str | device, **kwargs) Optimizer[source]

Move all Tensors and modules to ‘device’.