-
Notifications
You must be signed in to change notification settings - Fork 6
/
jps.h
527 lines (462 loc) · 22.2 KB
/
jps.h
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//
// Created by yash on 9/14/19.
//
#pragma once
#include "planner.h"
#include <iostream>
#include <queue>
#include <unordered_set>
namespace mpl
{
/// JPS Planner
class jps : public planner<jps>
{
public:
std::optional<std::vector<location_2d>> get_plan(const location_2d &start, const location_2d &goal) const
{
if(graph_(start.row, start.col) == 1 )
{
std::cout << "Cannot Find Path. Start Position is Occupied. \n";
return std::nullopt;
}
if(graph_(goal.row, goal.col) == 1)
{
std::cout << "Cannot Find Path. End Position is Occupied. \n";
return std::nullopt;
}
std::vector<location_2d> path;
std::unordered_set<location_2d> closed_set;
std::unordered_map<location_2d, double> f_costs;
std::unordered_map<location_2d, double> g_costs;
auto less = [&](const location_2d& left, const location_2d& right) {
return f_costs[left] > f_costs[right];
};
std::priority_queue<location_2d, std::vector<location_2d>, decltype(less)> open_queue(less);
std::unordered_set<location_2d> open_set;
// Mapping from Parent Node to Current Node
std::unordered_map<location_2d, location_2d> parent_from_node;
open_queue.push(start);
open_set.insert(start);
while(!open_queue.empty())
{
const auto current_location = open_queue.top();
open_queue.pop();
if(current_location == goal)
{
auto path_node = goal;
path.emplace_back(path_node);
while (path_node != start)
{
assert(parent_from_node.find(path_node) != parent_from_node.end() && "No Parent for Discovered Node! Check Logic");
linear_backtrace(path_node, parent_from_node[path_node], &path);
path_node = parent_from_node[path_node];
}
return path;
}
// do a-star using jps functions
for_all_adjacent_nodes(current_location, graph_, [&](location_2d&& adjacent_loc){
const location_2d& step = location_2d(adjacent_loc.row - current_location.row,
adjacent_loc.col - current_location.col);
if(closed_set.find(adjacent_loc) == closed_set.end())
{
const auto successors = get_successors(current_location,
step,
start,
goal);
for (const auto &successor: successors)
{
if(closed_set.find(successor) == closed_set.end())
{
if (open_set.find(successor) == open_set.end())
{
g_costs[successor] = g_costs[current_location] + get_distance(&successor, ¤t_location);
f_costs[successor] = g_costs[successor] + get_distance(&successor, &goal);
parent_from_node[successor] = current_location;
open_set.insert(successor);
open_queue.push(successor);
}
else if (const auto g_cost_adjacent = g_costs[successor] + get_distance(&successor, ¤t_location);
g_costs[successor] > g_cost_adjacent)
{
g_costs[successor] = g_cost_adjacent;
parent_from_node[successor] = current_location;
}
}
}
closed_set.insert(current_location);
}
});
}
std::cout << "Path does not exist! \n";
return std::nullopt;
}
std::optional<std::vector<location_2d>> get_plan_with_visualization(const location_2d &start, const location_2d &goal)
{
if(graph_(start.row, start.col) == 1 )
{
std::cout << "Cannot Find Path. Start Position is Occupied. \n";
return std::nullopt;
}
if(graph_(goal.row, goal.col) == 1)
{
std::cout << "Cannot Find Path. End Position is Occupied. \n";
return std::nullopt;
}
std::vector<location_2d> path;
std::unordered_set<location_2d> closed_set;
std::unordered_map<location_2d, double> f_costs;
std::unordered_map<location_2d, double> g_costs;
auto less = [&](const location_2d& left, const location_2d& right) {
return f_costs[left] > f_costs[right];
};
std::priority_queue<location_2d, std::vector<location_2d>, decltype(less)> open_queue(less);
std::unordered_set<location_2d> open_set;
// Mapping from Parent Node to Current Node
std::unordered_map<location_2d, location_2d> parent_from_node;
open_queue.push(start);
open_set.insert(start);
while(!open_queue.empty())
{
const auto current_location = open_queue.top();
open_queue.pop();
if(current_location == goal)
{
auto path_node = goal;
path.emplace_back(path_node);
while (path_node != start)
{
assert(parent_from_node.find(path_node) != parent_from_node.end() && "No Parent for Discovered Node! Check Logic");
linear_backtrace(path_node, parent_from_node[path_node], &path);
path_node = parent_from_node[path_node];
}
for(const auto & node: path)
{
explored_locations_.emplace_back(node, "yellow");
}
explored_locations_.emplace_back(start, "red");
explored_locations_.emplace_back(goal, "green");
return path;
}
// do a-star using jps functions
for_all_adjacent_nodes(current_location, graph_, [&](location_2d&& adjacent_loc){
const location_2d& step = location_2d(adjacent_loc.row - current_location.row,
adjacent_loc.col - current_location.col);
if(closed_set.find(adjacent_loc) == closed_set.end())
{
const auto successors = get_successors(current_location,
step,
start,
goal);
for (const auto &successor: successors)
{
if(closed_set.find(successor) == closed_set.end())
{
if (open_set.find(successor) == open_set.end())
{
g_costs[successor] = g_costs[current_location] + get_distance(&successor, ¤t_location);
f_costs[successor] = g_costs[successor] + get_distance(&successor, &goal);
parent_from_node[successor] = current_location;
open_set.insert(successor);
open_queue.push(successor);
explored_locations_.emplace_back(successor, "orange");
}
else if (const auto g_cost_adjacent = g_costs[successor] + get_distance(&successor, ¤t_location);
g_costs[successor] > g_cost_adjacent)
{
g_costs[successor] = g_cost_adjacent;
parent_from_node[successor] = current_location;
}
}
}
closed_set.insert(adjacent_loc);
}
});
}
std::cout << "Path does not exist! \n";
return std::nullopt;
}
private:
/// Get the JPS successors of a current node in a particular direction (Refer to JPS Paper for more info)
/// @param current_location
/// @param step
/// @param start
/// @param goal
/// @return
std::vector<mpl::location_2d> get_successors(const location_2d& current_location,
const location_2d& step,
const location_2d &start,
const location_2d &goal) const
{
assert(is_within_boundary(current_location, n_rows, n_cols));
std::vector<location_2d> successors;
const auto pruned_neighbors = get_pruned_neighbors(current_location, step);
for(const auto& neighbor: pruned_neighbors)
{
if(graph_(neighbor.first.row, neighbor.first.col) == 1) continue;
if(const auto successor = jump(current_location, neighbor.second, start, goal))
{
assert(is_within_boundary(*successor, n_rows, n_cols));
successors.emplace_back(*successor);
}
}
return successors;
}
std::optional<location_2d> jump(const location_2d& cur_loc,
const location_2d& step,
const location_2d &start,
const location_2d &goal) const
{
assert(cur_loc.is_within_boundary(n_rows, n_cols));
// take a step in the input direction
auto next_location = cur_loc + step;
if(!is_within_boundary(next_location, n_rows, n_cols) || graph_(next_location.row, next_location.col) == 1)
{
return std::nullopt;
}
else if(next_location == goal)
{
return next_location;
}
else if(has_forced_neighbors(next_location, step))
{
return next_location;
}
else if(step.row !=0 && step.col !=0)
{
if(jump(next_location, location_2d(step.row, 0), start, goal) ||
jump(next_location, location_2d(0, step.col), start, goal)) return next_location;
}
return jump(next_location, step, start, goal);
}
/// Checks if the current location has forced neighbors (Refer to JPS Paper for more info)
/// @param cur_loc - current node location in the graph
/// @param step - current step direction
/// @return true if the current location has a forced neighbor else false
bool has_forced_neighbors(const mpl::location_2d& cur_loc, const mpl::location_2d& step) const
{
assert(cur_loc.is_within_boundary(n_rows, n_cols));
auto is_within_boundary_after_offset = [&](int step_row, int step_col){
return is_within_boundary(location_2d(cur_loc.row + step_row, cur_loc.col + step_col), n_rows, n_cols);
};
if(step.row == 0 && step.col != 0) // Condition for Horizontal Direction
{
if(is_within_boundary_after_offset(-1, 0))
{
if(graph_(cur_loc.row - 1, cur_loc.col) == 1 &&
is_within_boundary_after_offset(-1, step.col) &&
graph_(cur_loc.row - 1, cur_loc.col+step.col) == 0) return true;
}
if(is_within_boundary_after_offset(1, 0))
{
if(graph_(cur_loc.row + 1, cur_loc.col) == 1 &&
is_within_boundary_after_offset(1, step.col) &&
graph_(cur_loc.row + 1, cur_loc.col+step.col) == 0) return true;
}
return false;
}
else if(step.row != 0 && step.col == 0) // Condition for Vertical Direction
{
if(is_within_boundary_after_offset(0, -1))
{
if(graph_(cur_loc.row, cur_loc.col - 1) == 1 &&
is_within_boundary_after_offset(step.row, 1) &&
graph_(cur_loc.row + step.row, cur_loc.col-1) == 0) return true;
}
if(is_within_boundary_after_offset(0, 1))
{
if(graph_(cur_loc.row, cur_loc.col + 1) == 1 &&
is_within_boundary_after_offset(step.row, 1) &&
graph_(cur_loc.row + step.row, cur_loc.col+1) == 0) return true;
}
return false;
}
else if(step.row == 1 && step.col == 1) // Condition for Diagonal 1
{
if(graph_(cur_loc.row, cur_loc.col - 1) == 1) return true;
if(graph_(cur_loc.row - 1, cur_loc.col) == 1) return true;
return false;
}
else if(step.row == -1 && step.col == -1) // Condition for Diagonal 2
{
if(graph_(cur_loc.row + 1, cur_loc.col) == 1) return true;
if(graph_(cur_loc.row, cur_loc.col + 1) == 1) return true;
return false;
}
else if(step.row == 1 && step.col == -1) // Condition for Diagonal 3
{
if(graph_(cur_loc.row - 1, cur_loc.col) == 1) return true;
if(graph_(cur_loc.row, cur_loc.col + 1) == 1) return true;
return false;
}
else if(step.row == -1 && step.col == 1) // Condition for Diagonal 4
{
if(graph_(cur_loc.row, cur_loc.col - 1) == 1) return true;
if(graph_(cur_loc.row + 1, cur_loc.col) == 1) return true;
return false;
}
}
/// Get pruned neighbors of a node with a particular direction (Refer to JPS Paper for more info)
/// @param cur_loc
/// @param step
/// @return Vector of Pair of Neighbor Locations and their direction with respect to the current location
std::vector<std::pair<mpl::location_2d, mpl::location_2d>> get_pruned_neighbors(
const mpl::location_2d& cur_loc, const mpl::location_2d& step) const
{
assert(cur_loc.is_within_boundary(n_rows, n_cols));
// Pair of Neighbor Location and Direction
std::vector<std::pair<mpl::location_2d, mpl::location_2d>> pruned_neighbors;
auto add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary = [&](int step_row, int step_col){
const auto neighbor = location_2d(cur_loc.row + step_row, cur_loc.col + step_col);
if(is_within_boundary(neighbor, n_rows, n_cols) && graph_(neighbor.row, neighbor.col) == 0)
{
pruned_neighbors.emplace_back(std::pair{neighbor, location_2d(step_row, step_col)});
}
};
auto is_within_boundary_after_offset = [&](int step_row, int step_col){
return is_within_boundary(location_2d(cur_loc.row + step_row, cur_loc.col + step_col), n_rows, n_cols);
};
if(step.row == 0 && step.col == 1) // Condition for Horizontal Direction 1
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(0, 1);
if(is_within_boundary_after_offset(-1, 0) && graph_(cur_loc.row - 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 1);
}
if(is_within_boundary_after_offset(1, 0) && graph_(cur_loc.row + 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 1);
}
}
else if(step.row == 0 && step.col == -1) // Condition for Horizontal Direction 2
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(0, -1);
if(is_within_boundary_after_offset(-1, 0) && graph_(cur_loc.row - 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, -1);
}
if(is_within_boundary_after_offset(1, 0) && graph_(cur_loc.row + 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, -1);
}
}
else if(step.row == 1 && step.col == 0) // Condition for Vertical Direction 1
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 0);
if(is_within_boundary_after_offset(0, -1) && graph_(cur_loc.row, cur_loc.col - 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, -1);
}
if(is_within_boundary_after_offset(0, 1) && graph_(cur_loc.row, cur_loc.col + 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 1);
}
}
else if(step.row == -1 && step.col == 0) // Condition for Vertical Direction 2
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 0);
if(is_within_boundary_after_offset(0, -1) && graph_(cur_loc.row , cur_loc.col - 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, -1);
}
if(is_within_boundary_after_offset(0, 1) && graph_(cur_loc.row , cur_loc.col + 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 1);
}
}
else if(step.row == 1 && step.col == 1) // Condition for Diagonal 1
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 0);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(0, 1);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 1);
if(is_within_boundary_after_offset(0, -1) && graph_(cur_loc.row, cur_loc.col - 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, -1);
}
if(is_within_boundary_after_offset(-1, 0) && graph_(cur_loc.row - 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 1);
}
}
else if(step.row == -1 && step.col == -1) // Condition for Diagonal 2
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 0);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(0, -1);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, -1);
if(is_within_boundary_after_offset(1, 0) && graph_(cur_loc.row + 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, -1);
}
if(is_within_boundary_after_offset(0, 1) && graph_(cur_loc.row, cur_loc.col + 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 1);
}
}
else if(step.row == 1 && step.col == -1) // Condition for Diagonal 3
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 0);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(0, -1);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, -1);
if(is_within_boundary_after_offset(-1, 0) && graph_(cur_loc.row - 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, -1);
}
if(is_within_boundary_after_offset(0, 1) && graph_(cur_loc.row, cur_loc.col + 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 1);
}
}
else if(step.row == -1 && step.col == 1) // Condition for Diagonal 4
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 0);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(0, 1);
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, 1);
if(is_within_boundary_after_offset(0, -1) && graph_(cur_loc.row, cur_loc.col - 1) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(-1, -1);
}
if(is_within_boundary_after_offset(1, 0) && graph_(cur_loc.row + 1, cur_loc.col) == 1)
{
add_neighbor_direction_pair_to_pruned_neighbors_if_within_boundary(1, 1);
}
}
return pruned_neighbors;
}
/// Backtrack from the start to the end and add the intermediate points to the path vector
/// @param backtrace_start
/// @param backtrace_end
/// @param path
void linear_backtrace(const location_2d& backtrace_start,
const location_2d& backtrace_end, std::vector<location_2d>* path) const
{
assert(is_linear_backtrace_possible(backtrace_start, backtrace_end));
const location_2d step = [&](){
const auto diff = backtrace_start - backtrace_end;
auto get_single_dimension_direction = [&](int x){
if(x < 0) return -1;
else if(x > 0) return 1;
else return 0;
};
return location_2d(get_single_dimension_direction(diff.row),
get_single_dimension_direction(diff.col));
}();
location_2d current_node = backtrace_start;
while(current_node != backtrace_end)
{
current_node = current_node - step;
assert(is_within_boundary(current_node, n_rows, n_cols) &&
"Adding the new step puts the location out of range");
path->emplace_back(current_node);
}
}
/// Debug function to check if linear backtracking is possible between two locations on the grid map
/// @param backtrace_start
/// @param backtrace_end
/// @return
bool is_linear_backtrace_possible(const location_2d& backtrace_start,
const location_2d& backtrace_end) const
{
const auto diff = backtrace_start - backtrace_end;
return !(diff.row != 0 && diff.col != 0 && diff.row != diff.col && diff.row != -diff.col);
}
};
} // namespace mpl