slim_gsgp.algorithms.SLIM_GSGP.representations

slim_gsgp.algorithms.SLIM_GSGP.representations.individual

Individual Class and Utility Functions for SLIM GSGP.

class slim_gsgp.algorithms.SLIM_GSGP.representations.individual.Individual(collection, train_semantics, test_semantics, reconstruct)[source]

Bases: object

Individual of the SLIM_GSGP algorithm. Composed of ‘blocks’ of trees.

Parameters:
  • collection (list) – The list of trees representing the individual.

  • structure (list) – The structure of each tree in the collection.

  • size (int) – The amount of trees in the collection

  • train_semantics (torch.Tensor) – Training semantics associated with the Individual.

  • test_semantics (torch.Tensor or None) – Testing semantics associated with the Individual. Can be None if not applicable.

  • fitness (float or None) – The fitness value of the Individual. Defaults to None.

  • test_fitness (float or None) – The fitness value of the Individual during testing. Defaults to None.

  • nodes_collection (int) – The number of nodes in each tree of the collection.

  • nodes_count (int) – The total amount of nodes in the tree.

  • depth_collection (int) – The maximum depth of each tree in the collection.

  • depth (int) – The maximum depth of the tree.

calculate_semantics(inputs, testing=False)[source]

Calculate the semantics for the Individual. Result is stored as an attribute associated with the object.

Parameters:
  • inputs (torch.Tensor) – Input data for calculating semantics.

  • testing (bool, optional) – Boolean indicating if the calculation is for testing semantics. Default is False.

Return type:

None

evaluate(ffunction, y, testing=False, operator='sum')[source]

Evaluate the Individual using a fitness function.

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

  • y (torch.Tensor) – Expected output (target) values.

  • testing (bool, optional) – Boolean indicating if the evaluation is for testing semantics (default is False).

  • operator (str, optional) – Operator to apply to the semantics (default is “sum”).

Return type:

None

get_tree_representation()[source]

Returns a string representation of the trees in the Individual.

Parameters:

operator (str, optional) – The operator to use in the representation (“sum” or “mul”). If None, it will be determined based on the version.

Returns:

A string representing the structure of the trees in the individual.

Return type:

str

Raises:

Exception – If reconstruct was set to False, indicating that the .get_tree_representation() method is not available.

predict(data)[source]

Predict the output for the given input data using the model’s collection of trees and the specified slim_gsgp version.

Parameters:

data (array-like or DataFrame) – The input data to predict. It should be an array-like structure (e.g., list, numpy array) or a pandas DataFrame, where each row represents a different observation and each column represents a feature.

Returns:

The predicted output for the input data. The output is a PyTorch Tensor whose values are clamped between -1e12 and 1e12.

Return type:

Tensor

Notes

The prediction involves several steps:

  1. The check_slim_version function is called with the slim_version flag to determine the appropriate operator (sum or prod), whether to apply a sigmoid function (sig), and the specific trees to use for prediction.

  2. For each tree in the self.collection: - If the tree structure is a tuple, predictions are made using the apply_tree function. - If the tree structure is a list:

    • For single-tree structures (length 3), predictions are made directly or with a sigmoid function applied, and training semantics are updated.

    • For two-tree structures (length 4), predictions for both trees are made with a sigmoid function applied, and training semantics are updated for both trees.

  3. The semantics (predicted outputs) of all trees are combined using the specified operator (sum or prod), and the final output is clamped to be within the range of -1e12 to 1e12.

This function relies on PyTorch for tensor operations, including torch.sigmoid, torch.sum, torch.prod, torch.stack, and torch.clamp.

print_tree_representation()[source]

Prints a string representation of the trees in the Individual.

Parameters:

operator (str, optional) – The operator to use in the representation (“sum” or “mul”). If None, it will be determined based on the version.

Returns:

Prints a string representing the structure of the trees in the individual.

Return type:

None

slim_gsgp.algorithms.SLIM_GSGP.representations.population

Population Class for SLIM GSGP using PyTorch.

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

Bases: object

calculate_semantics(inputs, testing=False)[source]

Calculate the semantics for each individual in the population.

Parameters:
  • inputs (torch.Tensor) – Input data for calculating semantics.

  • testing (bool, optional) – Boolean indicating if the calculation is for testing semantics.

Return type:

None

evaluate(ffunction, y, operator='sum', n_jobs=1)[source]

Evaluate the population using a fitness function.

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

  • y (torch.Tensor) – Expected output (target) values.

  • operator (str, optional) – Operator to apply to the semantics (“sum” or “prod”). Default is “sum”.

  • n_jobs (int, optional) – The maximum number of concurrently running jobs for joblib parallelization. Default is 1.

Return type:

None

evaluate_no_parall(ffunction, y, operator='sum')[source]

Evaluate the population using a fitness function (without parallelization). This function is not currently in use, but has been retained for potential future use at the developer’s discretion.

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

  • y (torch.Tensor) – Expected output (target) values.

  • operator (str, optional) – Operator to apply to the semantics. Default is “sum”.

Return type:

None