-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.py
86 lines (75 loc) · 2.79 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
start = ""
end = ""
colors = {"Targets": (255, 125, 125),
"OpenSet": (109, 220, 160),
"ClosedSet": (224, 146, 49),
"Path": (109, 166, 160)}
def GetGHFrom(curr, neighbour):
Gcost = 0
if curr.xpos == neighbour.xpos or curr.ypos == neighbour.ypos:
Gcost = curr.gCost + 1
else:
Gcost = curr.gCost + 1.4
HCost = 0
if end.xpos == neighbour.xpos:
HCost = 1 * abs(end.xpos - neighbour.xpos)
elif end.ypos == neighbour.ypos:
HCost = 1 * abs(end.ypos - neighbour.ypos)
else:
minA = min(abs(end.xpos - neighbour.xpos),
abs(end.ypos - neighbour.ypos))
maxA = max(abs(end.xpos - neighbour.xpos),
abs(end.ypos - neighbour.ypos))
HCost = 1.4 * minA
HCost += 1 * maxA - minA
return Gcost, HCost
def SetColors(_name, _value):
if _name in colors:
colors[_name] = _value
else:
print('Only use "Targets" , "OpenSet", "ClosedSet" , "Path"')
def Solve(_start, _end, openSet, closedSet, path, steps=False):
global start, end
start = _start
end = _end
while openSet.count() > 0:
current = openSet.pop()
if current == end:
# print("Donee")
prev = current
while current.parent != current:
path.append(current)
current.Show(colors["Path"], 0)
prev.DrawTo(current)
prev = current
current = current.parent
start.Show((255, 0, 0), 0)
end.Show((255, 0, 0), 0)
# start.Show((0, 0, 0), 3)
# end.Show((0, 0, 0), 3)
return False
else:
closedSet.append(current)
neighbours = current.neighbours
for neighbour in neighbours:
if neighbour not in closedSet:
newG, newH = GetGHFrom(current, neighbour)
if neighbour not in openSet.items or neighbour.gCost > newG:
neighbour.gCost = newG
neighbour.hCost = newH
neighbour.parent = current
neighbour.fCost = neighbour.gCost + neighbour.hCost
if neighbour not in openSet.items:
openSet.push(neighbour)
else:
openSet.updateItem(neighbour)
if steps:
neighbour.Show(colors["OpenSet"], 0)
if start != current and steps:
current.Show(colors["ClosedSet"], 0)
if steps:
break
if openSet.count() == 0:
print("No Solution Found")
return False
return True