Skip to content
AI360Xpert
Beta

Covariance Matrix Adaptation Evolution Strategy (CMA-ES)

A gradient-free optimizer that keeps reshaping a Gaussian cloud of tries. It slides the cloud toward the winners and stretches it along directions that keep paying off.

Three weighted tries move the mean to [0.40, 0.55] while the cloud stretches from a round ring into a narrow ellipse riding the valley
Three weighted tries move the mean to [0.40, 0.55] while the cloud stretches from a round ring into a narrow ellipse riding the valley

Why Does This Exist?

Some problems hand you no gradient. The simulator will not differentiate, the score lands once per episode, or the surface jumps. Stochastic gradient descent has nothing to chew on there, and even policy gradient methods want a gradient signal you may not trust.

Plain evolution fills the gap with a fixed cloud: sample around a mean, keep the best, move on. Round clouds waste samples on stretched problems. Picture one dial a hundred times touchier than the next, or a thin curved valley. Too wide across the valley, too timid along it. For the general spread question, see exploration vs exploitation and evolution strategies.

CMA-ES learns the cloud instead of hand-setting it. Each generation it relearns the center, the shape, and the stride from the winners. This page teaches that loop.

Think of It Like This

A rescue crew in a foggy valley

A crew hunts a lost hiker from a base camp. Each night searchers fan out and report back.

The camp is the mean. It moves toward the weighted average of the best reports, trusting the strongest signal most.

The search pattern is the covariance. When good news keeps coming from the northeast, tomorrow's pattern stretches northeast instead of staying round.

The lantern range is the step size. Nights that pull one way mean stride farther. Nights that zigzag mean tighten up.

Where it stops: real searchers chat and dodge cliffs. These samples are lone Gaussian draws with no memory and no obstacles.

How It Actually Works

One distribution rules every generation:

x=m+σC1/2z,z∼N(0,I)x = m + \sigma C^{1/2} z, \quad z \sim \mathcal{N}(0, I)

Here mm is the mean, the current best guess at the center. The scalar σ\sigma is the step size, one global stride. The matrix CC is the covariance, whose axes set the ellipse directions and whose eigenvalues set their squared lengths. Each draw zz is standard normal, reshaped by CC and scaled by σ\sigma into a candidate xx.

Each round samples λ\lambda candidates, scores them with fitness ff (lower better here), ranks them best first, and keeps the top μ\mu with fixed weights w1≥⋯≥wμw_1 \ge \dots \ge w_\mu summing to 1. Only the ranking matters, never the raw scores.

Worked example

Start at m=[0,0]m = [0, 0] with σ=1\sigma = 1 and C=IC = I. Three samples return ranked:

  • x1=[1.0,0.5]x_1 = [1.0, 0.5], weight w1=0.5w_1 = 0.5
  • x2=[0.2,1.2]x_2 = [0.2, 1.2], weight w2=0.3w_2 = 0.3
  • x3=[−0.8,−0.3]x_3 = [-0.8, -0.3], weight w3=0.2w_3 = 0.2

New mean is the weighted mix m′=∑wixim' = \sum w_i x_i. First coordinate: 0.5×1.0+0.3×0.2+0.2×(−0.8)=0.50+0.06−0.16=0.400.5 \times 1.0 + 0.3 \times 0.2 + 0.2 \times (-0.8) = 0.50 + 0.06 - 0.16 = 0.40. Second: 0.5×0.5+0.3×1.2+0.2×(−0.3)=0.25+0.36−0.06=0.550.5 \times 0.5 + 0.3 \times 1.2 + 0.2 \times (-0.3) = 0.25 + 0.36 - 0.06 = 0.55. So m′=[0.40,0.55]m' = [0.40, 0.55].

Selection mass is μeff=1/(0.25+0.09+0.04)=1/0.38≈2.63\mu_{\text{eff}} = 1 / (0.25 + 0.09 + 0.04) = 1 / 0.38 \approx 2.63, so this round counts as about two and a half independent winners.

How shape and stride follow

Covariance blends three terms: most of the old CC, a rank-one stretch along the multi-round path pcp_c, and a rank-μ\mu stretch along this round's winning steps. One lucky round cannot yank it because the learning rates c1c_1 and cμc_\mu stay small and keep CC positive-definite.

Step size watches a second path pσp_\sigma. Marches in one direction grow it past its random baseline near 1.251.25 in two dimensions, so σ\sigma grows and strides lengthen. Zigzags shrink it and the cloud fine-tunes.

Ranking-only choice buys three invariances: shifting the problem changes nothing, rotating it costs the same since CC learns orientation, and any increasing rescale of ff behaves identically. The bill is samples: populations grow like 4+3ln⁡n4 + 3 \ln n and full CC learning costs on the order of n2n^2 in dimension nn.

Code

The weighted mean step in plain Python:

samples: list[tuple[float, float]] = [(1.0, 0.5), (0.2, 1.2), (-0.8, -0.3)]weights: list[float] = [0.5, 0.3, 0.2]
mean_x: float = sum(w * x for w, (x, _) in zip(weights, samples))mean_y: float = sum(w * y for w, (_, y) in zip(weights, samples))mu_eff: float = 1.0 / sum(w * w for w in weights)
print(f"{mean_x:.2f} {mean_y:.2f}")  # -> 0.40 0.55print(f"{mu_eff:.2f}")  # -> 2.63

Both lines match the hand arithmetic. Swap the weights and watch the mean chase the heaviest try.

Watch Out For

Covariance collapse freezes the search

Symptom: fitness stalls early while σ\sigma and the ellipse axes shrink toward zero, and restarts land in the same spot. A tiny starting σ\sigma on flat ground is the usual trigger: chance clusters teach CC to stay tiny.

Fix: start σ\sigma near a quarter of your expected range and floor it. If the longest axis outgrows the shortest past about 10710^7, restart with fresh covariance.

Step size blows up on noisy scores

Symptom: on few-episode rollouts, σ\sigma keeps growing while the true mean barely moves. Noise reshuffles ranks each round and the path test reads churn as progress.

Fix: score each candidate over more episodes or widen the population so ranks average the noise out. Fix the evaluation budget first, never hand-tune σ\sigma to compensate.

The Quick Version

  • Candidates come from one Gaussian m+σC1/2zm + \sigma C^{1/2} z and the mean moves to the weighted winners, here [0.40,0.55][0.40, 0.55] from weights [0.5,0.3,0.2][0.5, 0.3, 0.2].
  • Covariance pairs a rank-μ\mu stretch along this round with a rank-one stretch along the multi-round path.
  • Step size grows on steady marches and shrinks on zigzags against a random baseline near 1.251.25 in two dimensions.
  • Rank-only selection gives translation, rotation, and rescale invariance for free.
  • No gradients needed, which suits black-box simulators, but full shape learning costs on the order of n2n^2 samples.