GP Primitives

class Terminal(terminal, symbolic, ret_type)

Class that encapsulates a terminal primitive in an expression. Terminals can be either values or 0-arity functions.

Parameters
  • terminal (TerminalTypes) – The terminal value or function.

  • symbolic (bool) – If True, the terminal is a string.

  • ret_type (type) – The return type of the terminal.

class Ephemeral

Class that encapsulates a terminal which value is set when the object is created. This is an abstract base class. When subclassing, a staticmethod named ‘func’ must be defined.

class Primitive(name, args, ret_type)

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

Parameters
  • name (str) – The name of the primitive.

  • args (list) – The list of arguments of the primitive.

  • ret_type (type) – The return type of the primitive.

class PrimitiveTree(content)

Tree specifically formatted for the optimization of genetic programming operations. This class is a subclass of ‘list’, where the nodes are appended, or are assumed to have been appended, when creating an object of this class with a list of primitives and terminals. The nodes appended to the tree are required to have the ‘arity’ attribute, which defines the arity of the primitive.

Parameters

content (Iterable) – List of primitives and terminals to be added to the tree.

classmethod from_string(string, prim_set)

Converts a string expression into a PrimitiveTree given a PrimitiveSet p_set. The primitive set needs to contain every primitive present in the expression.

Parameters
  • string (str) – String representation of a Python expression.

  • prim_set (PrimitiveSetTyped) – Primitive set from which primitives are selected.

Returns

PrimitiveTree populated with the deserialized primitives.

Return type

PrimitiveTree

search_subtree(begin)

Returns a slice object that corresponds to the range of values that defines the subtree which has the element with index ‘begin’ as its root.

Parameters

begin (int) – Index of the root of the subtree.

Returns

Slice object that corresponds to the range of values that defines the subtree.

Return type

slice

property height

The height of the tree or the depth of the deepest node.

property root

The root of the tree (element 0 in the list).

class PrimitiveSet(name, arity, prefix='ARG')

Subclass of ‘PrimitiveSetTyped’ without the type definition.

Parameters
  • name (str) – The name of the primitive set.

  • arity (int) – The arity of the primitive set.

  • prefix (str) – The prefix of the primitive set.

add_primitive(primitive, arity, name=None, *_, **__)

Adds a primitive to the set.

Parameters
  • primitive (Callable) – Callable object or a function.

  • in_types – List of primitives arguments’ type.

  • ret_type – Type returned by the primitive.

  • name (Optional[str]) – Alternative name for the primitive instead of its __name__ attribute.

Returns

Nothing.

Return type

None

add_terminal(terminal, name=None, *_, **__)

Adds a terminal to the set.

Parameters
  • terminal (Any) – Callable object or a function.

  • ret_type – Type returned by the terminal.

  • name (Optional[str]) – Alternative name for the terminal instead of its __name__ attribute.

Returns

Nothing.

Return type

None

add_ephemeral_constant(name, ephemeral, *_, **__)

Adds an ephemeral constant to the set. An ephemeral constant is a function without arguments that returns a random value. The value is immutable, but unique for each Tree.

Parameters
  • ephemeral (Callable) – Callable object or a function.

  • ret_type – Type returned by the ephemeral.

  • name (str) – Name of this ephemeral type.

Returns

Nothing.

Return type

None

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

Class that contains the primitives which can be used to solve a Strongly Typed GP problem.

Parameters
  • name (str) – The name of the primitive set.

  • in_types (list) – The list of input types.

  • ret_type (type) – The return type.

  • prefix (str) – The prefix of the primitive set.

add_primitive(primitive, in_types, ret_type, name=None)

Adds a primitive to the set.

Parameters
  • primitive (Callable) – Callable object or a function.

  • in_types (list) – List of primitives arguments’ type.

  • ret_type (type) – Type returned by the primitive.

  • name (Optional[str]) – Alternative name for the primitive instead of its __name__ attribute.

Returns

Nothing.

Return type

None

add_terminal(terminal, ret_type, name=None)

Adds a terminal to the set.

Parameters
  • terminal (Callable) – Callable object or a function.

  • ret_type (type) – Type returned by the terminal.

  • name (Optional[str]) – Alternative name for the terminal instead of its __name__ attribute.

Returns

Nothing.

Return type

None

add_ephemeral_constant(name, ephemeral, ret_type)

Adds an ephemeral constant to the set. An ephemeral constant is a function without arguments that returns a random value. The value is immutable, but unique for each Tree.

Parameters
  • ephemeral (Callable) – Callable object or a function.

  • ret_type (type) – Type returned by the ephemeral.

  • name (str) – Name of this ephemeral type.

Returns

Nothing.

Return type

None

add_adf(prim_set)

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

Parameters

prim_set (PrimitiveSetTyped) – PrimitiveSetTyped instance containing the primitives with which the ADF can be built.

Returns

Nothing.

Return type

None

rename_arguments(**kwargs)

Renames the arguments in self with new names from kwargs.

Parameters

kwargs – Dictionary of new names for the arguments.

Returns

Nothing.

Return type

None

property terminal_ratio

The ratio between the quantity of terminals and the quantity of all the other kinds of primitives.