4. Genetic Programming

The gp module provides the methods and classes to perform Genetic Programming with EAP. 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 eap.gp.Ephemeral(func, ret=None)

Class that encapsulates a terminal which value is set at run-time. The value of the Ephemeral can be regenerated with the method regen.

class eap.gp.EphemeralGenerator(ephemeral, ret=None)

Class that generates Ephemeral to be added to an expression.

class eap.gp.Operator(operator, args, ret=None)

Class that encapsulates an operator and when called with arguments it returns the Python code to call the operator with the arguments. It acts as the Primitive class, but instead of returning a function and its arguments, it returns an operator and its operands.

>>> import operator
>>> op = Operator(operator.mul, (int, int), int)
>>> op("1", "2")
'(1 * 2)'
>>> op2 = Operator(operator.neg, (int,), int)
>>> op2(1)
'-(1)'
class eap.gp.Primitive(primitive, args, ret=None)

Class that encapsulates a primitive and when called with arguments it returns the Python code to call the primitive with the arguments.

>>> import operator
>>> pr = Primitive(operator.mul, (int, int), int)
>>> pr("1", "2")
'mul(1, 2)'
class eap.gp.PrimitiveSet(name, arity, prefix='ARG')

Class same as PrimitiveSetTyped, except there is no definition of type.

addEphemeralConstant(ephemeral)

Add an ephemeral constant to the set.

addPrimitive(primitive, arity)

Add primitive primitive with arity arity to the set.

addTerminal(terminal)

Add a terminal to the set.

class eap.gp.PrimitiveSetTyped(name, in_types, ret_type, prefix='ARG')

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)

Add an Automatically Defined Function (ADF) to the set.

adfset is a PrimitiveSetTyped containing the primitives with which the ADF can be built.

addEphemeralConstant(ephemeral, ret_type)

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.

ephemeral function with no arguments that returns a random value. ret_type is the type of the object returned by the function.

addPrimitive(primitive, in_types, ret_type)

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.

addTerminal(terminal, ret_type)

Add a terminal to the set.

terminal is an object, or a function with no arguments. ret_type is the type of the terminal.

renameArguments(new_args)

Rename function arguments with new arguments name new_args.

terminalRatio

Return the ratio of the number of terminals on the number of all kind of primitives.

class eap.gp.PrimitiveTree(content=None)

Tree class faster than base Tree, optimized for Primitives.

class eap.gp.Terminal(terminal, ret=None)

Class that encapsulates terminal primitive in expression. Terminals can be symbols, values, or 0-arity functions.

eap.gp.evaluate(expr, pset=None)

Evaluate the expression expr into a string if pset is None or into Python code is pset is not None.

eap.gp.evaluateADF(seq)

Evaluate a list of ADF and return a dict mapping the ADF name with its lambda function.

eap.gp.generateFull(pset, min_, max_, type_=None)

Generate an expression where each leaf has a the same depth between min and max.

eap.gp.generateGrow(pset, min_, max_, type_=None)

Generate an expression where each leaf might have a different depth between min and max.

eap.gp.generateRamped(pset, min_, max_, type_=None)

Generate an expression with a PrimitiveSet pset. Half the time, the expression is generated with generateGrow, the other half, the expression is generated with generateFull.

eap.gp.lambdify(pset, expr)

Return a lambda function of the expression.

Remark: This function is a stripped version of the lambdify function of sympy0.6.6.

eap.gp.lambdifyList(expr)

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.

Previous topic

3. Algorithms

Next topic

5. Statistics

This Page