-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
178 lines (151 loc) · 6.79 KB
/
test.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
from mazerunner import MazeState
from dfs import depth_first_search
from bfs import breadth_first_search
from bestfs import best_first_search
from mazegenerator import generate_random_maze
import time
import json
import tracemalloc
def formatted_path_printing(path):
for i in range(len(path)):
if i == len(path) - 1:
print(path[i])
else:
print(f"{path[i]} =>", end=" ")
print()
with open("maze.json", "r") as f:
maze_file = json.load(f)
def input_fetcher():
maze_selector = input("Enter maze number (1 - 4 for the mazes in the config file. Enter 5 for a random maze) :")
if maze_selector == "1":
maze = maze_file["maze1"]
start_position = tuple(maze_file["start1"])
goal_position = tuple(maze_file["end1"])
elif maze_selector == "2":
maze = maze_file["maze2"]
start_position = tuple(maze_file["start2"])
goal_position = tuple(maze_file["end2"])
elif maze_selector == "3":
maze = maze_file["maze3"]
start_position = tuple(maze_file["start3"])
goal_position = tuple(maze_file["end3"])
elif maze_selector == "4":
maze = maze_file["maze4"]
start_position = tuple(maze_file["start4"])
goal_position = tuple(maze_file["end4"])
elif maze_selector == "5":
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))
num_walls = int(input("Enter number of walls: "))
maze = generate_random_maze(rows, cols, num_walls)
start_position = tuple(input("Enter start position (row, col): ").split(","))
if maze[int(start_position[0])][int(start_position[1])] == 1:
print("Invalid start position. Please try again.")
start_position = tuple(input("Enter start position (row, col): ").split(","))
goal_position = tuple(input("Enter goal position (row, col): ").split(","))
if maze[int(goal_position[0])][int(goal_position[1])] == 1:
print("Invalid goal position. Please try again.")
goal_position = tuple(input("Enter goal position (row, col): ").split(","))
start_position = int(start_position[0]), int(start_position[1])
goal_position = int(goal_position[0]), int(goal_position[1])
else:
print("Invalid input. Please try again.")
return maze, start_position, goal_position
def main():
maze, start_position, goal_position = input_fetcher()
initial_state = MazeState(maze, start_position)
# taking an average of 10 runs
time_dfs = []
states_dfs = []
memory_dfs = []
dfs_path_lengths = []
time_bfs = []
states_bfs = []
memory_bfs = []
bfs_path_lengths = []
time_bestfs = []
states_bestfs = []
memory_bestfs = []
bestfs_path_lengths = []
for i in range(10):
tracemalloc.start()
start_time_dfs = time.perf_counter()
try:
dfs_solution, no_of_states_explored_dfs, dfs_path_length = depth_first_search(initial_state, goal_position)
except:
dfs_solution = None
no_of_states_explored_dfs = 0
dfs_path_length = 0
end_time_dfs = time.perf_counter()
time_dfs.append(end_time_dfs - start_time_dfs)
states_dfs.append(no_of_states_explored_dfs)
memory_dfs.append(tracemalloc.get_traced_memory()[0])
dfs_path_lengths.append(dfs_path_length)
tracemalloc.stop()
tracemalloc.start()
start_time_bfs = time.perf_counter()
try:
bfs_solution, no_of_states_explored_bfs, bfs_path_length = breadth_first_search(initial_state, goal_position)
except:
bfs_solution = None
no_of_states_explored_bfs = 0
bfs_path_length = 0
end_time_bfs = time.perf_counter()
time_bfs.append(end_time_bfs - start_time_bfs)
states_bfs.append(no_of_states_explored_bfs)
memory_bfs.append(tracemalloc.get_traced_memory()[0])
bfs_path_lengths.append(bfs_path_length)
tracemalloc.stop()
tracemalloc.start()
start_time_bestfs = time.perf_counter()
try:
bestfs_solution, no_of_states_explored_bestfs, bestfs_path_length = best_first_search(initial_state, goal_position)
except:
bestfs_solution = None
no_of_states_explored_bestfs = 0
bestfs_path_length = 0
end_time_bestfs = time.perf_counter()
time_bestfs.append(end_time_bestfs - start_time_bestfs)
states_bestfs.append(no_of_states_explored_bestfs)
memory_bestfs.append(tracemalloc.get_traced_memory()[0])
bestfs_path_lengths.append(bestfs_path_length)
tracemalloc.stop()
avg_time_dfs = sum(time_dfs)/len(time_dfs)
print("Average time for DFS:", avg_time_dfs)
avg_states_dfs = sum(states_dfs)/len(states_dfs)
print("Average number of states explored with DFS:", avg_states_dfs)
avg_memory_dfs = sum(memory_dfs)/len(memory_dfs)
print("Average memory usage for DFS:", avg_memory_dfs, "bytes.")
avg_path_length_dfs = sum(dfs_path_lengths)/len(dfs_path_lengths)
print("Average path length for DFS:", avg_path_length_dfs)
if dfs_solution:
formatted_path_printing(dfs_solution)
else:
print("No path found with DFS")
avg_time_bfs = sum(time_bfs)/len(time_bfs)
print("Average time for BFS:", avg_time_bfs)
avg_states_bfs = sum(states_bfs)/len(states_bfs)
print("Average number of states explored with BFS:", avg_states_bfs)
avg_memory_bfs = sum(memory_bfs)/len(memory_bfs)
print("Average memory usage for BFS:", avg_memory_bfs, "bytes.")
avg_path_length_bfs = sum(bfs_path_lengths)/len(bfs_path_lengths)
print("Average path length for BFS:", avg_path_length_bfs)
if bfs_solution:
formatted_path_printing(bfs_solution)
else:
print("No path found with BFS")
avg_time_bestfs = sum(time_bestfs)/len(time_bestfs)
print("Average time for BestFS:", avg_time_bestfs)
avg_states_bestfs = sum(states_bestfs)/len(states_bestfs)
print("Average number of states explored with BestFS:", avg_states_bestfs)
avg_memory_bestfs = sum(memory_bestfs)/len(memory_bestfs)
print("Average memory usage for BestFS:", avg_memory_bestfs, "bytes.")
avg_path_length_bestfs = sum(bestfs_path_lengths)/len(bestfs_path_lengths)
print("Average path length for BestFS:", avg_path_length_bestfs)
if bestfs_solution:
formatted_path_printing(bestfs_solution)
else:
print("No path found with BestFS")
return maze, avg_time_dfs, avg_states_dfs, avg_memory_dfs, avg_path_length_dfs, avg_time_bfs, avg_states_bfs, avg_memory_bfs, avg_path_length_bfs, avg_time_bestfs, avg_states_bestfs, avg_memory_bestfs, avg_path_length_bestfs, dfs_solution, bfs_solution, bestfs_solution
if __name__ == "__main__":
main()