propulate.propagators.cmaes

Module Contents

Classes

CMAParameter

Instantiate a CMAParameter object.

CMAAdapter

Abstract base class for the adaption of strategy parameters of CMA-ES.

BasicCMA

Adaption of strategy parameters of CMA-ES according to the original CMA-ES algorithm.

ActiveCMA

Adaption of strategy parameters of CMA-ES according to the active CMA-ES algorithm.

CMAPropagator

Instantiate a CMA-ES propagator.

class propulate.propagators.cmaes.CMAParameter(lambd: int, mu: int, problem_dimension: int, weights: numpy.ndarray, mu_eff: float, c_c: float, c_1: float, c_mu: float, limits: Dict, initial_mean: numpy.ndarray, exploration: bool)

Instantiate a CMAParameter object.

Parameters:
  • lambd (int) – The number of individuals considered for each generation.

  • mu (int) – The number of positive recombination weights.

  • problem_dimension (int) – The number of dimensions in the search space.

  • weights (numpy.ndarray) – The recombination weights.

  • mu_eff (float) – The variance effective selection mass.

  • c_c (float) – The decay rate for the evolution path for the rank-one update of the covariance matrix.

  • c_1 (float) – The learning rate for the rank-one update of the covariance matrix update.

  • c_mu (float) – The learning rate for the rank-mu update of the covariance matrix update.

  • limits (dict) – The limits of the search space.

  • initial_mean (np.ndarray) – The initial mean of the distribution.

  • exploration (bool) – If True decompose covariance matrix for each generation (worse runtime, less exploitation, more decompose_in_each_generation); else decompose covariance matrix only after a certain number of individuals evaluated (better runtime, more exploitation, less decompose_in_each_generation).

update_mean(new_mean: numpy.ndarray) None

Update mean and old mean property.

Parameters:

new_mean (numpy.ndarray) – The new mean.

update_covariance_matrix(new_covariance_matrix: numpy.ndarray) None

Update the covariance matrix.

Computes new values for b_matrix, d_matrix, and covariance_inv_sqrt. Decomposition of covariance_matrix is O(n^3), hence the possibility of lazy updating b_matrix and d_matrix.

Parameters:

new_covariance_matrix (numpy.ndarray) – The new covariance matrix.

_decompose_co_matrix(new_co_matrix: numpy.ndarray) None

Eigen-decomposition of the covariance matrix into eigenvalues (d_matrix) and eigenvectors (columns of b_matrix).

Parameters:

new_co_matrix (numpy.ndarray) – The new covariance matrix that should be decomposed.

_limit_condition(limit: float) None

Limit the condition (squared ratio largest / smallest eigenvalue) of the cov. matrix if it exceeds a threshold.

Credits on how to limit the condition: https://github.com/CMA-ES/pycma/blob/development/cma/sampler.py

Parameters:

limit (float) – The threshold for the condition of the matrix.

_sort_b_d_matrix() None

Sort columns of b_matrix and d_matrix according to the eigenvalues in d_matrix.

mahalanobis_norm(dx: numpy.ndarray) numpy.ndarray

Compute the Mahalanobis distance using C^(-1/2) and the difference vector of a point to the distribution’s mean.

Parameters:

dx (numpy.ndarray) – The difference vector.

Returns:

The resulting Mahalanobis distance.

Return type:

numpy.ndarray

class propulate.propagators.cmaes.CMAAdapter

Abstract base class for the adaption of strategy parameters of CMA-ES.

Strategy class from the viewpoint of the strategy design pattern.

update_mean()

Abstract method for updating of mean in CMA-ES variants.

update_step_size()

Update step-size in CMA-ES variants.

update_covariance_matrix()

Abstract method for the adaptation of the covariance matrix of CMA-ES variants.

compute_weights()

Abstract method for computing the recombination weights of a CMA-ES variant.

compute_learning_rates()

Compute the learning rates for the CMA-variants.

abstract update_mean(par: CMAParameter, arx: numpy.ndarray) None

Abstract method for updating of mean in CMA-ES variants.

Parameters:
  • par (CMAParameter) – The parameter object of the CMA-ES propagation.

  • arx (numpy.ndarray) – The individuals of the distribution.

Raises:

NotImplementedError – Whenever called (abstract base class method).

static update_step_size(par: CMAParameter) None

Update step-size in CMA-ES variants. Calculate the current evolution path for the step-size adaption.

Parameters:

par (CMAParameter) – The parameter object of the CMA-ES propagation.

abstract update_covariance_matrix(par: CMAParameter, arx: numpy.ndarray) None

Abstract method for the adaptation of the covariance matrix of CMA-ES variants.

Parameters:
  • par (CMAParameter) – The parameter object of the CMA-ES propagation.

  • arx (numpy.ndarray) – The individuals of the distribution.

Raises:

NotImplementedError – Whenever called (abstract base class method).

abstract compute_weights(mu: int, lamb: int, problem_dimension: int) Tuple[numpy.ndarray, float, float, float, float]

Abstract method for computing the recombination weights of a CMA-ES variant.

Parameters:
  • mu (int) – The number of positive recombination weights.

  • lamb (int) – The number of individuals considered for each generation.

  • problem_dimension (int) – The number of dimensions in the search space.

Returns:

tuple of the weights, mu_eff, c_1, c_c and c_mu

Return type:

tuple[np.ndarray, float, float, float, float]

Raises:

NotImplementedError – Whenever called (abstract base class method).

static compute_learning_rates(mu_eff: float, problem_dimension: int) Tuple[float, float, float]

Compute the learning rates for the CMA-variants.

Parameters:
  • mu_eff (float) – The variance effective selection mass.

  • problem_dimension (int) – The number of dimensions in the search space.

Returns:

  • float – The decay rate for evolution path for the rank-one update of the covariance matrix, c_c.

  • float – The learning rate for the rank-one update of the covariance matrix update, c_1.

  • float – The learning rate for the rank-mu update of the covariance matrix update, c_mu.

class propulate.propagators.cmaes.BasicCMA

Bases: CMAAdapter

Adaption of strategy parameters of CMA-ES according to the original CMA-ES algorithm.

Concrete strategy class from the viewpoint of the strategy design pattern.

Notes

The BasicCMA class inherits all methods and attributes from the CMAAdapter class.

See also

CMAAdapter

The parent class.

compute_weights(mu: int, lamb: int, problem_dimension: int) Tuple[numpy.ndarray, float, float, float, float]

Compute the recombination weights for basic CMA-ES.

Parameters:
  • mu (int) – The number of positive recombination weights

  • lamb (int) – The number of individuals considered for each generation

  • problem_dimension (int) – The number of dimensions in the search space

Returns:

  • numpy.ndarray – The weights.

  • float – The variance effective selection mass, mu_eff.

  • float – The learning rate for the rank-one update of the covariance matrix update, c_1.

  • float – The decay rate for evolution path for the rank-one update of the covariance matrix, c_c.

  • float – The learning rate for the rank-mu update of the covariance matrix update, c_mu.

update_mean(par: CMAParameter, arx: numpy.ndarray) None

Update the mean in basic CMA-ES.

Parameters:
  • par (CMAParameter) – The parameter object of the CMA-ES propagation.

  • arx (numpy.ndarray) – The individuals of the distribution.

update_covariance_matrix(par: CMAParameter, arx: numpy.ndarray) None

Adapt the covariance matrix of basic CMA-ES.

Parameters:
  • par (CMAParameter) – The parameter object of the CMA-ES propagation.

  • arx (numpy.ndarray) – The individuals of the distribution.

class propulate.propagators.cmaes.ActiveCMA

Bases: CMAAdapter

Adaption of strategy parameters of CMA-ES according to the active CMA-ES algorithm.

Differently from the original CMA-ES algorithm, active CMA-ES uses negative recombination weights (only for the covariance matrix adaptation) for individuals with relatively low fitness. Concrete strategy class from the viewpoint of the strategy design pattern.

Notes

The ActiveCMA class inherits all methods and attributes from the CMAAdapter class.

See also

CMAAdapter

The parent class.

compute_weights(mu: int, lamb: int, problem_dimension: int) Tuple[numpy.ndarray, float, float, float, float]

Compute the recombination weights for active CMA-ES.

Parameters:
  • mu (int) – The number of positive recombination weights.

  • lamb (int) – The number of individuals considered for each generation.

  • problem_dimension (int) – The number of dimensions in the search space.

Returns:

  • numpy.ndarray – The weights.

  • float – The variance effective selection mass, mu_eff.

  • float – The learning rate for the rank-one update of the covariance matrix update, c_1.

  • float – The decay rate for evolution path for the rank-one update of the covariance matrix, c_c.

  • float – The learning rate for the rank-mu update of the covariance matrix update, c_mu.

update_mean(par: CMAParameter, arx: numpy.ndarray) None

Update the mean in active CMA-ES.

Parameters:
  • par (CMAParameter) – The parameter object of the CMA-ES propagation.

  • arx (numpy.ndarray) – The individuals of the distribution.

update_covariance_matrix(par: CMAParameter, arx: numpy.ndarray) None

Adapt the covariance matrix of active CMA-ES.

Parameters:
  • par (CMAParameter) – The parameter object of the CMA-ES propagation.

  • arx (numpy.ndarray) – The individuals of the distribution.

class propulate.propagators.cmaes.CMAPropagator(adapter: CMAAdapter, limits: Dict, decompose_in_each_generation: bool = False, select_worst_all_time: bool = False, pop_size: int | None = None, pool_size: int = 3, rng: random.Random | None = None)

Bases: propulate.propagators.base.Propagator

Instantiate a CMA-ES propagator.

Parameters:
  • adapter (CMAAdapter) – The adaptation strategy of CMA-ES.

  • limits (Dict[str, float]) – The limits of the search space.

  • decompose_in_each_generation (bool, optional) – If True, decompose covariance matrix for each generation (worse runtime, less exploitation, more exploration); else decompose covariance matrix only after a certain number of individuals evaluated (better runtime, more exploitation, less exploration). Default is False.

  • select_worst_all_time (bool, optional) – If True, use the worst individuals for negative recombination weights in active CMA-ES, else use the worst (lambda - mu) individuals of the best lambda individuals. If BasicCMA is used, the given value is irrelevant regarding functionality. Default is False.

  • pop_size (int, optional) – The number of individuals to be considered in each generation.

  • pool_size (int, optional) – The size of the pool of individuals pre-selected before selecting the best from this pool. Default is 3.

  • rng (random.Random, optional) – The separate random number generator for the Propulate optimization.

__call__(inds: List[propulate.population.Individual]) propulate.population.Individual

Define the skeleton of the CMA-ES algorithm using the template method design pattern.

Sampling individuals and adapting the strategy parameters. Template methods are update_mean, update_covariance_matrix, and update_step_size.

Parameters:

inds (List[propulate.population.Individual]) – Available individuals.

Returns:

new_ind – The newly sampled individual.

Return type:

propulate.population.Individual

_transform_individuals_to_matrix(inds: List[propulate.population.Individual]) numpy.ndarray

Take a list of individuals and transform it to a numpy array for easier subsequent computation.

Parameters:

inds (list[propulate.population.Individual]) – The list of individuals.

Returns:

arx – Array of shape [problem_dimension, len(inds)].

Return type:

numpy.ndarray

_sample_cma() propulate.population.Individual

Sample new individuals according to CMA-ES.

Returns:

new_ind – The newly sampled individual.

Return type:

propulate.population.Individual