optimisation.algorithms.algorithm module

Define the Abstract Base Class of optimisation algorithms.

Abstract methods are mandatory and a TypeError will be raised if you try to create your own algorithm and omit them.

When you add you own optimisation algorithm, do not forget to add it to the list of implemented algorithms in the config.optimisation.algorithm.

Todo

Check if it is necessary to pass out the whole elts to OptimisationAlgorithm?

Todo

Methods and flags to keep the optimisation history or not, and also to save it or not. See Explorator.

Todo

Better handling of the attribute folder. In particular, a correct value should be set at the OptimisationAlgorithm instanciation.

class OptiInfo

Bases: TypedDict

Hold information on how optimisation went.

hist_F: ndarray
hist_G: ndarray
hist_X: ndarray
class OptiSol

Bases: TypedDict

Hold information on the solution.

x: ndarray
class OptimisationAlgorithm(compensating_elements: Collection[Element], elts: ListOfElements, objectives: Collection[Objective], variables: Collection[Variable], compute_beam_propagation: Callable[[SetOfCavitySettings], SimulationOutput], compute_residuals: Callable[[SimulationOutput], Any], cavity_settings_factory: CavitySettingsFactory, constraints: Collection[Constraint] | None = None, compute_constraints: Callable[[SimulationOutput], ndarray] | None = None, folder: Path | None = None, optimisation_algorithm_kwargs: dict[str, Any] | None = None, **kwargs)

Bases: ABC

Holds the optimisation parameters, the methods to optimize.

Parameters:
  • compensating_elements (list[Element]) – Cavity objects used to compensate for the faults.

  • elts (ListOfElements) – Holds the whole compensation zone under study.

  • objectives (list[Objective]) – Holds objectives, initial values, bounds.

  • variables (list[Variable]) – Holds variables, their initial values, their limits.

  • constraints (list[Constraint] | None, optional) – Holds constraints and their limits. The default is None.

  • solution (dict) – Holds information on the solution that was found.

  • supports_constraints (bool) – If the method handles constraints or not.

  • compute_beam_propagation (ComputeBeamPropagationT) – Method to compute propagation of the beam with the given settings. Defined by a BeamCalculator.run_with_this() method, the positional argument elts being set by a functools.partial().

  • compute_residuals (ComputeResidualsT) – Method to compute residuals from a SimulationOutput.

  • compute_constraints (ComputeConstraintsT | None, optional) – Method to compute constraint violation. The default is None.

  • folder (str | None, optional) – Where history, phase space and other optimisation information will be saved if necessary. The default is None.

  • cavity_settings_factory (ICavitySettingsFactory) – A factory to easily create the cavity settings to try at each iteration of the optimisation algorithm.

__init__(compensating_elements: Collection[Element], elts: ListOfElements, objectives: Collection[Objective], variables: Collection[Variable], compute_beam_propagation: Callable[[SetOfCavitySettings], SimulationOutput], compute_residuals: Callable[[SimulationOutput], Any], cavity_settings_factory: CavitySettingsFactory, constraints: Collection[Constraint] | None = None, compute_constraints: Callable[[SimulationOutput], ndarray] | None = None, folder: Path | None = None, optimisation_algorithm_kwargs: dict[str, Any] | None = None, **kwargs) None

Instantiate the object.

_abc_impl = <_abc._abc_data object>
_create_set_of_cavity_settings(var: ndarray, status: str = 'compensate (in progress)') SetOfCavitySettings

Transform var into generic SetOfCavitySettings.

Parameters:
  • var – An array holding the variables to try.

  • status (str, optional) – mmmh

Returns:

Object holding the settings of all the cavities.

Return type:

SetOfCavitySettings

property _default_kwargs: dict[str, Any]

Give the default optimisation algorithm kwargs.

_format_constraints() Any

Transform all :class:`Constraint`s for this optimisation algorithm.

_format_objectives() Any

Transform all :class:`Objective`s for this optimisation algorithm.

_format_variables() Any

Transform all :class:`Variable`s for this optimisation algorithm.

_generate_optimisation_history(variables_values: ndarray, objectives_values: ndarray, constraints_values: ndarray) OptiInfo

Create optimisation history.

_get_objective_values() dict[str, float]

Save the full array of objective values.

_norm_wrapper_residuals(var: ndarray) float

Compute norm of residues vector from an array of variable values.

_wrapper_residuals(var: ndarray) ndarray

Compute residuals from an array of variable values.

property n_constr: int

Return number of (inequality) constraints.

property n_obj: int

Give number of objectives.

property n_var: int

Give number of variables.

abstract optimise(keep_history: bool = False, save_history: bool = False) tuple[bool, SetOfCavitySettings | None, OptiInfo]

Set up optimisation parameters and solve the problem.

Parameters:
  • keep_history (bool, optional) – To keep all the variables that were tried as well as the associated objective and constraint violation values.

  • save_history (bool, optional) – To save the history.

Returns:

  • success (bool) – Tells if the optimisation algorithm managed to converge.

  • optimized_cavity_settings (SetOfCavitySettings) – Best solution found by the optimization algorithm. None if no satisfactory solution was found.

  • info (OptiInfo) – Gives list of solutions, corresponding objective, convergence violation if applicable, etc.

supports_constraints: bool
property variable_names: list[str]

Give name of all variables.