class documentation

class Sections(object):

View In Hierarchy

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.ff.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__
Method __init__ objective function
Method do 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)...
Method flattened 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])
Method load load from file
Method plot plot the data we have, return self
Method save save to file
Instance Variable args Undocumented
Instance Variable basis Undocumented
Instance Variable func Undocumented
Instance Variable name Undocumented
Instance Variable plot_cmd Undocumented
Instance Variable res Undocumented
Instance Variable x Undocumented
def __init__(self, func, x, args=(), basis=None, name=None, plot_cmd=None, load=True):
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'
def do(self, repetitions=1, locations=np.arange(-0.5, 0.6, 0.2), 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).

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__
def 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])

def load(self, name=None):

load from file

def plot(self, plot_cmd=None, tf=(lambda y: y)):

plot the data we have, return self

def save(self, name=None):

save to file

args =

Undocumented

basis =

Undocumented

func =

Undocumented

name =

Undocumented

plot_cmd =

Undocumented

res =

Undocumented

x =

Undocumented