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

Class Sections

source code

object --+
         |
        Sections

plot sections through an objective function.

A first rational thing to do, when facing an (expensive) application. By default 6 points in each coordinate are evaluated. This class is still experimental.

Examples

>>> import cma, numpy as np
>>> s = cma.Sections(cma.Fcts.rosen, np.zeros(3)).do(plot=False)
>>> s.do(plot=False)  # evaluate the same points again, i.e. check for noise
>> try:
...     s.plot()
... except:
...     print('plotting failed: matplotlib.pyplot package missing?')

Details

Data are saved after each function call during do(). The filename is attribute name and by default str(func), see __init__().

A random (orthogonal) basis can be generated with cma.Rotation()(np.eye(3)).

CAVEAT: The default name is unique in the function name, but it should be unique in all parameters of __init__() but plot_cmd and load. If, for example, a different basis is chosen, either the name must be changed or the .pkl file containing the previous data must first be renamed or deleted.

s.res is a dictionary with an entry for each "coordinate" i and with an entry 'x', the middle point. Each entry i is again a dictionary with keys being different dx values and the value being a sequence of f-values. For example s.res[2][0.1] == [0.01, 0.01], which is generated using the difference vector s .basis[2] like

s.res[2][dx] += func(s.res['x'] + dx * s.basis[2]).


See Also: __init__()

Instance Methods [hide private]
 
__init__(self, func, x, args=(), basis=None, name=None, plot_cmd=<function plot at 0x106bd6140>, load=True)
objective function
source code
 
do(self, repetitions=1, locations=array([-0.5, -0.3, -0.1, 0.1, 0.3, 0.5]), plot=True)
generates, plots and saves function values func(y), where y is 'close' to x (see __init__()). The data are stored in the res attribute and the class instance is saved in a file with (the weired) name str(func).
source code
 
plot(self, plot_cmd=None, tf=<function <lambda> at 0x106bf2e60>)
plot the data we have, return self
source code
 
flattened(self)
return flattened data (x, f) such that for the sweep through coordinate i we have for data point j that f[i][j] == func(x[i][j])
source code
 
save(self, name=None)
save to file
source code
 
load(self, name=None)
load from file
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, func, x, args=(), basis=None, name=None, plot_cmd=<function plot at 0x106bd6140>, load=True)
(Constructor)

source code 
func
objective function
x
point in search space, middle point of the sections
args
arguments passed to func
basis
evaluated points are func(x + locations[j] * basis[i]) for i in len(basis) for j in len(locations), see do()
name
filename where to save the result
plot_cmd
command used to plot the data, typically matplotlib pyplots plot or semilogy
load
load previous data from file str(func) + '.pkl'
Overrides: object.__init__

do(self, repetitions=1, locations=array([-0.5, -0.3, -0.1, 0.1, 0.3, 0.5]), plot=True)

source code 

generates, plots and saves function values func(y), where y is 'close' to x (see __init__()). The data are stored in the res attribute and the class instance is saved in a file with (the weired) name str(func).

Parameters

repetitions
for each point, only for noisy functions is >1 useful. For repetitions==0 only already generated data are plotted.
locations
coordinated wise deviations from the middle point given in __init__