Skip to main content

tspy: An optimization package for the traveling salesman problem.

The tspy package gives a Python framework in which to study the famous Traveling Salesman Problem (TSP). In this package, one can work on specific instances of the TSP. Approximation methods and lower bounds are included by default. The structure of the package makes it easy to create and include your own methods.

Installation

The tspy package can be installed using pip:

pip install tspy

How to use tspy

Creating an instance

To create an instance of the TSP, use:

from tspy import TSP
tsp = TSP()

Currently, data can be given to the instance in two ways: by giving it the NxN distance matrix D or, in the case of an Euclidian TSP, a NxP data matrix X and a distance function.

# Using the distance matrix
tsp.read_mat(D)

# Using the data matrix and a distance function
tsp.read_data(X, dist)

Computing approximate solutions

The module tsp.solvers contains several algorithms providing approximate solutions of TSP instances. For example, the 2-opt algorithm gives good solutions rather quickly. Here is how it can be used in the tspy package:

from tspy.solvers import TwoOpt_solver
two_opt = TwoOpt_solver(initial_tour='NN', iter_num=100)
two_opt_tour = tsp.get_approx_solution(two_opt)

Other solvers are used similarly.

Current solutions are stored in the dictionary tsp.tours. If a data matrix has been provided to the instance, a plot of the solution can be shown:

tsp.plot_solution('TwoOpt_solver')

alt text

At any point, the best solution that has been computed can be retrieved:

best_tour = tsp.get_best_solution()

Computing lower bounds

The TSP being NP-hard, it is difficult to get exact solutions for large instances. However, by computing lower bounds we can know how good our approximations are. The tspy package provides several lower bounds methods. One example is given by the Simple_LP_bound which gives the optimal solution of the LP relaxation of the TSP:

from tspy.lower_bounds import Simple_LP_bound
lower_bound = tsp.get_lower_bound(Simple_LP_bound())

At any point, the best lower bound that has been computed can be retrieved:

best_lower_bound = tsp.get_best_lower_bound()

Future

The following features will be added soon:

  • Genetic programming
  • Ant colony optimization
  • Lin–Kernighan heuristic
  • Better LP lower bounds

Feel free to contact me if you would like to contribute.

Author

tspy was written by William Borgeaud (williamborgeaud[at]gmail.com).

Metadata

Release files for tspy 0.1.1.1

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

Source distribution (sdist)

Source distribution for tspy 0.1.1.1
File Size Uploaded
tspy-0.1.1.1.tar.gz 7.6 kB Details

Built distribution (wheel)

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

Total release size: 16.6 kB

Release files / tspy-0.1.1.1.tar.gz

Download URL tspy-0.1.1.1.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d15cc1f4f00a530565183a356ab21b489dc587be0382abe9d7dbb53380735976
BLAKE2b-256 checksum
How to use checksums
32a2b0a18fbfad43dc2adae262566d74af1dcbf7c2ac6da30856f88eb2021909
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.4

Release files / tspy-0.1.1.1-py3-none-any.whl

Download URL tspy-0.1.1.1-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c349dc86412190698698e40b8e843ead0f42b3ab8be6a7c91d78bff4ea32e219
BLAKE2b-256 checksum
How to use checksums
1ab5a2fc6445dd39dd32946854f9cebbb25527286de5293e464e6d971ef04e81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.4

Release history Release notifications | RSS feed

This release

0.1.1.1 This release

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