|
| 1 | +""" |
| 2 | +
|
| 3 | +Dijkstra Search library |
| 4 | +
|
| 5 | +author: Atsushi Sakai (@Atsushi_twi) |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import math |
| 11 | + |
| 12 | + |
| 13 | +class DijkstraSearch: |
| 14 | + |
| 15 | + class Node: |
| 16 | + """ |
| 17 | + Node class for dijkstra search |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, x, y, cost, parent): |
| 21 | + self.x = x |
| 22 | + self.y = y |
| 23 | + self.cost = cost |
| 24 | + self.parent = parent |
| 25 | + |
| 26 | + def __str__(self): |
| 27 | + return str(self.x) + "," + str(self.y) + "," + str( |
| 28 | + self.cost) + "," + str(self.parent) |
| 29 | + |
| 30 | + def __init__(self, show_animation): |
| 31 | + self.show_animation = show_animation |
| 32 | + |
| 33 | + def search(self, sx, sy, gx, gy, road_map, sample_x, sample_y): |
| 34 | + """ |
| 35 | + gx: goal x position [m] |
| 36 | + gx: goal x position [m] |
| 37 | + ox: x position list of Obstacles [m] |
| 38 | + oy: y position list of Obstacles [m] |
| 39 | + reso: grid resolution [m] |
| 40 | + rr: robot radius[m] |
| 41 | + """ |
| 42 | + |
| 43 | + start_node = self.Node(sx, sy, 0.0, -1) |
| 44 | + goal_node = self.Node(gx, gy, 0.0, -1) |
| 45 | + |
| 46 | + open_set, close_set = dict(), dict() |
| 47 | + open_set[len(road_map) - 2] = start_node |
| 48 | + |
| 49 | + while True: |
| 50 | + if not open_set: |
| 51 | + print("Cannot find path") |
| 52 | + break |
| 53 | + |
| 54 | + c_id = min(open_set, key=lambda o: open_set[o].cost) |
| 55 | + current = open_set[c_id] |
| 56 | + |
| 57 | + # show graph |
| 58 | + if self.show_animation and len( |
| 59 | + close_set.keys()) % 2 == 0: # pragma: no cover |
| 60 | + plt.plot(current.x, current.y, "xg") |
| 61 | + # for stopping simulation with the esc key. |
| 62 | + plt.gcf().canvas.mpl_connect( |
| 63 | + 'key_release_event', |
| 64 | + lambda event: [exit(0) if event.key == 'escape' else None]) |
| 65 | + plt.pause(0.001) |
| 66 | + |
| 67 | + if c_id == (len(road_map) - 1): |
| 68 | + print("goal is found!") |
| 69 | + goal_node.parent = current.parent |
| 70 | + goal_node.cost = current.cost |
| 71 | + break |
| 72 | + |
| 73 | + # Remove the item from the open set |
| 74 | + del open_set[c_id] |
| 75 | + # Add it to the closed set |
| 76 | + close_set[c_id] = current |
| 77 | + |
| 78 | + # expand search grid based on motion model |
| 79 | + for i in range(len(road_map[c_id])): |
| 80 | + n_id = road_map[c_id][i] |
| 81 | + dx = sample_x[n_id] - current.x |
| 82 | + dy = sample_y[n_id] - current.y |
| 83 | + d = math.hypot(dx, dy) |
| 84 | + node = self.Node(sample_x[n_id], sample_y[n_id], |
| 85 | + current.cost + d, c_id) |
| 86 | + |
| 87 | + if n_id in close_set: |
| 88 | + continue |
| 89 | + # Otherwise if it is already in the open set |
| 90 | + if n_id in open_set: |
| 91 | + if open_set[n_id].cost > node.cost: |
| 92 | + open_set[n_id].cost = node.cost |
| 93 | + open_set[n_id].parent = c_id |
| 94 | + else: |
| 95 | + open_set[n_id] = node |
| 96 | + |
| 97 | + # generate final course |
| 98 | + rx, ry = [goal_node.x], [goal_node.y] |
| 99 | + parent = goal_node.parent |
| 100 | + while parent != -1: |
| 101 | + n = close_set[parent] |
| 102 | + rx.append(n.x) |
| 103 | + ry.append(n.y) |
| 104 | + parent = n.parent |
| 105 | + |
| 106 | + return rx, ry |
0 commit comments