-
Notifications
You must be signed in to change notification settings - Fork 9
/
extract_waypoints.py
377 lines (309 loc) · 12.1 KB
/
extract_waypoints.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
""" Automatic waypoint selection """
import numpy as np
import copy
from waypoint_extraction.traj_reconstruction import (
pos_only_geometric_waypoint_trajectory,
reconstruct_waypoint_trajectory,
geometric_waypoint_trajectory,
)
""" Iterative waypoint selection """
def greedy_waypoint_selection(
env=None,
actions=None,
gt_states=None,
err_threshold=None,
initial_states=None,
remove_obj=None,
geometry=True,
pos_only=False,
):
# make the last frame a waypoint
waypoints = [len(actions) - 1]
# make the frames of gripper open/close waypoints
if not pos_only:
for i in range(len(actions) - 1):
if actions[i, -1] != actions[i + 1, -1]:
waypoints.append(i)
waypoints.append(i + 1)
waypoints.sort()
# reconstruct the trajectory, and record the reconstruction error for each state
for i in range(len(actions)):
if pos_only or geometry:
func = (
pos_only_geometric_waypoint_trajectory
if pos_only
else geometric_waypoint_trajectory
)
total_traj_err, reconstruction_error = func(
actions=actions,
gt_states=gt_states,
waypoints=waypoints,
return_list=True,
)
else:
_, reconstruction_error, total_traj_err = reconstruct_waypoint_trajectory(
env=env,
actions=actions,
gt_states=gt_states,
waypoints=waypoints,
verbose=False,
initial_state=initial_states[0],
remove_obj=remove_obj,
)
# break if the reconstruction error is below the threshold
if total_traj_err < err_threshold:
break
# add the frame of the highest reconstruction error as a waypoint, excluding frames that are already waypoints
max_error_frame = np.argmax(reconstruction_error)
while max_error_frame in waypoints:
reconstruction_error[max_error_frame] = 0
max_error_frame = np.argmax(reconstruction_error)
waypoints.append(max_error_frame)
waypoints.sort()
print("=======================================================================")
print(
f"Selected {len(waypoints)} waypoints: {waypoints} \t total trajectory error: {total_traj_err:.6f}"
)
return waypoints
def heuristic_waypoint_selection(
env=None,
actions=None,
gt_states=None,
err_threshold=None,
initial_states=None,
remove_obj=None,
geometry=True,
pos_only=False,
):
# make the last frame a waypoint
waypoints = [len(actions) - 1]
# make the frames of gripper open/close waypoints
for i in range(len(actions) - 1):
if actions[i, -1] != actions[i + 1, -1]:
waypoints.append(i)
waypoints.sort()
# if 'robot0_vel_ang' or 'robot0_vel_lin' in gt_states is close to 0, make the frame a waypoint
for i in range(len(gt_states)):
if (
np.linalg.norm(gt_states[i]["robot0_vel_ang"]) < err_threshold
or np.linalg.norm(gt_states[i]["robot0_vel_lin"]) < err_threshold
):
waypoints.append(i)
waypoints.sort()
print("=======================================================================")
print(f"Selected {len(waypoints)} waypoints: {waypoints}")
return waypoints
""" Backtrack waypoint selection """
def backtrack_waypoint_selection(
env, actions, gt_states, err_threshold, initial_states, remove_obj
):
# add heuristic waypoints
num_frames = len(actions)
# make the last frame a waypoint
waypoints = [num_frames - 1]
# make the frames of gripper open/close waypoints
for i in range(num_frames - 1):
if actions[i, -1] != actions[i + 1, -1]:
waypoints.append(i)
waypoints.sort()
# backtracing to find the optimal waypoints
start = 0
while start < num_frames - 1:
for end in range(num_frames - 1, 0, -1):
rel_waypoints = [k - start for k in waypoints if k >= start and k < end] + [
end - start
]
_, _, total_traj_err = reconstruct_waypoint_trajectory(
env=env,
actions=actions[start : end + 1],
gt_states=gt_states[start + 1 : end + 2],
waypoints=rel_waypoints,
verbose=False,
initial_state=initial_states[start],
remove_obj=remove_obj,
)
if total_traj_err < err_threshold:
waypoints.append(end)
waypoints = list(set(waypoints))
waypoints.sort()
break
start = end
print("=======================================================================")
print(
f"Selected {len(waypoints)} waypoints: {waypoints} \t total trajectory error: {total_traj_err:.6f}"
)
return waypoints
""" DP waypoint selection """
# use geometric interpretation
def dp_waypoint_selection(
env=None,
actions=None,
gt_states=None,
err_threshold=None,
initial_states=None,
remove_obj=None,
pos_only=False,
):
if actions is None:
actions = copy.deepcopy(gt_states)
elif gt_states is None:
gt_states = copy.deepcopy(actions)
num_frames = len(actions)
# make the last frame a waypoint
initial_waypoints = [num_frames - 1]
# make the frames of gripper open/close waypoints
if not pos_only:
for i in range(num_frames - 1):
if actions[i, -1] != actions[i + 1, -1]:
initial_waypoints.append(i)
# initial_waypoints.append(i + 1)
initial_waypoints.sort()
# Memoization table to store the waypoint sets for subproblems
memo = {}
# Initialize the memoization table
for i in range(num_frames):
memo[i] = (0, [])
memo[1] = (1, [1])
func = (
pos_only_geometric_waypoint_trajectory
if pos_only
else geometric_waypoint_trajectory
)
# Populate the memoization table using an iterative bottom-up approach
for i in range(1, num_frames):
min_waypoints_required = float("inf")
best_waypoints = []
for k in range(1, i):
# waypoints are relative to the subsequence
waypoints = [j - k for j in initial_waypoints if j >= k and j < i] + [i - k]
total_traj_err = func(
actions=actions[k : i + 1],
gt_states=gt_states[k : i + 1],
waypoints=waypoints,
)
if total_traj_err < err_threshold:
subproblem_waypoints_count, subproblem_waypoints = memo[k - 1]
total_waypoints_count = 1 + subproblem_waypoints_count
if total_waypoints_count < min_waypoints_required:
min_waypoints_required = total_waypoints_count
best_waypoints = subproblem_waypoints + [i]
memo[i] = (min_waypoints_required, best_waypoints)
min_waypoints_count, waypoints = memo[num_frames - 1]
waypoints += initial_waypoints
# remove duplicates
waypoints = list(set(waypoints))
waypoints.sort()
print(
f"Minimum number of waypoints: {len(waypoints)} \tTrajectory Error: {total_traj_err}"
)
print(f"waypoint positions: {waypoints}")
return waypoints
# iterative version, bottom-up
def dp_reconstruct_waypoint_selection(
env, actions, gt_states, err_threshold, initial_states, remove_obj
):
num_frames = len(actions)
# make the last frame a waypoint
initial_waypoints = [num_frames - 1]
# make the frames of gripper open/close waypoints
for i in range(num_frames - 1):
if actions[i, -1] != actions[i + 1, -1]:
initial_waypoints.append(i)
initial_waypoints.sort()
# Memoization table to store the waypoint sets for subproblems
memo = {}
# Initialize the memoization table
for i in range(num_frames):
memo[i] = (0, [])
memo[1] = (1, [1])
# Populate the memoization table using an iterative bottom-up approach
for i in range(1, num_frames):
min_waypoints_required = float("inf")
best_waypoints = []
for k in range(1, i):
# waypoints are relative to the subsequence
waypoints = [j - k for j in initial_waypoints if j >= k and j < i] + [i - k]
_, _, total_traj_err = reconstruct_waypoint_trajectory(
env=env,
actions=actions[k - 1 : i],
gt_states=gt_states[k : i + 1],
waypoints=waypoints,
verbose=False,
initial_state=initial_states[k - 1],
remove_obj=remove_obj,
)
print(f"i: {i}, k: {k}, total_traj_err: {total_traj_err}")
if total_traj_err < err_threshold:
subproblem_waypoints_count, subproblem_waypoints = memo[k - 1]
total_waypoints_count = 1 + subproblem_waypoints_count
if total_waypoints_count < min_waypoints_required:
min_waypoints_required = total_waypoints_count
best_waypoints = subproblem_waypoints + [i]
print(
f"min_waypoints_required: {min_waypoints_required}, best_waypoints: {best_waypoints}"
)
memo[i] = (min_waypoints_required, best_waypoints)
min_waypoints_count, waypoints = memo[num_frames - 1]
waypoints += initial_waypoints
# remove duplicates
waypoints = list(set(waypoints))
waypoints.sort()
print(f"Minimum number of waypoints: {len(waypoints)}")
print(f"waypoint positions: {waypoints}")
return waypoints
# backlog: recursive version, top-down
def recursive_dp_waypoint_selection(
env, actions, gt_states, err_threshold, initial_states, remove_obj
):
num_frames = len(actions)
# make the last frame a waypoint
initial_waypoints = [num_frames - 1]
# make the frames of gripper open/close as waypoints
for i in range(num_frames - 1):
if actions[i, -1] != actions[i + 1, -1]:
initial_waypoints.append(i)
initial_waypoints.sort()
# Memoization table to store the waypoint sets for subproblems
memo = {}
def min_waypoints(i, err_threshold):
if i < 1:
return (0, [])
if i in memo:
return memo[i]
min_waypoints_required = float("inf")
best_waypoints = []
for k in range(1, i):
# waypoints are relative to the subsequence
waypoints = [j - k for j in initial_waypoints if j >= k and j < i] + [i - k]
_, _, total_traj_err = reconstruct_waypoint_trajectory(
env=env,
actions=actions[k : i + 1],
gt_states=gt_states[k : i + 1],
waypoints=waypoints,
verbose=False,
initial_state=initial_states[k],
remove_obj=remove_obj,
)
# print some useful information for debugging
print(f"i: {i}, k: {k}, total_traj_err: {total_traj_err}")
if total_traj_err < err_threshold:
subproblem_waypoints_count, subproblem_waypoints = min_waypoints(
k - 1, err_threshold
)
total_waypoints_count = 1 + subproblem_waypoints_count
if total_waypoints_count < min_waypoints_required:
min_waypoints_required = total_waypoints_count
best_waypoints = subproblem_waypoints + [i]
print(
f"min_waypoints_required: {min_waypoints_required}, best_waypoints: {best_waypoints}"
)
memo[i] = (min_waypoints_required, best_waypoints)
return memo[i]
min_waypoints_count, waypoints = min_waypoints(num_frames - 1, err_threshold)
waypoints += initial_waypoints
# remove duplicates
waypoints = list(set(waypoints))
waypoints.sort()
print(f"Minimum number of waypoints: {min_waypoints_count}")
print(f"waypoint positions: {waypoints}")
return waypoints