culebra.fitness_function.tsp.DoublePathLength class

class DoublePathLength(*distance_matrices: Sequence[Sequence[float], ...])

Construct a fitness function.

Parameters:

distance_matrices (Sequence of two-dimensional array-like objects) – Distance matrices. One per objective

Type:

A Sequence of two-dimensional array-like objects

Raises:
  • TypeError – If distance is not a Sequence of array-like objects

  • ValueError – If any element in any array-like object is not a float number

  • ValueError – If any array-like object has not an homogeneous shape

  • ValueError – If any array-like object has not two dimensions

  • ValueError – If any array-like object is not square

  • ValueError – If any element in any array-like object is negative

  • ValueError – If the number of array-like objects provided is different from num_obj

  • ValueError – If the array-like objects have different shapes

Class attributes

class culebra.fitness_function.tsp.DoublePathLength.Fitness

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

Fitness.weights = (-1.0, -1.0)

Minimize the path lengths of a solution.

Fitness.names = ('Len0', 'Len1')

Name of the objectives.

Fitness.thresholds = [0, 0]

Similarity thresholds for fitness comparisons.

Class methods

classmethod DoublePathLength.load_pickle(filename: str) Base

Load a pickled object from a file.

Parameters:

filename (str) – The file name.

Raises:
classmethod DoublePathLength.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 DoublePathLength.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 DoublePathLength.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

classmethod DoublePathLength.fromPath(*paths: Sequence[int, ...]) PathLength

Create an instance from a sequence of optimum paths.

This class method has been designed for testing purposes.

Parameters:

paths (Sequence of Sequence of int) – Sequence of optimum paths. Each path is a node permutation

Raises:
classmethod DoublePathLength.fromTSPLib(*filepaths_or_buffers: str | PathLike[str] | TextIO) PathLength

Generate a fitness function from a sequence of TSPLib files.

Parameters:

filepaths_or_buffers (Sequence of path-like objects, urls or file-like objects) – Sequence of file paths, urls or buffers

Raises:
  • ValueError – If the number of filepaths or buffers provided is different from num_obj

  • RuntimeError – If any filepath or buffer can not be open

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

Properties

property DoublePathLength.is_noisy: int

Return True if the fitness function is noisy.

property DoublePathLength.num_obj: int

Get the number of objectives.

Type:

int

property DoublePathLength.num_nodes: int

Return the problem graph’s number of nodes.

Returns:

The problem graph’s number of nodes

Return type:

int

property DoublePathLength.distance: Sequence[ndarray, ...]

Get and set the distance matrices.

Getter:

Return the distance matrices

Setter:

Set new distance matrices. One per objective

Type:

Sequence of ndarray

Raises:
  • TypeError – If a Sequence of array-like objects is not provided

  • ValueError – If any element in any array-like object is not a float number

  • ValueError – If any array-like object has not an homogeneous shape

  • ValueError – If any array-like object has not two dimensions

  • ValueError – If any array-like object is not square

  • ValueError – If any element in any array-like object is negative

  • ValueError – If the number of array-like objects is different from num_obj

  • ValueError – If the array-like objects have different shapes

Methods

DoublePathLength.save_pickle(filename: str) None

Pickle this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
DoublePathLength.heuristic(species: Species) Sequence[ndarray]

Get the heuristic matrices for ACO-based trainers.

Parameters:

species (Species) – Species constraining the problem solutions

Raises:

TypeError – If species is not an instance of Species

Returns:

A sequence of heuristic matrices. One for each objective. Arcs involving any banned node or 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:

Sequence of ndarray

DoublePathLength.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

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

Evaluate a solution.

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

  • index (int, ignored) – 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 of Solution, ignored) – Representative solutions of each species being optimized. Only used by cooperative problems

Returns:

The fitness of sol

Return type:

tuple of float