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

2.1.1.1. 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.

2.1.1.2. 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.node_operators 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

2.1.1.3. Node class

class natural_selection.genetic_programs.__init__.Node(label: Optional[str] = None, arity: int = 1, operator: Optional[natural_selection.genetic_programs.node_operators.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.

Parameters
  • 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).

breadth(depth: Optional[int] = None)

Find the breadth at a given depth of the tree. If no depth is given (None), default to the trees depth.

Parameters

depth (int) – At which depth to find the breadth (default = None).

Raises

GeneticProgramError – If the depth > tree depth or depth < 0.

Returns

The breadth of the given depth.

Return type

int

depth()

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

Returns

Deepest node depth found.

Return type

int

get_subtree(depth: int, index: int)

Gets a subtree at the given depth and zero-indexed point.

Parameters
  • depth (int) – The depth at which to find the subtree.

  • index (int) – The zero-indexed point at the given depth.

Raises

GeneticProgramError – If the depth > tree depth or depth < 0, or index > length of nodes.

Returns

The subtree at the point.

Return type

Node

max_breadth()

Find the broadest part of the tree, returning the breadth and depth at which it occurs.

Returns

Breadth, depth

Return type

tuple

set_subtree(depth: int, index: int, subtree)

Set the subtree at the given depth and the index.

Raises

GeneticProgramError – If the depth > tree depth or depth < 0, or index > length of nodes.

Parameters
  • depth (int) – The depth at which to set the subtree.

  • index (int) – The zero-indexed point at the given depth to set the subtree.

  • subtree (Node) – The subtree to set at the given point.

2.1.2. Tree Generator

To generate a node tree at random, two possible generation methods are used. The full and grow methods.

natural_selection.genetic_programs.__init__.random_generate(operators: Optional[list] = None, terminals: Optional[list] = None, max_depth: Optional[int] = None, min_depth: Optional[int] = None, growth_mode: str = 'grow', terminal_prob: Optional[float] = None, genetic_program=None)

Function for random node tree creation. This function can be used for either Full tree generation or Grow generation. Additionally, minimum and maximum depths along with the probability of adding a terminal to a node can be defined.

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

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

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

  • min_depth (int) – Minimum depth that node tree must be (default = 1).

  • growth_mode (str) – Type of tree growth method to use, “full” or “grow” (default = “grow”).

  • terminal_prob (float) – Probability of a generated node is a terminal (default = 0.5).

  • genetic_program (GeneticProgram) – An optional program (default = None).

Returns

The node tree generated.

Return type

Node

2.1.3. GeneticProgram

class natural_selection.genetic_programs.__init__.GeneticProgram(fitness_function: Optional[Callable] = None, node_tree: Optional[natural_selection.genetic_programs.__init__.Node] = None, operators: Optional[List[Union[Type, natural_selection.genetic_programs.node_operators.Operator]]] = None, terminals: Optional[List[Union[str, int, float]]] = None, max_depth: int = 3, min_depth: int = 1, growth_mode: str = 'grow', terminal_prob: float = 0.5, tree_generator: Callable = <function random_generate>, name: Optional[str] = None, species_type: Optional[str] = None, filepath: Optional[str] = None, is_deterministic: bool = False, program_properties: Optional[dict] = None)

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

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

  • node_tree (Node) – A starting node tree (default = None).

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

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

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

  • min_depth (int) – Minimum depth that node tree must be (default = 1).

  • growth_mode (str) – Type of tree growth method to use, “full” or “grow” (default = “grow”).

  • terminal_prob (float) – Probability of a generated node is a terminal (default = 0.5).

  • tree_generator (Callable) – Function with to create the tree.

  • 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).

  • filepath (str) – Skip init and load from a pickled file.

  • is_deterministic (bool) – Sets the whole program as deterministic, which improves function calls through memoisation (default = False).

  • program_properties (dict) – For fitness functions, extra params may be given (default = None).

Notes

If no fitness function is given, the default __call__ is used.

fitness

The fitness score after evaluation.

Type

Numeric

age

How many generations was the individual alive.

Type

int

genetic_code

String representation of node tree.

Type

str

history

List of dicts of every evaluation.

Type

list

parents

List of strings of parent names.

Type

list

add_new_property(key: str, value: Any)

Method to add new properties (attributes).

Parameters
  • key (str) – Name of property.

  • value (Any) – Anything.

birthday(add: int = 1)

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

Parameters

add (int) – Amount to age.

depth()

Wrapping around the depth function of the node tree.

Returns

Node tree depth.

Return type

int

evaluate(params: Optional[dict] = None, island=None)Any

Run the fitness function with the given params.

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

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

Returns

Fitness value.

Return type

numeric

get(key: str, default=None)

Gets the value of a property or returns default if it doesn’t exist.

Parameters
  • key (str) – Property name.

  • default – Value to return if the property is not found (default = None).

Returns

The property of the individual.

Return type

any

get_properties()dict

Gets a dict of the custom properties that were added at initialisation or the add_new_property method.

Returns

All custom properties.

Return type

dict

load(filepath: str)

Load an individual from a pickle file.

Parameters

filepath (str) – File path to load from.

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.

Parameters

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.

Parameters
  • 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.

Parameters

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

save(filepath: str)

Save an individual to a pickle file.

Parameters

filepath (str) – File path to write to.

unique_genetic_code(force_update: bool = False)str

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

Parameters

force_update (bool) – Force update of genetic_code property (default = False).

Returns

String name of Chromosome.

Return type

str