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.
Tree spefically formated for optimization of genetic programming operations. The tree is represented with a list where the nodes are appended in a depth-first order. The nodes appended to the tree are required to define to have an attribute arity which defines the arity of the primitive. An arity of 0 is expected from terminals nodes.
Try to convert a string expression into a PrimitiveTree given a PrimitiveSet pset.
The primitive set needs to contain every primitives and terminals that are present in the expression. In the cases of terminals, the function will convert it to a Terminal even if it is not already present in the set. Numbers are converted to float, and strings stay strings.
This functions does not work with STGP terminals that are not defined in the PrimitiveSet since it is not possible to guess the type.
Return the height of the tree, or the depth of the deepest node.
Root of the tree, the element 0 of the list.
Return a slice object that corresponds to the range of values that defines the subtree which has the element with index begin as its root.
Return the expression in a human readable string.
Class same as PrimitiveSetTyped, except there is no definition of type.
Add an ephemeral constant to the set.
Add primitive primitive with arity arity to the set. If a name name is provided, it will replace the attribute __name__ attribute to represent/identify the primitive.
Add a terminal to the set.
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 that encapsulates terminal primitive in expression. Terminals can be values or 0-arity functions.
Class that encapsulates a terminal which value is set when the object is created. To mutate the value, a new object has to be generated.
Evaluate the expression expr into a string.
Deprecated since version 1.0: Use PrimitiveTree.to_string instead.
Evaluate the expression expr into Python code object.
Return a lambda function of the expression expr.
This function is a stripped version of the lambdify function of sympy0.6.6.
Return a lambda function created from 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.
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.
Add an Automatically Defined Function (ADF) to the set.
adfset is a PrimitiveSetTyped containing the primitives with which the ADF can be built.
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.
name is the name that will be used to refers to this ephemeral type. ephemeral function with no arguments that returns a random value. ret_type is the type of the object returned by the function.
Add a primitive to the set.
primitive is a callable object or a function. in_types is a list of argument’s types the primitive takes. ret_type is the type returned by the primitive. name is the alternative name for the primitive instead of its __name__ attribute.
Add a terminal to the set.
terminal is an object, or a function with no arguments. ret_type is the type of the terminal. name defines the name of the terminal in the expression. 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.
Rename function arguments with new names from kargs.
Return the ratio of the number of terminals on the number of all kind of primitives.
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()