-
Notifications
You must be signed in to change notification settings - Fork 0
/
ee.py
273 lines (227 loc) · 7.94 KB
/
ee.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
import cProfile
import pstats
import psutil
import os
import sys
from astar import AStar
from bidirbfs import BiDirBFS
from constants import *
from dijkstra import GridDijkstra
from io import StringIO
from mazegen import mgen
from mapparser import parsemap_WHOLE
# based on the answer of Triptych on StackOverflow:
# http://stackoverflow.com/a/616672/825916
class Logger(object):
"""Class for duplication of stdout both to terminal and file."""
def __init__(self):
self.terminal = sys.stdout
self.log = open("results_h", "w")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
self.terminal.flush()
self.log.flush()
# set stdout to Logger() class
sys.stdout = Logger()
At = []
Af = []
Aet = []
Aef = []
Act = []
Acf = []
Bt = []
Bf = []
Dt = []
Df = []
# A*
def A(m, h=0):
a = AStar(m, h)
for i in a.step():
pass
if not a.path:
raise Exception("\a[!] A* PATH NOT FOUND!")
# Bi-directional BFS
def B(m):
bdbfs = BiDirBFS(m)
for i in bdbfs.step():
pass
if not bdbfs.path:
raise Exception("\a[!] Bi-dirBFS PATH NOT FOUND!")
# Dijkstra
def D(m):
d = GridDijkstra(m)
for i in d.step():
pass
if not d.path:
raise Exception("\a[!] Dijkstra PATH NOT FOUND!")
# get the ID assigned to the testing script
p = psutil.Process(os.getpid())
# set the priority to realtime
p.set_nice(psutil.REALTIME_PRIORITY_CLASS)
# walk to current working folder to find all the maps which are there
for root, dirs, files in os.walk(os.getcwd()):
for file in files:
if file.endswith(".map") and "map" in root:
r, n = parsemap_WHOLE(os.path.join(root, file))
if r is not None:
print(n)
for i, j in enumerate(r):
case = j
p = cProfile
#A* - manhattan
p.run("A(case)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Af.append(s[6])
At.append(s[10])
print("[*]A*M - {}/20 done. F: {} T: {}".format(
i + 1, Af[-1], At[-1]))
#A* - euclidean
p.run("A(case, 1)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Aef.append(s[6])
Aet.append(s[10])
print("[*]A*E - {}/20 done. F: {} T: {}".format(
i + 1, Aef[-1], Aet[-1]))
#A* - chebyshev
p.run("A(case, 2)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Acf.append(s[6])
Act.append(s[10])
print("[*]A*C - {}/20 done. F: {} T: {}".format(
i + 1, Acf[-1], Act[-1]))
# Bi-directional BFS
p.run("B(case)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Bf.append(s[6])
Bt.append(s[10])
print("[*]B - {}/20 done. F: {} T: {}".format(
i + 1, Bf[-1], Bt[-1]))
# Dijkstra
p.run("D(case)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Df.append(s[6])
Dt.append(s[10])
print("[*]D - {}/20 done. F: {} T: {}".format(
i + 1, Df[-1], Dt[-1]))
print("MAPS")
#A* - manhattan
print("A*")
print("AVG CALLS: {}".format(sum(map(int, Af)) // len(Af)))
print("AVG TIMES: {}".format(sum(map(float, At)) / len(At)))
#A* - euclidean
print("A*E")
print("AVG CALLS: {}".format(sum(map(int, Aef)) // len(Aef)))
print("AVG TIMES: {}".format(sum(map(float, Aet)) / len(Aet)))
#A* - chebyshev
print("A*C")
print("AVG CALLS: {}".format(sum(map(int, Acf)) // len(Acf)))
print("AVG TIMES: {}".format(sum(map(float, Act)) / len(Act)))
# Bi-dirBFS
print("B")
print("AVG CALLS: {}".format(sum(map(int, Bf)) // len(Bf)))
print("AVG TIMES: {}".format(sum(map(float, Bt)) / len(Bt)))
# Dijkstra
print("D")
print("AVG CALLS: {}".format(sum(map(int, Df)) // len(Df)))
print("AVG TIMES: {}".format(sum(map(float, Dt)) / len(Dt)))
# RANDOM MAZES
for size in (32, 64, 128):
At = []
Af = []
Aet = []
Aef = []
Act = []
Acf = []
Bt = []
Bf = []
Dt = []
Df = []
for i in range(120):
# generate a random maze of size *size*
case = mgen(size)
p = cProfile
#A* - manhattan
p.run("A(case)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Af.append(s[6])
At.append(s[10])
print("[*]A*M - {}/20 done. F: {} T: {}".format(i + 1, Af[-1], At[-1]))
#A* - euclidean
p.run("A(case, 1)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Aef.append(s[6])
Aet.append(s[10])
print("[*]A*E - {}/20 done. F: {} T: {}".format(
i + 1, Aef[-1], Aet[-1]))
#A* - chebyshev
p.run("A(case, 2)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Acf.append(s[6])
Act.append(s[10])
print("[*]A*C - {}/20 done. F: {} T: {}".format(
i + 1, Acf[-1], Act[-1]))
# Bi-directional BFS
p.run("B(case)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Bf.append(s[6])
Bt.append(s[10])
print("[*]B - {}/20 done. F: {} T: {}".format(i + 1, Bf[-1], Bt[-1]))
# Dijkstra
p.run("D(case)", filename="statsfile")
stream = StringIO()
stats = pstats.Stats('statsfile', stream=stream)
stats.print_stats()
s = str(stream.getvalue()).split()
Df.append(s[6])
Dt.append(s[10])
print("[*]D - {}/20 done. F: {} T: {}".format(i + 1, Df[-1], Dt[-1]))
print("{}x{} MAZE".format(size, size))
#A* - manhattan
print("A*")
print("AVG CALLS: {}".format(sum(map(int, Af)) // len(Af)))
print("AVG TIMES: {}".format(sum(map(float, At)) / len(At)))
#A* - euclidean
print("A*E")
print("AVG CALLS: {}".format(sum(map(int, Aef)) // len(Aef)))
print("AVG TIMES: {}".format(sum(map(float, Aet)) / len(Aet)))
#A* - chebyshev
print("A*C")
print("AVG CALLS: {}".format(sum(map(int, Acf)) // len(Acf)))
print("AVG TIMES: {}".format(sum(map(float, Act)) / len(Act)))
# Bi-dirBFS
print("B")
print("AVG CALLS: {}".format(sum(map(int, Bf)) // len(Bf)))
print("AVG TIMES: {}".format(sum(map(float, Bt)) / len(Bt)))
# Dijkstra
print("D")
print("AVG CALLS: {}".format(sum(map(int, Df)) // len(Df)))
print("AVG TIMES: {}".format(sum(map(float, Dt)) / len(Dt)))