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 optimisation.design_space_specs.

class DesignSpaceFactory(design_space_kw)[source]

Bases: ABC

Base class to handle Variable and Constraint creation.

Parameters:
  • reference_elements – All the elements with the reference setting.

  • compensating_elements – The elements from the linac under fixing that will be used for compensation.

  • design_space_kw (dict[str, Path]) – The entries of [design_space] in TOML configuration file.

design_space_kw: dict[str, Path]
__post_init__()[source]

Declare complementary variables.

_check_can_be_retuned(compensating_elements)[source]

Check that given elements can be retuned.

Parameters:

compensating_elements (Collection[Element])

Return type:

None

_run_variables(compensating_elements, reference_elements)[source]

Set up all the required variables.

Parameters:
Return type:

list[Variable]

_run_constraints(compensating_elements, reference_elements)[source]

Set up all the required constraints.

Parameters:
Return type:

list[Constraint]

create(compensating_elements, reference_elements)[source]

Set up variables and constraints.

Parameters:
Return type:

DesignSpace

_get_initial_value_from_kw(variable, reference_element)[source]

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'] | Literal['phi_0_abs', 'phi_0_rel', 'phi_s']) – The variable from which you want the limits.

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

Return type:

float

Returns:

Initial value.

_get_limits_from_kw(variable, reference_element, reference_elements)[source]

Select limits for given variable.

Call this method for classic limits.

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

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

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

Return type:

tuple[float, float]

Returns:

Lower and upper limit for current variable.

use_files(variables_filepath, constraints_filepath=None, **design_space_kw)[source]

Tell factory to generate design space from the provided files.

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

  • constraints_filepath (Path | None, default: None) – Path to the constraints.csv file.

  • design_space_kw (Path)

Return type:

Self

_create_from_file(compensating_elements, reference_elements=None)[source]

Use the DesignSpace.from_files() constructor.

Parameters:
  • variables_names – Name of the variables to create.

  • constraints_names – Name of the constraints to create. The default is None.

  • compensating_elements (list[Element])

  • reference_elements (list[Element] | None, default: None)

Return type:

DesignSpace

__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f654014c0>
class AbsPhaseAmplitude(design_space_kw)[source]

Bases: DesignSpaceFactory

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

Parameters:

design_space_kw (dict[str, Path])

variables_names = ('phi_0_abs', 'k_e')
__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653b4500>
class RelPhaseAmplitude(design_space_kw)[source]

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.

Parameters:

design_space_kw (dict[str, Path])

variables_names = ('phi_0_rel', 'k_e')
__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653bfc80>
class SyncPhaseAmplitude(design_space_kw)[source]

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\).

Parameters:

design_space_kw (dict[str, Path])

variables_names = ('phi_s', 'k_e')
__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653b7900>
class AbsPhaseAmplitudeWithConstrainedSyncPhase(design_space_kw)[source]

Bases: DesignSpaceFactory

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

Warning

The selected OptimisationAlgorithm must support the constraints.

Parameters:

design_space_kw (dict[str, Path])

variables_names = ('phi_0_abs', 'k_e')
constraints_names = ('phi_s',)
__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653b7d40>
class RelPhaseAmplitudeWithConstrainedSyncPhase(design_space_kw)[source]

Bases: DesignSpaceFactory

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

Warning

The selected OptimisationAlgorithm must support the constraints.

Parameters:

design_space_kw (dict[str, Path])

variables_names = ('phi_0_rel', 'k_e')
constraints_names = ('phi_s',)
__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653c48c0>
class Everything(design_space_kw)[source]

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.

Parameters:

design_space_kw (dict[str, Path])

variables_names = ('k_e', 'phi_s', 'phi_0_abs', 'phi_0_rel')
constraints_names = ('phi_s',)
run(*args, **kwargs)[source]

Launch normal run but with an info message.

Return type:

DesignSpace

__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653e6500>
class Unconstrained(design_space_kw)[source]

Bases: AbsPhaseAmplitude

Deprecated alias to AbsPhaseAmplitude.

Deprecated since version 0.6.16: Prefer AbsPhaseAmplitude.

Parameters:

design_space_kw (dict[str, Path])

__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653c5880>
class UnconstrainedRel(design_space_kw)[source]

Bases: RelPhaseAmplitude

Deprecated alias to RelPhaseAmplitude.

Deprecated since version 0.6.16: Prefer RelPhaseAmplitude.

Parameters:

design_space_kw (dict[str, Path])

__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f65394a00>
class SyncPhaseAsVariable(design_space_kw)[source]

Bases: SyncPhaseAmplitude

Deprecated alias to SyncPhaseAmplitude.

Deprecated since version 0.6.16: Prefer SyncPhaseAmplitude.

Parameters:

design_space_kw (dict[str, Path])

__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653bf900>
class ConstrainedSyncPhase(design_space_kw)[source]

Bases: AbsPhaseAmplitudeWithConstrainedSyncPhase

Deprecated alias to AbsPhaseAmplitudeWithConstrainedSyncPhase.

Deprecated since version 0.6.16: Prefer AbsPhaseAmplitudeWithConstrainedSyncPhase.

Parameters:

design_space_kw (dict[str, Path])

__init__(design_space_kw)
Parameters:

design_space_kw (dict[str, Path])

Return type:

None

_abc_impl = <_abc._abc_data object at 0x7c8f653e6ac0>
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, **design_space_kw)[source]

Select proper factory, instantiate it and return it.

Parameters:
  • design_space_preset (str) – design_space_preset

  • design_space_kw (Path) – design_space_kw

Return type:

DesignSpaceFactory