UP (The CMA Evolution Strategy)
CMA Evolution Strategy Source Code
This page provides implementations of the CMA-ES and links to libraries that contain such implementations. All implementations provided in this page follow very closely Hansen (2006). They
support small and large population sizes, the latter by implementing
the rank-µ-update (Hansen et al, 2003) with
non-uniform weights (Hansen and Kern,
2004) and an improved parameter setting for the step-size
adaptation (Hansen and
Kern, 2004). The default population size is small (Hansen and Ostermeier,
2001). Further (optionally) supported features:
- All implementations, except for the minimalistic code for
reading, provide an independent restart procedure with increasing
population size (Auger
and Hansen, 2005) and standardized data output for
visualization.
- All implementations, except for the minimalistic code
for reading and the C version, optionally support a "separable"
initial phase, where the covariance matrix remains diagonal (Ros and Hansen, 2008). The
latter permits a faster learning of the diagonal elements and reduces the internal time complexity from quadratic to linear which might be useful for dimensionalities, say, larger than a hundred.
- Matlab version 3.x supports an additional uncertainty handling (Hansen et al, 2009, set option Noise.on='yes').
- Matlab version since 3.40.beta implements the idea of active CMA (Jastrebski and Arnold, 2006), where a rank-µ matrix is substracted from the original covariance matrix, therefore explicitely reducing the variance in directions where bad samples were found. The implementation deviates from the original paper.
Code for Reading
purecmaes.m is a
minimalistic implementation running in Matlab and
Octave. It is provided for reading and understanding. It contains
roughly 80 lines, where 20 lines are of algorithmic
interest. This implementation produces only rudimentary output and
should probably not be used for running "real" experiments.
C (ANSI)
cmaes_c.tar.gz, cmaes_c.tar provides a pure ANSI C
implementation in a slightly object-oriented way, where the iteration
step can be explicitly controlled. Therefore, the integration into any
existing program in C or C++, should be fairly easy.
C++
A few libraries have implemented the CMA-ES in C++:
Fortran
The pCMALib in FORTRAN 90 will be soon available, hopefully.
Java
A Java version is provided, where originally the core procedures from the ANSI C version were
used:
Also the OpenOpal OPtimization And Learning environment implements the CMA-ES in Java
Matlab and Octave
cmaes.m
(version 3, revised June 2008, former versions) provides an
interface similar to Matlab's optimization routines, e.g. fminsearch
and fminunc. The following (optional) variants/extensions are implemented.
- option LBounds and UBounds: coordinate wise boundary handling (Hansen et al, 2009)
- option Noise.on: uncertainty (noise) handling (Hansen et al, 2009)
- option CMA.active: a substractive update of the covariance matrix, similar to Jastrebski and Arnold (2006) (set option CMA.active = 1 with check for positive definiteness or CMA.active = 2 without checking). This version might become the default in near future.
- option DiagonalOnly: only the diagonal of the
covariance matrix is adapted for a number of initial iterations (Ros
and Hansen, 2008) which leads to a faster learning of a variable scaling and also reduces the internal time complexity of the algorithm from quadratic to linear (meanwhile no correlations are learned)
The
verbosity options have been rectified as Disp Log and Save options.
By default (LogModulo=1), output data is written to files
(ASCII) and plotcmaesdat.m is a stand
alone function to plots these data. The function can be used to
investigate the output data from cmaes.m while the optimization is
running. Under Octave plotcmaesdat.m works since Version 3,
for earlier versions option LogPlot of cmaes.m must be set to zero
(package octave-forge is needed in any case).
cmaesV255.m (version 2.55, July 2007) is
the former version which saves the data record history internally. plotcmaes.m and dispcmaes.m are stand alone functions to
plot/display data output from cmaes.m before version 3. They can be
used to investigate the output data while the
optimization is still running.
For Octave the package octave-forge is needed (thanks to
Luís Correia for the advise).
Python
The preliminary documentation is online. Contact me,
hansen (AT) lri.fr, for the source code.
Scilab
Two interfaces are provided. An object oriented
"ask-and-tell" interface (cma_new etc.), where the loop
control remains with the user, and a functional interface (cma_optim).
Sample Output
A sample output from a run on the 12-dimensional
Rosenbrock function, using plotcmaesdat.m.
The lower figures show the square root of eigenvalues (left) and of
diagonal elements (right) of the covariance matrix C. The actual sample
distribution has the covariance matrix σ2 C.
Testing your own source code (under construction)
Testing a stochastic optimization code is intricate because the algorithm
might be "robust" with respect to bugs. A CMA-ES code might run well
on quite a few test functions however an unobserved coding error
might later compromise its performance.
A simple and most effective way of testing is, to compare two codes,
furnished with the same "random" numbers. One can use a simple
one-line random number generator. The numbers need not to be normal,
but should be continuous and symmetric about zero. Tracking the internal state
variables for two iterations must lead to identical results (apart
from numerical effects that are usually in the order of
10-15). Internal state variables are the distribution mean,
the step-size and the covariance matrix, and furthermore the two
evolution paths. For comparison, one of the above codes can be
used.
Further, the code should also be tested on functions that can be found, for
example, in purecmaes.m, and the results
should be compared to one of the above codes.
- On a random
objective function frand (e.g. f(x) is uniform in [0,1] and
independent of its input x). Step-size σ and the eigenvalues of
C should be tracked and plotted. log(σ) must conduct a symmetric
(unbiased) random walk. The iteration-wise sorted log(eigenvalues) will slowly
drift apart, quite symmetrically but with the overall dendency to
decrease. Finally the condition number of C will exceed numerically
feasible limits, which should be covered in the code as termination
criterion.
- The linear function fplane (e.g. f(x) = x(1))
should be tested (X0=ones,sigma0=1,stopfitness=-1e5).
- The
sphere function fsphere should be tested
(X0=ones,sigma0=1,stopfitness=1e-10).
- The Ellipsoid felli
should be tested (X0=ones, sigma0=1, stopfitness=1e-10). The
eigenvalues get a nice regular configuration on this function.
- The Cigar fcigar should be tested (X0=ones, sigma0=1,
stopfitness=1e-10).
- The Rosenbrock function frosen is a good
test case (X0=zeros, sigma0=0.1, stopfitness=1e-10). Occasionally the
local minimum will be discovered with a function value close under
four.
- Finally, frastrigin10 (X0=5*ones, sigma0=5, stopfitness=1e-10),
where only large populations (about larger 10n) will find the
global optimum with a reasonable probability.
Ideally, all tests should be done
in different dimensions, e.g., 3,10,30, and for the default population
size and a large one (if the rank-μ update was implemented), say
20n.
I appreciate any feedback regarding the provided source code and regarding
successful and unsuccessful applications or modifications of the
CMA-ES.
Mail to hansen AT lri.fr .
last change $Date: 2010-01-11 19:50:55 +0100 (Mon, 11 Jan 2010) $