culebra.abc.FitnessFunction class

class FitnessFunction

Base fitness function.

Class attributes

class culebra.abc.FitnessFunction.Fitness

Handles the values returned by the evaluate() method, within a Solution.

This class must be implemented within all the FitnessFunction subclasses, as a subclass of the Fitness class, to define its three class attributes (weights, names, and thresholds) according to the fitness function.

Class methods

classmethod FitnessFunction.load_pickle(filename: str) Base

Load a pickled object from a file.

Parameters:

filename (str) – The file name.

Raises:
classmethod FitnessFunction.set_fitness_thresholds(thresholds: float | Sequence[float]) None

Set new fitness thresholds.

Modifies the thresholds of the Fitness objects generated by this fitness function.

Parameters:

thresholds (float or Sequence of float) – The new thresholds. If only a single value is provided, the same threshold will be used for all the objectives. Different thresholds can be provided in a Sequence

Raises:
classmethod FitnessFunction.get_fitness_objective_threshold(obj_name: str) None

Get the similarity threshold for the given objective.

Parameters:

obj_name (str) – Objective name whose threshold is returned

Raises:
  • TypeError – If obj_name isn’t a string

  • ValueError – If value isn’t a valid objective name

classmethod FitnessFunction.set_fitness_objective_threshold(obj_name: str, value: float) None

Set a similarity threshold for the given fitness objective.

Parameters:
  • obj_name (str) – Objective name whose threshold is modified

  • value (float) – New value for the similarity threshold.

Raises:
  • TypeError – If obj_name isn’t a string or value isn’t a real number

  • ValueError – If obj_name isn’t a valid objective name or value is lower than 0

Properties

property FitnessFunction.is_noisy: int

Return True if the fitness function is noisy.

property FitnessFunction.num_obj: int

Get the number of objectives.

Type:

int

property FitnessFunction.num_nodes: int | None

Return the problem graph’s number of nodes for ACO-based trainers.

Subclasses solvable with ACO-based approaches should override this property to return the problem graph’s number of nodes. Otherwise, None is returned

Returns:

The problem graph’s number of nodes if an ACO-based approach is applicable or None otherwise

Return type:

int

Methods

FitnessFunction.save_pickle(filename: str) None

Pickle this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
FitnessFunction.heuristic(species: Species) Sequence[ndarray, ...] | None

Get the heuristic matrices for ACO-based trainers.

Subclasses solvable with ACO-based approaches should override this method. Otherwise, None is returned

Parameters:

species (Species) – Species constraining the problem solutions

Returns:

A sequence of heuristic matrices if an ACO-based approach is applicable or None otherwise

Return type:

Sequence of ndarray

abstract FitnessFunction.evaluate(sol: Solution, index: int | None = None, representatives: Sequence[Solution] | None = None) Tuple[float, ...]

Evaluate a solution.

Parameters representatives and index are used only for cooperative evaluations

This method must be overridden by subclasses to return a correct value.

Parameters:
  • sol (Solution) – Solution to be evaluated.

  • index (int, optional) – Index where sol should be inserted in the representatives sequence to form a complete solution for the problem

  • representatives (A Sequence containing instances of Solution, optional) – Representative solutions of each species being optimized

Raises:

NotImplementedError – if has not been overridden

Returns:

The fitness values for sol

Return type:

tuple of float