2.1. Core GP classes

Simple classes for encapsulating Genetic Programs (GP).

2.1.1. Nodes

The most atomic part of a GP experiment is the node. Nodes can be either parents or terminal types. Terminal nodes can be either a constant value or a variable. Depending on which, the correct parameters need to be set.

from natural_selection.genetic_programs import Node Terminal nodes

Example of constant terminal node:

n = Node(is_terminal=True, terminal_value=42)

Where is_terminal has to be set to true, and the terminal_value is defined.

When the terminal value is a variable of the function, the label needs to be set. The label is the variable name and is a global name. Example:

n = Node(is_terminal=True, label='x')

When the function is called with ‘x’ as a parameter, the returned terminal value of the node will be replaced with the value. Operator nodes

Operator nodes are parent nodes that execute an operation with children nodes, and return the value(s) to their parents. Instances of nodes can be executed with parameters.

from natural_selection.genetic_programs.functions import OperatorAdd

n_1 = Node(is_terminal=True, label='x')
n_2 = Node(is_terminal=True, terminal_value=42)

n_add = Node(arity=2,operator=OperatorAdd(),children=[n_1,n_2])

value = n_add(x=8) #value = 50 = 8 + 42 Node class

class natural_selection.genetic_programs.__init__.Node(label: Optional[str] = None, arity: int = 1, operator: Optional[natural_selection.genetic_programs.functions.Operator] = None, is_terminal=False, terminal_value=None, children: Optional[List] = None)

Basic class for building Node trees. A node can be either a terminal or a parent. When initialised as a terminal node, is_terminal has to be set to True and either a label or a terminal_value has to be set. When setting a terminal_value, the terminal is a literal, constant value.

Example: n = Node(is_terminal=True, terminal_value=42).

On only setting a label, the terminal is treated as a variable passed on through the function.

Example: n = Node(is_terminal=True, label=’x’).

Setting the arity is optional for when no children nodes are added.

  • label (str) – Optionally set the label, only used for variable terminals (default = None).

  • arity (int) – Optionally set the function arity, the norm being 2 for functions (default = 1).

  • operator (Operator) – If the node is a function, set the operator (default = None).

  • is_terminal (bool) – Explicitly define if the node is a terminal (default = None).

  • terminal_value (Any) – Only set if the node is terminal and a constant value (default = None).

  • children (list) – Add a list of child nodes, list length must match arity (default = None).


Finds the depth of the current tree. This is done by traversing the tree and returning the deepest depth found.


Deepest node depth found.

Return type


2.1.2. GeneticProgram

class natural_selection.genetic_programs.__init__.GeneticProgram(fitness_function: Callable, operators: List[natural_selection.genetic_programs.functions.Operator], terminals: List[Union[str, int, float]], max_depth: int, name: Optional[str] = None, species_type: Optional[str] = None)

A class that encapsulates a single genetic program, with node tree and a fitness evaluation function.

  • fitness_function (Callable) – Function with func(Node, island, **params) signature.

  • operators (list) – List of all operators that nodes can be constructed from.

  • terminals (list) – List of all terminals that can be included in the node tree, can be numeric or strings for variables.

  • max_depth (int) – Maximum depth that node tree can grow.

  • name (str) – Name for keeping track of lineage (default = None).

  • species_type (str) – A unique string to identify the species type, for preventing cross polluting (default = None).


The fitness score after evaluation.




How many generations was the individual alive.




String representation of node tree.




List of dicts of every evaluation.




List of strings of parent names.



birthday(add: int = 1)

Add to the age. This is for keeping track of how many generations a program has “lived” through.


add (int) – Amount to age.

evaluate(params: dict, island=None)Any

Run the fitness function with the given params.

  • params (dict) – Named dict of eval params.

  • island (Island) – Pass the Island for advanced fitness functions based on Island properties and populations.


Fitness value.

Return type


register_parent_names(parents: list, reset_parent_name_list: bool = True)

In keeping lineage of family lines, the names of parents are kept track of.


parents (list) – A list of GeneticProgram of the parents.

reset_fitness(fitness: Optional[Any] = None, reset_genetic_code: bool = True)

Reset (or set) the fitness of the program.

  • fitness (Any) – New fitness value (default = None).

  • reset_genetic_code (bool) – Whether to reset the genetic code. (default = True)

reset_name(name: Optional[str] = None)

A function to reset the name of a program, helping to keep linage of families.


name (str) – Name (default = None).


Gets the unique genetic code, generating if it is undefined.


String name of Chromosome.

Return type