Module barecmaes2
[hide private]
[frames] | no frames]

Module barecmaes2

source code

Module barecmaes2 implements the CMA-ES without using numpy.

The Covariance Matrix Adaptation Evolution Strategy, CMA-ES, serves for nonlinear function minimization.

The main functionality is implemented in

This code has two purposes:

  1. it might be used for READING and UNDERSTANDING the basic flow and the details of the CMA-ES algorithm. The source code is meant to be read. For short, study the class CMAES, in particular its doc string and the code of the method CMAES.tell(), where all the real work is done in about 20 lines (see "def tell" in the source). Otherwise, reading from the top is a feasible option.
  2. it might be used when the python module numpy is not available. When numpy is available, rather use cma.py (see http://www.lri .fr/~hansen/cmaes_inmatlab.html#python) to run serious simulations: the latter code has many more lines, but executes much faster (roughly ten times), offers a richer user interface, far better termination options, supposedly quite useful output, and automatic restarts.

Dependencies: math.exp, math.log and random.normalvariate (modules matplotlib.pylab and sys are optional).

Testing: call python barecmaes2.py at the OS shell. Tested with Python 2.5 (only with removed import of print_function), 2.6, 2.7, 3.2. Small deviations between the versions are expected.

URL: http://www.lri.fr/~hansen/barecmaes2.py

Last change: May, 2014, version 2.00


Author: Nikolaus Hansen, 2010-2011, hansen[at-symbol]lri.fr

License: This code is released into the public domain (that is, you may use and modify it however you like).

Version: 1.10

Classes [hide private]
  OOOptimizer
"abstract" base class for an OO optimizer interface.
  CMAES
class for non-linear non-convex minimization. The class implements the interface define in OOOptimizer, namely the methods __init__(), ask(), tell(), stop(), result(), and disp().
  BestSolution
container to keep track of the best solution seen
  OptimDataLogger
"abstract" base class for a data logger that can be used with an OOOptimizer
  CMAESDataLogger
data logger for class CMAES, that can store and plot data.
  Fcts
versatile collection of test functions
Functions [hide private]
 
fmin(objectivefct, xstart, sigma, args=(), max_eval='1e3*N**2', ftarget=None, verb_disp=100, verb_log=1, verb_plot=100)
non-linear non-convex minimization procedure. The functional interface to CMA-ES.
source code
 
eye(dimension)
return identity matrix as list of "vectors"
source code
 
dot(A, b, t=False)
usual dot product of "matrix" A with "vector" b, where A[i] is the i-th row of A. With t=True, A transposed is used.
source code
 
dot1(a, b)
scalar a times vector b
source code
 
plus(a, b)
add vectors, return a + b
source code
 
minus(a, b)
substract vectors, return a - b
source code
 
argsort(a)
return index list to get a in order, ie a[argsort(a)[i]] == sorted(a)[i]
source code
 
eig(C)
eigendecomposition of a symmetric matrix, much slower than numpy.linalg.eigh, return the eigenvalues and an orthonormal basis of the corresponding eigenvectors, (EVals, Basis), where
source code
 
_test() source code
Variables [hide private]
  __author__ = 'Nikolaus Hansen'
  __package__ = None
Function Details [hide private]

fmin(objectivefct, xstart, sigma, args=(), max_eval='1e3*N**2', ftarget=None, verb_disp=100, verb_log=1, verb_plot=100)

source code 

non-linear non-convex minimization procedure. The functional interface to CMA-ES.

Parameters

objectivefct
a function that takes as input a list of floats (like [3.0, 2.2, 1.1]) and returns a single float (a scalar). The objective is to find x with objectivefct(x) to be as small as possible.
xstart
list of numbers (like [3,2,1.2]), initial solution vector
sigma
float, initial step-size, standard deviation in any coordinate
args
arguments to objectivefct
ftarget
float, target function value
max_eval
int or str, maximal number of function evaluations, a string is evaluated with N being the search space dimension
verb_disp
int, display on console every verb_disp iteration, 0 for never
verb_log
int, data logging every verb_log iteration, 0 for never
verb_plot
int, plot logged data every verb_plot iteration, 0 for never

Returns

return es.result() + (es.stop(), es, logger), that is, (xbest, fbest, evalsbest, evals, iterations, xmean, `` termination_condition, CMAES_object_instance, data_logger)``

Example

The following example minimizes the function Fcts.elli:

>> import barecmaes2 as cma
>> res = cma.fmin(cma.Fcts.elli, 10 * [0.5], 0.3, verb_disp=100)
evals: ax-ratio max(std)   f-value
   10:     1.0  2.8e-01  198003.585517
 1000:     8.4  5.5e-02  95.9162313173
 2000:    40.5  3.6e-02  5.07618122556
 3000:   149.1  8.5e-03  0.271537247667
 4000:   302.2  4.2e-03  0.0623570374451
 5000:   681.1  5.9e-03  0.000485971681802
 6000:  1146.4  9.5e-06  5.26919100476e-10
 6510:  1009.1  2.3e-07  3.34128914738e-13
termination by {'tolfun': 1e-12}
best f-value = 3.34128914738e-13

>> print(res[0])
[2.1187532328944602e-07, 6.893386424102321e-08, -2.008255256456535e-09, 4.472078873398156e-09, -9.421306741003398e-09, 7.331265238205156e-09, 2.4804701814730273e-10, -6.030651566971234e-10, -6.063921614755129e-10, -1.066906137937511e-10]

>> print(res[1])
3.34128914738e-13

>> res[-1].plot()  # needs pylab/matplotlib to be installed

Details

By default fmin() tries to plot some output. This works only with Python module matplotlib being installed. The first and last lines

cma.fmin(cma.Fcts.elli, 10 * [0.5], 0.3, verb_plot=0)
logger = cma.CMADataLogger()
cma.CMAES(10 * [0.5], 0.3).optimize(cma.Fcts.elli, logger=logger)

do the same. fmin() adds the possibility to plot during the execution.

See Also: CMAES, OOOptimizer

dot(A, b, t=False)

source code 

usual dot product of "matrix" A with "vector" b, where A[i] is the i-th row of A. With t=True, A transposed is used.

:rtype : "vector" (list of float)

eig(C)

source code 

eigendecomposition of a symmetric matrix, much slower than numpy.linalg.eigh, return the eigenvalues and an orthonormal basis of the corresponding eigenvectors, (EVals, Basis), where

Basis[i]
the i-th row of Basis and columns of Basis,
[Basis[j][i] for j in range(len(Basis))]
the i-th eigenvector with eigenvalue EVals[i]