Skip to content

Commit 734f3ae

Browse files
authored
Greedy Best-First Search (AtsushiSakai#315)
1 parent ab49acb commit 734f3ae

File tree

5 files changed

+326
-46
lines changed

5 files changed

+326
-46
lines changed

PathPlanning/AStar/a_star.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def main():
237237
grid_size = 2.0 # [m]
238238
robot_radius = 1.0 # [m]
239239

240-
# set obstable positions
240+
# set obstacle positions
241241
ox, oy = [], []
242242
for i in range(-10, 60):
243243
ox.append(i)

PathPlanning/BidirectionalAStar/bidirectional_a_star.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ def planning(self, sx, sy, gx, gy):
100100
self.calc_grid_position(current_B.y, self.miny), "xc")
101101
# for stopping simulation with the esc key.
102102
plt.gcf().canvas.mpl_connect('key_release_event',
103-
lambda event: [exit(
104-
0) if event.key == 'escape' else None])
103+
lambda event:
104+
[exit(0) if event.key == 'escape'
105+
else None])
105106
if len(closed_set_A.keys()) % 10 == 0:
106107
plt.pause(0.001)
107108

@@ -121,61 +122,50 @@ def planning(self, sx, sy, gx, gy):
121122

122123
# expand_grid search grid based on motion model
123124
for i, _ in enumerate(self.motion):
124-
continue_A = False
125-
continue_B = False
126125

127-
child_node_A = self.Node(current_A.x + self.motion[i][0],
128-
current_A.y + self.motion[i][1],
129-
current_A.cost + self.motion[i][2],
130-
c_id_A)
126+
c_nodes = [self.Node(current_A.x + self.motion[i][0],
127+
current_A.y + self.motion[i][1],
128+
current_A.cost + self.motion[i][2],
129+
c_id_A),
130+
self.Node(current_B.x + self.motion[i][0],
131+
current_B.y + self.motion[i][1],
132+
current_B.cost + self.motion[i][2],
133+
c_id_B)]
131134

132-
child_node_B = self.Node(current_B.x + self.motion[i][0],
133-
current_B.y + self.motion[i][1],
134-
current_B.cost + self.motion[i][2],
135-
c_id_B)
136-
137-
n_id_A = self.calc_grid_index(child_node_A)
138-
n_id_B = self.calc_grid_index(child_node_B)
135+
n_ids = [self.calc_grid_index(c_nodes[0]),
136+
self.calc_grid_index(c_nodes[1])]
139137

140138
# If the node is not safe, do nothing
141-
if not self.verify_node(child_node_A):
142-
continue_A = True
143-
144-
if not self.verify_node(child_node_B):
145-
continue_B = True
146-
147-
if n_id_A in closed_set_A:
148-
continue_A = True
149-
150-
if n_id_B in closed_set_B:
151-
continue_B = True
139+
continue_ = self.check_nodes_and_sets(c_nodes, closed_set_A,
140+
closed_set_B, n_ids)
152141

153-
if not continue_A:
154-
if n_id_A not in open_set_A:
142+
if not continue_[0]:
143+
if n_ids[0] not in open_set_A:
155144
# discovered a new node
156-
open_set_A[n_id_A] = child_node_A
145+
open_set_A[n_ids[0]] = c_nodes[0]
157146
else:
158-
if open_set_A[n_id_A].cost > child_node_A.cost:
147+
if open_set_A[n_ids[0]].cost > c_nodes[0].cost:
159148
# This path is the best until now. record it
160-
open_set_A[n_id_A] = child_node_A
149+
open_set_A[n_ids[0]] = c_nodes[0]
161150

162-
if not continue_B:
163-
if n_id_B not in open_set_B:
151+
if not continue_[1]:
152+
if n_ids[1] not in open_set_B:
164153
# discovered a new node
165-
open_set_B[n_id_B] = child_node_B
154+
open_set_B[n_ids[1]] = c_nodes[1]
166155
else:
167-
if open_set_B[n_id_B].cost > child_node_B.cost:
156+
if open_set_B[n_ids[1]].cost > c_nodes[1].cost:
168157
# This path is the best until now. record it
169-
open_set_B[n_id_B] = child_node_B
158+
open_set_B[n_ids[1]] = c_nodes[1]
170159

171160
rx, ry = self.calc_final_bidirectional_path(
172161
meetpointA, meetpointB, closed_set_A, closed_set_B)
173162

174163
return rx, ry
175164

176-
def calc_final_bidirectional_path(self, meetnode_A, meetnode_B, closed_set_A, closed_set_B):
177-
rx_A, ry_A = self.calc_final_path(meetnode_A, closed_set_A)
178-
rx_B, ry_B = self.calc_final_path(meetnode_B, closed_set_B)
165+
# takes two sets and two meeting nodes and return the optimal path
166+
def calc_final_bidirectional_path(self, n1, n2, setA, setB):
167+
rx_A, ry_A = self.calc_final_path(n1, setA)
168+
rx_B, ry_B = self.calc_final_path(n2, setB)
179169

180170
rx_A.reverse()
181171
ry_A.reverse()
@@ -198,6 +188,16 @@ def calc_final_path(self, ngoal, closedset):
198188

199189
return rx, ry
200190

191+
def check_nodes_and_sets(self, c_nodes, closedSet_A, closedSet_B, n_ids):
192+
continue_ = [False, False]
193+
if not self.verify_node(c_nodes[0]) or n_ids[0] in closedSet_A:
194+
continue_[0] = True
195+
196+
if not self.verify_node(c_nodes[1]) or n_ids[1] in closedSet_B:
197+
continue_[1] = True
198+
199+
return continue_
200+
201201
@staticmethod
202202
def calc_heuristic(n1, n2):
203203
w = 1.0 # weight of heuristic

PathPlanning/BreadthFirstSearch/breadth_first_search.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ def planning(self, sx, sy, gx, gy):
8484
self.calc_grid_position(current.y, self.miny), "xc")
8585
# for stopping simulation with the esc key.
8686
plt.gcf().canvas.mpl_connect('key_release_event',
87-
lambda event: [exit(
88-
0) if event.key == 'escape' else None])
87+
lambda event:
88+
[exit(0) if event.key == 'escape'
89+
else None])
8990
if len(closed_set.keys()) % 10 == 0:
9091
plt.pause(0.001)
9192

@@ -216,7 +217,7 @@ def main():
216217
grid_size = 2.0 # [m]
217218
robot_radius = 1.0 # [m]
218219

219-
# set obstable positions
220+
# set obstacle positions
220221
ox, oy = [], []
221222
for i in range(-10, 60):
222223
ox.append(i)

PathPlanning/DepthFirstSearch/depth_first_search.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ def planning(self, sx, sy, gx, gy):
8181
self.calc_grid_position(current.y, self.miny), "xc")
8282
# for stopping simulation with the esc key.
8383
plt.gcf().canvas.mpl_connect('key_release_event',
84-
lambda event: [exit(
85-
0) if event.key == 'escape' else None])
84+
lambda event:
85+
[exit(0) if event.key == 'escape'
86+
else None])
8687
plt.pause(0.01)
8788

8889
if current.x == ngoal.x and current.y == ngoal.y:
@@ -213,7 +214,7 @@ def main():
213214
grid_size = 2.0 # [m]
214215
robot_radius = 1.0 # [m]
215216

216-
# set obstable positions
217+
# set obstacle positions
217218
ox, oy = [], []
218219
for i in range(-10, 60):
219220
ox.append(i)

0 commit comments

Comments
 (0)