mirror of
https://github.com/AtsushiSakai/PythonRobotics.git
synced 2026-02-12 13:25:22 -05:00
Greedy Best-First Search (#315)
This commit is contained in:
@@ -237,7 +237,7 @@ def main():
|
|||||||
grid_size = 2.0 # [m]
|
grid_size = 2.0 # [m]
|
||||||
robot_radius = 1.0 # [m]
|
robot_radius = 1.0 # [m]
|
||||||
|
|
||||||
# set obstable positions
|
# set obstacle positions
|
||||||
ox, oy = [], []
|
ox, oy = [], []
|
||||||
for i in range(-10, 60):
|
for i in range(-10, 60):
|
||||||
ox.append(i)
|
ox.append(i)
|
||||||
|
|||||||
@@ -100,8 +100,9 @@ class BidirectionalAStarPlanner:
|
|||||||
self.calc_grid_position(current_B.y, self.miny), "xc")
|
self.calc_grid_position(current_B.y, self.miny), "xc")
|
||||||
# for stopping simulation with the esc key.
|
# for stopping simulation with the esc key.
|
||||||
plt.gcf().canvas.mpl_connect('key_release_event',
|
plt.gcf().canvas.mpl_connect('key_release_event',
|
||||||
lambda event: [exit(
|
lambda event:
|
||||||
0) if event.key == 'escape' else None])
|
[exit(0) if event.key == 'escape'
|
||||||
|
else None])
|
||||||
if len(closed_set_A.keys()) % 10 == 0:
|
if len(closed_set_A.keys()) % 10 == 0:
|
||||||
plt.pause(0.001)
|
plt.pause(0.001)
|
||||||
|
|
||||||
@@ -121,61 +122,50 @@ class BidirectionalAStarPlanner:
|
|||||||
|
|
||||||
# expand_grid search grid based on motion model
|
# expand_grid search grid based on motion model
|
||||||
for i, _ in enumerate(self.motion):
|
for i, _ in enumerate(self.motion):
|
||||||
continue_A = False
|
|
||||||
continue_B = False
|
|
||||||
|
|
||||||
child_node_A = self.Node(current_A.x + self.motion[i][0],
|
c_nodes = [self.Node(current_A.x + self.motion[i][0],
|
||||||
current_A.y + self.motion[i][1],
|
current_A.y + self.motion[i][1],
|
||||||
current_A.cost + self.motion[i][2],
|
current_A.cost + self.motion[i][2],
|
||||||
c_id_A)
|
c_id_A),
|
||||||
|
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)]
|
||||||
|
|
||||||
child_node_B = self.Node(current_B.x + self.motion[i][0],
|
n_ids = [self.calc_grid_index(c_nodes[0]),
|
||||||
current_B.y + self.motion[i][1],
|
self.calc_grid_index(c_nodes[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)
|
|
||||||
|
|
||||||
# If the node is not safe, do nothing
|
# If the node is not safe, do nothing
|
||||||
if not self.verify_node(child_node_A):
|
continue_ = self.check_nodes_and_sets(c_nodes, closed_set_A,
|
||||||
continue_A = True
|
closed_set_B, n_ids)
|
||||||
|
|
||||||
if not self.verify_node(child_node_B):
|
if not continue_[0]:
|
||||||
continue_B = True
|
if n_ids[0] not in open_set_A:
|
||||||
|
|
||||||
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:
|
|
||||||
# discovered a new node
|
# discovered a new node
|
||||||
open_set_A[n_id_A] = child_node_A
|
open_set_A[n_ids[0]] = c_nodes[0]
|
||||||
else:
|
else:
|
||||||
if open_set_A[n_id_A].cost > child_node_A.cost:
|
if open_set_A[n_ids[0]].cost > c_nodes[0].cost:
|
||||||
# This path is the best until now. record it
|
# This path is the best until now. record it
|
||||||
open_set_A[n_id_A] = child_node_A
|
open_set_A[n_ids[0]] = c_nodes[0]
|
||||||
|
|
||||||
if not continue_B:
|
if not continue_[1]:
|
||||||
if n_id_B not in open_set_B:
|
if n_ids[1] not in open_set_B:
|
||||||
# discovered a new node
|
# discovered a new node
|
||||||
open_set_B[n_id_B] = child_node_B
|
open_set_B[n_ids[1]] = c_nodes[1]
|
||||||
else:
|
else:
|
||||||
if open_set_B[n_id_B].cost > child_node_B.cost:
|
if open_set_B[n_ids[1]].cost > c_nodes[1].cost:
|
||||||
# This path is the best until now. record it
|
# This path is the best until now. record it
|
||||||
open_set_B[n_id_B] = child_node_B
|
open_set_B[n_ids[1]] = c_nodes[1]
|
||||||
|
|
||||||
rx, ry = self.calc_final_bidirectional_path(
|
rx, ry = self.calc_final_bidirectional_path(
|
||||||
meetpointA, meetpointB, closed_set_A, closed_set_B)
|
meetpointA, meetpointB, closed_set_A, closed_set_B)
|
||||||
|
|
||||||
return rx, ry
|
return rx, ry
|
||||||
|
|
||||||
def calc_final_bidirectional_path(self, meetnode_A, meetnode_B, closed_set_A, closed_set_B):
|
# takes two sets and two meeting nodes and return the optimal path
|
||||||
rx_A, ry_A = self.calc_final_path(meetnode_A, closed_set_A)
|
def calc_final_bidirectional_path(self, n1, n2, setA, setB):
|
||||||
rx_B, ry_B = self.calc_final_path(meetnode_B, closed_set_B)
|
rx_A, ry_A = self.calc_final_path(n1, setA)
|
||||||
|
rx_B, ry_B = self.calc_final_path(n2, setB)
|
||||||
|
|
||||||
rx_A.reverse()
|
rx_A.reverse()
|
||||||
ry_A.reverse()
|
ry_A.reverse()
|
||||||
@@ -198,6 +188,16 @@ class BidirectionalAStarPlanner:
|
|||||||
|
|
||||||
return rx, ry
|
return rx, ry
|
||||||
|
|
||||||
|
def check_nodes_and_sets(self, c_nodes, closedSet_A, closedSet_B, n_ids):
|
||||||
|
continue_ = [False, False]
|
||||||
|
if not self.verify_node(c_nodes[0]) or n_ids[0] in closedSet_A:
|
||||||
|
continue_[0] = True
|
||||||
|
|
||||||
|
if not self.verify_node(c_nodes[1]) or n_ids[1] in closedSet_B:
|
||||||
|
continue_[1] = True
|
||||||
|
|
||||||
|
return continue_
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def calc_heuristic(n1, n2):
|
def calc_heuristic(n1, n2):
|
||||||
w = 1.0 # weight of heuristic
|
w = 1.0 # weight of heuristic
|
||||||
|
|||||||
@@ -84,8 +84,9 @@ class BreadthFirstSearchPlanner:
|
|||||||
self.calc_grid_position(current.y, self.miny), "xc")
|
self.calc_grid_position(current.y, self.miny), "xc")
|
||||||
# for stopping simulation with the esc key.
|
# for stopping simulation with the esc key.
|
||||||
plt.gcf().canvas.mpl_connect('key_release_event',
|
plt.gcf().canvas.mpl_connect('key_release_event',
|
||||||
lambda event: [exit(
|
lambda event:
|
||||||
0) if event.key == 'escape' else None])
|
[exit(0) if event.key == 'escape'
|
||||||
|
else None])
|
||||||
if len(closed_set.keys()) % 10 == 0:
|
if len(closed_set.keys()) % 10 == 0:
|
||||||
plt.pause(0.001)
|
plt.pause(0.001)
|
||||||
|
|
||||||
@@ -216,7 +217,7 @@ def main():
|
|||||||
grid_size = 2.0 # [m]
|
grid_size = 2.0 # [m]
|
||||||
robot_radius = 1.0 # [m]
|
robot_radius = 1.0 # [m]
|
||||||
|
|
||||||
# set obstable positions
|
# set obstacle positions
|
||||||
ox, oy = [], []
|
ox, oy = [], []
|
||||||
for i in range(-10, 60):
|
for i in range(-10, 60):
|
||||||
ox.append(i)
|
ox.append(i)
|
||||||
|
|||||||
@@ -81,8 +81,9 @@ class DepthFirstSearchPlanner:
|
|||||||
self.calc_grid_position(current.y, self.miny), "xc")
|
self.calc_grid_position(current.y, self.miny), "xc")
|
||||||
# for stopping simulation with the esc key.
|
# for stopping simulation with the esc key.
|
||||||
plt.gcf().canvas.mpl_connect('key_release_event',
|
plt.gcf().canvas.mpl_connect('key_release_event',
|
||||||
lambda event: [exit(
|
lambda event:
|
||||||
0) if event.key == 'escape' else None])
|
[exit(0) if event.key == 'escape'
|
||||||
|
else None])
|
||||||
plt.pause(0.01)
|
plt.pause(0.01)
|
||||||
|
|
||||||
if current.x == ngoal.x and current.y == ngoal.y:
|
if current.x == ngoal.x and current.y == ngoal.y:
|
||||||
@@ -213,7 +214,7 @@ def main():
|
|||||||
grid_size = 2.0 # [m]
|
grid_size = 2.0 # [m]
|
||||||
robot_radius = 1.0 # [m]
|
robot_radius = 1.0 # [m]
|
||||||
|
|
||||||
# set obstable positions
|
# set obstacle positions
|
||||||
ox, oy = [], []
|
ox, oy = [], []
|
||||||
for i in range(-10, 60):
|
for i in range(-10, 60):
|
||||||
ox.append(i)
|
ox.append(i)
|
||||||
|
|||||||
278
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
Normal file
278
PathPlanning/GreedyBestFirstSearch/greedy_best_first_search.py
Normal file
@@ -0,0 +1,278 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
Greedy Best-First grid planning
|
||||||
|
|
||||||
|
author: Erwin Lejeune (@spida_rwin)
|
||||||
|
|
||||||
|
See Wikipedia article (https://en.wikipedia.org/wiki/Best-first_search)
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
show_animation = True
|
||||||
|
|
||||||
|
|
||||||
|
class BestFirstSearchPlanner:
|
||||||
|
|
||||||
|
def __init__(self, ox, oy, reso, rr):
|
||||||
|
"""
|
||||||
|
Initialize grid map for greedy best-first 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, parent):
|
||||||
|
self.x = x # index of grid
|
||||||
|
self.y = y # index of grid
|
||||||
|
self.cost = cost
|
||||||
|
self.pind = pind
|
||||||
|
self.parent = parent
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.x) + "," + str(self.y) + "," + str(
|
||||||
|
self.cost) + "," + str(self.pind)
|
||||||
|
|
||||||
|
def planning(self, sx, sy, gx, gy):
|
||||||
|
"""
|
||||||
|
Greedy Best-First 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, None)
|
||||||
|
ngoal = self.Node(self.calc_xyindex(gx, self.minx),
|
||||||
|
self.calc_xyindex(gy, self.miny), 0.0, -1, None)
|
||||||
|
|
||||||
|
open_set, closed_set = dict(), dict()
|
||||||
|
open_set[self.calc_grid_index(nstart)] = nstart
|
||||||
|
|
||||||
|
while 1:
|
||||||
|
if len(open_set) == 0:
|
||||||
|
print("Open set is empty..")
|
||||||
|
break
|
||||||
|
|
||||||
|
c_id = min(
|
||||||
|
open_set,
|
||||||
|
key=lambda o: self.calc_heuristic(ngoal, open_set[o]))
|
||||||
|
|
||||||
|
current = open_set[c_id]
|
||||||
|
|
||||||
|
# show graph
|
||||||
|
if show_animation: # pragma: no cover
|
||||||
|
plt.plot(self.calc_grid_position(current.x, self.minx),
|
||||||
|
self.calc_grid_position(current.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.keys()) % 10 == 0:
|
||||||
|
plt.pause(0.001)
|
||||||
|
|
||||||
|
# Remove the item from the open set
|
||||||
|
del open_set[c_id]
|
||||||
|
|
||||||
|
# Add it to the closed set
|
||||||
|
closed_set[c_id] = current
|
||||||
|
|
||||||
|
if current.x == ngoal.x and current.y == ngoal.y:
|
||||||
|
print("Found goal")
|
||||||
|
ngoal.pind = current.pind
|
||||||
|
ngoal.cost = current.cost
|
||||||
|
break
|
||||||
|
|
||||||
|
# expand_grid search grid based on motion model
|
||||||
|
for i, _ in enumerate(self.motion):
|
||||||
|
node = self.Node(current.x + self.motion[i][0],
|
||||||
|
current.y + self.motion[i][1],
|
||||||
|
current.cost + self.motion[i][2],
|
||||||
|
c_id, current)
|
||||||
|
|
||||||
|
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:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if n_id not in open_set:
|
||||||
|
open_set[n_id] = node
|
||||||
|
else:
|
||||||
|
if open_set[n_id].cost > node.cost:
|
||||||
|
open_set[n_id] = node
|
||||||
|
closed_set[ngoal.pind] = current
|
||||||
|
rx, ry = self.calc_final_path(ngoal, closed_set)
|
||||||
|
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)]
|
||||||
|
n = closedset[ngoal.pind]
|
||||||
|
while n is not None:
|
||||||
|
rx.append(self.calc_grid_position(n.x, self.minx))
|
||||||
|
ry.append(self.calc_grid_position(n.y, self.miny))
|
||||||
|
n = n.parent
|
||||||
|
|
||||||
|
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 _ 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):
|
||||||
|
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 obstacle 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, "xb")
|
||||||
|
plt.grid(True)
|
||||||
|
plt.axis("equal")
|
||||||
|
|
||||||
|
greedybestfirst = BestFirstSearchPlanner(ox, oy, grid_size, robot_radius)
|
||||||
|
rx, ry = greedybestfirst.planning(sx, sy, gx, gy)
|
||||||
|
|
||||||
|
if show_animation: # pragma: no cover
|
||||||
|
plt.plot(rx, ry, "-r")
|
||||||
|
plt.pause(0.01)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user