1. Core Architecture

The core architecture of DEAP is composed of two simple structures, the creator and the toolbox. The former provides structuring capabilities, while the latter adds genericity potential to every algorithm. Both structures are described in detail in the following sections.

1.1. Creator

The creator module is the heart and soul of DEAP, it allows to create classes that will fulfill the needs of your evolutionary algorithms. This module follows the meta-factory paradigm by allowing to create new classes via both composition and inheritance. Attributes both datas and functions are added to existing types in order to create new types empowered with user specific evolutionary computation capabilities. In effect, new classes can be built from any imaginable type, from list to set, dict, Tree and more, providing the possibility to implement genetic algorithms, genetic programming, evolution strategies, particle swarm optimizers, and many more.

deap.creator.create(name, base[, attribute[, ...]])

Creates a new class named name inheriting from base in the creator module. The new class can have attributes defined by the subsequent keyword arguments passed to the function create. If the argument is a class (without the parenthesis), the __init__ function is called in the initialization of an instance of the new object and the returned instance is added as an attribute of the class’ instance. Otherwise, if the argument is not a class, (for example an int), it is added as a “static” attribute of the class.

The following is used to create a class Foo inheriting from the standard list and having an attribute bar being an empty dictionary and a static attribute spam initialized to 1.

create("Foo", list, bar=dict, spam=1)

This above line is exactly the same as defining in the creator module something like the following.

def Foo(list):
    spam = 1
    
    def __init__(self):
        self.bar = dict()
deap.creator.class_replacers = {<type 'numpy.ndarray'>: <class 'deap.creator._numpy_array'>, <type 'array.array'>: <class 'deap.creator._array'>}

Some classes in Python’s standard library as well as third party library may be in part incompatible with the logic used in DEAP. In order to palliate to this problem, the method create() uses the dictionary class_replacers to identify if the base type provided is problematic, and if so the new class inherits from the replacement class instead of the original base class.

class_replacers keys are classes to be replaced and the values are the replacing classes.

1.2. Toolbox

The Toolbox is a container for the tools that are selected by the user. The toolbox is manually populated with the desired tools that best apply with the chosen representation and algorithm from the user’s point of view. This way it is possible to build algorithms that are totally decoupled from the operator set, as one only need to update the toolbox in order to make the algorithm run with a different operator set as the algorithms are built to use aliases instead of direct function names.

class deap.base.Toolbox

A toolbox for evolution that contains the evolutionary operators. At first the toolbox contains two simple methods. The first method clone() duplicates any element it is passed as argument, this method defaults to the copy.deepcopy() function. The second method map() 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.

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

Register a method in the toolbox under the name alias. You may provide default arguments that will be passed automatically when calling the registered method. Fixed arguments can then be overriden at function call time. The following code block is a 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
unregister(alias)

Unregister alias from the toolbox.

decorate(alias, decorator[, decorator[, ...]])

Decorate alias with the specified decorators, alias has to be a registered function in the current toolbox. Decorate uses the signature preserving decoration function decorate().

1.3. Additional Base Types

Even if there is a very large variety of implemented types in Python, we must provide additional ones for sake of completeness.

1.3.1. 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).

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.

Note

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

isDominated(other)

In addition to the comparison operators that are used to sort lexically the fitnesses, this method returns True if this fitness is dominated by the other fitness and False otherwise. The weights are used to compare minimizing and maximizing fitnesses. If there is more fitness values than weights, the last weight get repeated until the end of the comparison.

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.

weights = None

The weights are used in the fitness comparison. They are shared among all fitnesses of the same type. When subclassing Fitness, weights must be defined as a tuple where each element is associated to an objective. A negative weight element corresponds to the minimization of the associated objective and positive weight to the maximization.

Note

If weights is not defined during subclassing, the following error will occur at instantiation of a subclass fitness object:

TypeError: Can't instantiate abstract <class Fitness[...]> with abstract attribute weights.

wvalues = ()

Contains the weighted values of the fitness, the multiplication with the weights is made when the values are set via the property values. Multiplication is made on setting of the values for efficiency.

Generally it is unnecessary to manipulate wvalues as it is an internal attribute of the fitness used in the comparison operators.

1.3.2. Tree

class deap.base.Tree([content])

Basic N-ary tree class. A tree is initialized from the list content. The first element of the list is the root of the tree, then the following elements are the nodes. Each node can be either a list or a single element. In the case of a list, it is considered as a subtree, otherwise a leaf.

classmethod convertNode(node)

Convert node into the proper object either a Tree or a Node.

getstate()

Return the state of the Tree as a list of arbitrary elements. It is mainly used for pickling a Tree object.

height

Return the height of the tree.

The height of a tree is the length of the path from the root to the deepest node in the tree. A (rooted) tree with only one node (the root) has a height of zero.

iter

Return a generator function that iterates on the element of the tree in linear time using depth first algorithm.

>>> t = Tree([1,2,3[4,5,[6,7]],8])
>>> [i for i in t.iter]:
[1, 2, 3, 4, 5, 6, 7, 8]
iter_leaf

Return a generator function that iterates on the leaf of the tree in linear time using depth first algorithm.

>>> t = Tree([1,2,3,[4,5,[6,7]],8])
>>> [i for i in t.iter_leaf]
[2, 3, 5, 7, 8]
iter_leaf_idx

Return a generator function that iterates on the leaf indices of the tree in linear time using depth first algorithm.

>>>  t = Tree([1,2,3,[4,[5,6,7],[8,9]],[10,11]]);
>>> [i for i in t.iter_leaf_idx]
[1, 2, 5, 6, 8, 10]
root

Return the root element of the tree.

The root node of a tree is the node with no parents. There is at most one root node in a rooted tree.

searchSubtreeBF(index)

Search the subtree with the corresponding index based on a breadth-first search.

searchSubtreeDF(index)

Search the subtree with the corresponding index based on a depth-first search.

setSubtreeBF(index, subtree)

Replace the subtree with the corresponding index by subtree based on a breadth-first search.

setSubtreeDF(index, subtree)

Replace the tree with the corresponding index by subtree based on a depth-first search.

size

Return the number of nodes in the tree.

The size of a node is the number of descendants it has including itself.

Table Of Contents

Previous topic

API

Next topic

2. Evolutionary Tools

This Page