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 citiesdistances: Dictionary or list of distancesdepot: First citymax_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 pointslat1: Latitude of location 1lon1: Longitude of location 1lat2: Latitude of location 2lon2: 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
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9faf9d64871a03b95bdc21706ec3941efd3a08ef1ae3943c0e0206ddf2193fa
|
|
| MD5 |
a86fcc84c73a761998974976b10b5095
|
|
| BLAKE2b-256 |
41011c7fc9b95404dfa4d8dfaecf8d9f3654c2421c98c74c02ab46d706175d52
|