-
Notifications
You must be signed in to change notification settings - Fork 0
/
nearest_neighbour.py
92 lines (75 loc) · 2.99 KB
/
nearest_neighbour.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import math
def get_route(list_x, list_y):
assert len(list_x) == len(list_y)
# the memory table so that it only has to do this distance calculations once between points
# master_arr['loc1,loc2'] = distance
master_arr = {}
unvisited = []
for x, y in zip(list_x, list_y):
unvisited.append([x, y])
master_unvisited = unvisited.copy()
best_dist = -1
for i in range(len(master_unvisited)):
# print(i, ' ', '-'*20)
unvisited = master_unvisited.copy()
visited = []
u = unvisited.pop(i)
visited.append(u)
while len(unvisited) != 0:
min_distance = -1
for idx, cord in enumerate(unvisited):
# dist = math.sqrt(abs(u[0] - cord[0])**2 + abs(u[1] - cord[1])**2)
# picking loc1 < loc2 so order is kept 'loc1,loc2'
# so that you don't get 'loc2,loc1'
if u[0] <= cord[0]:
key = str(u[0]) + ',' + str(u[1]) + ',' + str(cord[0]) + ',' + str(cord[1])
else:
key = str(cord[0]) + ',' + str(cord[1]) + ',' + str(u[0]) + ',' + str(u[1])
if key in master_arr.keys():
dist = master_arr[key]
else:
master_arr[key] = math.sqrt(abs(u[0] - cord[0]) ** 2 + abs(u[1] - cord[1]) ** 2)
dist = master_arr[key]
if min_distance == -1:
min_distance = dist
best_node = idx
if min_distance > dist:
min_distance = dist
best_node = idx
u = unvisited.pop(idx)
visited.append(u)
# print(visited)
tot_dist = 0
for idx in range(len(visited) - 1):
x, y, = visited[idx]
x1, y1 = visited[idx + 1]
if x <= x1:
key = str(x) + ',' + str(y) + ',' + str(x1) + ',' + str(y1)
else:
key = str(x1) + ',' + str(y1) + ',' + str(x) + ',' + str(y)
if key in master_arr.keys():
dist = master_arr[key]
else:
master_arr[key] = math.sqrt(abs(x - x1) ** 2 + abs(y - y1) ** 2)
dist = master_arr[key]
tot_dist += dist
# way back like james
x, y = visited[0]
x1, y1 = visited[-1]
if x <= x1:
key = str(x) + ',' + str(y) + ',' + str(x1) + ',' + str(y1)
else:
key = str(x1) + ',' + str(y1) + ',' + str(x) + ',' + str(y)
if key in master_arr.keys():
dist = master_arr[key]
else:
master_arr[key] = math.sqrt(abs(x - x1) ** 2 + abs(y - y1) ** 2)
dist = master_arr[key]
tot_dist += dist
# print(tot_dist, best_dist)
if best_dist > tot_dist or best_dist == -1:
best_dist = tot_dist
best_visited = visited.copy()
print(best_visited, best_dist)
print(master_arr.items())
return best_visited