Creator

The creator is a meta-factory allowing to create classes that will fulfill the needs of your evolutionary algorithms. In effect, new classes can be built from any imaginable type, from list to set, dict, PrimitiveTree 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.

Parameters:
  • name – The name of the class to create.
  • base – A base class from which to inherit.
  • attribute – One or more attributes to add on instantiation of this class, optional.

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.

class Foo(list):
    spam = 1

    def __init__(self):
        self.bar = dict()

The Creating Types tutorial gives more examples of the creator usage.

Warning

If your are inheriting from numpy.ndarray see the tutorials/advanced/numpy tutorial and the One Max Problem: Using Numpy example.

deap.creator.class_replacers = {<type 'array.array'>: <class 'deap.creator._array'>, <type 'numpy.ndarray'>: <class 'deap.creator._numpy_array'>}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)