Skip to main content

python-tsp is a library written in pure Python for solving typical Traveling Salesperson Problems (TSP). It can work with symmetric and asymmetric versions.

Installation

pip install python-tsp
poetry add python-tsp  # if using Poetry in the project

Examples

Given a distance matrix as a numpy array, it is easy to compute a Hamiltonian path with least cost. For instance, to use a Dynamic Programming method:

import numpy as np
from python_tsp.exact import solve_tsp_dynamic_programming

distance_matrix = np.array([
    [0,  5, 4, 10],
    [5,  0, 8,  5],
    [4,  8, 0,  3],
    [10, 5, 3,  0]
])
permutation, distance = solve_tsp_dynamic_programming(distance_matrix)

The solution will be [0, 1, 3, 2], with total distance 17. Notice it is always a closed path, so after node 2 we go back to 0.

To solve the same problem with a metaheuristic method:

from python_tsp.heuristics import solve_tsp_simulated_annealing

permutation, distance = solve_tsp_simulated_annealing(distance_matrix)

Keep in mind that, being a metaheuristic, the solution may vary from execution to execution, and there is no guarantee of optimality. However, it may be a way faster alternative in larger instances.

If you with for an open TSP version (it is not required to go back to the origin), just set all elements of the first column of the distance matrix to zero:

distance_matrix[:, 0] = 0
permutation, distance = solve_tsp_dynamic_programming(distance_matrix)

and in this case we obtain [0, 2, 3, 1], with distance 12. Notice that in this case the distance matrix is actually asymmetric, and the methods here are applicable as well.

The previous examples assumed you already had a distance matrix. If that is not the case, the distances module has prepared some functions to compute an Euclidean distance matrix or a Great Circle Distance.

For example, if you have an array where each row has the latitude and longitude of a point,

import numpy as np
from python_tsp.distances import great_circle_distance_matrix

sources = np.array([
    [ 40.73024833, -73.79440675],
    [ 41.47362495, -73.92783272],
    [ 41.26591   , -73.21026228],
    [ 41.3249908 , -73.507788  ]
])
distance_matrix = great_circle_distance_matrix(sources)

See the project’s repository for more examples and a list of available methods.

Release files for python_tsp 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python_tsp 0.4.2
File Size Uploaded
python_tsp-0.4.2.tar.gz 17.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python_tsp 0.4.2
File Interpreter ABI Platform
python_tsp-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 44.5 kB

Release files / python_tsp-0.4.2.tar.gz

Download URL python_tsp-0.4.2.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b41b8f9fb87d4b33a1b22bc946f3a951f156a4d99e2be1936f771e7e0abc62c7
BLAKE2b-256 checksum
How to use checksums
a4e1557adcdffdc5a9b556f9415bd052ca9b51c756153e3bb7efc927de44694d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.4 Linux/6.9.7-100.fc39.x86_64

Release files / python_tsp-0.4.2-py3-none-any.whl

Download URL python_tsp-0.4.2-py3-none-any.whl
Size 26.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67866b9a6665d2bba3b072e79b5b65e1012e173d101a45e467291c01b5316aa3
BLAKE2b-256 checksum
How to use checksums
d589d59246a9262e738bb6c52c799f89a1c9f873cd5e73478aa1fda5a7bb60db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.4 Linux/6.9.7-100.fc39.x86_64

Release history Release notifications | RSS feed

0.5.0

2 release files

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page