11"""
2+
23A* grid based planning
34
45author: 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+
511"""
612
713import matplotlib .pyplot as plt
@@ -59,9 +65,8 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
5965
6066 while 1 :
6167 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 ]))
6369 current = openset [c_id ]
64- # print("current", current)
6570
6671 # show graph
6772 if show_animation :
@@ -82,7 +87,8 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
8287
8388 # expand search grid based on motion model
8489 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 ],
8692 current .cost + motion [i ][2 ], c_id )
8793 n_id = calc_index (node , xw , minx , miny )
8894
@@ -91,22 +97,27 @@ def a_star_planning(sx, sy, gx, gy, ox, oy, reso, rr):
9197
9298 if n_id in closedset :
9399 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 :
100102 openset [n_id ] = node
101103
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+
102113 rx , ry = calc_fianl_path (ngoal , closedset , reso )
103114
104115 return rx , ry
105116
106117
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 )
110121 return d
111122
112123
0 commit comments