API Documentation¶
Model classes¶
-
class
tsplib95.models.
File
(**kwargs)[source]¶ Bases:
object
Base file format type.
This class isn’t meant to be used directly. It contains the common keyword values among all formats. Note that all information is optional. Missing information values are set to None. See the official TSPLIB documentation for more details.
name
- NAMEcomment
- COMMENTtype
- TYPEdimension
- DIMENSION
-
class
tsplib95.models.
Problem
(special=None, **kwargs)[source]¶ Bases:
tsplib95.models.File
A TSPLIB problem file.
Provides a python-friendly way to access the fields of a TSPLIB probem. The fields are mapped as follows:
name
- NAMEcomment
- COMMENTtype
- TYPEdimension
- DIMENSIONcapacity
- CAPACITYedge_weight_type
- EDGE_WEIGHT_TYPEedge_weight_format
- EDGE_WEIGHT_FORMATedge_data_format
- EDGE_DATA_FORMATnode_coord_type
- NODE_COORD_TYPEdisplay_data_type
- DISPLAY_DATA_TYPEdepots
- DEPOT_SECTIONdemands
- DEMAND_SECTIONnode_coords
- NODE_COORD_SECTIONedge_weights
- EDGE_WEIGHT_SECTIONdisplay_data
- DISPLAY_DATA_SECTIONedge_data
- EDGE_DATA_SECTIONfixed_edges
- FIXED_EDGES_SECTION
For problems that require a special distance function, you must set the special function in one of two ways:
>>> problem = Problem(special=func, ...) # at creation time >>> problem.special = func # on existing problem
Special distance functions are ignored for explicit problems but are required for some.
Regardless of problem type or specification, the weight of the edge between two nodes given by index can always be found using
wfunc
. For example, to get the weight of the edge between nodes 13 and 6:>>> problem.wfunc(13, 6) 87
The length of a problem is the number of nodes it contains.
-
get_display
(i)[source]¶ Return the display data for node at index i, if available.
Parameters: i (int) – node index Returns: display data for node i
-
get_graph
()[source]¶ Return the corresponding networkx.Graph instance.
If the graph is not symmetric then a DiGraph is returned. If present, the coordinates of each node are set to the
coord
key, and each edge has anis_fixed
key that is True if the edge is in the list of fixed edges.Returns: graph
-
is_full_matrix
()[source]¶ Return True if the problem is specified as a full matrix.
Return type: bool
-
is_special
()[source]¶ Return True if the problem requires a special distance function.
Return type: bool
-
is_symmetric
()[source]¶ Return True if the problem is not asymmetrical.
Note that even if this method returns False there is no guarantee that there are any two nodes with an asymmetrical distance between them.
Return type: bool
-
special
¶ Special distance function
-
class
tsplib95.models.
Solution
(**kwargs)[source]¶ Bases:
tsplib95.models.File
A TSPLIB solution file containing one or more tours to a problem.
name
- NAMEcomment
- COMMENTtype
- TYPEdimension
- DIMENSIONtours
- TOUR_SECTION
The length of a solution is the number of tours it contains.
Matrix weights¶
-
class
tsplib95.matrix.
FullMatrix
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.Matrix
A complete square matrix.
Parameters: - numbers (list) – the elements of the matrix
- size (int) – the width (also height) of the matrix
- min_index (int) – the minimum index
-
class
tsplib95.matrix.
HalfMatrix
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.Matrix
A triangular half-matrix.
Parameters: - numbers (list) – the elements of the matrix
- size (int) – the width (also height) of the matrix
- min_index (int) – the minimum index
-
has_diagonal
= True¶ True if the half-matrix includes the diagonal
-
class
tsplib95.matrix.
LowerCol
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.UpperRow
-
class
tsplib95.matrix.
LowerDiagCol
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.UpperDiagRow
-
class
tsplib95.matrix.
LowerDiagRow
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.HalfMatrix
Lower-triangular matrix that includes the diagonal.
Parameters: - numbers (list) – the elements of the matrix
- size (int) – the width (also height) of the matrix
- min_index (int) – the minimum index
-
get_index
(i, j)[source]¶ Return the linear index for the element at (i,j).
Parameters: - i (int) – row
- j (int) – column
Returns: linear index for element (i,j)
Return type: int
-
has_diagonal
= True¶
-
class
tsplib95.matrix.
LowerRow
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.LowerDiagRow
Lower-triangular matrix that does not include the diagonal.
Parameters: - numbers (list) – the elements of the matrix
- size (int) – the width (also height) of the matrix
- min_index (int) – the minimum index
-
has_diagonal
= False¶
-
class
tsplib95.matrix.
Matrix
(numbers, size, min_index=0)[source]¶ Bases:
object
A square matrix created from a list of numbers.
Elements are accessible using matrix notation. Negative indexing is not allowed.
Parameters: - numbers (list) – the elements of the matrix
- size (int) – the width (also height) of the matrix
- min_index (int) – the minimum index
-
get_index
(i, j)[source]¶ Return the linear index for the element at (i,j).
Parameters: - i (int) – row
- j (int) – column
Returns: linear index for element (i,j)
Return type: int
-
class
tsplib95.matrix.
UpperCol
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.LowerRow
-
class
tsplib95.matrix.
UpperDiagCol
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.LowerDiagRow
-
class
tsplib95.matrix.
UpperDiagRow
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.HalfMatrix
Upper-triangular matrix that includes the diagonal.
Parameters: - numbers (list) – the elements of the matrix
- size (int) – the width (also height) of the matrix
- min_index (int) – the minimum index
-
get_index
(i, j)[source]¶ Return the linear index for the element at (i,j).
Parameters: - i (int) – row
- j (int) – column
Returns: linear index for element (i,j)
Return type: int
-
has_diagonal
= True¶
-
class
tsplib95.matrix.
UpperRow
(numbers, size, min_index=0)[source]¶ Bases:
tsplib95.matrix.UpperDiagRow
Upper-triangular matrix that does not include the diagonal.
Parameters: - numbers (list) – the elements of the matrix
- size (int) – the width (also height) of the matrix
- min_index (int) – the minimum index
-
has_diagonal
= False¶
Distance functions¶
-
tsplib95.distances.
euclidean
(start, end, round=<function nint>)[source]¶ Return the Euclidean distance between start and end.
Parameters: - start (tuple) – n-dimensional coordinate
- end (tuple) – n-dimensional coordinate
- round (callable) – function to use to round the result
Returns: rounded distance
-
tsplib95.distances.
geographical
(start, end, round=<function nint>, radius=6378.388)[source]¶ Return the geographical distance between start and end.
Parameters: - start (tuple) – n-dimensional coordinate
- end (tuple) – n-dimensional coordinate
- round (callable) – function to use to round the result
- radius (float) – the radius of the Earth
Returns: rounded distance
-
tsplib95.distances.
manhattan
(start, end, round=<function nint>)[source]¶ Return the Manhattan distance between start and end.
Parameters: - start (tuple) – n-dimensional coordinate
- end (tuple) – n-dimensional coordinate
- round (callable) – function to use to round the result
Returns: rounded distance
-
tsplib95.distances.
maximum
(start, end, round=<function nint>)[source]¶ Return the Maximum distance between start and end.
Parameters: - start (tuple) – n-dimensional coordinate
- end (tuple) – n-dimensional coordinate
- round (callable) – function to use to round the result
Returns: rounded distance
-
tsplib95.distances.
pseudo_euclidean
(start, end, round=<function nint>)[source]¶ Return the pseudo-Euclidean distance between start and end.
Parameters: - start (tuple) – n-dimensional coordinate
- end (tuple) – n-dimensional coordinate
- round (callable) – function to use to round the result
Returns: rounded distance
-
tsplib95.distances.
xray
(start, end, sx=1, sy=1, sz=1, round=<function icost>)[source]¶ Return x-ray crystallography distance.
Parameters: - start (tuple) – 3-dimensional coordinate
- end (tuple) – 3-dimensional coordinate
- sx (float) – x motor speed
- sy (float) – y motor speed
- sz (float) – z motor speed
Returns: distance
Utilities¶
-
tsplib95.utils.
load_problem
(filepath, special=None)[source]¶ Load a problem at the given filepath.
Parameters: - filepath (str) – path to a TSPLIB problem file
- special (callable) – special/custom distance function
Returns: problem instance
Return type: Problem