Skip to main content

Solving the Traveling Salesman Problem

This package uses PyVRP to solve the traveling salesman problem. It can handle distance data in the following three ways.

  • A dictionary whose keys are tuples of two cities and whose values are integer distances
    • A dictionary whose city type is an integer (pattern 1)
    • A dictionary whose key is a tuple of cities and whose value is a string (Pattern 2)
  • Nested lists whose values are distances (pattern 3)

Functions

  • tsp(distances, depot, max_iterations): Solve a traveling salesman problem and return a list of cities
    • distances: Dictionary or list of distances
    • depot: First city
    • max_iterations: Number of iterations to use in PyVRP
tsp(distances: dict[tuple[int, int], int], depot: int = 0, max_iterations: int = 100) -> list[int]
tsp(distances: dict[tuple[str, str], int], depot: str, max_iterations: int = 100) -> list[str]
tsp(distances: list[list[int]], depot: int = 0, max_iterations: int = 100) -> list[str]
  • distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float: Return the great circle distance (km) between two points
    • lat1: Latitude of location 1
    • lon1: Longitude of location 1
    • lat2: Latitude of location 2
    • lon2: Longitude of location 2

Usage

from simple_tsp import tsp

# Pattern 1
distances1 = {(0, 2): 1, (2, 3): 1, (3, 1): 1, (1, 0): 1}
print(tsp(distances1, 0))  # [0, 2, 3, 1]

# Pattern 2
distances2 = {("a", "c"): 1, ("c", "d"): 1, ("d", "b"): 1, ("b", "a"): 1}
print(tsp(distances2, "a"))  # ['a', 'c', 'd', 'b']

# Pattern 3
distances3 = [[0, 9, 1, 9], [1, 0, 9, 9], [9, 9, 0, 1], [9, 1, 9, 0]]
print(tsp(distances3))  # [0, 2, 3, 1]

Metadata

Release files for simple-tsp 0.2.1

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

Built distribution (wheel)

Table of built distributions (wheels) for simple-tsp 0.2.1
File Interpreter ABI Platform
simple_tsp-0.2.1-py3-none-any.whl Python 3 none any Details

Release files / simple_tsp-0.2.1-py3-none-any.whl

Download URL simple_tsp-0.2.1-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9faf9d64871a03b95bdc21706ec3941efd3a08ef1ae3943c0e0206ddf2193fa
BLAKE2b-256 checksum
How to use checksums
41011c7fc9b95404dfa4d8dfaecf8d9f3654c2421c98c74c02ab46d706175d52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.2.1 This release

1 release file

0.2.0

1 release file

0.1.0

1 release file

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