"abstract" base class for an Object Oriented Optimizer interface.
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
|
|
|
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__
|