Module cma :: Class OOOptimizer
[hide private]
[frames] | no frames]

Class OOOptimizer

source code

object --+
         |
        OOOptimizer
Known Subclasses:

"abstract" base class for an Object Oriented Optimizer interface.

Relevant methods are __init__, ask, tell, stop, result, and optimize. Only optimize is fully implemented in this base class.

Examples

All examples minimize the function elli, the output is not shown. (A preferred environment to execute all examples is ipython in %pylab mode.)

First we need:

from cma import CMAEvolutionStrategy
# CMAEvolutionStrategy derives from the OOOptimizer class
felli = lambda x: sum(1e3**((i-1.)/(len(x)-1.)*x[i])**2 for i in range(len(x)))

The shortest example uses the inherited method OOOptimizer.optimize():

es = CMAEvolutionStrategy(8 * [0.1], 0.5).optimize(felli)

The input parameters to CMAEvolutionStrategy are specific to this inherited class. The remaining functionality is based on interface defined by OOOptimizer. We might have a look at the result:

print(es.result()[0])  # best solution and
print(es.result()[1])  # its function value

In order to display more exciting output we do:

es.logger.plot()  # if matplotlib is available

Virtually the same example can be written with an explicit loop instead of using optimize(). This gives the necessary insight into the OOOptimizer class interface and entire control over the iteration loop:

optim = CMAEvolutionStrategy(9 * [0.5], 0.3)
# a new CMAEvolutionStrategy instance

# this loop resembles optimize()
while not optim.stop():  # iterate
    X = optim.ask()      # get candidate solutions
    f = [felli(x) for x in X]  # evaluate solutions
    #  in case do something else that needs to be done
    optim.tell(X, f)     # do all the real "update" work
    optim.disp(20)       # display info every 20th iteration
    optim.logger.add()   # log another "data line"

# final output
print('termination by', optim.stop())
print('best f-value =', optim.result()[1])
print('best solution =', optim.result()[0])
optim.logger.plot()  # if matplotlib is available

Details

Most of the work is done in the method tell(...). The method result() returns more useful output.

Instance Methods [hide private]
 
__init__(self, xstart, **more_args)
xstart is a mandatory argument
source code
 
initialize(self)
(re-)set to the initial state
source code
 
ask(self, gradf=None, **more_args)
abstract method, AKA "get" or "sample_distribution", deliver new candidate solution(s), a list of "vectors"
source code
 
tell(self, solutions, function_values)
abstract method, AKA "update", pass f-values and prepare for next iteration
source code
 
stop(self)
abstract method, return satisfied termination conditions in a dictionary like {'termination reason': value, ...}, for example {'tolfun': 1e-12}, or the empty dictionary {}. The implementation of stop() should prevent an infinite loop.
source code
 
disp(self, modulo=None)
abstract method, display some iteration infos if self.iteration_counter % modulo == 0
source code
 
result(self)
abstract method, return (x, f(x), ...), that is, the minimizer, its function value, ...
source code
 
optimize(self, objective_fct, iterations=None, min_iterations=1, args=(), verb_disp=None, logger=None, call_back=None)
find minimizer of objective_fct.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, xstart, **more_args)
(Constructor)

source code 
xstart is a mandatory argument
Overrides: object.__init__

optimize(self, objective_fct, iterations=None, min_iterations=1, args=(), verb_disp=None, logger=None, call_back=None)

source code 

find minimizer of objective_fct.

CAVEAT: the return value for optimize has changed to self.

Arguments

objective_fct
function be to minimized
iterations
number of (maximal) iterations, while not self.stop()
min_iterations
minimal number of iterations, even if not self.stop()
args
arguments passed to objective_fct
verb_disp
print to screen every verb_disp iteration, if None the value from self.logger is "inherited", if available.
logger
a BaseDataLogger instance, which must be compatible with the type of self.
call_back
call back function called like call_back(self) or a list of call back functions.

return self, that is, the OOOptimizer instance.

Example

>>> import cma
>>> es = cma.CMAEvolutionStrategy(7 * [0.1], 0.5
...              ).optimize(cma.fcts.rosen, verb_disp=100)
(4_w,9)-CMA-ES (mu_w=2.8,w_1=49%) in dimension 7 (seed=630721393)
Iterat #Fevals   function value    axis ratio  sigma   minstd maxstd min:sec
    1       9 3.163954777181882e+01 1.0e+00 4.12e-01  4e-01  4e-01 0:0.0
    2      18 3.299006223906629e+01 1.0e+00 3.60e-01  3e-01  4e-01 0:0.0
    3      27 1.389129389866704e+01 1.1e+00 3.18e-01  3e-01  3e-01 0:0.0
  100     900 2.494847340045985e+00 8.6e+00 5.03e-02  2e-02  5e-02 0:0.3
  200    1800 3.428234862999135e-01 1.7e+01 3.77e-02  6e-03  3e-02 0:0.5
  300    2700 3.216640032470860e-04 5.6e+01 6.62e-03  4e-04  9e-03 0:0.8
  400    3600 6.155215286199821e-12 6.6e+01 7.44e-06  1e-07  4e-06 0:1.1
  438    3942 1.187372505161762e-14 6.0e+01 3.27e-07  4e-09  9e-08 0:1.2
  438    3942 1.187372505161762e-14 6.0e+01 3.27e-07  4e-09  9e-08 0:1.2
('termination by', {'tolfun': 1e-11})
('best f-value =', 1.1189867885201275e-14)
('solution =', array([ 1.        ,  1.        ,  1.        ,  0.99999999,  0.99999998,
        0.99999996,  0.99999992]))
>>> print(es.result()[0])
array([ 1.          1.          1.          0.99999999  0.99999998  0.99999996
  0.99999992])