Skip to main content

Solving the Traveling Salesman Problem

Project description

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]

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_tsp-0.2.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file simple_tsp-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: simple_tsp-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for simple_tsp-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e9faf9d64871a03b95bdc21706ec3941efd3a08ef1ae3943c0e0206ddf2193fa
MD5 a86fcc84c73a761998974976b10b5095
BLAKE2b-256 41011c7fc9b95404dfa4d8dfaecf8d9f3654c2421c98c74c02ab46d706175d52

See more details on using hashes here.

Supported by

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