1
1
"""
2
+
2
3
A* grid based planning
3
4
4
5
author: Atsushi Sakai(@Atsushi_twi)
6
+ Nikos Kanargias ([email protected] )
7
+
8
+ See Wikipedia article (https://en.wikipedia.org/wiki/A*_search_algorithm)
9
+
5
10
"""
6
11
7
12
import matplotlib .pyplot as plt
@@ -22,7 +27,7 @@ def __str__(self):
22
27
return str (self .x ) + "," + str (self .y ) + "," + str (self .cost ) + "," + str (self .pind )
23
28
24
29
25
- def calc_fianl_path (ngoal , closedset , reso ):
30
+ def calc_final_path (ngoal , closedset , reso ):
26
31
# generate final course
27
32
rx , ry = [ngoal .x * reso ], [ngoal .y * reso ]
28
33
pind = ngoal .pind
@@ -59,9 +64,8 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
59
64
60
65
while 1 :
61
66
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 ]))
63
68
current = openset [c_id ]
64
- # print("current", current)
65
69
66
70
# show graph
67
71
if show_animation :
@@ -82,31 +86,36 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
82
86
83
87
# expand search grid based on motion model
84
88
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 ],
86
91
current .cost + motion [i ][2 ], c_id )
87
92
n_id = calc_index (node , xw , minx , miny )
88
93
89
- if not verify_node ( node , obmap , minx , miny , maxx , maxy ) :
94
+ if n_id in closedset :
90
95
continue
91
96
92
- if n_id in closedset :
97
+ if not verify_node ( node , obmap , minx , miny , maxx , maxy ) :
93
98
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
101
99
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 )
103
112
104
113
return rx , ry
105
114
106
115
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 )
110
119
return d
111
120
112
121
0 commit comments