culebra.fitness_func.tsp.abc.TSPFitnessFunction class

class TSPFitnessFunction

Bases: FitnessFunction

Construct the fitness function.

Class methods

classmethod TSPFitnessFunction.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property TSPFitnessFunction.fitness_cls: type[Fitness]

Fitness class.

Return type:

type[Fitness]

abstract property TSPFitnessFunction.heuristic: tuple[ndarray[float], ...]

Heuristic matrices.

This property must be overridden by subclasses.

Returns:

A sequence of heuristic matrices. One for each objective. Arcs from a node to itself have a heuristic value of 0. For the rest of arcs, the reciprocal of their nodes distance is used as heuristic

Return type:

tuple[ndarray[float]]

Raises:

NotImplementedError – If has not been overridden

abstract property TSPFitnessFunction.num_nodes: int

Number of nodes of the problem graph.

This property must be overridden by subclasses to return the problem graph’s number of nodes.

Return type:

int

Raises:

NotImplementedError – If has not been overridden

property TSPFitnessFunction.num_obj: int

Number of objectives.

Return type:

int

abstract property TSPFitnessFunction.obj_names: tuple[str, ...]

Objective names.

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

Return type:

tuple[str]

property TSPFitnessFunction.obj_thresholds: tuple[float]

Objective similarity thresholds.

Return type:

tuple[float]

Setter:

Set new thresholds.

Parameters:

values (float | Sequence[float]) – The new values. 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. If set to None, all the thresholds are set to _default_similarity_threshold

Raises:
  • TypeError – If neither a real number nor a Sequence of real numbers is provided

  • ValueError – If any value is negative

  • ValueError – If the length of the thresholds sequence does not match the number of objectives

abstract property TSPFitnessFunction.obj_weights: tuple[int, ...]

Objective weights.

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

Return type:

tuple[int]

Raises:

NotImplementedError – If has not been overridden

abstract property TSPFitnessFunction.objectives: tuple[FitnessFunction]

Objectives to be optimized.

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

Return type:

tuple[FitnessFunction]

Private properties

property TSPFitnessFunction._default_similarity_threshold: float

Default similarity threshold for fitnesses.

Returns:

DEFAULT_SIMILARITY_THRESHOLD

Return type:

float

Methods

TSPFitnessFunction.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
abstract TSPFitnessFunction.evaluate(sol: Solution, index: int | None = None, cooperators: Sequence[Solution | None] | None = None) tuple[float, ...]

Evaluate a solution.

Neither the solution nor its fitness should be modified.

Parameters cooperators 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) – Index where sol should be inserted in the cooperators sequence to form a complete solution for the problem, optional

  • cooperators (Sequence[Solution]) – Cooperators of each species being optimized, optional

Returns:

The fitness values for sol

Return type:

tuple[float, …]

Raises:

NotImplementedError – If has not been overridden

Private methods

TSPFitnessFunction._get_repr_properties() dict[str, object]

Return the subset of properties used for __repr__.

Filters and evaluates all class-level @property attributes, returning only those intended for representation purposes. Private properties (names starting with _) are excluded.

Returns:

Mapping of property names to their corresponding values.

Return type:

dict[str, object]