# Base¶

The base module provides basic structures to build evolutionary algorithms. It contains the Toolbox, useful to store evolutionary operators, and a virtual Fitness class used as base class, for the fitness member of any individual.

## Toolbox¶

class deap.base.Toolbox

A toolbox for evolution that contains the evolutionary operators. At first the toolbox contains a clone() method that duplicates any element it is passed as argument, this method defaults to the copy.deepcopy() function. and a map() method that applies the function given as first argument to every items of the iterables given as next arguments, this method defaults to the map() function. You may populate the toolbox with any other function by using the register() method.

Concrete usages of the toolbox are shown for initialization in the Creating Types tutorial and for tools container in the Operators and Algorithms tutorial.

register(alias, method[, argument[, ...]])

Register a function in the toolbox under the name alias. You may provide default arguments that will be passed automatically when calling the registered function. Fixed arguments can then be overriden at function call time.

Parameters: alias – The name the operator will take in the toolbox. If the alias already exist it will overwrite the the operator already present. function – The function to which refer the alias. argument – One or more argument (and keyword argument) to pass automatically to the registered function when called, optional.

The following code block is an example of how the toolbox is used.

>>> def func(a, b, c=3):
...     print a, b, c
...
>>> tools = Toolbox()
>>> tools.register("myFunc", func, 2, c=4)
>>> tools.myFunc(3)
2 3 4


The registered function will be given the attributes __name__ set to the alias and __doc__ set to the original function’s documentation. The __dict__ attribute will also be updated with the original function’s instance dictionary, if any.

unregister(alias)

Unregister alias from the toolbox.

Parameters: alias – The name of the operator to remove from the toolbox.
decorate(alias, decorator[, decorator[, ...]])

Decorate alias with the specified decorators, alias has to be a registered function in the current toolbox.

Parameters: alias – The name of the operator to decorate. decorator – One or more function decorator. If multiple decorators are provided they will be applied in order, with the last decorator decorating all the others.

Note

Decorate a function using the toolbox makes it unpicklable, and will produce an error on pickling. Although this limitation is not relevant in most cases, it may have an impact on distributed environments like multiprocessing. A function can still be decorated manually before it is added to the toolbox (using the @ notation) in order to be picklable.

## Fitness¶

class deap.base.Fitness([values])

The fitness is a measure of quality of a solution. If values are provided as a tuple, the fitness is initalized using those values, otherwise it is empty (or invalid).

Parameters: values – The initial values of the fitness as a tuple, optional.

Fitnesses may be compared using the >, <, >=, <=, ==, !=. The comparison of those operators is made lexicographically. Maximization and minimization are taken care off by a multiplication between the weights and the fitness values. The comparison can be made between fitnesses of different size, if the fitnesses are equal until the extra elements, the longer fitness will be superior to the shorter.

Different types of fitnesses are created in the Creating Types tutorial.

Note

When comparing fitness values that are minimized, a > b will return True if a is smaller than b.

dominates(other, obj=slice(None, None, None))

Return true if each objective of self is not strictly worse than the corresponding objective of other and at least one objective is strictly better.

Parameters: obj – Slice indicating on which objectives the domination is tested. The default value is slice(None), representing every objectives.
valid

Assess if a fitness is valid or not.

values

Fitness values. Use directly individual.fitness.values = values in order to set the fitness and del individual.fitness.values in order to clear (invalidate) the fitness. The (unweighted) fitness can be directly accessed via individual.fitness.values.