Skip to content

Covariance Matrix Adaptation Evolution Strategy (CMA-ES)

CMAES

CMAES(*, pop_size=None, mu=None, sigma0=0.3, x0=None, **kwargs)

Covariance Matrix Adaptation Evolution Strategy (CMA-ES).

CMA-ES learns the shape of its search distribution, which makes it strong on smooth, badly scaled or rotated continuous problems with small or medium budgets. This version has rank-mu and rank-one updates and cumulative step size adaptation.

Parameters

  • pop_size: number of samples per iteration (lambda). None (default) gives the standard 4 + floor(3 ln n), where n is the number of codes.
  • mu: number of parents. None (default) gives pop_size // 2.
  • sigma0: initial step size, in code units (the search space is [0, 1] per code).
  • x0: one starting solution (any form x0 accepts), used as the initial mean. None (default) draws it at random.

Samples that leave [0, 1] are reflected back at the bounds, and the reflected points are used both for evaluation and for the update. The default population size, weights and learning rates follow Table 1 of the tutorial, with positive weights only (no active CMA). The step size is capped at 1, the width of the search space.

Reference: N. Hansen, "The CMA Evolution Strategy: A Tutorial", arXiv:1604.00772 (2016).

Example

import ypea

problem = ypea.Problem(
    ypea.Real("x", shape=20, lb=-10, ub=10),
    lambda sol: ypea.benchmarks.sphere(sol.x),
    vectorized=True,
    name="sphere",
)

alg = ypea.CMAES(max_nfe=20_000)
result = alg.solve(problem, seed=1)
print(result.summary())
CMA-ES on sphere
  best objective value: 0
  feasible: True (violation 0)
  evaluations: 20000, iterations: 1667, time: 0.224 s, stopped by: max_nfe

Differs from Python: MATLAB draws its own random numbers, so the best value differs from Python's.

problem = ypea.Problem( ...
    vars=ypea.Real("x", shape=20, lb=-10, ub=10), ...
    objective=@(sol) ypea.benchmarks.sphere(sol.x), ...
    vectorized=true, ...
    name="sphere");

alg = ypea.CMAES(max_nfe=20000);
result = alg.solve(problem, seed=1);
disp(result.summary())
CMA-ES on sphere
  best objective value: 0
  feasible: true (violation 0)
  evaluations: 20000, iterations: 1667, time: 0.488 s, stopped by: max_nfe

Search on Rastrigin

CMA-ES solutions on Rastrigin at four iterations, and its convergence
CMA-ES with default settings, 50 iterations on Rastrigin in two dimensions (seed 1). The red points are the points sampled in that iteration. The open diamond is the best solution so far, and the plus sign the global minimum.