-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.py
215 lines (164 loc) · 6.05 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# PSEUDOCODE OF A* Search Algorithm
"""
1. Initialize the open list
2. Initialize the closed list
put the starting node on the open
list (you can leave its f at zero)
3. while the open list is not empty
a) find the node with the least f on
the open list, call it "q"
b) pop q off the open list
c) generate q's 8 successors and set their
parents to q
d) for each successor
i) if successor is the goal, stop search
successor.g = q.g + distance between
successor and q
successor.h = distance from goal to
successor (This can be done using many
ways, we will discuss three heuristics-
Manhattan, Diagonal and Euclidean
Heuristics)
successor.f = successor.g + successor.h
ii) if a node with the same position as
successor is in the OPEN list which has a
lower f than successor, skip this successor
iii) if a node with the same position as
successor is in the CLOSED list which has
a lower f than successor, skip this successor
otherwise, add the node to the open list
end (for loop)
e) push q on the closed list
end (while loop)
"""
import numpy as np
import Board
CHILDREN_MOVE = []
CHILDREN_F = []
END_COORDINATE = None
START_COORDINATE = None
class Node:
"""A node class for A*"""
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0 # From current node to start node
self.h = 0 # Heuristic -> Estimated from current to end(destination)
self.f = 0 # F = G + H
def __eq__(self, other):
return self.position == other.position
def AStar(maze, start, end):
global CHILDREN_MOVE, CHILDREN_F
# Create start and end node
# Setting F = G + H to 0
start_node = Node(None, start)
start_node.g = start_node.h = start_node.f = 0
end_node = Node(None, end)
end_node.g = end_node.h = end_node.f = 0
# Open and closed list
open_list = []
closed_list = []
# Add the start node
open_list.append(start_node)
# Loop until you find the end
while len(open_list) > 0:
# Get the current node
current_node = open_list[0] # Start_node at first loop
current_index = 0
for index, item in enumerate(open_list):
if item.f < current_node.f:
current_node = item
current_index = index
# Pop current off open list, add to closed list
open_list.pop(current_index)
closed_list.append(current_node)
# Check if reached the destination
if current_node == end_node:
path = [] # Will keep traversed path
current = current_node
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1] # Traverse the path back
# Generate children, explained below
children = []
# Smejniye kvadrati
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
# Get node position
node_position = (
current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
# Make sure new square is within the range
if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze) - 1]) - 1) or node_position[1] < 0:
continue
if maze[node_position[0]][node_position[1]] != 0:
continue
if Node(current_node, node_position) in closed_list:
continue
# Create new node
new_node = Node(current_node, node_position)
# Append new node to children list
children.append(new_node)
for child in children:
# Child is on the closed list already
for closed_child in closed_list:
if child == closed_child:
continue
# Create the f, g, and h values
child.g = current_node.g + 1
child.h = ((child.position[0] - end_node.position[0]) **
2) + ((child.position[1] - end_node.position[1]) ** 2)
child.f = child.g + child.h
# DANGER
CHILDREN_MOVE.append(child.position) # try to get expanded children list
CHILDREN_F.append(child.g)
# Child is already in the open list
for open_node in open_list:
if child == open_node and child.g > open_node.g:
continue
# Add the child to the open list
open_list.append(child)
class Green:
TO_GREEN = None
def __init__(self, path):
Green.TO_GREEN = path
def main():
# init a board
obj1 = Board.chess_board(10)
def solve(obj1):
global END_COORDINATE, START_COORDINATE
maze = obj1.returnChessArray()
temp_maze = np.array(maze)
maze = (temp_maze.T).tolist()
# print(maze)
start = obj1.startXY
end = obj1.endXY
END_COORDINATE = end
START_COORDINATE = start
path = AStar(maze, start, end)
#print(path)
#print("child moves ",CHILDREN_MOVE)
return path
if __name__ == '__main__':
main()
"""
Generating all the 8 successor of this cell
N.W N N.E
\ | /
\ | /
W----Cell----E
/ | \
/ | \
S.W S S.E
Cell-->Popped Cell (i, j)
N --> North (i-1, j)
S --> South (i+1, j)
E --> East (i, j+1)
W --> West (i, j-1)
N.E--> North-East (i-1, j+1)
N.W--> North-West (i-1, j-1)
S.E--> South-East (i+1, j+1)
S.W--> South-West (i+1, j-1)*/
// To store the 'g', 'h' and 'f' of the 8 successors
double gNew, hNew, fNew;
//----------- 1st Successor (North) ------------
"""