culebra.fitness_function.tsp.PathLength class¶
- class PathLength(distance_matrix: Sequence[Sequence[float]], index: int | None = None)¶
Bases:
SingleObjectiveFitnessFunction,TSPFitnessFunctionConstruct a fitness function.
- Parameters:
- 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:
- Returns:
The fitness function
- Return type:
- Raises:
ValueError – If path is not a
SequenceofintValueError – If path has any not int element
ValueError – If path is empty
ValueError – If path has negative values
ValueError – If path has loops
ValueError – If path has missing nodes
- 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:
- 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:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
SERIALIZED_FILE_EXTENSION
Properties¶
- property PathLength.distance: ndarray¶
Distance matrix.
- Return type:
- Setter:
Set an array-like object as the new distance matrix
- Parameters:
- 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.index: int¶
Objective index.
- Return type:
- Setter:
Set a new index
- Parameters:
value (int) – The new index. If set to
None,_default_indexis chosen- Raises:
TypeError – If value is not an integer number
ValueError – If value is a negative number
- property PathLength.obj_thresholds: list[float]¶
Objective similarity thresholds.
- Return type:
- 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 toNone, all the thresholds are set to_default_similarity_threshold- Raises:
TypeError – If neither a real number nor a
Sequenceof real numbers is providedValueError – If any value is negative
ValueError – If the length of the thresholds sequence does not match the number of objectives
Private properties¶
Methods¶
- PathLength.dump(filename: str) None¶
Serialize this object and save it to a file.
- Parameters:
filename (str) – The file name.
- Raises:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
SERIALIZED_FILE_EXTENSION
- 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:

