-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBFS_Astar.py
193 lines (150 loc) · 5.99 KB
/
GBFS_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
# Name: Talha Mustafa
# Roll No: 18I-0573
# Section: D
# Function To Print Maze
def print_matrix(matrix):
for i in matrix:
for j in i:
print(j, end="\t")
print("")
# Check if a neigbour should be added to arr list
def in_open(arr, neigbour):
for node in arr:
if neigbour == node and neigbour.f >= node.f:
return False
return True
# This Function is taken from sample function given in Lab-06
# This class represent a node
class Node:
# Initialize the class
def __init__(self, index, parent, cost):
self.index = index
self.parent = parent
self.cost = cost
self.g = 0 # Distance to start node
self.h = 0 # Distance to goal node
self.f = 0 # Total cost
# Compare nodes
def __eq__(self, other):
return self.index == other.index
# Sort nodes
def __lt__(self, other):
return self.f < other.f
# Print node
def __repr__(self):
return '({0},{1})'.format(self.index, self.f)
# Function to Check Maze Neighbours if the provided position in within the maze
def check_neighbours(current, maze, visited):
i, j = current.index
rows = len(maze)
cols = len(maze[0])
if current not in visited:
return (i >= 0) and (i < rows) and (j >= 0) and (j < cols)
else:
return False
# Search neighbours of given position
def search_neighbours(current, maze, visited):
i, j = current.index
neighbours = []
# UP
if check_neighbours(Node((i - 1, j), None, 0), maze, visited) and maze[i - 1][j] == 1:
temp = i - 1, j
neighbours.append(Node(temp, current, current.cost + 1))
# LEFT
if check_neighbours(Node((i, j - 1), None, 0), maze, visited) and maze[i][j - 1] == 1:
temp = i, j - 1
neighbours.append(Node(temp, current, current.cost + 1))
# RIGHT
if check_neighbours(Node((i, j + 1), None, 0), maze, visited) and maze[i][j + 1] == 1:
temp = i, j + 1
neighbours.append(Node(temp, current, current.cost + 1))
# DOWN
if check_neighbours(Node((i + 1, j), None, 0), maze, visited) and maze[i + 1][j] == 1:
temp = i + 1, j
neighbours.append(Node(temp, current, current.cost + 1))
return neighbours
# Functions to apply Algorithms
def search_maze(maze, start, end, algo):
arr = []
visited = []
total = 0
start_node = Node(start, None, 0)
goal_node = Node(end, None, 0)
# Distance of start node from goal node
start_node.f = abs(start_node.index[0] - goal_node.index[0]) + abs(start_node.index[1] - goal_node.index[1])
arr.append(start_node)
while arr:
arr.sort()
current_node = arr.pop(0)
if current_node == goal_node:
result = []
while current_node != start_node:
result.append(current_node)
current_node = current_node.parent
result.append(start_node)
return total, result[::-1]
neigbours = search_neighbours(current_node, maze, visited)
for neigbour in neigbours:
neigbour.g = current_node.cost
neigbour.h = abs(neigbour.index[0] - goal_node.index[0]) + abs(neigbour.index[1] - goal_node.index[1])
if algo == "A*":
neigbour.f = neigbour.g + neigbour.h
elif algo == "GBFS":
neigbour.f = neigbour.h
else:
return -1
if in_open(arr, neigbour):
arr.append(neigbour)
if current_node not in visited:
total += 1
visited.append(current_node)
return -1
def main():
maze = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 0,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print_matrix(maze)
print("\t\t\t ========================================= \t\t\t")
result = search_maze(maze, start, end, "A*")
if result == -1:
print("Invalid.")
else:
print("Algorithm: A*")
print("Path(Position, Distance(f)): \n", result[1])
print("Number of Moves: ", result[0])
print("Path Cost: ", len(result[1]) - 1)
print("======================================================")
result = search_maze(maze, start, end, "GBFS")
if result == -1:
print("Invalid.")
else:
print("Algorithm: GBFS")
print("Path(Position, Distance(h)): \n", result[1])
print("Number of Moves: ", result[0])
print("Path Cost: ", len(result[1]) - 1)
print("======================================================")
if __name__ == "__main__":
main()