Skip to content

Commit 32b0d78

Browse files
committed
Update the logic; correct minor spelling
1 parent 88f7e25 commit 32b0d78

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

PathPlanning/AStar/a_star.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
"""
2+
23
A* grid based planning
34
45
author: Atsushi Sakai(@Atsushi_twi)
6+
Nikos Kanargias ([email protected])
7+
8+
See Wikipedia article (https://en.wikipedia.org/wiki/A*_search_algorithm)
9+
510
"""
611

712
import matplotlib.pyplot as plt
@@ -22,7 +27,7 @@ def __str__(self):
2227
return str(self.x) + "," + str(self.y) + "," + str(self.cost) + "," + str(self.pind)
2328

2429

25-
def calc_fianl_path(ngoal, closedset, reso):
30+
def calc_final_path(ngoal, closedset, reso):
2631
# generate final course
2732
rx, ry = [ngoal.x * reso], [ngoal.y * reso]
2833
pind = ngoal.pind
@@ -59,9 +64,8 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
5964

6065
while 1:
6166
c_id = min(
62-
openset, key=lambda o: openset[o].cost + calc_h(ngoal, openset[o].x, openset[o].y))
67+
openset, key=lambda o: openset[o].cost + calc_heuristic(ngoal, openset[o]))
6368
current = openset[c_id]
64-
# print("current", current)
6569

6670
# show graph
6771
if show_animation:
@@ -82,31 +86,36 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
8286

8387
# expand search grid based on motion model
8488
for i in range(len(motion)):
85-
node = Node(current.x + motion[i][0], current.y + motion[i][1],
89+
node = Node(current.x + motion[i][0],
90+
current.y + motion[i][1],
8691
current.cost + motion[i][2], c_id)
8792
n_id = calc_index(node, xw, minx, miny)
8893

89-
if not verify_node(node, obmap, minx, miny, maxx, maxy):
94+
if n_id in closedset:
9095
continue
9196

92-
if n_id in closedset:
97+
if not verify_node(node, obmap, minx, miny, maxx, maxy):
9398
continue
94-
# Otherwise if it is already in the open set
95-
if n_id in openset:
96-
if openset[n_id].cost > node.cost:
97-
openset[n_id].cost = node.cost
98-
openset[n_id].pind = c_id
99-
else:
100-
openset[n_id] = node
10199

102-
rx, ry = calc_fianl_path(ngoal, closedset, reso)
100+
if n_id not in openset:
101+
openset[n_id] = node # Discover a new node
102+
103+
tcost = current.cost + calc_heuristic(current, node)
104+
105+
if tcost >= node.cost:
106+
continue # this is not a better path
107+
108+
node.cost = tcost + calc_heuristic(node, ngoal)
109+
openset[n_id] = node # This path is the best until now. record it!
110+
111+
rx, ry = calc_final_path(ngoal, closedset, reso)
103112

104113
return rx, ry
105114

106115

107-
def calc_h(ngoal, x, y):
108-
w = 10.0 # weight of heuristic
109-
d = w * math.sqrt((ngoal.x - x)**2 + (ngoal.y - y)**2)
116+
def calc_heuristic(n1, n2):
117+
w = 1.0 # weight of heuristic
118+
d = w * math.sqrt((n1.x - n2.x)**2 + (n1.y - n2.y)**2)
110119
return d
111120

112121

0 commit comments

Comments
 (0)