API Documentation

Loaders

The loaders are responsible for loading from a filepath, reading a file-like object, or parsing a string.

tsplib95.loaders.load(filepath, problem_class=None, special=None)[source]

Load a problem at the given filepath.

Parameters:
  • filepath (str) – path to a TSPLIB problem file
  • problem_class (type) – special/custom problem class
  • special (callable) – special/custom distance function
Returns:

problem instance

Return type:

Problem

tsplib95.loaders.load_problem(filepath, special=None)[source]

Load a problem at the given filepath.

param str filepath:
 path to a TSPLIB problem file
param callable special:
 special/custom distance function
return:problem instance
rtype:Problem

Deprecated since version 7.0.0: Will be removed in newer versions. Use tsplib95.load instead.

tsplib95.loaders.load_problem_fromstring(text, special=None)[source]

Load a problem from raw text.

param str text:text of a TSPLIB problem
param callable special:
 special/custom distance function
return:problem instance
rtype:Problem

Deprecated since version 7.0.0: Will be removed in newer versions. Use tsplib95.parse instead.

tsplib95.loaders.load_solution(filepath)[source]

Load a solution at the given filepath.

param str filepath:
 path to a TSPLIB solution file
return:solution instance
rtype:Solution

Deprecated since version 7.0.0: Will be removed in newer versions. Use tsplib95.load instead.

tsplib95.loaders.load_solution_fromstring(text)[source]

Load a solution from raw text.

param str text:text of a TSPLIB solution
return:solution instance
rtype:Solution

Deprecated since version 7.0.0: Will be removed in newer versions. Use tsplib95.parse instead.

tsplib95.loaders.load_unknown(filepath)[source]

Load any TSPLIB file.

This is particularly useful when you do not know in advance whether the file contains a problem or a solution.

param str filepath:
 path to a TSPLIB problem file
return:either a problem or solution instance

Deprecated since version 7.0.0: Will be removed in newer versions. Use tsplib95.load instead.

tsplib95.loaders.load_unknown_fromstring(text)[source]

Load any problem/solution from raw text.

This is particularly useful when you do not know in advance whether the file contains a problem or a solution.

param str text:text of a TSPLIB problem/solution
return:either a problem or solution instance

Deprecated since version 7.0.0: Will be removed in newer versions. Use tsplib95.parse instead.

tsplib95.loaders.parse(text, problem_class=None, special=None)[source]

Load a problem from raw text.

Parameters:
  • text (str) – text of a TSPLIB problem
  • problem_class (type) – special/custom problem class
  • special (callable) – special/custom distance function
Returns:

problem instance

Return type:

Problem

tsplib95.loaders.read(f, problem_class=None, special=None)[source]

Read a problem from a file-like object.

Parameters:
  • f (file) – file-like object
  • problem_class (type) – special/custom problem class
  • special (callable) – special/custom distance function
Returns:

problem instance

Return type:

Problem

Problems

Problem classes define what fields can be present and how they are converted to and from text.

class tsplib95.models.Problem(**data)[source]

Bases: object

Base class for all problems.

Parameters:data – name-value data
as_dict(by_keyword=False)[source]

Return the problem data as a dictionary.

Parameters:by_keyword (bool) – use keywords (True) or names (False) or keys
Returns:problem data
Return type:dict
as_keyword_dict()[source]

Return the problem data as a dictionary by field keyword.

Returns:problem data
Return type:dict
as_name_dict()[source]

Return the problem data as a dictionary by field name.

Returns:problem data
Return type:dict
classmethod load(filepath, **options)[source]

Load a problem instance from a text file.

Any keyword options are passed to the class constructor. If a keyword argument has the same name as a field then they will collide and cause an error.

Parameters:
  • filepath (str) – path to a problem file
  • options – any keyword arguments to pass to the constructor
Returns:

problem instance

Return type:

Problem

classmethod parse(text, **options)[source]

Parse text into a problem instance.

Any keyword options are passed to the class constructor. If a keyword argument has the same name as a field then they will collide and cause an error.

Parameters:
  • text (str) – problem text
  • options – any keyword arguments to pass to the constructor
Returns:

problem instance

Return type:

Problem

classmethod read(fp, **options)[source]

Read a problem instance from a file-like object.

Any keyword options are passed to the class constructor. If a keyword argument has the same name as a field then they will collide and cause an error.

Parameters:
  • fp (str) – a file-like object
  • options – any keyword arguments to pass to the constructor
Returns:

problem instance

Return type:

Problem

class tsplib95.models.StandardProblem(special=None, **data)[source]

Bases: tsplib95.models.Problem

Standard problem as outlined in the original TSLIB95 documentation.

The available fields and their keywords are:

  • name - NAME
  • comment - COMMENT
  • type - TYPE
  • dimension - DIMENSION
  • capacity - CAPACITY
  • edge_weight_type - EDGE_WEIGHT_TYPE
  • edge_weight_format - EDGE_WEIGHT_FORMAT
  • edge_data_format - EDGE_DATA_FORMAT
  • node_coord_type - NODE_COORD_TYPE
  • display_data_type - DISPLAY_DATA_TYPE
  • depots - DEPOT_SECTION
  • demands - DEMAND_SECTION
  • node_coords - NODE_COORD_SECTION
  • edge_weights - EDGE_WEIGHT_SECTION
  • display_data - DISPLAY_DATA_SECTION
  • edge_data - EDGE_DATA_SECTION
  • fixed_edges - FIXED_EDGES_SECTION
  • tours - TOUR_SECTION

For SPECIAL FUNCTION problems, the special function must accept a start and an end node and return the weight, distance, or cost of the edge that joins them. It can be provided at construction time or simply set on an existing object using the special attribute.

Parameters:
  • special (callable) – special function for distance
  • data – name-value data
get_display(i)[source]

Return the display data for node at index i.

If the problem is not depictable, None is returned instead.

Parameters:i (int) – node index
Returns:display data for node i
get_edges()[source]

Return an iterator over the edges.

This method provides a single way to obtain the edges of a problem regardless of how it is specified. If the EDGE_DATA_FORMAT is not set and the nodes are undefined, then the edges are also undefined.

Returns:edges
Return type:iter
Raises:ValueError – if the nodes and therefore the edges are undefined
get_graph(normalize=False)[source]

Return a networkx graph instance representing the problem.

The metadata of the problem is associated with the graph itself. Additional problem information is associated with the nodes and edges. For example:

>>> G = problem.get_graph()
>>> G.graph
{'name': None,
 'comment': '14-Staedte in Burma (Zaw Win)',
 'type': 'TSP',
 'dimension': 14,
 'capacity': None}
>>> G.nodes[1]
{'coord': (16.47, 96.1),
 'display': None,
 'demand': None,
 'is_depot': False}
>>> G.edges[1, 2]
{'weight': 2, 'is_fixed': False}

If the graph is asymmetric then a networkx.DiGraph is returned. Optionally, the nodes can be renamed to be sequential and zero-indexed.

Parameters:normalize (bool) – rename nodes to be zero-indexed
Returns:graph
Return type:networkx.Graph
get_nodes()[source]

Return an iterator over the nodes.

This method provides a single way to obtain the nodes of a problem regardless of how it is specified. However, if the nodes are not specified, the EDGE_DATA_FORMAT is not set, and DIMENSION has no value, then nodes are undefined.

Returns:nodes
Return type:iter
Raises:ValueError – if the nodes are undefined
get_weight(start, end)[source]

Return the weight of the edge between start and end.

This method provides a single way to obtain edge weights regardless of whether the problem uses an explicit matrix or a distance function.

Parameters:
  • start (int) – starting node index
  • end (int) – ending node index
Returns:

weight of the edge between start and end

Return type:

float

is_complete()[source]

Check whether the problem specifies a complete graph.

Returns:True if the problem specifies a complete graph
Return type:bool
is_depictable()[source]

Check whether the problem can be depicted.

A problem is depictable if it has display data or has node coordinates and does not specify NO_DISPLAY.

Returns:True if the problem can be depicted
Return type:bool
is_explicit()[source]

Check whether the problem specifies edge weights explicitly.

Returns:True if the problem specifies edge weights explicitly
Return type:bool
is_full_matrix()[source]

Check whether the problem is specified as a full matrix.

Returns:True if the problem is specified as a full matrix
Return type:bool
is_special()[source]

Check whether the problem is special.

SPECIAL problems require a special distance function.

Returns:True if the problem requires a special distance function
Return type:bool
is_symmetric()[source]

Check whether the problem is symmetrical.

Warning

Although a result of True guarantees symmetry, a value of False merely indicates the possibliity for asymmetry. Avoid using not problem.is_symmetric() when possible.

Returns:True if the problem is symmetrical
Return type:bool
is_weighted()[source]

Check whether the problem has weighted edges.

A problem is considered unweighted if neither the EDGE_WEIGHT_FORMAT nor the EDGE_WEIGHT_TYPE are defined.

Returns:True if the problem is weighted
Return type:bool
special

Special distance function.

Special/custom distance functions must accept two coordinates of appropriate dimension and return the distance between them.

trace_canonical_tour()[source]

Return the weight of the canonical tour.

The “canonical tour” uses the nodes in order. This method is present primarily for testing and purposes.

Returns:weight of the canonical tour
Return type:float
trace_tours(tours)[source]

Return the weights of the given tours.

Each tour is a list of node indices. The weights returned are the sum of the individual weights of the edges in each tour including the final edge back to the starting node.

The list of weights returned parallels the list of tours given so that weights[i] corresponds to tours[i]:

weights = p.trace_tours(tours)
Parameters:tours (list) – one or more lists of node indices
Returns:one weight for each given tour
Return type:list

Fields

class tsplib95.fields.Field(keyword, *, default=None)[source]

Bases: object

Contains base functionality for all fields.

The default value can be a callable, in which case it is invoked for each call to get_default_value(). The default can be set on an instance or as a class attribute, but the class attribute is only checked when the field is initially created.

Parameters:
  • keyword (str) – keyword (typically all caps)
  • default – a default value or callable that will return a default
get_default_value()[source]

Return the default value.

Callables are called for a default value to return each time.

Returns:the default value
Return type:Any
parse(text)[source]

Convert text into a value.

This must be implemented in a subclass.

Parameters:text (str) –
Returns:a value
render(value)[source]

Convert a value into text.

This must be implemented in a subclass.

Parameters:value – a value
Returns:text
Return type:str
validate(value)[source]

Validate a value.

Raise an exception if the value fails validation.

The default implementation does nothing.

Parameters:value – a value
Raises:Exception – if the value does not pass validation
class tsplib95.fields.TransformerField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.Field

Field that delegates to a Transformer.

Parameters:
  • keyword (str) – keyword
  • transformer (callable) – transformer to use
classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
parse(text)[source]

Parse the text into a value using the transformer.

Parameters:text (str) – text to parse
Returns:value
render(value)[source]

Render the value into text using the transformer.

Parameters:text (str) – value to render
Returns:text
validate(value)[source]

Validate the value using the transformer.

Parameters:text (str) – value to validate
class tsplib95.fields.StringField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Simple string field.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
class tsplib95.fields.IntegerField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Simple integer field.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
class tsplib95.fields.FloatField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Simple float field.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
class tsplib95.fields.NumberField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Number field, supporting ints and floats.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
class tsplib95.fields.IndexedCoordinatesField(*args, dimensions=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Field for coordinates by index.

When given, dimensions stipulates the possible valid dimensionalities for the coordinates. For exapmle, dimensions=(2, 3) indicates the coordinates are either all 2d or all 3d, whereas dimensions=2 indicates all coordinates must be 2d. The check is only enforced during validation.

Parameters:dimensions – one or more valid dimensionalities
classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
default

alias of builtins.dict

validate(value)[source]

Validate the value using the transformer.

Parameters:text (str) – value to validate
class tsplib95.fields.AdjacencyListField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Field for an adjancency list.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
default

alias of builtins.dict

class tsplib95.fields.EdgeListField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Field for a list of edges.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
default

alias of builtins.list

class tsplib95.fields.MatrixField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Field for a matrix of numbers.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
default

alias of builtins.list

class tsplib95.fields.EdgeDataField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Field for edge data.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
default

alias of builtins.dict

class tsplib95.fields.DepotsField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Field for depots.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
default

alias of builtins.list

class tsplib95.fields.DemandsField(keyword, *, transformer=None, **kwargs)[source]

Bases: tsplib95.fields.TransformerField

Field for demands.

classmethod build_transformer()[source]

Construct an appropriate transformer for the field.

Returns:transformer
Return type:Transformer
default

alias of builtins.dict

class tsplib95.fields.ToursField(*args, require_terminal=True)[source]

Bases: tsplib95.fields.Field

Field for one or more tours.

default

alias of builtins.list

parse(text)[source]

Parse the text into a list of tours.

Parameters:text (str) – text to parse
Returns:tours
Return type:list
render(tours)[source]

Render the tours as text.

Parameters:tours (list) – tours to render
Returns:rendered text
Return type:str

Transformers

class tsplib95.transformers.Transformer[source]

Bases: object

Reusable transformer between text and data.

parse(text)[source]

Return the value of the text.

Parameters:text (str) – the text
Returns:the value
Raises:ParsingError – if the text cannot be parsed into a value
render(value)[source]

Return the text for the value.

Parameters:value (str) – the value
Returns:the text
validate(value)[source]

Validate the value.

Parameters:value – the value
class tsplib95.transformers.FuncT(*, func)[source]

Bases: tsplib95.transformers.Transformer

Transformer that simply wraps a parsing function.

The parsing function must accept a single positional argument for the text to parse. It must parse and return the value.

Values are rendered back into values using the builtin str(), so it’s generally best to use this for primitives.

Parameters:func (callable) – parsing function
parse(text)[source]

Return the value of the text.

Parameters:text (str) – the text
Returns:the value
Raises:ParsingError – if the text cannot be parsed into a value
class tsplib95.transformers.NumberT[source]

Bases: tsplib95.transformers.Transformer

Transformer for any number, int or float.

parse(text)[source]

Return the value of the text.

Parameters:text (str) – the text
Returns:the value
Raises:ParsingError – if the text cannot be parsed into a value
class tsplib95.transformers.ContainerT(*, value=None, sep=None, terminal=None, terminal_required=True, size=None, filter_empty=True)[source]

Bases: tsplib95.transformers.Transformer

Transformer that acts as a generic container.

Parameters:
  • value (Transformer) – transformer for each item
  • sep (str) – separator between items
  • terminal (str) – text that marks the end
  • terminal_required (bool) – whether the terminal is required
  • size (int) – required number of items
  • filter_empty (bool) – filter out empty items (zero-length/blank)
join_items(items)[source]

Join zero or more items into a single text.

Parameters:items (list) – items to join
Returns:joined text
Return type:str
pack(items)[source]

Pack the given items into a container.

Parameters:items (list) – items to pack
Returns:container with items in it
parse(text)[source]

Parse the text into a container of items.

Parameters:text (str) – the text to parse
Returns:container
parse_item(text)[source]

Parse the text into a single item.

Parameters:text (str) – the text to parse
Returns:container
render(container)[source]

Render the container into text.

Parameters:container – container to render
Returns:text
render_item(item)[source]

Render the item into text.

Parameters:item – item to render
Returns:text
split_items(text)[source]

Split the text into multiple items.

Parameters:text (str) – text to split
Returns:muliple items
Return type:list
unpack(container)[source]

Unpack items from the container.

Parameters:container – container with items in it
Returns:items from container
Return type:list
class tsplib95.transformers.ListT(*, value=None, sep=None, terminal=None, terminal_required=True, size=None, filter_empty=True)[source]

Bases: tsplib95.transformers.ContainerT

Transformer for a list of items.

pack(items)[source]

Pack the given items into a container.

Parameters:items (list) – items to pack
Returns:container with items in it
unpack(container)[source]

Unpack items from the container.

Parameters:container – container with items in it
Returns:items from container
Return type:list
class tsplib95.transformers.MapT(*, kv_sep=None, key=None, **kwargs)[source]

Bases: tsplib95.transformers.ContainerT

Transformer for a key-value mapping of items.

Parameters:
  • kv_sep (str) – separator between keys and values
  • key (Transformer) – transformer for the keys
pack(items)[source]

Pack the given items into a container.

Parameters:items (list) – items to pack
Returns:container with items in it
parse_item(text)[source]

Parse the text into a single item.

Parameters:text (str) – the text to parse
Returns:container
parse_key(text)[source]

Parse the text into a key.

Parameters:text (str) – the text to parse
Returns:key
parse_value(text)[source]

Parse the text into a value.

Parameters:text (str) – the text to parse
Returns:value
render_item(item)[source]

Render the item into text.

Parameters:item – item to render
Returns:text
render_key(key)[source]

Render the key into text.

Parameters:key – the key to render
Returns:text
Return type:str
render_value(value)[source]

Render the value into text.

Parameters:value – the value to render
Returns:text
Return type:str
unpack(container)[source]

Unpack items from the container.

Parameters:container – container with items in it
Returns:items from container
Return type:list
class tsplib95.transformers.UnionT(*tfs, **kwargs)[source]

Bases: tsplib95.transformers.Transformer

parse(text)[source]

Return the value of the text.

Parameters:text (str) – the text
Returns:the value
Raises:ParsingError – if the text cannot be parsed into a value
render(value)[source]

Return the text for the value.

Parameters:value (str) – the value
Returns:the text

Matrices

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

value_at(i, j)[source]

Get the element at row i and column j.

Parameters:
  • i (int) – row
  • j (int) – column
Returns:

value of element at (i,j)

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

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

is_valid_row_column(i, j)[source]

Return True if (i,j) is a row and column within the matrix.

Parameters:
  • i (int) – row
  • j (int) – column
Returns:

whether (i,j) is within the bounds of the matrix

Return type:

bool

value_at(i, j)[source]

Get the element at row i and column j.

Parameters:
  • i (int) – row
  • j (int) – column
Returns:

value of element at (i,j)

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

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

Distances

tsplib95.distances.TYPES = {'ATT': <function pseudo_euclidean>, 'CEIL_2D': functools.partial(<function euclidean>, round=<built-in function ceil>), 'EUC_2D': <function euclidean>, 'EUC_3D': <function euclidean>, 'GEO': <function geographical>, 'MAN_2D': <function manhattan>, 'MAN_3D': <function manhattan>, 'MAX_2D': <function maximum>, 'MAX_3D': <function maximum>, 'XRAY1': <function xray>, 'XRAY2': functools.partial(<function xray>, sx=1.25, sy=1.5, sz=1.15)}

Map of distance function types to distance functions

tsplib95.distances.euclidean(start, end, round=<function nint>)[source]

Return the Euclidean distance between start and end.

This is capable of performing distance calculations for EUC_2D and EUC_3D problems. If round=math.ceil is passed, this is suitable for CEIL_2D problems as well.

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.manhattan(start, end, round=<function nint>)[source]

Return the Manhattan distance between start and end.

This is capable of performing distance calculations for MAN_2D and MAN_3D problems.

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.

This is capable of performing distance calculations for MAX_2D and MAX_3D problems.

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.

This is capable of performing distance calculations for GEO problems.

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.pseudo_euclidean(start, end, round=<function nint>)[source]

Return the pseudo-Euclidean distance between start and end.

This is capable of performing distance calculations for ATT problems.

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.0, sy=1.0, sz=1.0, round=<function nint>)[source]

Return x-ray crystallography distance.

This is capable of performing distance calculations for xray crystallography problems. As is, it is suitable for XRAY1 problems. However, using sx=1.25, sy=1.5, and sz=1.15 makes it suitable for XRAY2 problems.

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

Biseps

Utilities

tsplib95.utils.nint(x)[source]

Round a value to an integer.

Parameters:x (float) – original value
Returns:rounded integer
Return type:int
tsplib95.utils.parse_degrees(coord)[source]

Parse an encoded geocoordinate value into real degrees.

Parameters:coord (float) – encoded geocoordinate value
Returns:real degrees
Return type:float

Exceptions

exception tsplib95.exceptions.TsplibError[source]

Bases: Exception

Base exception for all tsplib95 errors.

exception tsplib95.exceptions.ParsingError[source]

Bases: tsplib95.exceptions.TsplibError, ValueError

Exception raised when a value cannot be parsed from the text.

exception tsplib95.exceptions.RenderingError[source]

Bases: tsplib95.exceptions.TsplibError, ValueError

Exception raised when a value cannot be rendered into text.

exception tsplib95.exceptions.ValidationError[source]

Bases: tsplib95.exceptions.TsplibError

Exception raised when a problem fails validation.