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
C++ Shell M4 Python CMake Makefile C
Latest commit 7d289d2 Jan 27, 2017 @beniz committed on GitHub Merge pull request #158 from ithron/MSVC
MS Visual Studio compatibility


libcmaes is a multithreaded C++11 implementation (with Python bindings) of algorithms of the CMA-ES family 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 many of the most effective CMA-ES algorihms, and defaults to the 'vanilla' version (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 (both with linear time & space complexity), IPOP/BIPOP sep and VD-CMA;
  • high performances, the fastest implementation (9), some operations benefit from multicores;
  • some algorithms can scale up to millions of dimensions;
  • support for objective function gradient, when available;
  • support for surrogate models(10);
  • support for elitism and uncertainty handling;
  • support for profile likelihood and contour of objective functions;
  • a control exe in the command line for running the algorithm over a range of classical single-objective optimization problems;
  • Python bindings along with high-level easy-to-use interface.



  • 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);
  • numpy for Python bindings (optional).

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


libcmaes is designed and implemented by Emmanuel Benazera with help of Nikolaus Hansen, on behalf of Inria Saclay / Research group TAO and Laboratoir de l'Accélérateur linéaire, research group Appstats.


Below are instruction for Linux systems, for building on Mac OSX, 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:


For compiling with CMake:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=~/.local/ .
make -j2
make install

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(x0,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, see instructions below, and for more details, see

  • 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 and 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.



This work was supported by the ANR-2010-COSI-002 grant of the French NationalA Research Agency.