slim_gsgp.algorithms.GP.operators

slim_gsgp.algorithms.GP.operators.crossover_operators

Crossover operator implementation.

slim_gsgp.algorithms.GP.operators.crossover_operators.crossover_trees(FUNCTIONS)[source]

Returns a function that performs crossover between two tree representations.

To avoid passing the FUNCTIONS parameter unnecessarily, a new function is created utilizing it. This function is returned and passed as a parameter to the GP algorithm, where it is then called when crossover is performed.

Parameters:

FUNCTIONS (dict) – Dictionary of allowed functions in the trees.

Returns:

A function (inner_xo) that performs crossover between two tree representations. Inner function to perform crossover between two trees.

param tree1:

The first tree representation.

type tree1:

tuple

param tree2:

The second tree representation.

type tree2:

tuple

param tree1_n_nodes:

Number of nodes in the first tree representation.

type tree1_n_nodes:

int

param tree2_n_nodes:

Number of nodes in the second tree representation.

type tree2_n_nodes:

int

returns:

Two new tree representations after performing crossover.

rtype:

tuple

Notes

This function selects random crossover points from both tree1 and tree2 and swaps their subtrees at those points. If either tree is a terminal node, it returns the tree representations unchanged.

Return type:

Callable

Notes

The returned function (inner_xo) takes two tree representations and their node counts, selects random subtrees, and swaps them to create the representations of the new offspring trees.

slim_gsgp.algorithms.GP.operators.mutators

Mutator operator implementation.

slim_gsgp.algorithms.GP.operators.mutators.mutate_tree_node(max_depth, TERMINALS, CONSTANTS, FUNCTIONS, p_c)[source]

Generates a function for mutating a node within a tree representation based on a set of terminals, constants, and functions.

This function returns another function that can mutate a specific node in the tree representation. The mutation process involves randomly choosing between modifying a terminal, constant, or function node, while ensuring the resulting tree representation maintains valid arity (i.e., the number of child nodes expected by the function node).

Parameters:
  • max_depth (int) – Maximum depth of the tree to consider during mutation.

  • TERMINALS (dict) – Dictionary of terminal symbols allowed in the tree.

  • CONSTANTS (dict) – Dictionary of constant values allowed in the tree.

  • FUNCTIONS (dict) – Dictionary of functions allowed in the tree.

  • p_c (float) – Probability of choosing a constant node for mutation.

Returns:

A function (‘m_tn’) that performs subtree mutation within a tree representation.

The mutation process involves randomly choosing between modifying a terminal, constant, or function node, while ensuring the resulting tree representation maintains valid arity (i.e., the number of child nodes expected by the function node). Depending on the maximum depth of the tree or the size of the original, the mutation process may only return a single node.

param tree:

The tree representation to mutate.

type tree:

tuple

returns:
  • tuple – The structure of the mutated tree representation.

  • str – The node resulting from mutation

Return type:

Callable

Notes

The returned function (m_tn) operates recursively to traverse the tree representation and randomly select a node for mutation.

slim_gsgp.algorithms.GP.operators.mutators.mutate_tree_subtree(max_depth, TERMINALS, CONSTANTS, FUNCTIONS, p_c)[source]

Generates a function for performing subtree mutation within a tree representation.

This function returns another function that can perform subtree mutation by selecting a random subtree in the tree representation and replacing it with a newly generated random subtree.

Parameters:
  • max_depth (int) – Maximum depth of the tree to consider during mutation.

  • TERMINALS (dict) – Dictionary of terminal symbols allowed in the tree.

  • CONSTANTS (dict) – Dictionary of constant values allowed in the tree.

  • FUNCTIONS (dict) – Dictionary of functions allowed in the tree.

  • p_c (float) – Probability of choosing a constant node for mutation.

Returns:

A function (‘innee_mur’) that mutates a subtree in the given tree representation by replacing a randomly selected subtree.

This function selects a random subtree in the input tree representation and substitutes it with a newly generated random subtree of the same maximum depth. If a terminal is passed, returns the original.

param tree1:

The tree representation to mutate.

type tree1:

tuple or str

param num_of_nodes:

The number of nodes in the tree, used for selecting a random subtree.

type num_of_nodes:

int, optional

returns:
  • tuple – The mutated tree representation with a new subtree

  • str – The original terminal node if the input was a terminal

Return type:

Callable

Notes

The returned function (inner_mut) operates by selecting a random subtree from the input tree representation and replacing it with a randomly generated tree representation of the same maximum depth.