slim_gsgp.algorithms.GSGP.representations
slim_gsgp.algorithms.GSGP.representations.population
Population Class for Evolutionary Computation with Tree Structures using PyTorch.
- class slim_gsgp.algorithms.GSGP.representations.population.Population(pop)[source]
Bases:
object- calculate_semantics(inputs, testing=False, logistic=False)[source]
Calculate the semantics for each individual in the population.
- Parameters:
inputs (array-like) – Input data for calculating semantics.
testing (bool, optional) – Indicates if the calculation is for testing semantics. Defaults to False.
logistic (bool, optional) – Indicates whether to apply a logistic function to the semantics. Defaults to False.
- Return type:
None
- evaluate(ffunction, y, n_jobs=1)[source]
Evaluate the population using a fitness function.
The fitnesses of each individual are stored as attributes in their respective objects.
- Parameters:
ffunction (callable) – Fitness function to evaluate the individuals.
y (torch.Tensor) – Expected output (target) values as a torch tensor.
n_jobs (int, optional) – The maximum number of concurrently running jobs for joblib parallelization. Defaults to 1.
- Return type:
None
slim_gsgp.algorithms.GSGP.representations.tree
Tree Class for Genetic Programming using PyTorch.
- class slim_gsgp.algorithms.GSGP.representations.tree.Tree(structure, train_semantics, test_semantics, reconstruct)[source]
Bases:
objectTree class implementation for representing tree structures in GSGP.
- structure
The tree structure, either as a tuple or a list of pointers.
- Type:
tuple or str
- FUNCTIONS
Dictionary of allowed functions in the tree.
- Type:
dict
- TERMINALS
Dictionary of terminal symbols allowed in the tree.
- Type:
dict
- CONSTANTS
Dictionary of constant values allowed in the tree.
- Type:
dict
- depth
The maximum depth of the tree structure.
- Type:
int
- nodes
The total number of nodes in the tree.
- Type:
int
- train_semantics
The training semantics associated with the tree.
- Type:
torch.Tensor
- test_semantics
The testing semantics associated with the tree.
- Type:
torch.Tensor
- fitness
The fitness value of the tree. Defaults to None.
- Type:
float or None
- test_fitness
The fitness value of the tree during testing. Defaults to None.
- Type:
float or None
- calculate_semantics(inputs, testing=False, logistic=False)[source]
Calculate the semantics for the tree.
Semantics are stored as an attribute in their respective objects.
- Parameters:
inputs (torch.Tensor) – Input data for calculating semantics.
testing (bool, optional) – Indicates if the calculation is for testing semantics. Defaults to False.
logistic (bool, optional) – Indicates if a logistic (Sigmoid) function should be applied. Defaults to False.
- Return type:
None
- evaluate(ffunction, y, testing=False, X=None)[source]
Evaluate the tree using a fitness function.
During the evolution process, stores the fitness as an attribute. If evaluating with new data, fitness is returned as a float.
- Parameters:
ffunction (callable) – Fitness function to evaluate the individual.
y (torch.Tensor) – Expected output (target) values as a torch tensor.
testing (bool, optional) – Indicates if the evaluation is for testing semantics. Defaults to False.
X (torch.Tensor, optional) – Input data used for calculation. Optional inside the evolution process as only the semantics are needed, but necessary outside of it.
- Returns:
None or float – Returns nothing if no new data is provided, as the training and testing fitness is stored as an attribute.
float – The fitness value of the tree when evaluated with new data.
- predict(data)[source]
Predict the output for the given input data using the model’s structure.
Uses recursive logic to call itself on the structure of the tree until arriving at a basic tuple structure, and then applies the necessary operations to arrive at the final result for the whole tree.
- Parameters:
data (torch.Tensor) – The input data to predict.
- Returns:
The predicted output for the input data.
- Return type:
torch.Tensor
Notes
The prediction process depends on the structure of the model:
If self.structure is a tuple, the apply_tree function is used for prediction.
If self.structure is a list, the first element is assumed to be a function that combines the predictions of multiple base trees (contained in the list) along with additional parameters (floats) extracted from the list. The base trees are instances of the Tree class, and their individual predictions are passed to the combining function along with any extracted parameters.
The combining function is called with the predictions of the base trees and the extracted parameters, along with testing set to False and new_data set to True.
slim_gsgp.algorithms.GSGP.representations.tree_utils
Utility functions for Tree Evaluation and Mutation in GSGP.
- slim_gsgp.algorithms.GSGP.representations.tree_utils.apply_tree(tree, inputs)[source]
Evaluates the tree on input vectors.
- Parameters:
tree (Tree) – The tree structure to be evaluated.
inputs (torch.Tensor) – Input vectors x and y.
- Returns:
Output of the evaluated tree.
- Return type:
torch.Tensor
- slim_gsgp.algorithms.GSGP.representations.tree_utils.nested_depth_calculator(operator, depths)[source]
Calculate the depth of nested structures.
To save computational effort, the new depth is calculated based on the operator used to generate the new tree.
- Parameters:
operator (callable) – The operator applied to the tree.
depths (list of int) – List of depths of subtrees.
- Returns:
Maximum depth after applying the operator.
- Return type:
int
- slim_gsgp.algorithms.GSGP.representations.tree_utils.nested_nodes_calculator(operator, nodes)[source]
Calculate the number of nodes in nested structures.
- Parameters:
operator (callable) – The operator applied to the tree.
nodes (list of int) – List of node counts of subtrees.
- Returns:
Total number of nodes after applying the operator.
- Return type:
int