-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp_class.py
61 lines (50 loc) · 2.33 KB
/
tsp_class.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
from node_class import Node
from itertools import permutations
class TravelingSalesman:
def __init__(self, nodes: list[Node]):
self.nodes = nodes
self.visited = []
def brute_force(self, **kwargs):
convert_str_to_node = lambda string: [node for node in self.nodes if node.name == kwargs[string]][0]
if isinstance(kwargs["start"], str):
start = convert_str_to_node("start")
else:
start = kwargs["start"]
if isinstance(kwargs["end"], str):
end = convert_str_to_node("end")
else:
end = kwargs["end"]
methods: list = kwargs["methods"]
all_permutations = list(permutations([node for node in self.nodes if node != start and node != end]))
print("Number of Permutations:", len(all_permutations))
paths = {}
count = 1
for permutation in all_permutations:
sys.stdout.write(f"\r{round(count / len(all_permutations) * 100, 2)}% Complete")
count += 1
total_cost = {}
for i in range(len(permutation)):
if i == len(permutation) - 1:
break
current_node: Node = permutation[i]
next_node: Node = permutation[i + 1]
for method in methods:
try:
total_cost[method] += current_node.branches[next_node][method]
except KeyError:
total_cost[method] = current_node.branches[next_node][method]
for method in methods:
total_cost[method] += start.branches[permutation[0]][method] + permutation[-1].branches[end][method]
paths[permutation] = total_cost
print()
print("Finding shortest path...")
shortest_costs = [float("inf")] * len(methods)
shortest_paths = [None] * len(methods)
for permutation, cost_dict in paths.items():
for i, method in enumerate(methods):
if cost_dict[method] < shortest_costs[i]:
shortest_costs[i] = cost_dict[method]
shortest_paths[i] = [start.name] + [node.name for node in permutation] + [end.name]
return {method: {"path": path, "cost": cost} for method, path, cost in
zip(methods, shortest_paths, shortest_costs)}