-
Notifications
You must be signed in to change notification settings - Fork 0
/
aStart.py
89 lines (76 loc) · 2.62 KB
/
aStart.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
87
88
89
class Node(object):
"""docstring for Node"""
def __init__(self, parent=None, pos=None, goal=None, biggerN=1):
super(Node, self).__init__()
self.goal = goal
self.parent = parent
self.position = pos
self.n = biggerN
self.f = 0
self.child = []
if self.parent:
self.n = parent.n + 1
self.f = self.n + self.distance(self, self.goal)
def __eq__(self, other):
return self.position == other.position
@staticmethod
def distance(self, goal):
return ((self.position[0]-goal[0])**2)+((self.position[1]-goal[1])**2)
def aStar(grid, start, end):
openedList = []
closedList = []
visited = []
newPos = None
path = []
if start == end:
print("start = end!")
return [start.position]
openedList.append(start)
visited.append(start.position)
currentNode = openedList[0]
openedList.remove(currentNode)
while True:
if currentNode == end:
break
for x, y in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
newPos = [currentNode.position[0] + x, currentNode.position[1] + y]
if newPos[0] > (len(grid) - 1) or newPos[0] < 0 or newPos[1] > (len(grid[len(grid)-1]) - 1) or newPos[1] < 0:
continue
if grid[newPos[0]][newPos[1]] != 0:
continue
if newPos not in visited:
if abs(x) == abs(y):
openedList.append(Node(currentNode, newPos, currentNode.goal, 1.4))
else:
openedList.append(Node(currentNode, newPos, currentNode.goal))
visited.append(newPos)
openedList.sort(key=lambda x: x.f)
if len(openedList) >= 1:
currentNode = openedList.pop(0)
closedList.append(currentNode)
else:
return -1
try:
while True:
path.append(currentNode.position)
currentNode = currentNode.parent
except Exception:
return path[::-1]
def main():
grid = [
[3, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
end = Node(None, [5, 5], [5, 5])
start = Node(None, [0, 0], end.position)
print(aStar(grid, start, end))
if __name__ == '__main__':
main()