-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
143 lines (115 loc) · 3.66 KB
/
benchmark.py
File metadata and controls
143 lines (115 loc) · 3.66 KB
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
from PyMaze import *
import algorithms
import time
import functools
#Dead Ends
#Straightaways
#Turns
#Junctions
#Crossroads
#time
##########
#needed to instrument in code:
#number of loops
#number of visited
#number of other stuff
class Benchmark(Maze):
def __init__(self, sz=(100,100)):
self._sz = sz
Maze.__init__(self, None, sz)
self.no_visuals = True
def benchmark_all(self):
self.benchmark_solve_all(gen=True)
def benchmark(self, a, b):
stats = self.benchmark_solve(a, b, True)
print(stats)
def benchmark_solve_all(self, gen=False):
for galg in self.genAlgs:
for salg in self.solveAlgs:
self.__init__(self._sz)
stats = self.benchmark_solve(galg, salg, gen)
print(galg, salg, stats)
self.__init__(self._sz)
def benchmark_solve(self, a, b, gen=False):
stats = {}
if gen:
stats["gen"] = self.benchmark_gen(a)
else:
self.genAlg = self.genAlgs[a] if isinstance(a, str) else a
self.genAlg(self)
self.solveAlg = self.solveAlgs[b] if isinstance(b, str) else b
t0 = time.time()
self.solveAlg(self)
ttime = time.time() - t0
stats["solve"] = {}
stats["solve"]["time"] = ttime
return stats
def benchmark_gen_all(self, n):
results = {}
for alg in self.genAlgs:
self.__init__(self._sz)
results[alg] = self.benchmark_gen_n(alg, n)
self.__init__(self._sz)
return results
def benchmark_gen_n(self, a, n):
res = []
for i in range(n):
self.__init__(self._sz)
res.append(self.benchmark_gen(a))
totres = dict.fromkeys(res[0].keys())
for attrib in totres:
tot = 0
for r in res:
tot += r[attrib]
tot /= n
totres[attrib] = tot
return totres
def benchmark_gen(self, a):
self.genAlg = self.genAlgs[a] if isinstance(a, str) else a
t0 = time.time()
self.genAlg(self)
ttime = time.time() - t0
stats = {}
deadends, straights, turns, junctions, crossroads = self.calc_dirs()
tot = deadends+straights+turns+junctions+crossroads
stats["total"] = tot
stats["deadends"] = deadends/tot
stats["straightAways"] = straights/tot
stats["turns"] = turns/tot
stats["junctions"] = junctions/tot
stats["crossroads"] = crossroads/tot
stats["time"] = ttime
return stats
def calc_deadEnds(self):
pass
def calc_dirs(self):
straightCount = 0
turnCount = 0
junctionCount = 0
crossroadCount = 0
deadendCount = 0
DIR = [0,1,2,1,2]
for v in self.vertices():
e = self.edge(v)
l = len(e)
if l == 1:
deadendCount += 1
elif l == 2:
e1 = self.edgeToDir(e[0],v) #down or right
e2 = self.edgeToDir(e[1],v) #up or left
dir1 = DIR[e1.value]
dir2 = DIR[e2.value]
if dir1 == dir2:
straightCount += 1
else:
turnCount += 1
elif l == 3:
junctionCount += 1
elif l == 4:
crossroadCount += 1
return (deadendCount, straightCount, turnCount, junctionCount, crossroadCount)
def main():
bm = Benchmark()
print(bm.benchmark_gen_all(10))
if __name__ == "__main__":
main()