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.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for simple_tsp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87c13c0e2177fcc1b1532442ac58dd0fb6364a285c2cf76acef24acef558346f
MD5 61b7b0c2e26ea80d8d23a4ab0da18b1e
BLAKE2b-256 2d3fb9bbeb726b8fb6c095fa9e6c9c22f58bb0a7588c22a045b7943df44bef08

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