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 algorithm module.
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 OptiSol[source]
Bases:
TypedDictHold information on the solution.
-
cavity_settings:
SetOfCavitySettings
-
cavity_settings:
- class OptimisationAlgorithm(*, compensating_elements, elts, objectives, variables, compute_beam_propagation, compute_residuals, cavity_settings_factory, reference_simulation_output, constraints=None, compute_constraints=None, optimisation_algorithm_kwargs=None, history_kwargs=None, **kwargs)[source]
Bases:
ABCHolds the optimization parameters, the methods to optimize.
- Parameters:
compensating_elements (
Collection[Element]) – Cavity objects used to compensate for the faults.elts (
ListOfElements) – Holds the whole compensation zone under study.objectives (
Collection[Objective]) – Holds objectives, initial values, bounds.variables (
Collection[Variable]) – Holds variables, their initial values, their limits.constraints (
Collection[Constraint] |None, default:None) – Holds constraints and their limits.opti_sol – Holds information on the solution that was found.
supports_constraints – 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 argumenteltsbeing set by afunctools.partial.compute_residuals (
Callable[[SimulationOutput],Any]) – Method to compute residuals from aSimulationOutput.compute_constraints (
Callable[[SimulationOutput],ndarray] |None, default:None) – Method to compute constraint violation.cavity_settings_factory (
CavitySettingsFactory) – A factory to easily create the cavity settings to try at each iteration of the optimisation algorithm.history_kwargs (
dict[str,Any] |None, default:None) – kwargs for theOptimizationHistorycreation.reference_simulation_output (
SimulationOutput) – Used for theOptimizationHistory.optimisation_algorithm_kwargs (
dict[str,Any] |None, default:None)
- __init__(*, compensating_elements, elts, objectives, variables, compute_beam_propagation, compute_residuals, cavity_settings_factory, reference_simulation_output, constraints=None, compute_constraints=None, optimisation_algorithm_kwargs=None, history_kwargs=None, **kwargs)[source]
Instantiate the object.
- Parameters:
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)reference_simulation_output (
SimulationOutput)constraints (
Collection[Constraint] |None, default:None)compute_constraints (
Callable[[SimulationOutput],ndarray] |None, default:None)optimisation_algorithm_kwargs (
dict[str,Any] |None, default:None)
- abstract optimize()[source]
Set up optimization parameters and solve the problem.
- Returns:
Gives list of solutions, corresponding objective, convergence violation if applicable, etc.
- Return type:
info
- abstract _generate_opti_sol(*args, **kwargs)[source]
Takes the results of the optimization in any form, returns dict.
- Return type:
- _format_constraints()[source]
Adapt all
Constraintto this optimisation algorithm.- Return type:
- _norm_wrapper_residuals(var)[source]
Compute norm of residuals vector from array of variable values.
- _create_set_of_cavity_settings(var, status='compensate (in progress)')[source]
Transform
varinto genericSetOfCavitySettings.- Parameters:
- Returns:
Object holding the settings of all the cavities.
- Return type:
- _output_some_info(opti_sol, *complementary_info)[source]
Show the most useful data from optimization.
- _abc_impl = <_abc._abc_data object at 0x70e25243cb80>
- class OptimizationHistory(reference_simulation_output, objectives_names, get_args=(), get_kwargs=None, folder=None, save_interval=100, **kwargs)[source]
Bases:
objectKeep all the settings that were tried.
- Parameters:
- _settings_filename = 'settings.csv'
- _objectives_filename = 'objectives.csv'
- _constraints_filename = 'constraints.csv'
- __init__(reference_simulation_output, objectives_names, get_args=(), get_kwargs=None, folder=None, save_interval=100, **kwargs)[source]
Instantiate the object.
- Parameters:
get_args (
tuple[str,...], default:()) – args and kwargs passed to theSimulationOutput.getmethod. Used to add some values to the output files.get_kwargs (
dict[str,Any] |None, default:None) – args and kwargs passed to theSimulationOutput.getmethod. Used to add some values to the output files.get_kwargs – Keyword arguments for the SimulationOutput.get method.
folder (
Path|str|None, default:None) – Where the histories will be saved. If not provided or None is given, this class will not have any effect and every public method wil be overriden with dummy methods.save_interval (
int, default:100) – Files will be saved everysave_intervaliteration.reference_simulation_output (
SimulationOutput)objectives_names (
Collection[str])
- _make_public_methods_useless()[source]
Override some methods so that they do not do anything.
- Return type:
- _init_objective_hist(objectives_names, reference_simulation_output)[source]
Create the objective history, with header and reference values.
- Parameters:
objectives_names (
Collection[str])reference_simulation_output (
SimulationOutput)
- Return type:
- _simulation_output_to_objectives(simulation_output)[source]
Extract and format desired values from
simulation_output.- Parameters:
simulation_output (
SimulationOutput)- Return type:
- add_objective_values(objectives, simulation_output)[source]
Add some objective values.
- Parameters:
objectives (
list)simulation_output (
SimulationOutput)
- Return type: