module documentation

Utility classes and functionalities loosely related to optimization

Class BestSolution container to keep track of the best solution seen.
Class BestSolution2 minimal tracker of a smallest f-value with variable meta-info
Class BinaryEvolutionPath No class docstring; 2/2 properties, 1/1 method documented
Class EvalParallel2 A class and context manager for parallel evaluations.
Class EvolutionPath not in use (yet)
Class ExponentialSmoothing not in use (yet)
Class NoiseHandler Noise handling according to [Hansen et al 2009, A Method for Handling Uncertainty in Evolutionary Optimization...]
Class OldEvolutionPath not in use (yet)
Class Sections plot sections through an objective function.
Function contour_data generate x,y,z-data for contour plot.
Function semilogy_signed signed semilogy plot.
Function step_data return x, y ECDF data for ECDF plot. Smoothing may look strange in a semilogx plot.
def contour_data(fct, x_range, y_range=None):

generate x,y,z-data for contour plot.

fct is a 2-D function. x- and y_range are iterable (e.g. list or arrays) to define the meshgrid.

CAVEAT: this function calls fct len(list(x_range)) * len(list(y_range)) times. Hence using Sections may be the better first choice to investigate an expensive function.

Examples:

>>> from cma import optimization_tools
>>> import numpy as np
...
>>> def plt_contour():  # def avoids doctest execution
...     from matplotlib import pyplot as plt
...
...     X, Y, Z = optimization_tools.contour_data(
...                   lambda x: sum([xi**2 for xi in x]),
...                   np.arange(0.90, 1.10, 0.02),
...                   np.arange(-0.10, 0.10, 0.02))
...     CS = plt.contour(X, Y, Z)
...     plt.gca().set_aspect('equal')
...     plt.clabel(CS)
>>> def plt_surface():  # def avoids doctest execution
...     from matplotlib import pyplot as plt
...     from mpl_toolkits import mplot3d
...
...     X, Y, Z = optimization_tools.contour_data(
...                   lambda x: sum([xi**2 for xi in x]),
...                   np.arange(-1, 1.1, 0.02))
...     ax = plt.axes(projection='3d')
...     ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')

See cma.fitness_transformations.FixVariables to create a 2-D function from a d-D function, e.g. like

>>> import cma
...
>>> fd = cma.ff.elli
>>> x0 = np.zeros(22)
>>> indices_to_vary = [2, 4]
>>> f2 = cma.fitness_transformations.FixVariables(fd,
...          dict((i, x0[i]) for i in range(len(x0))
...                          if i not in indices_to_vary))
>>> isinstance(f2, cma.fitness_transformations.FixVariables)
True
>>> isinstance(f2, cma.fitness_transformations.ComposedFunction)
True
>>> f2[0] is fd, len(f2) == 2
(True, True)
def semilogy_signed(x=None, y=None, yoffset=0, minabsy=None, iabscissa=1, **kwargs):

signed semilogy plot.

plt.yscale('symlog', linthreshy=min(abs(data[data != 0]))) should do the same job as least as good.

y (or x if y is None) is a data array, by default read from outcmaesxmean.dat or (first) from the default logger output file like:

xy = cma.logger.CMADataLogger().load().data['xmean']
x, y = xy[:, iabscissa], xy[:, 5:]
semilogy_signed(x, y)

Plotted is y - yoffset vs x for positive values as a semilogy plot and for negative values as a semilogy plot of absolute values with inverted axis.

minabsy controls the minimum shown value away from zero, which can be useful if extremely small non-zero values occur in the data.

def step_data(data, smooth_corners=0.1):

return x, y ECDF data for ECDF plot. Smoothing may look strange in a semilogx plot.