-
Notifications
You must be signed in to change notification settings - Fork 0
/
astar.py
57 lines (46 loc) · 1.65 KB
/
astar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
def planning(graph, s, g, get_path=True):
open, closed = set(), set()
s.cost = 0
open.add(s)
while True:
if len(open) == 0:
return np.inf
current = min(open, key=lambda o: o.cost + graph.get_heuristic(o, g))
if current.x == g.x and current.y == g.y:
#print("find goal")
g.parent = current.parent
g.cost = current.cost
#print("final cost: ", g.cost)
if get_path:
path = [(g.x, g.y)]
parent_ = g.parent
while parent_ is not s:
path.append((parent_.x, parent_.y))
parent_ = parent_.parent
path.append((s.x, s.y))
path.reverse()
return g.cost, path
else:
return g.cost
# remove the item from the open set
open.remove(current)
# add it to the closed set
closed.add(current)
for frm in graph.edges[current.id]:
v_frm = graph.nodes[frm]
cost_frm = current.cost + graph.edges[current.id][v_frm.id]["distance"]
# if the node is not safe, do nothing
if v_frm.collision:
continue
if v_frm in closed:
continue
if v_frm not in open:
open.add(v_frm)
v_frm.cost = cost_frm
v_frm.parent = current
else:
if v_frm.cost > cost_frm:
# this path is the best until now. record it
v_frm.cost = cost_frm
v_frm.parent = current