Skip to main content

Travelling Salesman Problem Using Simulated Annealing

This package solves the Travelling Salesman Problem (TSP) using Simulated Annealing (SA).

Required Libraries

  • Numpy
  • Pandas
  • Matplotlib
  • Math

Installation

The satsp package can be installed using pip:

pip install satsp

Usage

Basic usage:

from satsp import solver

cities = [[1, 6.0, 7.0],
          [2, 4.0, 9.0],
	  [3, 7.0, 1.0]]

solver.Solve(cities)

The three columns in cities represent id, x coordinate and y coordinate.

Advanced Usage:

solver.Solve(city_list = None, dist_matrix = None, start_temp = None, \
          stop_temp = None, alpha = None, epochs = None, epoch_length = None, \
          epoch_length_factor = 1.00, stopping_count = 100, screen_output = True):

Arguments:

city_list: a N*3 matrix containing three columns representing id, x coordinate and y coordinate of the N cities. Can be None.

dist_matrix: a N*N matrix containing the distances between the N cities. If None is passed and a None city_list is passed, the program will calculate the Eclidean distances between the cities. If both city_list and dist_matrix are None, a test instance with 48 cities will be solved.

start_temp: initial temperature for SA. If None is passed, the program will estimate the initial temperature using a small sample from the data.

stop_temp: stopping temperature for SA. Can be None.

alpha: cooling rate for SA. If None is passed, the program will calculate alpha if stop_temp and epochs are given. Otherwise alpha is set at 0.99.

epochs: number of epochs for SA. A decisive factor for the running time of the algorithm. The program will terminate after this number of epochs. If None is passed, and stop_temp and alpha are given, the program will calculate number of epochs. Otherwise, the stopping condition will be switched to no improvement after a certain number of epochs, where the number is decided by stopping_count.

epoch_length: number of iterations in each epoch. The default is min(100, N*(N-1) / 2).

epoch_length_factor: the rate at which epoch length increases at each epoch. Should be greater than or equal to 1. Default is 1.00. A small value is recommended for large instances.

stopping_count: the number of epochs after which the program will stop if no improvement is made. This stopping condition is only activated if the number of total epochs is neither specified by the user nor can be calculated by the program. Default is 100. screen_output: Parameters of SA, progress of the algorithm and the results will be displayed if set to True. Default is True.

Return: the distance of the best TSP tour found

Algorithm

This package implements the simulated annealing (SA) metaheuristic to solve TSP. A sketch of the algorithm is as follows:

  1. Generate a random initial tour, and set an initial temperature.
  2. Set a number for the iterations to be performed, determined by epoch length.
  3. Randomly pick two cities in the tour and perform a "2-opt" operation, i.e., reverse the tour between the two cities.
  4. If a tour with shorter distance is obtained, accept the new tour. Otherwise, accept the new tour with a certain probability determined by the difference between old and new distances and the current temperature.
  5. Repeat 3-4 for the number of iterations determined in 2.
  6. If a stopping condition is met, terminate. Otherwise go to 7.
  7. Decrease the temperature and repeat 2-5.

References

Kirkpatrick, Scott, C. Daniel Gelatt, and Mario P. Vecchi. "Optimization by simulated annealing." Science 220.4598 (1983): 671-680.

Park, Moon-Won, and Yeong-Dae Kim. "A systematic procedure for setting parameters in simulated annealing algorithms." Computers & Operations Research 25.3 (1998): 207-217.

Metadata

Release files for satsp 0.8

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

Source distribution (sdist)

Source distribution for satsp 0.8
File Size Uploaded
satsp-0.8.tar.gz 6.5 kB Details

Built distribution (wheel)

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

Total release size: 13.9 kB

Release files / satsp-0.8.tar.gz

Download URL satsp-0.8.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
387026d6e695199593bbff6f1ccc590d080fc8187f611e16741b62b19a0aa029
BLAKE2b-256 checksum
How to use checksums
0c7d8ef9354cd08f80d3a39606bd38b1df00ef25a39fb2b1e2c142b03278fd4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.3.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

Release files / satsp-0.8-py3-none-any.whl

Download URL satsp-0.8-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67909dfef866c46554c0dcc3ae6278f262b5b8bbdd068c5fc9006ff452494ae0
BLAKE2b-256 checksum
How to use checksums
e70e4e19d82f42ad62ac3c4a5e562e5f8370bf497301a1a6a2844a306ea88cc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.3.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

Release history Release notifications | RSS feed

0.9

2 release files

This release

0.8 This release

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