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[, ...]])[source]¶ 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 anint
), 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 standardlist
and having an attributebar
being an empty dictionary and a static attributespam
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 Inheriting from Numpy tutorial and the One Max Problem: Using Numpy example.
-
deap.creator.
class_replacers
= {<class 'numpy.ndarray'>: <class 'deap.creator._numpy_array'>, <class '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. To palliate 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.