-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtravelling_salesman.py
More file actions
37 lines (30 loc) · 1.08 KB
/
travelling_salesman.py
File metadata and controls
37 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Travelling Salesman dynamic programming routine."""
import itertools as IT
import math
from collections import defaultdict as D
from collections import namedtuple as T
Set = frozenset
Place = T("Place", "name x y")
Graph = T("Graph", "V E")
INFTY = float("inf")
Sol = T("Sol", "cost path")
def dist(p1, p2):
return math.hypot(p1.x - p2.x, p1.y - p2.y)
def tsp(G, start, end):
DP = D(lambda: Sol(INFTY, None))
DP[(Set([start]), start)] = Sol(0, [start])
V = Set(G.V) - Set([start, end])
for k in range(len(V) + 2):
for S in IT.combinations(G.V, k):
set_s = Set(S) | Set([start])
for v in S:
v_sol = DP[(set_s, v)] # cost for v, parent for v
for u in G.V:
if u in S:
continue
set_su = set_s | Set([u])
c_sol = DP[(set_su, u)]
n_cost = v_sol.cost + dist(v, u)
if n_cost < c_sol.cost:
DP[(set_su, u)] = Sol(n_cost, v_sol.path + [u])
return DP[Set(G.V), end]