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 objectsValueError – 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 aSolution
.- 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:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
PICKLE_FILE_EXTENSION
- classmethod DoublePathLength.set_fitness_thresholds(thresholds: float | Sequence[float]) None ¶
Set new fitness thresholds.
Modifies the
thresholds
of theFitness
objects generated by this fitness function.- Parameters:
thresholds (
float
orSequence
offloat
) – 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 aSequence
- Raises:
TypeError – If thresholds is not a real number or a
Sequence
of real numbersValueError – If any threshold is negative
- 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:
- 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
ofSequence
ofint
) – Sequence of optimum paths. Each path is a node permutation- Raises:
ValueError – If the number of paths provided is different from
num_obj
ValueError – If the array-like objects have different shapes
ValueError – If any path in paths has any not
int
elementValueError – If any path in paths is empty
ValueError – If any path in paths has negative values
ValueError – If any path in paths has loops
ValueError – If any path in paths has missing nodes
- 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.num_nodes: int¶
Return the problem graph’s number of nodes.
- Returns:
The problem graph’s number of nodes
- Return type:
- 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:
- Raises:
TypeError – If a
Sequence
of array-like objects is not providedValueError – 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:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
PICKLE_FILE_EXTENSION
- DoublePathLength.heuristic(species: Species) Sequence[ndarray] ¶
Get the heuristic matrices for ACO-based trainers.
- Parameters:
species (
Species
) – Species constraining the problem solutions- Raises:
- 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:
- DoublePathLength.greedy_solution(species: Species) Solution ¶
Return a greddy solution for the problem.
- 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 problemsrepresentatives (
Sequence
ofSolution
, ignored) – Representative solutions of each species being optimized. Only used by cooperative problems
- Returns:
The fitness of sol
- Return type: