slim_gsgp.algorithms.GP.representations

slim_gsgp.algorithms.GP.representations.population

Population class implementation for evaluating genetic programming individuals.

class slim_gsgp.algorithms.GP.representations.population.Population(pop)[source]

Bases: object

evaluate(ffunction, X, y, n_jobs=1)[source]

Evaluates the population given a certain fitness function, input data (X), and target data (y).

Attributes a fitness tensor to the population.

Parameters:
  • ffunction (function) – Fitness function to evaluate the individuals.

  • X (torch.Tensor) – The input data (which can be training or testing).

  • y (torch.Tensor) – The expected output (target) values.

  • n_jobs (int) – The maximum number of concurrently running jobs for joblib parallelization.

Return type:

None

slim_gsgp.algorithms.GP.representations.tree

Tree class implementation for representing tree structures in genetic programming.

class slim_gsgp.algorithms.GP.representations.tree.Tree(repr_)[source]

Bases: object

The Tree class representing the candidate solutions in genetic programming.

repr_

Representation of the tree structure.

Type:

tuple or str

FUNCTIONS

Dictionary of allowed functions in the tree representation.

Type:

dict

TERMINALS

Dictionary of terminal symbols allowed in the tree representation.

Type:

dict

CONSTANTS

Dictionary of constant values allowed in the tree representation.

Type:

dict

depth

Depth of the tree.

Type:

int

fitness

Fitness value of the tree.

Type:

float

test_fitness

Test fitness value of the tree.

Type:

float

node_count

Number of nodes in the tree.

Type:

int

apply_tree(inputs)[source]

Evaluates the tree on input vectors.

Parameters:

inputs (tuple) – Input vectors.

Returns:

Output of the evaluated tree.

Return type:

float

evaluate(ffunction, X, y, testing=False, new_data=False)[source]

Evaluates the tree given a fitness function, input data (X), and target data (y).

Parameters:
  • ffunction (function) – Fitness function to evaluate the individual.

  • X (torch.Tensor) – The input data (which can be training or testing).

  • y (torch.Tensor) – The expected output (target) values.

  • testing (bool, optional) – Flag indicating if the data is testing data. Default is False.

  • new_data (bool, optional) – Flag indicating that the input data is new and the model is being used outside the training process.

Returns:

  • None – If the data is training or testing data, the fitness value is attributed to the individual.

  • float – If exposed to new data, the fitness value is returned.

get_tree_representation(indent='')[source]

Returns the tree representation as a string with indentation.

Parameters:

indent (str, optional) – Indentation for tree structure representation. Default is an empty string.

Returns:

Returns the tree representation with the chosen indentation.

Return type:

str

predict(X)[source]

Predict the tree semantics (output) for the given input data.

Parameters:

X (torch.Tensor) – The input data to predict.

Returns:

The predicted output for the input data.

Return type:

torch.Tensor

Notes

This function delegates the actual prediction task to the apply_tree method, which is assumed to be another method in the same class. The apply_tree method should be defined to handle the specifics of how predictions are made based on the tree structure used in this model.

print_tree_representation(indent='')[source]

Prints the tree representation with indentation.

Parameters:

indent (str, optional) – Indentation for tree structure representation. Default is an empty string.

Returns:

Prints the tree representation as a string with indentation.

Return type:

None

slim_gsgp.algorithms.GP.representations.tree_utils

Utility functions and tree operations for genetic programming.

slim_gsgp.algorithms.GP.representations.tree_utils.bound_value(vector, min_val, max_val)[source]

Constrains the values within a specific range.

Parameters:
  • vector (torch.Tensor) – Input tensor to be bounded.

  • min_val (float) – Minimum value for bounding.

  • max_val (float) – Maximum value for bounding.

Returns:

A Tensor with values bounded between min_val and max_val.

Return type:

torch.Tensor

slim_gsgp.algorithms.GP.representations.tree_utils.create_full_random_tree(depth, FUNCTIONS, TERMINALS, CONSTANTS, p_c=0.3)[source]

Generates a full random tree representation with a specified depth.

Utilizes recursion to call itself on progressively smaller depths to form the whole tree, until the leaf nodes.

Parameters:
  • depth (int) – Maximum depth of the tree to be created.

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

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

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

  • p_c (float, optional) – Probability of choosing a constant node. Default is 0.3.

Returns:

  • tuple – The generated tree representation according to the specified parameters.

  • str – The terminal or constant node selected, depending on depth and random probabilities.

slim_gsgp.algorithms.GP.representations.tree_utils.create_grow_random_tree(depth, FUNCTIONS, TERMINALS, CONSTANTS, p_c=0.3, first_call=True)[source]

Generates a random tree representation using the Grow method with a maximum specified depth.

Utilizes recursion to call itself on progressively smaller depths to form the whole tree, until the leaf nodes.

Parameters:
  • depth (int) – Maximum depth of the tree to be created.

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

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

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

  • p_c (float, optional) – Probability of choosing a constant node. Default is 0.3.

  • first_call (bool, optional) – Variable that controls whether the function is being called for the first time. Default is True.

Returns:

  • tuple – The generated tree representation according to the specified parameters.

  • str – The terminal or constant node selected, depending on depth and random probabilities.

slim_gsgp.algorithms.GP.representations.tree_utils.flatten(data)[source]

Flattens a nested tuple structure.

Parameters:

data (tuple) – Input nested tuple data structure.

Yields:

object – Flattened data element by element. If data is not a tuple, returns the original data itself.

slim_gsgp.algorithms.GP.representations.tree_utils.random_subtree(FUNCTIONS)[source]

Creates a function that selects a random subtree from a given tree representation.

This function generates another function that traverses a tree representation to randomly select a subtree based on the arity of the functions within the tree.

Parameters:

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

Returns:

A function (‘random_subtree_picker’) that selects a random subtree from the given tree representation.

This function navigates the tree representation recursively, choosing a subtree based on probabilities determined by the overall representation of the tree.

param tree:

The tree representation from which to select a subtree.

type tree:

tuple

param first_call:

Indicates whether this is the initial call to the function. Defaults to True.

type first_call:

bool, optional

param num_of_nodes:

The total number of nodes in the tree. Used to calculate probabilities.

type num_of_nodes:

int, optional

returns:

The randomly selected subtree (or the original node if not applicable).

rtype:

tuple

Return type:

Callable

Notes

The returned function traverses the tree representation recursively, selecting subtrees based on random probabilities influenced by the representation of the tree.

slim_gsgp.algorithms.GP.representations.tree_utils.substitute_subtree(FUNCTIONS)[source]

Generates a function that substitutes a specific subtree in a tree representation with a new subtree.

This function returns another function that can recursively traverse a tree representation to replace occurrences of a specified subtree with a new one, maintaining the representation and validity of the original tree.

Parameters:

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

Returns:

A function (‘substitute’) that substitutes a specified subtree within the given tree representation with a new subtree.

This function recursively searches for occurrences of the target subtree within the tree representation and replaces it with the new subtree when found. If the original tree representation is a terminal or equal to the new one, return it.

param tree:

The tree representation in which to perform the substitution. Can be a terminal.

type tree:

tuple or str

param target_subtree:

The subtree to be replaced.

type target_subtree:

tuple or str

param new_subtree:

The subtree to insert in place of the target subtree.

type new_subtree:

tuple or str

returns:
  • tuple – The modified tree representation with the target subtree replaced by the new subtree.

  • str – The new tree leaf node if the original is a leaf.

Return type:

Callable

Notes

The returned function performs replacements while preserving the tree structure based on the arity of the function nodes.

slim_gsgp.algorithms.GP.representations.tree_utils.tree_depth(FUNCTIONS)[source]

Generates a function that calculates the depth of a given tree representation.

This function returns another function that can be used to compute the depth of a tree representation, which is defined as the length of the longest path from the root node to a leaf node.

Parameters:

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

Returns:

A function (‘depth’) that calculates the depth of the given tree.

This function determines the depth by recursively computing the maximum depth of the left and right subtrees and adding one for the current node.

param tree:

The tree representation for which to calculate the depth. It can also be a terminal node represented as a string.

type tree:

tuple or str

returns:

The depth of the tree.

rtype:

int

Return type:

Callable

Notes

The returned function traverses the tree representation recursively, determining the depth based on the max of the subtree depths.

slim_gsgp.algorithms.GP.representations.tree_utils.tree_pruning(TERMINALS, CONSTANTS, FUNCTIONS, p_c=0.3)[source]

Generates a function that reduces both sides of a tree representation to a specific depth.

This function returns another function that can prune a given tree representation to a specified depth by replacing nodes with terminals or constants based on a defined probability.

Parameters:
  • 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, optional) – Probability of choosing a constant node. Default is 0.3.

Returns:

A function (‘pruning’) that prunes the given tree representation to the specified depth.

This function replaces nodes in the tree representation with terminals or constants if the target depth is reached, ensuring the tree representation does not exceed the specified depth.

param tree:

The tree representation to be pruned.

type tree:

tuple or str

param target_depth:

The depth to which the tree representation should be pruned.

type target_depth:

int

returns:
  • tuple – The pruned tree representation, which may consist of terminals, constants, or a modified subtree.

  • str – The pruned tree if it is a leaf.

Return type:

Callable