Skip to content

Fix the CMA-ES cumulation constant c_c - #256

Merged
BillHuang2001 merged 1 commit into
EMI-Group:mainfrom
flyingdoubleG:fix-cmaes-cc
Oct 28, 2026
Merged

BillHuang2001 merged 1 commit into
EMI-Group:mainfrom
flyingdoubleG:fix-cmaes-cc

Conversation

@flyingdoubleG

Copy link
Copy Markdown
Contributor

Description

The CMA-ES cumulation constant for the rank-one evolution path uses the wrong numerator:

c_c = (mu_eff + 2) / (self.dim + 4 + 2 * mu_eff / self.dim)

(mu_eff + 2) is the numerator of c_sigma, two lines above. The tutorial this class cites (Hansen, arXiv:1604.00772, eq. 56) gives

$$c_c = \frac{4 + \mu_\text{eff}/n}{n + 4 + 2\mu_\text{eff}/n},$$

and pycma and evosax use the same formula.

At the default population size the difference is moderate: with n = 10, c_c is 0.353 instead of 0.295. It grows with mu_eff, though. Once c_c > 1, the (1 - c_c) decay in the p_c update flips sign every generation. Once c_c > 2, sqrt(c_c * (2 - c_c) * mu_eff) is NaN, and the NaN spreads into C. Large populations are a main use case for a GPU library, so this matters:

n pop_size mu_eff c_c (current) c_c (tutorial)
10 10 3.2 0.353 0.295
10 100 27.0 1.494 0.345
10 1000 254.6 3.953 0.454
100 1000 254.6 2.352 0.060
1000 10000 2508.6 2.488 0.007

I ran 100 generations on the 10-d sphere starting at mean 3·1, with sigma = 1. With pop_size = 1000, the current constant makes C NaN, and the run stops with torch._C._LinAlgError: linalg.eigh: The algorithm failed to converge. With the fixed constant it converges (best fitness 1.6e-9). This end-to-end run also needs #255, because CMAES.step currently fails in torch.cond before reaching this point. This PR does not depend on #255 otherwise.

test_cmaes_cumulation_constant checks c_c against eq. (56) for several (n, pop_size) pairs and checks that the p_c update stays finite. Before the fix it fails on the first case (0.3531 != 0.2950); with the fix it passes.

Checklist

  • I have formatted my Python code with ruff.
  • I have good commit messages.
  • If adding new algorithms, problems, operators:
    • Added related test cases.
    • Added docstring to explain important parameters.
    • Added entries in the docs.

c_c used (mu_eff + 2) as its numerator, which belongs to c_sigma. The
tutorial (eq. 56), pycma and evosax all use
c_c = (4 + mu_eff / n) / (n + 4 + 2 mu_eff / n).

With the wrong numerator c_c exceeds 2 once mu_eff is large relative to n
(e.g. n = 10, pop_size = 100 gives 1.49; pop_size = 1000 gives 3.95), so
sqrt(c_c * (2 - c_c) * mu_eff) in the p_c update is NaN.
@BillHuang2001

Copy link
Copy Markdown
Collaborator

Thanks for the fix

@BillHuang2001
BillHuang2001 merged commit 4dabc1f into EMI-Group:main Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants