Skip to content

SciPy generators

xopt.generators.sequential.neldermead.NelderMeadGenerator

NelderMeadGenerator(**kwargs)

Bases: SequentialGenerator

Nelder-Mead algorithm from SciPy in Xopt's Generator form. Converted to use a state machine to resume in exactly the last state.

Attributes:

name : str The name of the generator. initial_point : Optional[Dict[str, float]] Initial point for the optimization. initial_simplex : Optional[Dict[str, Union[List[float], np.ndarray]]] Initial simplex for the optimization. adaptive : bool Change hyperparameters based on dimensionality. current_state : SimplexState Current state of the simplex. future_state : Optional[SimplexState] Future state of the simplex. x : Optional[np.ndarray] Current x value. y : Optional[float] Current y value.

Methods:

add_data(self, new_data: pd.DataFrame) Add new data to the generator. generate(self, n_candidates: int) -> Optional[List[Dict[str, float]]] Generate a specified number of candidate samples. _call_algorithm(self) Call the Nelder-Mead algorithm. simplex(self) -> Dict[str, np.ndarray] Returns the simplex in the current state.

Source code in xopt/generators/sequential/neldermead.py
126
127
128
129
130
131
132
133
134
135
136
137
138
def __init__(self, **kwargs):
    super().__init__(**kwargs)

    self._saved_options = self.model_dump(
        exclude={"current_state", "future_state"}
    ).copy()  # Used to keep track of changed options

    if self.initial_simplex:
        self._initial_simplex = np.array(
            [self.initial_simplex[k] for k in self.vocs.variable_names]
        ).T
    else:
        self._initial_simplex = None

Attributes

xopt.generators.sequential.neldermead.NelderMeadGenerator.simplex property
simplex

Returns the simplex in the current state.

Returns:

Dict[str, np.ndarray] The simplex in the current state.

xopt.generators.sequential.neldermead.NelderMeadGenerator.x0 property
x0

Raw internal initial point for convenience. If initial_point is set, it will be used. Otherwise, if data is set, the last point will be used.

Returns:

np.ndarray The initial point as a NumPy array.

xopt.generators.sequential.rcds.RCDSGenerator

RCDSGenerator(**kwargs)

Bases: SequentialGenerator

RCDS algorithm.

Reference: An algorithm for online optimization of accelerators Huang, X., Corbett, J., Safranek, J. and Wu, J. doi: 10.1016/j.nima.2013.05.046

This algorithm must be stepped serially.

Attributes:

Name Type Description
name str

Name of the generator.

x0 Optional[list]

Initial solution vector.

init_mat Optional[ndarray]

Initial direction matrix.

noise PositiveFloat

Estimated noise level.

step PositiveFloat

Step size for the optimization.

_ub ndarray

Upper bounds of the variables.

_lb ndarray

Lower bounds of the variables.

_powell PowellMainStateMachine

Instance of the PowellMainStateMachine.

_sign int

Sign of the objective function (1 for MINIMIZE, -1 for MAXIMIZE).

Source code in xopt/generator.py
102
103
104
105
106
107
108
def __init__(self, **kwargs):
    """
    Initialize the generator.

    """
    super().__init__(**kwargs)
    logger.info(f"Initialized generator {self.name}")

xopt.generators.sequential.extremumseeking.ExtremumSeekingGenerator

ExtremumSeekingGenerator(**kwargs)

Bases: SequentialGenerator

Extremum seeking algorithm.

Reference: Extremum Seeking-Based Control System for Particle Accelerator Beam Loss Minimization A. Scheinker, E. -C. Huang and C. Taylor doi: 10.1109/TCST.2021.3136133

This algorithm must be stepped serially.

Attributes:

name : str The name of the generator. k : PositiveFloat Feedback gain. oscillation_size : PositiveFloat Oscillation size. decay_rate : PositiveFloat Decay rate. _nES : int Number of extremum seeking parameters. _wES : np.ndarray Frequencies for extremum seeking. _dtES : float Time step for extremum seeking. _aES : np.ndarray Amplitudes for extremum seeking. _p_ave : np.ndarray Average of parameter bounds. _p_diff : np.ndarray Difference of parameter bounds. _amplitude : float Amplitude of oscillation. _i : int Evaluation counter. _last_input : np.ndarray Last input values. _last_outcome : float Last outcome value.

Methods:

add_data(self, new_data: pd.DataFrame) Add new data to the generator. p_normalize(self, p: np.ndarray) -> np.ndarray Normalize parameters. p_un_normalize(self, p: np.ndarray) -> np.ndarray Un-normalize parameters. generate(self, n_candidates: int) -> List[Dict[str, float]] Generate a specified number of candidate samples.

Source code in xopt/generators/sequential/extremumseeking.py
82
83
84
85
86
87
88
89
90
91
92
93
94
def __init__(self, **kwargs):
    super().__init__(**kwargs)

    self._nES = len(self.vocs.variables)
    self._wES = np.linspace(1.0, 1.75, int(np.ceil(self._nES / 2)))
    self._dtES = 2 * np.pi / (10 * np.max(self._wES))
    self._aES = np.zeros(self._nES)
    for n in np.arange(self._nES):
        jw = int(np.floor(n / 2))
        self._aES[n] = self._wES[jw] * (self.oscillation_size) ** 2
    bound_low, bound_up = self.vocs.bounds
    self._p_ave = (bound_up + bound_low) / 2
    self._p_diff = bound_up - bound_low

Functions

xopt.generators.sequential.extremumseeking.ExtremumSeekingGenerator.p_normalize
p_normalize(p)

Normalize parameters.

Parameters:

p : np.ndarray The parameters to normalize.

Returns:

np.ndarray The normalized parameters.

Source code in xopt/generators/sequential/extremumseeking.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def p_normalize(self, p: np.ndarray) -> np.ndarray:
    """
    Normalize parameters.

    Parameters:
    -----------
    p : np.ndarray
        The parameters to normalize.

    Returns:
    --------
    np.ndarray
        The normalized parameters.
    """
    p_norm = 2.0 * (p - self._p_ave) / self._p_diff
    return p_norm
xopt.generators.sequential.extremumseeking.ExtremumSeekingGenerator.p_un_normalize
p_un_normalize(p)

Un-normalize parameters.

Parameters:

p : np.ndarray The parameters to un-normalize.

Returns:

np.ndarray The un-normalized parameters.

Source code in xopt/generators/sequential/extremumseeking.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def p_un_normalize(self, p: np.ndarray) -> np.ndarray:
    """
    Un-normalize parameters.

    Parameters:
    -----------
    p : np.ndarray
        The parameters to un-normalize.

    Returns:
    --------
    np.ndarray
        The un-normalized parameters.
    """
    p_un_norm = p * self._p_diff / 2.0 + self._p_ave
    return p_un_norm