Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
libcmaes is a multithreaded C++11 library with Python bindings for high performance blackbox stochastic optimization using the CMA-ES algorithm for Covariance Matrix Adaptation Evolution Strategy
Failed to load latest commit information.
doc added PDF documentation to lib
examples new default value for RankSVM number of iterations, ref #57, #106
m4 added detection of lib boost python for building the Python wrapper, …
python added README.legacy with explanations on how to plot with legacy form…
src fixed profile likelihood with geno/pheno transforms + unit tests, fixes
tests fixed profile likelihood with geno/pheno transforms + unit tests, fixes
.travis.yml Revert "tentative gtest on travis with linux, ref #122"
COPYING added LGPLv3 license
ChangeLog toward 0.9.5
INSTALL Merge branch 'master' into python
NEWS updated README, ref #116 initial commit, alpha version toward 0.9.5
depcomp initial commit, alpha version
install-sh initial commit, alpha version fixed pkg-config file


libcmaes is a multithreaded C++ implementation (with Python bindings) of the CMA-ES algorithm for optimization of nonlinear non-convex 'blackbox' functions. The implemented algorithms have a wide range of applications in various disciplines, ranging from pure function minimization, optimization in industrial and scientific applications, to the solving of reinforcement and machine learning problems.

Over the past decade, both the original CMA-ES and its improved flavors have proven very effective in optimizing functions when no gradient is available. Typically, the algorithm does find the minimum value of an objective function in a minimal number of function calls, compared to other methods. For a full report of recent results, see (3).

CMA-ES is mostly the work of Nikolaus Hansen (4) and a few others (8). Other implementations can be found in (5).

Main functionalities: At the moment, the library implements a vanilla version of CMA-ES (1). Current features include:

  • high-level API for simple use in external applications;
  • implements several flavors of CMA-ES, IPOP-CMA-ES, BIPOP-CMA-ES, active CMA-ES, active IPOP and BIPOP restart strategies, sep-CMA-ES and VD-CMA (linear time & space complexity) along with support for IPOP and BIPOP flavors as well;
  • high performances, the fastest implementation (9), some operations benefit from multicores;
  • scales up to millions of dimensions;
  • support for objective function gradient, when available;
  • support for surrogate models(10);
  • a control exe in the command line for running the algorithm over a range of classical single-objective optimization problems.
  • python bindings



  • eigen for all matrix operations;
  • glog for logging events and debug (optional);
  • gflags for command line parsing (optional);
  • gtest for unit testing (optional);
  • libboost-python for Python bindings (optional).

Implementation: The library makes use of C++ policy design for modularity, performance and putting the maximum burden onto the compile-time checks. The implementation closely follows the algorithms described in (2) and (6).


libcmaes is designed and implemented by Emmanuel Benazera on behalf of Inria Saclay / Research group TAO / LAL Appstats.


Below are instruction for Linux systems, for building on Mac, see

Beware of dependencies, typically on Debian/Ubuntu Linux, do:

sudo apt-get install autoconf automake libtool libgoogle-glog-dev libgflags-dev libeigen3-dev

For compiling with basic options enabled:


Run examples

cd tests
./test_functions --dim 30 --lambda 100 --max_iter 120 --fname fsphere

to minimize the sphere function in 30D with 100 offsprings per generation,

./test_functions --dim 20 --lambda 100 --max_iter 1000 --fname rosenbrock

to minimize the Rosenbrock function in 20D with 100 offsprings. To see available function, do

./test_functions --list

to plot results, use a file output and then the included Gnuplot script

./test_functions --fname rastrigin --dim 10 --lambda 200 --max_iter 130 --fplot out.dat -sigma0 5 -x0 5 -seed 5489
gnuplot -e "filename='out.dat'" cma_multiplt.dem

to plot results with matplotlib instead

python ../python/ out.dat

to run a check across a range of classical single-objective optimization functions:

./test_functions --all

for help, do

./test_functions --help

Sample code

#include "cmaes.h"
#include <iostream>

using namespace libcmaes;

FitFunc fsphere = [](const double *x, const int N)
  double val = 0.0;
  for (int i=0;i<N;i++)
    val += x[i]*x[i];
  return val;

int main(int argc, char *argv[])
  int dim = 10; // problem dimensions.
  std::vector<double> x0(dim,10.0);
  double sigma = 0.1;
  //int lambda = 100; // offsprings at each generation.
  CMAParameters<> cmaparams(dim,&x0.front(),sigma);
  CMASolutions cmasols = cmaes<>(fsphere,cmaparams);
  std::cout << "best solution: " << cmasols << std::endl;
  std::cout << "optimization took " << cmasols.elapsed_time() / 1000.0 << " seconds\n";
  return cmasols.run_status();

Python bindings

To build the Python bindings and use libcmaes from Python code:

  • install 'boost-python', on Debian/Ubuntu systems:
sudo apt-get install libboost-python-dev
  • build the libcmaes with support for Python bindings:
./configure --enable-python --with-prefix=/home/yourusername
make install
  • test the bindings:
cd python
export LD_LIBRARY_PATH=/home/yourusername/lib

Sample python code:

import lcmaes

# input parameters for a 10-D problem
x = [10]*10
olambda = 10 # lambda is a reserved keyword in python, using olambda instead.
seed = 0 # 0 for seed auto-generated within the lib.
sigma = 0.1
p = lcmaes.make_simple_parameters(x,sigma,olambda,seed)

# objective function.
def nfitfunc(x,n):
    val = 0.0
    for i in range(0,n):
        val += x[i]*x[i]
    return val

# generate a function object
objfunc = lcmaes.fitfunc_pbf.from_callable(nfitfunc);

# pass the function and parameter to cmaes, run optimization and collect solution object.
cmasols = lcmaes.pcmaes(objfunc,p)

# collect and inspect results
bcand = cmasols.best_candidate()
bx = lcmaes.get_candidate_x(bcand)
print "best x=",bx
print "distribution mean=",lcmaes.get_solution_xmean(cmasols)
cov = lcmaes.get_solution_cov(cmasols) # numpy array
print "cov=",cov
print "elapsed time=",cmasols.elapsed_time(),"ms"

Practical hints

CMA-ES requires two components from the user:

  • the initial start point x0;
  • the initial value for sigma, the so-called step-size or error guess.

In short: the optimum that is looked after should better not be far away from the interval [x0 - sigma0, x0 + sigma0] in each dimension, where distance is defined by sigma0.

See for more detailed useful advices using CMA-ES.

Run BBOB 2013 Black-Box Optimization Benchmark

There's an install script in the repository. Do:

cd tests

you can now benchmark any of the implemented flavors of CMA-ES (beware, this make take a while, ~hours):

./bbobexperiment -alg bipop

for the command above, results will be in repository bipop_bbob See (7) for more information and details.


Something went wrong with that request. Please try again.