Skip to main content

Simple library to solve the Traveling Salesperson Problem in pure Python.

Project description

python-tsp is a library written in pure Python for solving typical Traveling Salesperson Problems (TSP).

Installation

pip install python-tsp

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.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_tsp-0.1.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

python_tsp-0.1.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file python_tsp-0.1.1.tar.gz.

File metadata

  • Download URL: python_tsp-0.1.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.10.17_1

File hashes

Hashes for python_tsp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5066ebbdcf8ae593dd28b5bfd9225d1b7ac8ad9ebd3729b9ec62b96b8293777b
MD5 9c2c6fc9adc9ded2f404fe641abc28ea
BLAKE2b-256 1755e58548e677869236c5f6ac9b86bd5a5112679f8b2afe3d18a5fe03333e47

See more details on using hashes here.

File details

Details for the file python_tsp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: python_tsp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.10.17_1

File hashes

Hashes for python_tsp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b96d78af4fe5e826efa2bd2baf34b3b504e2fd29caf7aa0aea2bfc6d17490ba6
MD5 3cb6be802ce8e024bc74ecba5ec5cf97
BLAKE2b-256 0514479778e7d810b1f3d89bd46e448d87dc5f993cdb0e5c708d96ef2eb3f334

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page