Genetic Programming¶
The gp
module provides the methods and classes to perform
Genetic Programming with DEAP. It essentially contains the classes to
build a Genetic Program Tree, and the functions to evaluate it.
This module support both strongly and loosely typed GP.
- class deap.gp.PrimitiveTree(content)[source]¶
Tree specifically formatted for optimization of genetic programming operations. The tree is represented with a list, where the nodes are appended, or are assumed to have been appended when initializing an object of this class with a list of primitives and terminals e.g. generated with the method gp.generate, in a depth-first order. The nodes appended to the tree are required to have an attribute arity, which defines the arity of the primitive. An arity of 0 is expected from terminals nodes.
- classmethod from_string(string, pset)[source]¶
Try to convert a string expression into a PrimitiveTree given a PrimitiveSet pset. The primitive set needs to contain every primitive present in the expression.
- Parameters:
string – String representation of a Python expression.
pset – Primitive set from which primitives are selected.
- Returns:
PrimitiveTree populated with the deserialized primitives.
- property height¶
Return the height of the tree, or the depth of the deepest node.
- property root¶
Root of the tree, the element 0 of the list.
- class deap.gp.PrimitiveSet(name, arity, prefix='ARG')[source]¶
Class same as
PrimitiveSetTyped
, except there is no definition of type.
- class deap.gp.Primitive(name, args, ret)[source]¶
Class that encapsulates a primitive and when called with arguments it returns the Python code to call the primitive with the arguments.
>>> pr = Primitive("mul", (int, int), int) >>> pr.format(1, 2) 'mul(1, 2)'
- class deap.gp.Terminal(terminal, symbolic, ret)[source]¶
Class that encapsulates terminal primitive in expression. Terminals can be values or 0-arity functions.
- deap.gp.compile(expr, pset)[source]¶
Compile the expression expr.
- Parameters:
expr – Expression to compile. It can either be a PrimitiveTree, a string of Python code or any object that when converted into string produced a valid Python code expression.
pset – Primitive set against which the expression is compile.
- Returns:
a function if the primitive set has 1 or more arguments, or return the results produced by evaluating the tree.
- deap.gp.compileADF(expr, psets)[source]¶
Compile the expression represented by a list of trees. The first element of the list is the main tree, and the following elements are automatically defined functions (ADF) that can be called by the first tree.
- Parameters:
expr – Expression to compile. It can either be a PrimitiveTree, a string of Python code or any object that when converted into string produced a valid Python code expression.
psets – List of primitive sets. Each set corresponds to an ADF while the last set is associated with the expression and should contain reference to the preceding ADFs.
- Returns:
a function if the main primitive set has 1 or more arguments, or return the results produced by evaluating the tree.
- class deap.gp.PrimitiveSetTyped(name, in_types, ret_type, prefix='ARG')[source]¶
Class that contains the primitives that can be used to solve a Strongly Typed GP problem. The set also defined the researched function return type, and input arguments type and number.
- addADF(adfset)[source]¶
Add an Automatically Defined Function (ADF) to the set.
- Parameters:
adfset – PrimitiveSetTyped containing the primitives with which the ADF can be built.
- addEphemeralConstant(name, ephemeral, ret_type)[source]¶
Add an ephemeral constant to the set. An ephemeral constant is a no argument function that returns a random value. The value of the constant is constant for a Tree, but may differ from one Tree to another.
- Parameters:
name – name used to refers to this ephemeral type.
ephemeral – function with no arguments returning a random value.
ret_type – type of the object returned by ephemeral.
- addPrimitive(primitive, in_types, ret_type, name=None)[source]¶
Add a primitive to the set.
- Parameters:
primitive – callable object or a function.
in_types – list of primitives arguments’ type
ret_type – type returned by the primitive.
name – alternative name for the primitive instead of its __name__ attribute.
- addTerminal(terminal, ret_type, name=None)[source]¶
Add a terminal to the set. Terminals can be named using the optional name argument. This should be used : to define named constant (i.e.: pi); to speed the evaluation time when the object is long to build; when the object does not have a __repr__ functions that returns the code to build the object; when the object class is not a Python built-in.
- Parameters:
terminal – Object, or a function with no arguments.
ret_type – Type of the terminal.
name – defines the name of the terminal in the expression.
- property terminalRatio¶
Return the ratio of the number of terminals on the number of all kind of primitives.
- deap.gp.graph(expr)[source]¶
Construct the graph of a tree expression. The tree expression must be valid. It returns in order a node list, an edge list, and a dictionary of the per node labels. The node are represented by numbers, the edges are tuples connecting two nodes (number), and the labels are values of a dictionary for which keys are the node numbers.
- Parameters:
expr – A tree expression to convert into a graph.
- Returns:
A node list, an edge list, and a dictionary of labels.
The returned objects can be used directly to populate a pygraphviz graph:
import pygraphviz as pgv # [...] Execution of code that produce a tree expression nodes, edges, labels = graph(expr) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw("tree.pdf")
or a NetworX graph:
import matplotlib.pyplot as plt import networkx as nx # [...] Execution of code that produce a tree expression nodes, edges, labels = graph(expr) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = nx.graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
Note
We encourage you to use pygraphviz as the nodes might be plotted out of order when using NetworX.