culebra.fitness_function.tsp.PathLength class

class PathLength(distance_matrix: Sequence[Sequence[float]], index: int | None = None)

Bases: SingleObjectiveFitnessFunction, TSPFitnessFunction

Construct a fitness function.

Parameters:
  • distance_matrix (Sequence[Sequence[float]]) – Distance matrix.

  • index (int) – Index of this objective when it is used for multi-objective fitness functions, optional

Raises:
  • ValueError – If any element in distance_matrix is not a float number

  • ValueError – If distance_matrix has not an homogeneous shape

  • ValueError – If distance_matrix has not two dimensions

  • ValueError – If distance_matrix is not square

  • ValueError – If any element in distance_matrix is negative

  • TypeError – If index is not an integer number

  • ValueError – If index is not positive

Class methods

classmethod PathLength.from_path(path: Sequence[int, ...]) PathLength

Create an instance from an optimum path.

This class method has been designed for testing purposes.

Parameters:

path (Sequence[int]) – An optimum path (a node permutation)

Returns:

The fitness function

Return type:

PathLength

Raises:
classmethod PathLength.from_tsplib(filepath_or_buffer: str | PathLike[str] | TextIO) PathLength

Generate a fitness function from a TSPLib file.

Parameters:

filepath_or_buffer (str | PathLike[str] | TextIO) – File path, url or buffer

Returns:

The fitness function

Return type:

PathLength

Raises:
  • RuntimeError – If the filepath or buffer can not be open

  • RuntimeError – If an unsupported or incorrect feature is found while parsing the filepath or buffer

classmethod PathLength.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property PathLength.distance: ndarray

Distance matrix.

Return type:

ndarray

Setter:

Set an array-like object as the new distance matrix

Parameters:

value (Sequence[Sequence[float]]) – The new distance matrix

Raises:
  • ValueError – If any element in value is not a float number

  • ValueError – If value has not an homogeneous shape

  • ValueError – If value has not two dimensions

  • ValueError – If value is not square

  • ValueError – If any element in value is negative

property PathLength.fitness_cls: type[Fitness]

Fitness class.

Return type:

type[Fitness]

property PathLength.heuristic: tuple[ndarray[float], ...]

Heuristic matrices.

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]]

property PathLength.index: int

Objective index.

Return type:

int

Setter:

Set a new index

Parameters:

value (int) – The new index. If set to None, _default_index is chosen

Raises:
property PathLength.num_nodes: int

Number of nodes of the problem graph.

Return type:

int

property PathLength.num_obj: int

Number of objectives.

Return type:

int

property PathLength.obj_names: tuple[str, ...]

Objective names.

Returns:

(“Len”,)

Return type:

tuple[str]

property PathLength.obj_thresholds: list[float]

Objective similarity thresholds.

Return type:

list[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

property PathLength.obj_weights: tuple[int, ...]

Objective weights.

Minimize the path length.

Returns:

(-1, )

Return type:

tuple[int]

Private properties

property PathLength._default_index: int

Default index.

Returns:

DEFAULT_INDEX

Return type:

int

property PathLength._default_similarity_threshold: float

Default similarity threshold for fitnesses.

Returns:

DEFAULT_SIMILARITY_THRESHOLD

Return type:

float

Methods

PathLength.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
PathLength.evaluate(sol: Solution, index: int | None = None, representatives: Sequence[Solution] | None = None) Fitness

Evaluate a solution.

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

  • index (int) – Index where sol should be inserted in the representatives sequence to form a complete solution for the problem. Only used by cooperative problems

  • representatives (Sequence[Solution]) – Representative solutions of each species being optimized. Only used by cooperative problems

Returns:

The fitness for sol

Return type:

Fitness

PathLength.greedy_solution(species: Species) Solution

Return a greddy solution for the problem.

Parameters:

species (Species) – Species constraining the problem solutions

Raises:

TypeError – If species is not an instance of Species

Returns:

The greddy solution

Return type:

Solution

abstract PathLength.is_evaluable(sol: Solution) bool

Assess the evaluability of a solution.

Parameters:

sol (Solution) – Solution to be evaluated.

Returns:

True if the solution can be evaluated

Return type:

bool

Raises:

NotImplementedError – If has not been overridden