Skip to main content

CMA-ES, Covariance Matrix Adaptation Evolution Strategy for non-linear numerical optimization in Python

Project description

A stochastic numerical optimization algorithm for difficult (non-convex, ill-conditioned, multi-modal, rugged, noisy) optimization problems in continuous search spaces, implemented in Python.

Typical domain of application are bound-constrained or unconstrained objective functions with:

  • search space dimension between, say, 5 and (a few) 100,

  • no gradients available,

  • at least, say, 100 times dimension function evaluations needed to get satisfactory solutions,

  • non-separable, ill-conditioned, or rugged/multi-modal landscapes.

The CMA-ES is quite reliable, however for small budgets (fewer function evaluations than, say, 100 times dimension) or in very small dimensions better (i.e. faster) methods are available.

The pycma module provides two independent implementations of the CMA-ES algorithm in the classes cma.CMAEvolutionStrategy and cma.purecma.CMAES.

Installation

There are several ways of installation:

  • In the terminal command line type:

    python -m pip install cma

    Typing just pip instead of python -m pip may be sufficient. Or, alternatively:

    easy_install cma

    The package will be downloaded and installed automatically. To upgrade an existing installation, ‘cma’ must be replaced by ‘-U cma’ in both cases. If you never heard of pip, see here.

  • Download and unpack the cma-...tar.gz file and type:

    pip install -e cma

    or:

    python setup.py install

    in the cma-... folder (under Windows just “setup.py install”).

  • Under Windows one may also download the MS Windows installer.

Installation might require root privileges. In this case, try the --user option of pip or prepended with sudo.

The folder cma from the tar archive can also be used without any installation (just import needs to find it).

Usage Example

In a Python shell:

>>> import cma
>>> help(cma)
    <output omitted>
>>> es = cma.CMAEvolutionStrategy(8 * [0], 0.5)
(5_w,10)-aCMA-ES (mu_w=3.2,w_1=45%) in dimension 8 (seed=468976, Tue May  6 19:14:06 2014)
>>> help(es)  # the same as help(cma.CMAEvolutionStrategy)
    <output omitted>
>>> es.optimize(cma.ff.rosen)
Iterat #Fevals   function value    axis ratio  sigma  minstd maxstd min:sec
    1      10 1.042661803766204e+02 1.0e+00 4.50e-01  4e-01  5e-01 0:0.0
    2      20 7.322331708590002e+01 1.2e+00 3.89e-01  4e-01  4e-01 0:0.0
    3      30 6.048150359372417e+01 1.2e+00 3.47e-01  3e-01  3e-01 0:0.0
  100    1000 3.165939452385367e+00 1.1e+01 7.08e-02  2e-02  7e-02 0:0.2
  200    2000 4.157333035296804e-01 1.9e+01 8.10e-02  9e-03  5e-02 0:0.4
  300    3000 2.413696640005903e-04 4.3e+01 9.57e-03  3e-04  7e-03 0:0.5
  400    4000 1.271582136805314e-11 7.6e+01 9.70e-06  8e-08  3e-06 0:0.7
  439    4390 1.062554035878040e-14 9.4e+01 5.31e-07  3e-09  8e-08 0:0.8
>>> es.result_pretty()  # pretty print result
termination on tolfun=1e-11
final/bestever f-value = 3.729752e-15 3.729752e-15
mean solution: [ 1.          1.          1.          1.          0.99999999  0.99999998
  0.99999995  0.99999991]
std deviation: [  2.84303359e-09   2.74700402e-09   3.28154576e-09   5.92961588e-09
   1.07700123e-08   2.12590385e-08   4.09374304e-08   8.16649754e-08]

optimizes the 8-dimensional Rosenbrock function with initial solution all zeros and initial sigma = 0.5.

Pretty much the same can be achieved a little less “elaborate” with:

>>> import cma
>>> xopt, es = cma.fmin2(cma.ff.rosen, 8 * [0], 0.5)
    <output omitted>

And a little more elaborate exposing the ask-and-tell interface:

>>> import cma
>>> es = cma.CMAEvolutionStrategy(12 * [0], 0.5)
>>> while not es.stop():
...     solutions = es.ask()
...     es.tell(solutions, [cma.ff.rosen(x) for x in solutions])
...     es.logger.add()  # write data to disc to be plotted
...     es.disp()
    <output omitted>
>>> es.result_pretty()
    <output omitted>
>>> cma.plot()  # shortcut for es.logger.plot()
CMA-ES on Rosenbrock function in dimension 8

A single run on the 12-dimensional Rosenbrock function.

The CMAOptions class manages options for CMAEvolutionStrategy, e.g. verbosity options can be found like:

>>> import cma
>>> cma.s.pprint(cma.CMAOptions('erb'))
{'verb_log': '1  #v verbosity: write data to files every verb_log iteration, writing can be time critical on fast to evaluate functions'
 'verbose': '1  #v verbosity e.v. of initial/final message, -1 is very quiet, not yet implemented'
 'verb_plot': '0  #v in fmin(): plot() is called every verb_plot iteration'
 'verb_disp': '100  #v verbosity: display console output every verb_disp iteration'
 'verb_filenameprefix': 'outcmaes  # output filenames prefix'
 'verb_append': '0  # initial evaluation counter, if append, do not overwrite output files'
 'verb_time': 'True  #v output timings on console'}

Options are passed like:

>>> import cma
>>> es = cma.CMAEvolutionStrategy(8 * [0], 0.5,
                                  {'verb_disp': 1}) # display each iteration

Documentations

Read the full package documentation:

See also

Dependencies

  • required: numpy – array processing for numbers, strings, records, and objects

  • optional (highly recommended): matplotlib – Python plotting package (includes pylab)

Use pip install numpy etc. for installation. For a Python implementation of CMA-ES with lesser dependencies see here.

License: BSD

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cma-2.7.0.tar.gz (225.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cma-2.7.0.macosx-10.7-x86_64.exe (700.6 kB view details)

Uploaded Source

cma-2.7.0-py2.py3-none-any.whl (239.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cma-2.7.0.tar.gz.

File metadata

  • Download URL: cma-2.7.0.tar.gz
  • Upload date:
  • Size: 225.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.7

File hashes

Hashes for cma-2.7.0.tar.gz
Algorithm Hash digest
SHA256 f4630d405efdbf4d85ba42b6bdfe5708b716bb088880b938b60c3bfbf1c1cd59
MD5 082de4a0157938e863742a76dbdfb418
BLAKE2b-256 7812bc7256b9bd56051f796ff1bad70f91a38c9a241e6a1e9abb9a49b52394c7

See more details on using hashes here.

File details

Details for the file cma-2.7.0.macosx-10.7-x86_64.exe.

File metadata

  • Download URL: cma-2.7.0.macosx-10.7-x86_64.exe
  • Upload date:
  • Size: 700.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.7

File hashes

Hashes for cma-2.7.0.macosx-10.7-x86_64.exe
Algorithm Hash digest
SHA256 924946fcf2e94911956d02bb75121e4aec325eb2fc148c6edf97337eddef8418
MD5 22285c75dd9d02eb2128871f5e05682f
BLAKE2b-256 f102d87650ccee6db0f27d121861f2de57ef5df2c3e1ef0a06bbcd0a17b48a80

See more details on using hashes here.

File details

Details for the file cma-2.7.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cma-2.7.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 239.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.7

File hashes

Hashes for cma-2.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 136e457c97e837152404b868c4ec6f163dee923fcee8b94ff446d13d4242b227
MD5 db09a158a703610223d152b5fb09824f
BLAKE2b-256 b93b87a4efbcfeaf3172d81ef843f0b0c34c3ba60ec884aa6777f34f68b57418

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page