Skip to content

Commit b7f18b8

Browse files
committed
Improve readability to fetch lowest cost node
1 parent b827b3e commit b7f18b8

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

PathPlanning/BidirectionalAStar/bidirectional_a_star.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,20 @@ def planning(self, sx, sy, gx, gy):
7575
if len(open_set_A) == 0:
7676
print("Open set A is empty..")
7777
break
78-
78+
7979
if len(open_set_B) == 0:
8080
print("Open set B is empty..")
8181
break
8282

8383
c_id_A = min(
8484
open_set_A,
85-
key=lambda o: open_set_A[o].cost + self.calc_heuristic(current_B,
86-
open_set_A[
87-
o]))
88-
85+
key=lambda o: self.find_lowest_cost(open_set_A, o, current_B))
86+
8987
current_A = open_set_A[c_id_A]
9088

9189
c_id_B = min(
9290
open_set_B,
93-
key=lambda o: open_set_B[o].cost + self.calc_heuristic(current_A,
94-
open_set_B[
95-
o]))
91+
key=lambda o: self.find_lowest_cost(open_set_B, o, current_A))
9692

9793
current_B = open_set_B[c_id_B]
9894

@@ -129,46 +125,49 @@ def planning(self, sx, sy, gx, gy):
129125
continue_B = False
130126

131127
child_node_A = self.Node(current_A.x + self.motion[i][0],
132-
current_A.y + self.motion[i][1],
133-
current_A.cost + self.motion[i][2], c_id_A)
128+
current_A.y + self.motion[i][1],
129+
current_A.cost + self.motion[i][2], c_id_A)
134130

135131
child_node_B = self.Node(current_B.x + self.motion[i][0],
136-
current_B.y + self.motion[i][1],
137-
current_B.cost + self.motion[i][2], c_id_B)
132+
current_B.y + self.motion[i][1],
133+
current_B.cost + self.motion[i][2], c_id_B)
138134

139135
n_id_A = self.calc_grid_index(child_node_A)
140136
n_id_B = self.calc_grid_index(child_node_B)
141137

142138
# If the node is not safe, do nothing
143139
if not self.verify_node(child_node_A):
144140
continue_A = True
145-
141+
146142
if not self.verify_node(child_node_B):
147143
continue_B = True
148144

149145
if n_id_A in closed_set_A:
150146
continue_A = True
151-
147+
152148
if n_id_B in closed_set_B:
153149
continue_B = True
154150

155151
if not(continue_A):
156152
if n_id_A not in open_set_A:
157-
open_set_A[n_id_A] = child_node_A # discovered a new node
153+
# discovered a new node
154+
open_set_A[n_id_A] = child_node_A
158155
else:
159156
if open_set_A[n_id_A].cost > child_node_A.cost:
160157
# This path is the best until now. record it
161158
open_set_A[n_id_A] = child_node_A
162-
159+
163160
if not(continue_B):
164161
if n_id_B not in open_set_B:
165-
open_set_B[n_id_B] = child_node_B # discovered a new node
162+
# discovered a new node
163+
open_set_B[n_id_B] = child_node_B
166164
else:
167165
if open_set_B[n_id_B].cost > child_node_B.cost:
168166
# This path is the best until now. record it
169167
open_set_B[n_id_B] = child_node_B
170168

171-
rx, ry = self.calc_final_path_bidir(meetpointA, meetpointB, closed_set_A, closed_set_B)
169+
rx, ry = self.calc_final_path_bidir(
170+
meetpointA, meetpointB, closed_set_A, closed_set_B)
172171

173172
return rx, ry
174173

@@ -203,6 +202,11 @@ def calc_heuristic(n1, n2):
203202
d = w * math.hypot(n1.x - n2.x, n1.y - n2.y)
204203
return d
205204

205+
def find_lowest_cost(self, open_set, lambda_, n1):
206+
cost = open_set[lambda_].cost + \
207+
self.calc_heuristic(n1, open_set[lambda_])
208+
return cost
209+
206210
def calc_grid_position(self, index, minp):
207211
"""
208212
calc grid position

0 commit comments

Comments
 (0)