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
+ See also code of Christian Careaga (http://code.activestate.com/recipes/578919-python-a-pathfinding-with-binary-heap/)
10
+
5
11
"""
6
12
7
13
import matplotlib .pyplot as plt
@@ -59,9 +65,8 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
59
65
60
66
while 1 :
61
67
c_id = min (
62
- openset , key = lambda o : openset [o ].cost + calc_h (ngoal , openset [o ]. x , openset [ o ]. y ))
68
+ openset , key = lambda o : openset [o ].cost + calc_heuristic (ngoal , openset [o ]))
63
69
current = openset [c_id ]
64
- # print("current", current)
65
70
66
71
# show graph
67
72
if show_animation :
@@ -82,7 +87,8 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
82
87
83
88
# expand search grid based on motion model
84
89
for i in range (len (motion )):
85
- node = Node (current .x + motion [i ][0 ], current .y + motion [i ][1 ],
90
+ node = Node (current .x + motion [i ][0 ],
91
+ current .y + motion [i ][1 ],
86
92
current .cost + motion [i ][2 ], c_id )
87
93
n_id = calc_index (node , xw , minx , miny )
88
94
@@ -91,22 +97,27 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
91
97
92
98
if n_id in closedset :
93
99
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
+
101
+ if n_id not in openset :
100
102
openset [n_id ] = node
101
103
104
+ # The distance from start to a neighbor.
105
+ # The "dist_between" function may vary as per the solution requirements.
106
+ if node .cost >= openset [n_id ].cost :
107
+ continue # This is not a better path.
108
+
109
+ # This path is the best until now. Record it!
110
+ openset [n_id ].cost = node .cost
111
+ openset [n_id ].pind = c_id
112
+
102
113
rx , ry = calc_fianl_path (ngoal , closedset , reso )
103
114
104
115
return rx , ry
105
116
106
117
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 )
118
+ def calc_heuristic ( n1 , n2 ):
119
+ w = 1 .0 # weight of heuristic
120
+ d = w * math .sqrt ((n1 .x - n2 . x )** 2 + (n1 .y - n2 . y )** 2 )
110
121
return d
111
122
112
123
0 commit comments