-
Notifications
You must be signed in to change notification settings - Fork 0
/
Astar.py
288 lines (225 loc) · 7.11 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#
#made by ahmadreza alizadefard
#name : A*
#version : 1.0
#data structure : Priority Queue
#
import randomname
def name_generator():
name = randomname.get_name(noun=('houses'))
return name
id = 0
def get_id():
global id
id += 1
return id
def generate_matrix(row, column):
matrix = []
print("enter a matrix row by row:")
for r in range(row):
row = []
print("\nenter a row:")
for c in range(column):
rc = input("enter a 0/1:")
row.append(rc)
matrix.append(row)
return matrix
# rc(row, column) is equal to exact location(x, y) of house(node) in maze(matrix)
class Node:
def __init__(self, rc_in_matrix, children=None, parent=None, depth=0, Astar_functionality=0):
self.id = get_id()
self.name = name_generator()
self.rc_in_matrix = rc_in_matrix
self.depth = depth
self.parent = parent
self.Astar_functionality = Astar_functionality
self.children = children # children are (r, c)
# all attrs are const => there is no set method
# get functions
def get_idd(self):
return self.id
def get_name(self):
return self.name
def get_rc_in_matrix(self):
return self.rc_in_matrix
def get_parent(self):
return self.parent
def get_children(self):
return self.children
class state_space_graph:
def __init__(self, matrix, root, goal):
self.matrix = matrix
self.root = root
self.goal = goal
self.node_base_matrix = self.node_base_matrix()
self.root_node = self.node_base_matrix[root[0]][root[1]]
self.goal_node = self.node_base_matrix[goal[0]][goal[1]]
self.graph = self.graphdict_generator()
# name functions
def get_name(self):
return self.name
def set_name(self, new_name):
self.name = new_name
# matrix functions
def get_matrix(self):
return self.matrix
def get_node_base_matrix(self):
return self.node_base_matrix
def show_matrix(self):
for r in range(len(self.matrix)):
print(r)
def set_matrix(self, new_matrix):
self.matrix = new_matrix
def get_node_base_matrix(self):
return self.node_base_matrix
def get_graph(self):
return self.graph
def node_base_matrix(self):
nbm = list()
# r_counter = 0
for r in range(len(self.matrix)):
nbm.append([])
for c in range(len(self.matrix[r])):
if (self.matrix[r][c] == 1):
N = Node([r, c])
nbm[r].append(N)
else:
nbm[r].append(0)
return nbm
def get_node_children(self, node):
children = []
rc_node = node.rc_in_matrix #a list: [r, c]
r = rc_node[0]
c = rc_node[1]
up = r-1
left = c-1
right = c+1
down = r+1
#order of going ahead in maze:
# 1- up
# 2- left
# 3- right
# 4-down
#down
if down < len(self.matrix):
if self.node_base_matrix[down][c] != 0: #down
children.append(self.node_base_matrix[down][c])
#right
if right < len(self.matrix[0]):
if self.node_base_matrix[r][right] != 0: #right
children.append(self.node_base_matrix[r][right])
#left
if left >= 0:
if self.node_base_matrix[r][left] != 0: #left
children.append(self.node_base_matrix[r][left])
#up
if up >= 0:
if self.node_base_matrix[up][c] != 0:
children.append(self.node_base_matrix[up][c])
return(children)
# key=rc : value = Node(int(id), str(name), rc, Node(parent) [children])
def graphdict_generator(self):
graphdict = {}
for r in self.node_base_matrix:
for c in r:
if(c != 0):
graphdict[c] = self.get_node_children(c) # c is equal node == a house(1) in maze(matrix by 1 value)
return graphdict
class Maze:
def __init__(self, name, matrix, start_element, target_element):
self.name = name
self.matrix = matrix
self.start_element = start_element
self.target_element = target_element
self.state_space_graph = state_space_graph(self.matrix, self.start_element, self.target_element)
def get_name(self):
return self.name
def get_matrix(self):
return self.matrix
def set_name(self, new_name):
self.name = new_name
def set_matrix(self, new_matrix):
self.matrix = new_matrix
def show_matrix(self):
for r in range(len(self.matrix)):
print(r)
# maze modeled by spaarse matrix:
# enter a matrix row by row (obstcles:0 houses:1)
matrix = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
maze = Maze("my maze", matrix, [3,0], [0,13])
state_space_graph = maze.state_space_graph
nbm = state_space_graph.node_base_matrix # node base matrix
graph_dict = state_space_graph.get_graph()
starter = state_space_graph.root_node
goal = state_space_graph.goal_node
def Goal_Test(node):
return goal.id == node.id
# start of A*
# It is g(n) : its depends on search tree
def cost_of_getting_here(node):
cost = 0
while(node.parent != starter):
cost += 1
node = node.parent
return cost
goal_r = goal.rc_in_matrix[0]
goal_c = goal.rc_in_matrix[1]
# It is h(n)
def heuristic(node):
node_r = node.rc_in_matrix[0]
node_c = node.rc_in_matrix[1]
v = node_r - goal_r #vertically
h = node_c - goal_c #horizentally
straight_line = v ** 2 + h ** 2
straight_line = straight_line ** 0.5
return int(straight_line)
def Visited_Astar(node):
explored_Astar.add(node)
def Not_Explored_Astar(node):
return node not in explored_Astar
explored_Astar = set()
fringe_Astar = list()
def Astar_goal__node(graph_dict, starter):
fringe_Astar.append(starter)
while(True):
Visited_Astar(fringe_Astar[-1])
neighborhood = graph_dict[fringe_Astar[-1]]
#goal test(before expand)
if(Goal_Test(fringe_Astar[-1])):
return fringe_Astar[-1]
parent = fringe_Astar[-1]
fringe_Astar.pop()
# expand
for neighbor in neighborhood:
if(Not_Explored_Astar(neighbor)):
neighbor.parent = parent
neighbor.Astar_functionality = heuristic(neighbor) + cost_of_getting_here(neighbor)
fringe_Astar.append(neighbor)
#Queue in order by Astar_functionality: star to zero
fringe_Astar.sort(key=lambda x: x.Astar_functionality, reverse=True)
goal_node_Astar = Astar_goal__node(graph_dict, starter)
track_Astar = list()
def Astar_track(last_target):
track_Astar.append(last_target)
while(last_target.parent):
track_Astar.append(last_target.parent)
last_target = last_target.parent
else:
return track_Astar
track_node = Astar_track(goal_node_Astar)
for node in reversed(track_node):
print(node.id, node.rc_in_matrix, "f(n):", node.Astar_functionality, end=" => ")
# end of A*
print("\n------------------------------------------------------")