forked from amit-sarker/GBFS_AI_Final_Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparison.py
357 lines (280 loc) · 9.8 KB
/
comparison.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import time
from math import ceil
from Graph_Construction import construct_graph
from OCL_Framework import OCL_Algo
from a_star_search import a_star_algo
from best_first_search import best_first_algo
from comparison_graph import compare_by_time_and_node, compare_by_time_and_edge, compare_by_expanded_node, \
compare_by_expanded_node_infinite, compare_by_time_infinite
from infinite_graph_construction import construct_infinite_graph
def single_run():
total_nodes = 5
total_edges = 10
goal_node = total_nodes - 1
graph = construct_graph(total_nodes, total_edges)
start_ocl = time.time()
result_ocl = OCL_Algo(graph, goal_node)
end_ocl = time.time()
elapsed_ocl = (end_ocl - start_ocl) * 1000
print("Elapsed time OCL: ", elapsed_ocl)
print('\n')
start_astar = time.time()
result_astar = a_star_algo(graph, goal_node)
end_astar = time.time()
elapsed_astar = (end_astar - start_astar) * 1000
print("Elapsed time aStar: ", elapsed_astar)
print('\n')
start_best_first = time.time()
result_best_first = best_first_algo(graph, goal_node)
end_best_first = time.time()
elapsed_best_first = (end_best_first - start_best_first) * 1000
print("Elapsed time best first: ", elapsed_best_first)
print('\n')
def compare_infinite_graph():
total_nodes = 1004
graph = construct_infinite_graph(total_nodes)
goal_node = total_nodes - 1
result_ocl = OCL_Algo(graph, goal_node)
nodes_ocl = result_ocl.__len__()
result_astar = a_star_algo(graph, goal_node)
nodes_astar = result_astar.__len__()
result_best_first = best_first_algo(graph, goal_node)
nodes_best_first = result_best_first.__len__()
print(result_ocl)
print(result_astar)
print(result_best_first)
print("OCL: ", nodes_ocl)
print("A star: ", nodes_astar)
print("Best first: ", nodes_best_first)
def comp_by_infinite_time():
total_nodes = 100
x = []
y1 = []
y2 = []
y3 = []
while True:
if total_nodes >= 1000:
break
goal_node = total_nodes - 1
time1 = 0
time2 = 0
time3 = 0
for i in range(200):
graph = construct_infinite_graph(total_nodes)
start_ocl = time.time()
result_ocl = OCL_Algo(graph, goal_node)
end_ocl = time.time()
elapsed_ocl = (end_ocl - start_ocl) * 1000
start_astar = time.time()
result_astar = a_star_algo(graph, goal_node)
end_astar = time.time()
elapsed_astar = (end_astar - start_astar) * 1000
start_best_first = time.time()
result_best_first = best_first_algo(graph, goal_node)
end_best_first = time.time()
elapsed_best_first = (end_best_first - start_best_first) * 1000
time1 += elapsed_ocl
time2 += elapsed_astar
time3 += elapsed_best_first
x.append(total_nodes)
y1.append(time1 / 200)
y2.append(time2 / 200)
y3.append(time3 / 200)
total_nodes += 100
compare_by_time_infinite(x, y1, y2, y3)
def comp_by_infinite_graph():
total_nodes = 100
x = []
y1 = []
y2 = []
y3 = []
while True:
if total_nodes >= 1000:
break
goal_node = total_nodes - 1
nodes1 = 0
nodes2 = 0
nodes3 = 0
for i in range(200):
graph = construct_infinite_graph(total_nodes)
result_ocl = OCL_Algo(graph, goal_node)
nodes_ocl = result_ocl.__len__()
result_astar = a_star_algo(graph, goal_node)
nodes_astar = result_astar.__len__()
result_best_first = best_first_algo(graph, goal_node)
nodes_best_first = result_best_first.__len__()
nodes1 += nodes_ocl
nodes2 += nodes_astar
nodes3 += nodes_best_first
x.append(total_nodes)
y1.append(nodes1 / 200)
y2.append(nodes2 / 200)
y3.append(nodes3 / 200)
total_nodes += 100
compare_by_expanded_node_infinite(x, y1, y2, y3)
def comp_by_total_expanded_nodes():
total_nodes = 5
x = []
y1 = []
y2 = []
y3 = []
f_ocl = open("f_ocl_nodes_ex.txt", "w")
f_astar = open("f_astar_nodes_ex.txt", "w")
f_best = open("f_best_nodes_ex.txt", "w")
while True:
if total_nodes >= 500:
break
total_edges = 3 * total_nodes
goal_node = total_nodes - 1
nodes1 = 0
nodes2 = 0
nodes3 = 0
for i in range(200):
graph = construct_graph(total_nodes, total_edges)
result_ocl = OCL_Algo(graph, goal_node)
nodes_ocl = result_ocl.__len__()
result_astar = a_star_algo(graph, goal_node)
nodes_astar = result_astar.__len__()
result_best_first = best_first_algo(graph, goal_node)
nodes_best_first = result_best_first.__len__()
nodes1 += nodes_ocl
nodes2 += nodes_astar
nodes3 += nodes_best_first
x.append(total_nodes)
y1.append(nodes1 / 200)
y2.append(nodes2 / 200)
y3.append(nodes3 / 200)
for i in range(len(y1)):
f_ocl.write(str(y1[i]))
f_ocl.write('\n')
f_astar.write(str(y2[i]))
f_astar.write('\n')
f_best.write(str(y3[i]))
f_best.write('\n')
if total_nodes < 50:
total_nodes += 5
elif total_nodes < 200:
total_nodes += 10
elif total_nodes < 350:
total_nodes += 30
else:
total_nodes += 50
if total_nodes == 50:
compare_by_expanded_node(x, y1, y2, y3)
elif total_nodes == 200:
compare_by_expanded_node(x, y1, y2, y3)
elif total_nodes == 350:
compare_by_expanded_node(x, y1, y2, y3)
elif total_nodes == 500:
compare_by_expanded_node(x, y1, y2, y3)
def comp_by_nodes_time():
total_nodes = 5
x = []
y1 = []
y2 = []
y3 = []
f_ocl = open("f_ocl_nodes.txt", "w")
f_astar = open("f_astar_nodes.txt", "w")
f_best = open("f_best_nodes.txt", "w")
while True:
if total_nodes >= 500:
break
total_edges = 3 * total_nodes
goal_node = total_nodes - 1
time1 = 0
time2 = 0
time3 = 0
for i in range(200):
graph = construct_graph(total_nodes, total_edges)
start_ocl = time.time()
result_ocl = OCL_Algo(graph, goal_node)
end_ocl = time.time()
elapsed_ocl = end_ocl - start_ocl
print("elapsed OCL: ", elapsed_ocl)
start_astar = time.time()
result_astar = a_star_algo(graph, goal_node)
end_astar = time.time()
elapsed_astar = end_astar - start_astar
print("elapsed aStar: ", elapsed_astar)
start_best_first = time.time()
result_best_first = best_first_algo(graph, goal_node)
end_best_first = time.time()
elapsed_best_first = end_best_first - start_best_first
print("elapsed best first: ", elapsed_best_first)
time1 += elapsed_ocl
time2 += elapsed_astar
time3 += elapsed_best_first
x.append(total_nodes)
y1.append(time1 / 200)
y2.append(time2 / 200)
y3.append(time3 / 200)
if total_nodes < 50:
total_nodes += 5
elif total_nodes < 200:
total_nodes += 10
elif total_nodes < 350:
total_nodes += 30
else:
total_nodes += 50
if total_nodes == 50:
compare_by_time_and_node(x, y1, y2, y3)
elif total_nodes == 200:
compare_by_time_and_node(x, y1, y2, y3)
elif total_nodes == 350:
compare_by_time_and_node(x, y1, y2, y3)
elif total_nodes == 500:
compare_by_time_and_node(x, y1, y2, y3)
def comp_by_edges_time():
total_edges = 15
x = []
y1 = []
y2 = []
y3 = []
while True:
if total_edges >= 2000:
break
total_nodes = ceil(total_edges / 3)
goal_node = total_nodes - 1
time1 = 0
time2 = 0
time3 = 0
for i in range(200):
graph = construct_graph(total_nodes, total_edges)
start_ocl = time.time()
result_ocl = OCL_Algo(graph, goal_node)
end_ocl = time.time()
elapsed_ocl = end_ocl - start_ocl
print("elapsed OCL: ", elapsed_ocl)
start_astar = time.time()
result_astar = a_star_algo(graph, goal_node)
end_astar = time.time()
elapsed_astar = end_astar - start_astar
print("elapsed aStar: ", elapsed_astar)
start_best_first = time.time()
result_best_first = best_first_algo(graph, goal_node)
end_best_first = time.time()
elapsed_best_first = end_best_first - start_best_first
print("elapsed best first: ", elapsed_best_first)
time1 += elapsed_ocl
time2 += elapsed_astar
time3 += elapsed_best_first
x.append(total_edges)
y1.append(time1 / 200)
y2.append(time2 / 200)
y3.append(time3 / 200)
if total_edges < 150:
total_edges += 15
elif total_edges < 600:
total_edges += 30
elif total_edges < 1050:
total_edges += 90
else:
total_edges += 150
if total_edges == 200:
compare_by_time_and_edge(x, y1, y2, y3)
elif total_edges == 600:
compare_by_time_and_edge(x, y1, y2, y3)
elif total_edges == 1000:
compare_by_time_and_edge(x, y1, y2, y3)
elif total_edges >= 2000:
compare_by_time_and_edge(x, y1, y2, y3)