-
Notifications
You must be signed in to change notification settings - Fork 1
/
trail.py
404 lines (337 loc) · 17.2 KB
/
trail.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import numpy as np
import math
import constants
import matplotlib.pyplot as plt
from collections import namedtuple, deque
from place_recognition import PlaceRecognition
class Waypoint:
def __init__(self, state, rep=None, position=None, created_at=None, density=None, steps_to_goal=None):
self.state = state
self.rep = rep
self.position = position
self.created_at = created_at
self.density = density
self.steps_to_goal = steps_to_goal
class Trail:
def __init__(self, placeRecognition):
self.waypoints = []
self.placeRecognition = placeRecognition
self.sequence_similarity = deque(maxlen=constants.SEQUENCE_LENGTH)
def append_waypoints(self, waypoints, created_at): # [{'state': state, 'position': position}]
# steps_to_goal = len(waypoints)
steps_to_goal = 0
for waypoint in waypoints:
if (waypoint['action'] not in [constants.ACTION_TURN_RIGHT, constants.ACTION_TURN_LEFT]):
steps_to_goal += 1
for waypoint in waypoints:
if (self.placeRecognition is None): # ground-base trail
rep = None
else:
rep = self.placeRecognition.forward(waypoint['state'])
rep = rep.data.cpu() # temporary for cpu()
self.waypoints.append(Waypoint(state=waypoint['state'], rep=rep, position=waypoint['position'], created_at=created_at, density=1.0, steps_to_goal=steps_to_goal))
# steps_to_goal -= 1
if (waypoint['action'] not in [constants.ACTION_TURN_RIGHT, constants.ACTION_TURN_LEFT]):
steps_to_goal -= 1
return True
def len(self):
return len(self.waypoints)
def clear(self):
self.waypoints = []
def clear_sequence(self):
self.sequence_similarity = deque(maxlen=constants.SEQUENCE_LENGTH)
def update_waypoints(self):
i = 0
while i < len(self.waypoints):
# waypoint.density = 1.0 - (constants.TRAIL_EVAPORATION_COEFFICIENT_PER_CYCLE * (cycle - waypoint.created_at));
self.waypoints[i].density -= constants.TRAIL_EVAPORATION_COEFFICIENT_RATE
if (self.waypoints[i].density < 0):
del self.waypoints[i]
else:
i += 1
# for index, waypoint in enumerate(self.waypoints):
# # waypoint.density = 1.0 - (constants.TRAIL_EVAPORATION_COEFFICIENT_PER_CYCLE * (cycle - waypoint.created_at));
# waypoint.density -= constants.TRAIL_EVAPORATION_COEFFICIENT_RATE
# if (waypoint.density < 0):
# del self.waypoints[index]
def draw_waypoints(self):
plt.clf()
x, y, z = [], [], []
for waypoint in self.waypoints:
x.append(waypoint.position[0])
y.append(waypoint.position[1])
z.append(waypoint.density)
x, y, z = np.asarray(x), np.asarray(y), np.asarray(z)
idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]
# fig, ax = plt.subplots()
plt.scatter(x, y, c=z, s=10, edgecolor='')
plt.pause(0.05)
def calculate_threshold(self, similarity_array, k):
n = len(similarity_array)
if (n == 0):
return 0.
size = min([n, k])
threshold = np.percentile(similarity_array, (n - size) * 100 / float(n))
return threshold
def find_closest(self, input):
rep = self.placeRecognition.forward(input).data.cpu()
similarities = np.asarray([ self.placeRecognition.compute_similarity_score(rep, waypoint.rep) for waypoint in self.waypoints ])
index = similarities.argmax()
similarity = similarities[index]
if (similarity > constants.GOAL_SIMILARITY_THRESHOLD):
return self.waypoints[index], index, similarity
else:
return None, -1, 0.0
def find_best_waypoint(self, state, backward=False, last_matched=[]):
results = self.relocalize(state, backward, last_matched) # results contains (index, similaity, velocity)
min_steps_to_goal = 10000
for item in results:
if (self.waypoints[item[0]].steps_to_goal < min_steps_to_goal):
min_steps_to_goal = self.waypoints[item[0]].steps_to_goal
last_matched_indexes = []
best_state = None
best_score = 0.
best_index = -1
best_velocity = 0
for item in results:
score = (constants.TRAIL_STEP_TO_TARGET_WEIGHT * (min_steps_to_goal / self.waypoints[item[0]].steps_to_goal) +
constants.TRAIL_SIMILARITY_WEIGHT * (item[1])) / (constants.TRAIL_STEP_TO_TARGET_WEIGHT + constants.TRAIL_SIMILARITY_WEIGHT)
if (score > best_score):
best_score = score
best_state = self.waypoints[item[0]].state
best_index = item[0]
best_velocity = item[2]
return best_state, best_score, best_velocity, results
def find_closest_waypoint(self, state, backward=False, last_matched=[]):
results = self.relocalize(state, backward, last_matched) # results contains (index, similaity, velocity)
best_state = None
best_score = 0.
best_index = -1
best_velocity = 0
min_steps_to_goal = 10000
for item in results:
if (self.waypoints[item[0]].steps_to_goal <= min_steps_to_goal):
min_steps_to_goal = self.waypoints[item[0]].steps_to_goal
best_state = self.waypoints[item[0]].state
best_score = min_steps_to_goal
best_index = item[0]
best_velocity = item[2]
print ("closest match: ", best_score, best_index)
return best_state, best_score, best_velocity, results
def find_most_similar_waypoint(self, state, backward=False, last_matched=[]):
results = self.relocalize(state, backward, last_matched) # results contains (index, similaity, velocity)
best_state = None
best_score = 0.
best_index = -1
best_velocity = 0
for item in results:
if (item[1] > best_score):
best_state = self.waypoints[item[0]].state
best_score = item[1]
best_index = item[0]
best_velocity = item[2]
return best_state, best_score, best_velocity, results
def calculate_threshold_domain(self, rep, search_domain):
similarity_dict = {}
similarity_array = []
for index in search_domain:
similarity = self.placeRecognition.compute_similarity_score(self.waypoints[index].rep, rep)
similarity_dict[index] = similarity
similarity_array.append(similarity)
threshold = self.calculate_threshold(similarity_array, constants.TRAIL_K_NEAREST_NEIGHBORS)
return threshold, similarity_dict
def relocalize(self, state, backward=False, last_matched=[]):
return self.global_relocalize(state, backward, last_matched)
# return self.knn_relocalize(state, backward, last_matched)
def global_relocalize(self, state, backward=False, last_matched=[]):
if (self.len() == 0):
return []
rep = self.placeRecognition.forward(state).data.cpu()
memory_size = len(self.waypoints)
# Temporality
search_domain = []
for i in last_matched:
for index in range(i[0]-int(constants.TRAIL_TEMPORALITY_BEHIND_WINDOW_SIZE), i[0]+int(constants.TRAIL_TEMPORALITY_AHEAD_WINDOW_SIZE)):
if (index > 0 and index < memory_size and index not in search_domain):
search_domain.append(index)
# print (search_domain)
threshold, similarity_dict = self.calculate_threshold_domain(rep, search_domain)
# threshold = 0.
if (threshold < constants.TRAIL_WEAK_SIMILARITY_THRESHOLD):
print ('low threshold: ', threshold)
# print (similarity_dict)
search_domain = range(memory_size)
threshold, similarity_dict = self.calculate_threshold_domain(rep, search_domain)
# threshold = max([threshold, constants.TRAIL_SIMILARITY_THRESHOLD])
threshold = constants.TRAIL_SIMILARITY_THRESHOLD
else:
threshold = constants.TRAIL_WEAK_SIMILARITY_THRESHOLD
results = []
matched_indexes = []
for index, similarity in similarity_dict.items():
if (similarity >= threshold):
results.append((index, similarity, 0.))
matched_indexes.append(index)
results_size = len(results)
left_bound = int(results_size * constants.TRAIL_SIMILARITY_INNER_BOUND_RATE)
right_bound = int(results_size * (1. - constants.TRAIL_SIMILARITY_INNER_BOUND_RATE))
# results = results[left_bound:right_bound]
# matched_indexes = matched_indexes[left_bound:right_bound]
if (len(results) > 2):
# adding next states if still higher than a number
# lookahead_base_index = results[int(len(results)-1)][0]
matched_indexes_copy = matched_indexes.copy()
for lookahead_base_index in matched_indexes_copy:
for i in range(constants.TRAIL_LOOKAHEAD_MIN_INDEX, constants.TRAIL_LOOKAHEAD_MAX_INDEX):
index = lookahead_base_index + i
if (index < 0 or index > memory_size):
break
if (index not in similarity_dict):
# print ('---> index not in similarity dict: ', index, similarity_dict.keys())
break
if (index not in matched_indexes and similarity_dict[index] > constants.TRAIL_LOOKAHEAD_SIMILARITY_THRESHOLD):
results.append((index, similarity_dict[index], 0.))
matched_indexes.append(index)
# print ("lookahead: ", index)
else:
results = []
print ('results: ', threshold, results)
return results
def knn_relocalize(self, state, backward=False, last_matched=[]):
if (self.len() == 0):
return []
rep = self.placeRecognition.forward(state).data.cpu()
memory_size = len(self.waypoints)
# Temporality
search_domain = []
for i in last_matched:
for index in range(i[0]-int(constants.TRAIL_TEMPORALITY_BEHIND_WINDOW_SIZE), i[0]+int(constants.TRAIL_TEMPORALITY_AHEAD_WINDOW_SIZE)):
if (index > 0 and index < memory_size and index not in search_domain):
search_domain.append(index)
# print (search_domain)
threshold, similarity_dict = self.calculate_threshold_domain(rep, search_domain)
# threshold = 0.
if (threshold < constants.TRAIL_WEAK_SIMILARITY_THRESHOLD):
print ('low threshold: ', threshold)
# print (similarity_dict)
search_domain = range(memory_size)
threshold, similarity_dict = self.calculate_threshold_domain(rep, search_domain)
threshold = max([threshold, constants.TRAIL_SIMILARITY_THRESHOLD])
results = []
matched_indexes = []
for index, similarity in similarity_dict.items():
if (similarity >= threshold):
results.append((index, similarity, 0.))
matched_indexes.append(index)
results_size = len(results)
left_bound = int(results_size * constants.TRAIL_SIMILARITY_INNER_BOUND_RATE)
right_bound = int(results_size * (1. - constants.TRAIL_SIMILARITY_INNER_BOUND_RATE))
results = results[left_bound:right_bound]
matched_indexes = matched_indexes[left_bound:right_bound]
if (len(results) > 2):
# adding next states if still higher than a number
# lookahead_base_index = results[int(len(results)-1)][0]
matched_indexes_copy = matched_indexes.copy()
for lookahead_base_index in matched_indexes_copy:
for i in range(constants.TRAIL_LOOKAHEAD_MIN_INDEX, constants.TRAIL_LOOKAHEAD_MAX_INDEX):
index = lookahead_base_index + i
if (index not in similarity_dict):
# print ('---> index not in similarity dict: ', index, similarity_dict.keys())
break
if (index not in matched_indexes and similarity_dict[index] > constants.TRAIL_LOOKAHEAD_SIMILARITY_THRESHOLD):
results.append((index, similarity_dict[index], 0.))
matched_indexes.append(index)
# print ("lookahead: ", index)
else:
results = []
# print ('results: ', threshold, results)
return results
def relocalize1(self, state, backward=False):
if (self.len() == 0):
return []
rep = self.placeRecognition.forward(state).data.cpu()
memory_size = len(self.waypoints)
# Applying SeqSLAM
similarity_array = []
for index in range(memory_size): # heuristic on the search domain
similarity_array.append(self.placeRecognition.compute_similarity_score(self.waypoints[index].rep, rep))
self.sequence_similarity.append(similarity_array)
results = []
sequence_size = len(self.sequence_similarity)
max_similarity_score = 0
best_velocity = 0
matched_index = -1
for index in range(memory_size):
iter_max_similarity_score = 0
iter_best_velocity = 0
for sequence_velocity in constants.SEQUENCE_VELOCITIES:
similarity_score = 0
for sequence_index in range(len(self.sequence_similarity)):
if backward:
calculated_index = min(int(index + (sequence_velocity * sequence_index)), memory_size-1)
else: # forward
calculated_index = max(int(index - (sequence_velocity * sequence_index)), 0)
similarity_score += self.sequence_similarity[sequence_size - sequence_index - 1][calculated_index]
similarity_score /= sequence_size
# if (similarity_score > max_similarity_score):
# matched_index = index
# max_similarity_score = similarity_score
# best_velocity = sequence_velocity
if (similarity_score > iter_max_similarity_score):
iter_max_similarity_score = similarity_score
iter_best_velocity = sequence_velocity
if (iter_max_similarity_score > constants.TRAIL_SIMILARITY_THRESHOLD):
results.append((index, iter_max_similarity_score, iter_best_velocity))
return results
def find_closest_ground_waypoint(self, pose, backward=False):
results = self.ground_relocalize(pose, backward) # results contains (index, similaity, velocity)
best_state = None
best_position = None
best_score = 0.
best_index = -1
min_steps_to_goal = 10000
for item in results:
if (self.waypoints[item[0]].steps_to_goal <= min_steps_to_goal):
min_steps_to_goal = self.waypoints[item[0]].steps_to_goal
best_state = self.waypoints[item[0]].state
best_position = self.waypoints[item[0]].position
best_score = min_steps_to_goal
best_index = item[0]
print ("closest match: ", best_score, best_index)
return best_state, best_position, best_score, results
def ground_relocalize(self, position, backward=False):
results = []
memory_size = len(self.waypoints)
for index in range(memory_size):
distance = math.sqrt((position[0] - self.waypoints[index].position[0]) ** 2 +
(position[1] - self.waypoints[index].position[1]) ** 2 +
(position[2] - self.waypoints[index].position[2]) ** 2)
if (distance < constants.TRAIL_GROUND_RADIUS_THRESHOLD):
results.append((index, distance))
return results
### closest point
# memory_size = len(self.waypoints)
# min_distance = 10000.
# matched_index = -1
# for index in range(memory_size):
# distance = math.sqrt((position[0] - self.waypoints[index].position[0]) ** 2 +
# (position[1] - self.waypoints[index].position[1]) ** 2 +
# (position[2] - self.waypoints[index].position[2]) ** 2)
# if (distance < min_distance):
# min_distance = distance
# matched_index = index
#
# return matched_index, min_distance, 0
def ground_lookahead_relocalize(self, position):
memory_size = len(self.waypoints)
min_distance = 10000.
matched_index = -1
for index in reversed(range(memory_size)):
distance = math.sqrt((position[0] - self.waypoints[index].position[0]) ** 2 +
(position[1] - self.waypoints[index].position[1]) ** 2 +
(position[2] - self.waypoints[index].position[2]) ** 2)
if (distance < constants.DQN_MAX_DISTANCE_THRESHOLD):
matched_index = index
return matched_index, distance, 0
return matched_index, min_distance, 0