From 89ef0a9a5287695c928db24a0fa7e15c51c02206 Mon Sep 17 00:00:00 2001 From: Erwin Lejeune Date: Fri, 10 Apr 2020 15:08:27 +0200 Subject: [PATCH 1/7] add bidirectional A* --- .../BidirectionalAStar/bidir_a_star.py | 331 ++++++++++++++++++ 1 file changed, 331 insertions(+) create mode 100644 PathPlanning/BidirectionalAStar/bidir_a_star.py diff --git a/PathPlanning/BidirectionalAStar/bidir_a_star.py b/PathPlanning/BidirectionalAStar/bidir_a_star.py new file mode 100644 index 00000000..3c709c64 --- /dev/null +++ b/PathPlanning/BidirectionalAStar/bidir_a_star.py @@ -0,0 +1,331 @@ +""" + +Bidirectional A* grid planning + +author: Erwin Lejeune (@spida_rwin) + +See Wikipedia article (https://en.wikipedia.org/wiki/Bidirectional_search) + +""" + +import math + +import matplotlib.pyplot as plt + +show_animation = True + + +class BidirAStarPlanner: + + def __init__(self, ox, oy, reso, rr): + """ + Initialize grid map for a star planning + + ox: x position list of Obstacles [m] + oy: y position list of Obstacles [m] + reso: grid resolution [m] + rr: robot radius[m] + """ + + self.reso = reso + self.rr = rr + self.calc_obstacle_map(ox, oy) + self.motion = self.get_motion_model() + + class Node: + def __init__(self, x, y, cost, pind): + self.x = x # index of grid + self.y = y # index of grid + self.cost = cost + self.pind = pind + + def __str__(self): + return str(self.x) + "," + str(self.y) + "," + str( + self.cost) + "," + str(self.pind) + + def planning(self, sx, sy, gx, gy): + """ + Bidirectional A star path search + + input: + sx: start x position [m] + sy: start y position [m] + gx: goal x position [m] + gy: goal y position [m] + + output: + rx: x position list of the final path + ry: y position list of the final path + """ + + nstart = self.Node(self.calc_xyindex(sx, self.minx), + self.calc_xyindex(sy, self.miny), 0.0, -1) + ngoal = self.Node(self.calc_xyindex(gx, self.minx), + self.calc_xyindex(gy, self.miny), 0.0, -1) + + open_set_A, closed_set_A = dict(), dict() + open_set_B, closed_set_B = dict(), dict() + open_set_A[self.calc_grid_index(nstart)] = nstart + open_set_B[self.calc_grid_index(ngoal)] = ngoal + + current_A = nstart + current_B = ngoal + + while 1: + if len(open_set_A) == 0: + print("Open set A is empty..") + break + + if len(open_set_B) == 0: + print("Open set B is empty..") + break + + c_id_A = min( + open_set_A, + key=lambda o: open_set_A[o].cost + self.calc_heuristic(current_B, + open_set_A[ + o])) + + current_A = open_set_A[c_id_A] + + c_id_B = min( + open_set_B, + key=lambda o: open_set_B[o].cost + self.calc_heuristic(current_A, + open_set_B[ + o])) + + current_B = open_set_B[c_id_B] + + # show graph + if show_animation: # pragma: no cover + plt.plot(self.calc_grid_position(current_A.x, self.minx), + self.calc_grid_position(current_A.y, self.miny), "xc") + plt.plot(self.calc_grid_position(current_B.x, self.minx), + self.calc_grid_position(current_B.y, self.miny), "xc") + # for stopping simulation with the esc key. + plt.gcf().canvas.mpl_connect('key_release_event', + lambda event: [exit( + 0) if event.key == 'escape' else None]) + if len(closed_set_A.keys()) % 10 == 0: + plt.pause(0.001) + + if current_A.x == current_B.x and current_A.y == current_B.y: + print("Found goal") + meetpointA = current_A + meetpointB = current_B + break + + # Remove the item from the open set + del open_set_A[c_id_A] + del open_set_B[c_id_B] + + # Add it to the closed set + closed_set_A[c_id_A] = current_A + closed_set_B[c_id_B] = current_B + + # expand_grid search grid based on motion model + for i, _ in enumerate(self.motion): + node = self.Node(current_A.x + self.motion[i][0], + current_A.y + self.motion[i][1], + current_A.cost + self.motion[i][2], c_id_A) + n_id = self.calc_grid_index(node) + + # If the node is not safe, do nothing + if not self.verify_node(node): + continue + + if n_id in closed_set_A: + continue + + if n_id not in open_set_A: + open_set_A[n_id] = node # discovered a new node + else: + if open_set_A[n_id].cost > node.cost: + # This path is the best until now. record it + open_set_A[n_id] = node + + for i, _ in enumerate(self.motion): + node = self.Node(current_B.x + self.motion[i][0], + current_B.y + self.motion[i][1], + current_B.cost + self.motion[i][2], c_id_B) + n_id = self.calc_grid_index(node) + + # If the node is not safe, do nothing + if not self.verify_node(node): + continue + + if n_id in closed_set_B: + continue + + if n_id not in open_set_B: + open_set_B[n_id] = node # discovered a new node + else: + if open_set_B[n_id].cost > node.cost: + # This path is the best until now. record it + open_set_B[n_id] = node + + rx, ry = self.calc_final_path_bidir(meetpointA, meetpointB, closed_set_A, closed_set_B) + + return rx, ry + + def calc_final_path_bidir(self, meetnode_A, meetnode_B, closed_set_A, closed_set_B): + rx_A, ry_A = self.calc_final_path(meetnode_A, closed_set_A) + rx_B, ry_B = self.calc_final_path(meetnode_B, closed_set_B) + + rx_A.reverse() + ry_A.reverse() + + rx = rx_A + rx_B + ry = ry_A + ry_B + + return rx, ry + + def calc_final_path(self, ngoal, closedset): + # generate final course + rx, ry = [self.calc_grid_position(ngoal.x, self.minx)], [ + self.calc_grid_position(ngoal.y, self.miny)] + pind = ngoal.pind + while pind != -1: + n = closedset[pind] + rx.append(self.calc_grid_position(n.x, self.minx)) + ry.append(self.calc_grid_position(n.y, self.miny)) + pind = n.pind + + return rx, ry + + @staticmethod + def calc_heuristic(n1, n2): + w = 1.0 # weight of heuristic + d = w * math.hypot(n1.x - n2.x, n1.y - n2.y) + return d + + def calc_grid_position(self, index, minp): + """ + calc grid position + + :param index: + :param minp: + :return: + """ + pos = index * self.reso + minp + return pos + + def calc_xyindex(self, position, min_pos): + return round((position - min_pos) / self.reso) + + def calc_grid_index(self, node): + return (node.y - self.miny) * self.xwidth + (node.x - self.minx) + + def verify_node(self, node): + px = self.calc_grid_position(node.x, self.minx) + py = self.calc_grid_position(node.y, self.miny) + + if px < self.minx: + return False + elif py < self.miny: + return False + elif px >= self.maxx: + return False + elif py >= self.maxy: + return False + + # collision check + if self.obmap[node.x][node.y]: + return False + + return True + + def calc_obstacle_map(self, ox, oy): + + self.minx = round(min(ox)) + self.miny = round(min(oy)) + self.maxx = round(max(ox)) + self.maxy = round(max(oy)) + print("minx:", self.minx) + print("miny:", self.miny) + print("maxx:", self.maxx) + print("maxy:", self.maxy) + + self.xwidth = round((self.maxx - self.minx) / self.reso) + self.ywidth = round((self.maxy - self.miny) / self.reso) + print("xwidth:", self.xwidth) + print("ywidth:", self.ywidth) + + # obstacle map generation + self.obmap = [[False for i in range(self.ywidth)] + for i in range(self.xwidth)] + for ix in range(self.xwidth): + x = self.calc_grid_position(ix, self.minx) + for iy in range(self.ywidth): + y = self.calc_grid_position(iy, self.miny) + for iox, ioy in zip(ox, oy): + d = math.hypot(iox - x, ioy - y) + if d <= self.rr: + self.obmap[ix][iy] = True + break + + @staticmethod + def get_motion_model(): + # dx, dy, cost + motion = [[1, 0, 1], + [0, 1, 1], + [-1, 0, 1], + [0, -1, 1], + [-1, -1, math.sqrt(2)], + [-1, 1, math.sqrt(2)], + [1, -1, math.sqrt(2)], + [1, 1, math.sqrt(2)]] + + return motion + + +def main(): + print(__file__ + " start!!") + + # start and goal position + sx = 10.0 # [m] + sy = 10.0 # [m] + gx = 50.0 # [m] + gy = 50.0 # [m] + grid_size = 2.0 # [m] + robot_radius = 1.0 # [m] + + # set obstable positions + ox, oy = [], [] + for i in range(-10, 60): + ox.append(i) + oy.append(-10.0) + for i in range(-10, 60): + ox.append(60.0) + oy.append(i) + for i in range(-10, 61): + ox.append(i) + oy.append(60.0) + for i in range(-10, 61): + ox.append(-10.0) + oy.append(i) + for i in range(-10, 40): + ox.append(20.0) + oy.append(i) + for i in range(0, 40): + ox.append(40.0) + oy.append(60.0 - i) + + if show_animation: # pragma: no cover + plt.plot(ox, oy, ".k") + plt.plot(sx, sy, "og") + plt.plot(gx, gy, "ob") + plt.grid(True) + plt.axis("equal") + + bidir_a_star = BidirAStarPlanner(ox, oy, grid_size, robot_radius) + rx, ry = bidir_a_star.planning(sx, sy, gx, gy) + + if show_animation: # pragma: no cover + plt.plot(rx, ry, "-r") + plt.pause(.0001) + plt.show() + + +if __name__ == '__main__': + main() From 6f358adc5ea9c3a80ef6448e69006540f7f726e3 Mon Sep 17 00:00:00 2001 From: Erwin Lejeune Date: Mon, 13 Apr 2020 14:23:13 +0200 Subject: [PATCH 2/7] shorten motion expansion loop --- ...idir_a_star.py => bidirectional_a_star.py} | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) rename PathPlanning/BidirectionalAStar/{bidir_a_star.py => bidirectional_a_star.py} (84%) diff --git a/PathPlanning/BidirectionalAStar/bidir_a_star.py b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py similarity index 84% rename from PathPlanning/BidirectionalAStar/bidir_a_star.py rename to PathPlanning/BidirectionalAStar/bidirectional_a_star.py index 3c709c64..37d14eab 100644 --- a/PathPlanning/BidirectionalAStar/bidir_a_star.py +++ b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py @@ -15,7 +15,7 @@ import matplotlib.pyplot as plt show_animation = True -class BidirAStarPlanner: +class BidirectionalAStarPlanner: def __init__(self, ox, oy, reso, rr): """ @@ -125,44 +125,48 @@ class BidirAStarPlanner: # expand_grid search grid based on motion model for i, _ in enumerate(self.motion): - node = self.Node(current_A.x + self.motion[i][0], + continue_A = False + continue_B = False + + child_node_A = self.Node(current_A.x + self.motion[i][0], current_A.y + self.motion[i][1], current_A.cost + self.motion[i][2], c_id_A) - n_id = self.calc_grid_index(node) - # If the node is not safe, do nothing - if not self.verify_node(node): - continue - - if n_id in closed_set_A: - continue - - if n_id not in open_set_A: - open_set_A[n_id] = node # discovered a new node - else: - if open_set_A[n_id].cost > node.cost: - # This path is the best until now. record it - open_set_A[n_id] = node - - for i, _ in enumerate(self.motion): - node = self.Node(current_B.x + self.motion[i][0], + child_node_B = self.Node(current_B.x + self.motion[i][0], current_B.y + self.motion[i][1], current_B.cost + self.motion[i][2], c_id_B) - n_id = self.calc_grid_index(node) + + n_id_A = self.calc_grid_index(child_node_A) + n_id_B = self.calc_grid_index(child_node_B) # If the node is not safe, do nothing - if not self.verify_node(node): - continue + if not self.verify_node(child_node_A): + continue_A = True + + if not self.verify_node(child_node_B): + continue_B = True - if n_id in closed_set_B: - continue + if n_id_A in closed_set_A: + continue_A = True + + if n_id_B in closed_set_B: + continue_B = True - if n_id not in open_set_B: - open_set_B[n_id] = node # discovered a new node - else: - if open_set_B[n_id].cost > node.cost: - # This path is the best until now. record it - open_set_B[n_id] = node + if not(continue_A): + if n_id_A not in open_set_A: + open_set_A[n_id_A] = child_node_A # discovered a new node + else: + if open_set_A[n_id_A].cost > child_node_A.cost: + # This path is the best until now. record it + open_set_A[n_id_A] = child_node_A + + if not(continue_B): + if n_id_B not in open_set_B: + open_set_B[n_id_B] = child_node_B # discovered a new node + else: + if open_set_B[n_id_B].cost > child_node_B.cost: + # This path is the best until now. record it + open_set_B[n_id_B] = child_node_B rx, ry = self.calc_final_path_bidir(meetpointA, meetpointB, closed_set_A, closed_set_B) @@ -318,7 +322,7 @@ def main(): plt.grid(True) plt.axis("equal") - bidir_a_star = BidirAStarPlanner(ox, oy, grid_size, robot_radius) + bidir_a_star = BidirectionalAStarPlanner(ox, oy, grid_size, robot_radius) rx, ry = bidir_a_star.planning(sx, sy, gx, gy) if show_animation: # pragma: no cover From 0400ce3ad962f9f3ec7fa27455ba106abd580bec Mon Sep 17 00:00:00 2001 From: Erwin Lejeune Date: Thu, 16 Apr 2020 16:11:18 +0200 Subject: [PATCH 3/7] Replace meaningless variable name --- PathPlanning/BidirectionalAStar/bidirectional_a_star.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py index 37d14eab..4ffd3471 100644 --- a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py +++ b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py @@ -256,8 +256,8 @@ class BidirectionalAStarPlanner: print("ywidth:", self.ywidth) # obstacle map generation - self.obmap = [[False for i in range(self.ywidth)] - for i in range(self.xwidth)] + self.obmap = [[False for _ in range(self.ywidth)] + for _ in range(self.xwidth)] for ix in range(self.xwidth): x = self.calc_grid_position(ix, self.minx) for iy in range(self.ywidth): From b827b3e2eef46a393cacd1fa31f2cc1acafb84fe Mon Sep 17 00:00:00 2001 From: Erwin Lejeune Date: Thu, 16 Apr 2020 18:38:09 +0200 Subject: [PATCH 4/7] add plt pause in astar path display --- PathPlanning/AStar/a_star.py | 1 + 1 file changed, 1 insertion(+) diff --git a/PathPlanning/AStar/a_star.py b/PathPlanning/AStar/a_star.py index 55881f4e..cd5a365c 100644 --- a/PathPlanning/AStar/a_star.py +++ b/PathPlanning/AStar/a_star.py @@ -271,6 +271,7 @@ def main(): if show_animation: # pragma: no cover plt.plot(rx, ry, "-r") plt.show() + plt.pause(0.001) if __name__ == '__main__': From b7f18b8416603237793effb70ed519d61c38b557 Mon Sep 17 00:00:00 2001 From: Erwin Lejeune Date: Sun, 19 Apr 2020 15:02:08 +0200 Subject: [PATCH 5/7] Improve readability to fetch lowest cost node --- .../bidirectional_a_star.py | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py index 4ffd3471..1924cf59 100644 --- a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py +++ b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py @@ -75,24 +75,20 @@ class BidirectionalAStarPlanner: if len(open_set_A) == 0: print("Open set A is empty..") break - + if len(open_set_B) == 0: print("Open set B is empty..") break c_id_A = min( open_set_A, - key=lambda o: open_set_A[o].cost + self.calc_heuristic(current_B, - open_set_A[ - o])) - + key=lambda o: self.find_lowest_cost(open_set_A, o, current_B)) + current_A = open_set_A[c_id_A] c_id_B = min( open_set_B, - key=lambda o: open_set_B[o].cost + self.calc_heuristic(current_A, - open_set_B[ - o])) + key=lambda o: self.find_lowest_cost(open_set_B, o, current_A)) current_B = open_set_B[c_id_B] @@ -129,12 +125,12 @@ class BidirectionalAStarPlanner: continue_B = False child_node_A = self.Node(current_A.x + self.motion[i][0], - current_A.y + self.motion[i][1], - current_A.cost + self.motion[i][2], c_id_A) + current_A.y + self.motion[i][1], + current_A.cost + self.motion[i][2], c_id_A) child_node_B = self.Node(current_B.x + self.motion[i][0], - current_B.y + self.motion[i][1], - current_B.cost + self.motion[i][2], c_id_B) + current_B.y + self.motion[i][1], + current_B.cost + self.motion[i][2], c_id_B) n_id_A = self.calc_grid_index(child_node_A) n_id_B = self.calc_grid_index(child_node_B) @@ -142,33 +138,36 @@ class BidirectionalAStarPlanner: # If the node is not safe, do nothing if not self.verify_node(child_node_A): continue_A = True - + if not self.verify_node(child_node_B): continue_B = True if n_id_A in closed_set_A: continue_A = True - + if n_id_B in closed_set_B: continue_B = True if not(continue_A): if n_id_A not in open_set_A: - open_set_A[n_id_A] = child_node_A # discovered a new node + # discovered a new node + open_set_A[n_id_A] = child_node_A else: if open_set_A[n_id_A].cost > child_node_A.cost: # This path is the best until now. record it open_set_A[n_id_A] = child_node_A - + if not(continue_B): if n_id_B not in open_set_B: - open_set_B[n_id_B] = child_node_B # discovered a new node + # discovered a new node + open_set_B[n_id_B] = child_node_B else: if open_set_B[n_id_B].cost > child_node_B.cost: # This path is the best until now. record it open_set_B[n_id_B] = child_node_B - rx, ry = self.calc_final_path_bidir(meetpointA, meetpointB, closed_set_A, closed_set_B) + rx, ry = self.calc_final_path_bidir( + meetpointA, meetpointB, closed_set_A, closed_set_B) return rx, ry @@ -203,6 +202,11 @@ class BidirectionalAStarPlanner: d = w * math.hypot(n1.x - n2.x, n1.y - n2.y) return d + def find_lowest_cost(self, open_set, lambda_, n1): + cost = open_set[lambda_].cost + \ + self.calc_heuristic(n1, open_set[lambda_]) + return cost + def calc_grid_position(self, index, minp): """ calc grid position From 63a920223b034296edc3c4eae75ac4c0e80bb120 Mon Sep 17 00:00:00 2001 From: Erwin Lejeune Date: Sun, 19 Apr 2020 15:04:38 +0200 Subject: [PATCH 6/7] Fix pep8 indents --- .../BidirectionalAStar/bidirectional_a_star.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py index 1924cf59..3839e4df 100644 --- a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py +++ b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py @@ -82,13 +82,13 @@ class BidirectionalAStarPlanner: c_id_A = min( open_set_A, - key=lambda o: self.find_lowest_cost(open_set_A, o, current_B)) + key=lambda o: self.find_total_cost(open_set_A, o, current_B)) current_A = open_set_A[c_id_A] c_id_B = min( open_set_B, - key=lambda o: self.find_lowest_cost(open_set_B, o, current_A)) + key=lambda o: self.find_total_cost(open_set_B, o, current_A)) current_B = open_set_B[c_id_B] @@ -148,7 +148,7 @@ class BidirectionalAStarPlanner: if n_id_B in closed_set_B: continue_B = True - if not(continue_A): + if not continue_A: if n_id_A not in open_set_A: # discovered a new node open_set_A[n_id_A] = child_node_A @@ -157,7 +157,7 @@ class BidirectionalAStarPlanner: # This path is the best until now. record it open_set_A[n_id_A] = child_node_A - if not(continue_B): + if not continue_B: if n_id_B not in open_set_B: # discovered a new node open_set_B[n_id_B] = child_node_B @@ -202,10 +202,11 @@ class BidirectionalAStarPlanner: d = w * math.hypot(n1.x - n2.x, n1.y - n2.y) return d - def find_lowest_cost(self, open_set, lambda_, n1): - cost = open_set[lambda_].cost + \ - self.calc_heuristic(n1, open_set[lambda_]) - return cost + def find_total_cost(self, open_set, lambda_, n1): + g_cost = open_set[lambda_].cost + h_cost = self.calc_heuristic(n1, open_set[lambda_]) + f_cost = g_cost + h_cost + return f_cost def calc_grid_position(self, index, minp): """ From 38519b5df83141f6fc6bf452593fa748de2eae0f Mon Sep 17 00:00:00 2001 From: Erwin Lejeune Date: Fri, 24 Apr 2020 15:16:05 +0200 Subject: [PATCH 7/7] fix requests --- .../BidirectionalAStar/bidirectional_a_star.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py index 3839e4df..ed1e5869 100644 --- a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py +++ b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py @@ -126,11 +126,13 @@ class BidirectionalAStarPlanner: child_node_A = self.Node(current_A.x + self.motion[i][0], current_A.y + self.motion[i][1], - current_A.cost + self.motion[i][2], c_id_A) + current_A.cost + self.motion[i][2], + c_id_A) child_node_B = self.Node(current_B.x + self.motion[i][0], current_B.y + self.motion[i][1], - current_B.cost + self.motion[i][2], c_id_B) + current_B.cost + self.motion[i][2], + c_id_B) n_id_A = self.calc_grid_index(child_node_A) n_id_B = self.calc_grid_index(child_node_B) @@ -166,12 +168,12 @@ class BidirectionalAStarPlanner: # This path is the best until now. record it open_set_B[n_id_B] = child_node_B - rx, ry = self.calc_final_path_bidir( + rx, ry = self.calc_final_bidirectional_path( meetpointA, meetpointB, closed_set_A, closed_set_B) return rx, ry - def calc_final_path_bidir(self, meetnode_A, meetnode_B, closed_set_A, closed_set_B): + def calc_final_bidirectional_path(self, meetnode_A, meetnode_B, closed_set_A, closed_set_B): rx_A, ry_A = self.calc_final_path(meetnode_A, closed_set_A) rx_B, ry_B = self.calc_final_path(meetnode_B, closed_set_B) @@ -299,7 +301,7 @@ def main(): grid_size = 2.0 # [m] robot_radius = 1.0 # [m] - # set obstable positions + # set obstacle positions ox, oy = [], [] for i in range(-10, 60): ox.append(i)