factory module

Define factory and presets to handle variables, constraints, limits, etc..

Note

If you add your own DesignSpaceFactory preset, do not forget to add it to the list of supported presets in config.design_space.

class DesignSpaceFactory(design_space_kw: dict[str, float | bool | str | Path])

Bases: ABC

Base class to handle Variable and Constraint creation.

Parameters:
  • reference_elements (list[Element]) – All the elements with the reference setting.

  • compensating_elements (list[Element]) – The elements from the linac under fixing that will be used for compensation.

  • design_space_kw (dict[str, float | bool) – The entries of [design_space] in .ini file.

design_space_kw: dict[str, float | bool | str | Path]
__post_init__()

Declare complementary variables.

_check_can_be_retuned(compensating_elements: list[Element]) None

Check that given elements can be retuned.

use_files(variables_filepath: Path, constraints_filepath: Path | None = None, **design_space_kw: float | bool | str | Path) None

Tell factory to generate design space from the provided files.

Parameters:
  • variables_filepath (pathlib.Path) – Path to the variables.csv file.

  • constraints_filepath (pathlib.Path | None) – Path to the constraints.csv file. The default is None.

_run_variables(compensating_elements: list[Element], reference_elements: list[Element]) list[Variable]

Set up all the required variables.

_run_constraints(compensating_elements: list[Element], reference_elements: list[Element]) list[Constraint]

Set up all the required constraints.

run(compensating_elements: list[Element], reference_elements: list[Element]) DesignSpace

Set up variables and constraints.

_get_initial_value_from_kw(variable: Literal['k_e', 'phi_0_rel', 'phi_0_abs', 'phi_s'], reference_element: Element) float

Select initial value for given variable.

The default behavior is to return the value of variable from reference_element, which is a good starting point for optimisation.

Parameters:
  • variable (Literal["k_e", "phi_0_rel", "phi_0_abs", "phi_s"]) – The variable from which you want the limits.

  • reference_element (Element) – The element in its nominal tuning.

Returns:

Initial value.

Return type:

float

_get_limits_from_kw(variable: Literal['k_e', 'phi_0_rel', 'phi_0_abs', 'phi_s'], reference_element: Element, reference_elements: list[Element]) tuple[float, float]

Select limits for given variable.

Call this method for classic limits.

Parameters:
  • variable (Literal["k_e", "phi_0_rel", "phi_0_abs", "phi_s"]) – The variable from which you want the limits.

  • reference_element (Element) – The element in its nominal tuning.

  • reference_elements (list[Element]) – List of reference elements.

Returns:

Lower and upper limit for current variable.

Return type:

tuple[float | None]

_run_from_file(compensating_elements: list[Element], reference_elements: list[Element] | None = None) DesignSpace

Use the DesignSpace.from_files() constructor.

Parameters:
  • variables_names (tuple[str, ...]) – Name of the variables to create.

  • constraints_names (tuple[str, ...] | None, optional) – Name of the constraints to create. The default is None.

Return type:

DesignSpace

__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8add580>
class AbsPhaseAmplitude(design_space_kw: dict[str, float | bool | str | Path])

Bases: DesignSpaceFactory

Optimise over \(\phi_{0,\,\mathrm{abs}}\) and \(k_e\).

variables_names = ('phi_0_abs', 'k_e')
__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8ade700>
class RelPhaseAmplitude(design_space_kw: dict[str, float | bool | str | Path])

Bases: DesignSpaceFactory

Optimise over \(\phi_{0,\,\mathrm{rel}}\) and \(k_e\).

The same as AbsPhaseAmplitude, but the phase variable is \(\phi_{0,\,\mathrm{rel}}\) instead of \(\phi_{0,\,\mathrm{abs}}\). It may be better for convergence, because it makes cavities more independent.

variables_names = ('phi_0_rel', 'k_e')
__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8ade500>
class SyncPhaseAmplitude(design_space_kw: dict[str, float | bool | str | Path])

Bases: DesignSpaceFactory

Optimise over \(\phi_s\) and \(k_e\).

Synchronous phases outside of the bounds will not ocurr, without setting any Constraint. This kind of optimisation takes more time as we need, for every iteration of the OptimisationAlgorithm, to find the \(\phi_{0,\,\mathrm{rel}}\) that corresponds to the desired \(\phi_s\).

variables_names = ('phi_s', 'k_e')
__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8fe37c0>
class AbsPhaseAmplitudeWithConstrainedSyncPhase(design_space_kw: dict[str, float | bool | str | Path])

Bases: DesignSpaceFactory

Optimise \(\phi_{0,\,\mathrm{abs}}\), \(k_e\). \(\phi_s\) is constrained.

Warning

The selected OptimisationAlgorithm must support the constraints.

variables_names = ('phi_0_abs', 'k_e')
constraints_names = ('phi_s',)
__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8f32c40>
class RelPhaseAmplitudeWithConstrainedSyncPhase(design_space_kw: dict[str, float | bool | str | Path])

Bases: DesignSpaceFactory

Optimise \(\phi_{0,\,\mathrm{rel}}\), \(k_e\). \(\phi_s\) is constrained.

Warning

The selected OptimisationAlgorithm must support the constraints.

variables_names = ('phi_0_rel', 'k_e')
constraints_names = ('phi_s',)
__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8ade880>
class Everything(design_space_kw: dict[str, float | bool | str | Path])

Bases: DesignSpaceFactory

This class creates all possible variables and constraints.

This is not to be used in an optimisation problem, but rather to save in a .csv all the limits and initial values for every variable/constraint.

variables_names = ('k_e', 'phi_s', 'phi_0_abs', 'phi_0_rel')
constraints_names = ('phi_s',)
run(*args, **kwargs) DesignSpace

Launch normal run but with an info message.

__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8adeb80>
class Unconstrained(design_space_kw: dict[str, float | bool | str | Path])

Bases: AbsPhaseAmplitude

Deprecated alias to AbsPhaseAmplitude.

Deprecated since version 0.6.16: Prefer AbsPhaseAmplitude.

__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8ade3c0>
class UnconstrainedRel(design_space_kw: dict[str, float | bool | str | Path])

Bases: RelPhaseAmplitude

Deprecated alias to RelPhaseAmplitude.

Deprecated since version 0.6.16: Prefer RelPhaseAmplitude.

__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8ade400>
class SyncPhaseAsVariable(design_space_kw: dict[str, float | bool | str | Path])

Bases: SyncPhaseAmplitude

Deprecated alias to SyncPhaseAmplitude.

Deprecated since version 0.6.16: Prefer SyncPhaseAmplitude.

__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8e9d180>
class ConstrainedSyncPhase(design_space_kw: dict[str, float | bool | str | Path])

Bases: AbsPhaseAmplitudeWithConstrainedSyncPhase

Deprecated alias to AbsPhaseAmplitudeWithConstrainedSyncPhase.

Deprecated since version 0.6.16: Prefer AbsPhaseAmplitudeWithConstrainedSyncPhase.

__init__(design_space_kw: dict[str, float | bool | str | Path]) None
_abc_impl = <_abc._abc_data object at 0x7fd3c8ade6c0>
DESIGN_SPACE_FACTORY_PRESETS = {   'abs_phase_amplitude': <class 'lightwin.optimisation.design_space.factory.AbsPhaseAmplitude'>,     'abs_phase_amplitude_with_constrained_sync_phase': <class 'lightwin.optimisation.design_space.factory.AbsPhaseAmplitudeWithConstrainedSyncPhase'>,     'constrained_sync_phase': <class 'lightwin.optimisation.design_space.factory.AbsPhaseAmplitudeWithConstrainedSyncPhase'>,     'everything': <class 'lightwin.optimisation.design_space.factory.Everything'>,     'rel_phase_amplitude': <class 'lightwin.optimisation.design_space.factory.RelPhaseAmplitude'>,     'rel_phase_amplitude_with_constrained_sync_phase': <class 'lightwin.optimisation.design_space.factory.RelPhaseAmplitudeWithConstrainedSyncPhase'>,     'sync_phase_amplitude': <class 'lightwin.optimisation.design_space.factory.SyncPhaseAmplitude'>,     'sync_phase_as_variable': <class 'lightwin.optimisation.design_space.factory.SyncPhaseAmplitude'>,     'unconstrained': <class 'lightwin.optimisation.design_space.factory.AbsPhaseAmplitude'>,     'unconstrained_rel': <class 'lightwin.optimisation.design_space.factory.RelPhaseAmplitude'>}
get_design_space_factory(design_space_preset: str, **design_space_kw: float | bool) DesignSpaceFactory

Select proper factory, instantiate it and return it.

Parameters:
  • design_space_preset (str) – design_space_preset

  • design_space_kw (float | bool) – design_space_kw

Return type:

DesignSpaceFactory