-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_manager.py
290 lines (242 loc) · 11.3 KB
/
node_manager.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import time
import heapq
import numpy as np
from utils import *
from parameter import *
import quads
class NodeManager:
def __init__(self, plot=False):
self.nodes_dict = quads.QuadTree((0, 0), 1000, 1000)
self.plot = plot
self.frontier = None
def check_node_exist_in_dict(self, coords):
key = (coords[0], coords[1])
exist = self.nodes_dict.find(key)
return exist
def add_node_to_dict(self, coords, frontiers, updating_map_info):
key = (coords[0], coords[1])
node = Node(coords, frontiers, updating_map_info)
self.nodes_dict.insert(point=key, data=node)
return node
def remove_node_from_dict(self, node):
for neighbor_coords in node.neighbor_set:
if neighbor_coords != (node.coords[0], node.coords[1]):
neighbor_node = self.nodes_dict.find(neighbor_coords)
neighbor_node.data.neighbor_set.remove(node.coords.tolist())
self.nodes_dict.remove(node.coords.tolist())
def update_graph(self, robot_location, frontiers, updating_map_info, map_info):
node_coords, _ = get_updating_node_coords(robot_location, updating_map_info)
if self.frontier is None:
new_frontier = frontiers
else:
new_frontier = frontiers - self.frontier
new_out_range= []
for frontier in new_frontier:
if np.linalg.norm(robot_location - np.array(frontier).reshape(2)) > SENSOR_RANGE + FRONTIER_CELL_SIZE:
new_out_range.append(frontier)
for frontier in new_out_range:
new_frontier.remove(frontier)
self.frontier = frontiers
all_node_list = []
global_frontiers = get_frontier_in_map(map_info)
for coords in node_coords:
node = self.check_node_exist_in_dict(coords)
if node is None:
node = self.add_node_to_dict(coords, frontiers, updating_map_info)
else:
node = node.data
if node.utility == 0 or np.linalg.norm(node.coords - robot_location) > 2 * SENSOR_RANGE:
pass
else:
node.update_node_observable_frontiers(new_frontier, global_frontiers, updating_map_info)
all_node_list.append(node)
for node in all_node_list:
if node.need_update_neighbor and np.linalg.norm(node.coords - robot_location) < (
SENSOR_RANGE + NODE_RESOLUTION):
node.update_neighbor_nodes(updating_map_info, self.nodes_dict)
def Dijkstra(self, start, boundary=None):
q = set()
dist_dict = {}
prev_dict = {}
for node in self.nodes_dict.__iter__():
coords = node.data.coords
key = (coords[0], coords[1])
dist_dict[key] = 1e8
prev_dict[key] = None
q.add(key)
assert (start[0], start[1]) in dist_dict.keys()
dist_dict[(start[0], start[1])] = 0
while len(q) > 0:
u = None
for coords in q:
if u is None:
u = coords
elif dist_dict[coords] < dist_dict[u]:
u = coords
q.remove(u)
# assert self.nodes_dict.find(u) is not None
node = self.nodes_dict.find(u).data
for neighbor_node_coords in node.neighbor_set:
v = (neighbor_node_coords[0], neighbor_node_coords[1])
if v in q:
cost = ((neighbor_node_coords[0] - u[0]) ** 2 + (
neighbor_node_coords[1] - u[1]) ** 2) ** (1 / 2)
cost = np.round(cost, 2)
alt = dist_dict[u] + cost
if alt < dist_dict[v]:
dist_dict[v] = alt
prev_dict[v] = u
return dist_dict, prev_dict
def get_Dijkstra_path_and_dist(self, dist_dict, prev_dict, end):
if (end[0], end[1]) not in dist_dict:
print("destination is not in Dijkstra graph")
return [], 1e8
dist = dist_dict[(end[0], end[1])]
path = [(end[0], end[1])]
prev_node = prev_dict[(end[0], end[1])]
while prev_node is not None:
path.append(prev_node)
temp = prev_node
prev_node = prev_dict[temp]
path.reverse()
return path[1:], np.round(dist, 2)
def h(self, coords_1, coords_2):
# h = abs(coords_1[0] - coords_2[0]) + abs(coords_1[1] - coords_2[1])
# h = ((coords_1[0] - coords_2[0]) ** 2 + (coords_1[1] - coords_2[1]) ** 2) ** (1 / 2)
h = np.linalg.norm(np.array([coords_1[0] - coords_2[0], coords_1[1] - coords_2[1]]))
# h = np.round(h, 2)
return h
def a_star(self, start, destination, max_dist=None):
# the path does not include the start
if not self.check_node_exist_in_dict(start):
print(start)
Warning("start position is not in node dict")
return [], 1e8
if not self.check_node_exist_in_dict(destination):
Warning("end position is not in node dict")
return [], 1e8
if start[0] == destination[0] and start[1] == destination[1]:
return [], 0
open_list = {(start[0], start[1])}
closed_list = set()
g = {(start[0], start[1]): 0}
parents = {(start[0], start[1]): (start[0], start[1])}
open_heap = []
heapq.heappush(open_heap, (0, (start[0], start[1])))
while len(open_list) > 0:
_, n = heapq.heappop(open_heap)
n_coords = n
node = self.nodes_dict.find(n).data
if max_dist is not None:
if g[n] > max_dist:
return [], 1e8
if n_coords[0] == destination[0] and n_coords[1] == destination[1]:
path = []
length = g[n]
while parents[n] != n:
path.append(n)
n = parents[n]
path.reverse()
return path, np.round(length, 2)
costs = np.linalg.norm(np.array(list(node.neighbor_set)).reshape(-1, 2) - [n_coords[0], n_coords[1]],
axis=1)
for cost, neighbor_node_coords in zip(costs, node.neighbor_set):
m = (neighbor_node_coords[0], neighbor_node_coords[1])
if m not in open_list and m not in closed_list:
open_list.add(m)
parents[m] = n
g[m] = g[n] + cost
heapq.heappush(open_heap, (g[m], m))
else:
if g[m] > g[n] + cost:
g[m] = g[n] + cost
parents[m] = n
open_list.remove(n)
closed_list.add(n)
print('Path does not exist!')
return [], 1e8
class Node:
def __init__(self, coords, frontiers, updating_map_info):
self.coords = coords
self.utility_range = UTILITY_RANGE
self.utility = 0
self.observable_frontiers = self.initialize_observable_frontiers(frontiers, updating_map_info)
self.visited = 0
self.neighbor_matrix = -np.ones((5, 5))
self.neighbor_set = set()
self.neighbor_matrix[2, 2] = 1
self.neighbor_set.add((self.coords[0], self.coords[1]))
self.need_update_neighbor = True
def initialize_observable_frontiers(self, frontiers, updating_map_info):
if len(frontiers) == 0:
self.utility = 0
return set()
else:
observable_frontiers = set()
frontiers = np.array(list(frontiers)).reshape(-1, 2)
dist_list = np.linalg.norm(frontiers - self.coords, axis=-1)
new_frontiers_in_range = frontiers[dist_list < self.utility_range]
for point in new_frontiers_in_range:
collision = check_collision(self.coords, point, updating_map_info)
if not collision:
observable_frontiers.add((point[0], point[1]))
self.utility = len(observable_frontiers)
if self.utility <= MIN_UTILITY:
self.utility = 0
observable_frontiers = set()
return observable_frontiers
def update_neighbor_nodes(self, updating_map_info, nodes_dict):
for i in range(self.neighbor_matrix.shape[0]):
for j in range(self.neighbor_matrix.shape[1]):
if self.neighbor_matrix[i, j] != -1:
continue
else:
center_index = self.neighbor_matrix.shape[0] // 2
if i == center_index and j == center_index:
self.neighbor_matrix[i, j] = 1
continue
neighbor_coords = np.around(np.array([self.coords[0] + (i - center_index) * NODE_RESOLUTION,
self.coords[1] + (j - center_index) * NODE_RESOLUTION]), 1)
neighbor_node = nodes_dict.find((neighbor_coords[0], neighbor_coords[1]))
if neighbor_node is None:
#cell = get_cell_position_from_coords(neighbor_coords, updating_map_info)
#if updating_map_info.map[cell[1], cell[0]] == 1:
# self.neighbor_matrix[i, j] = 1
continue
else:
neighbor_node = neighbor_node.data
collision = check_collision(self.coords, neighbor_coords, updating_map_info)
neighbor_matrix_x = center_index + (center_index - i)
neighbor_matrix_y = center_index + (center_index - j)
if not collision:
self.neighbor_matrix[i, j] = 1
self.neighbor_set.add((neighbor_coords[0], neighbor_coords[1]))
neighbor_node.neighbor_matrix[neighbor_matrix_x, neighbor_matrix_y] = 1
neighbor_node.neighbor_set.add((self.coords[0], self.coords[1]))
if self.utility == 0:
self.need_update_neighbor = False
def update_node_observable_frontiers(self, new_frontiers, global_frontiers, updating_map_info):
# remove frontiers observed
frontiers_observed = []
for frontier in self.observable_frontiers:
if frontier not in global_frontiers:
frontiers_observed.append(frontier)
for frontier in frontiers_observed:
self.observable_frontiers.remove(frontier)
# add new frontiers in the observable frontiers
if len(new_frontiers) > 0:
new_frontiers = np.array(list(new_frontiers)).reshape(-1, 2)
dist_list = np.linalg.norm(new_frontiers - self.coords, axis=-1)
new_frontiers_in_range = new_frontiers[dist_list < self.utility_range]
for point in new_frontiers_in_range:
collision = check_collision(self.coords, point, updating_map_info)
if not collision:
self.observable_frontiers.add((point[0], point[1]))
self.utility = len(self.observable_frontiers)
if self.utility <= MIN_UTILITY:
self.utility = 0
self.observable_frontiers = set()
def set_visited(self):
self.visited = 1
self.observable_frontiers = set()
self.utility = 0