-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark.py
executable file
·231 lines (176 loc) · 9.72 KB
/
Benchmark.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
#!/usr/bin/env python3
from time import time
from ConstraintPropagationSolver import ConstraintPropagationSolver
from LocalSearchSolver import LocalSearchSolver
from GlobalSearchSolver import GlobalSearchSolver
from Kronecker import Kronecker
class Benchmark:
def run_cpb(self):
times = []
steps = []
for i in range(self.iterations):
starting_time = time()
s1 = ConstraintPropagationSolver(self.queens)
s1.solve()
times.append(time() - starting_time)
steps.append(s1.count)
m = round(sum(times) / self.iterations, 3)
var = sum([(i - m)**2 for i in times]) / (self.iterations - 1)
var_steps = sum([(i - m)**2 for i in steps]) / (self.iterations - 1)
print("\nCPB BENCHMARK: queens = {}, iterations = {}\n".format(self.queens,self.iterations))
print("Total time required " + str(round(sum(times), 2)) + " seconds")
print("Average time required: " + str(m) + " seconds")
print("Time taken by the worst iteration: " + str(round(max(times), 2)) + " seconds")
print("Standard deviation (corrected) of the results " + str(round(var**0.5, 2)) + " seconds")
times.remove(max(times))
print("Average time required not considering the worst iteration: " + str(round(sum(times)/(self.iterations-1), 3)) + " seconds")
print("Total steps: " + str(sum(steps)))
print("Average steps required: " + str(sum(steps)/self.iterations))
print("Steps taken by the worst iteration: " + str(max(steps)))
print("Standard deviation (corrected) of the steps " + str(round(var_steps**0.5, 2)) + " steps")
steps.remove(max(steps))
print("Average steps required not considering the worst iteration: " + str(round(sum(steps)/(self.iterations-1), 3)) + " steps")
def run_local(self):
times = []
solutions = []
steps = []
for i in range(self.iterations):
starting_time = time()
s1 = LocalSearchSolver(self.queens)
sol1 = s1.solve()
times.append(time() - starting_time)
if sol1.is_a_solution():
solutions.append(1)
else:
solutions.append(0)
steps.append(s1.count)
m = round(sum(times) / self.iterations, 3)
var = sum([(i - m)**2 for i in times]) / (self.iterations - 1)
var_steps = sum([(i - m)**2 for i in steps]) / (self.iterations - 1)
print("\nLOCAL SEARCH BENCHMARK: queens = {}, iterations = {}\n".format(self.queens,self.iterations))
print("Total time required " + str(round(sum(times), 2)) + " seconds")
print("Average time required: " + str(m) + " seconds")
print("Time taken by the worst iteration: " + str(round(max(times), 2)) + " seconds")
print("Number of actual solutions = {} out of {} ({}%)".format(sum(solutions), self.iterations, round(sum(solutions)/self.iterations * 100, 2)))
print("Standard deviation (corrected) of the results " + str(round(var**0.5, 2)) + " seconds")
times.remove(max(times))
print("Average time required not considering the worst iteration: " + str(round(sum(times)/(self.iterations-1), 3)) + " seconds")
print("Total steps: " + str(sum(steps)))
print("Average steps required: " + str(sum(steps)/self.iterations))
print("Steps taken by the worst iteration: " + str(max(steps)))
print("Standard deviation (corrected) of the steps " + str(round(var_steps**0.5, 2)) + " steps")
steps.remove(max(steps))
print("Average steps required not considering the worst iteration: " + str(round(sum(steps)/(self.iterations-1), 3)) + " steps")
def run_global(self):
times = []
steps = []
for i in range(self.iterations):
starting_time = time()
s1 = GlobalSearchSolver(self.queens)
sol1 = s1.solve()
times.append(time() - starting_time)
steps.append(s1.count)
m = round(sum(times) / self.iterations, 3)
var = sum([(i - m)**2 for i in times]) / (self.iterations - 1)
var_steps = sum([(i - m)**2 for i in steps]) / (self.iterations - 1)
print("\nGLOBAL SEARCH BENCHMARK: queens = {}, iterations = {}\n".format(self.queens,self.iterations))
print("Total time required " + str(round(sum(times), 2)) + " seconds")
print("Average time required: " + str(m) + " seconds")
print("Time taken by the worst iteration: " + str(round(max(times), 2)) + " seconds")
print("Standard deviation (corrected) of the results " + str(round(var**0.5, 2)) + " seconds")
times.remove(max(times))
print("Average time required not considering the worst iteration: " + str(round(sum(times)/(self.iterations-1), 3)) + " seconds")
print("Total steps: " + str(sum(steps)))
print("Average steps required: " + str(sum(steps)/self.iterations))
print("Steps taken by the worst iteration: " + str(max(steps)))
print("Standard deviation (corrected) of the steps " + str(round(var_steps**0.5, 2)) + " steps")
steps.remove(max(steps))
print("Average steps required not considering the worst iteration: " + str(round(sum(steps)/(self.iterations-1), 3)) + " steps")
def run_kronecker(self, method="GS"):
times = []
solutions = []
for i in range(self.iterations):
starting_time = time()
s1 = Kronecker(self.queens, method)
sol1 = s1.solve()
times.append(time() - starting_time)
if sol1.is_a_solution():
solutions.append(1)
else:
solutions.append(0)
m = round(sum(times) / self.iterations, 3)
var = sum([(i - m)**2 for i in times]) / (self.iterations - 1)
print("\nKRONECKER BENCHMARK ({}): queens = {}, iterations = {}\n".format(method, self.queens,self.iterations))
print("Total time required " + str(round(sum(times), 2)) + " seconds")
print("Average time required: " + str(m) + " seconds")
print("Time taken by the worst iteration: " + str(round(max(times), 2)) + " seconds")
print("Number of actual solutions = {} out of {} ({}%)".format(sum(solutions), self.iterations, round(sum(solutions)/self.iterations * 100, 2)))
print("Standard deviation (corrected) of the results " + str(round(var**0.5, 2)) + " seconds")
times.remove(max(times))
print("Average time required not considering the worst iteration: " + str(round(sum(times)/(self.iterations-1), 3)) + " seconds")
def run_gs_from_kron(self):
times = []
for i in range(self.iterations):
starting_time = time()
s1 = Kronecker(self.queens, 'GS')
sol1 = s1.solve()
s2 = GlobalSearchSolver(sol1).solve()
times.append(time() - starting_time)
m = round(sum(times) / self.iterations, 3)
var = sum([(i - m)**2 for i in times]) / (self.iterations - 1)
print("\nGLOBAL SEARCH FROM KRON BENCHMARK: queens = {}, iterations = {}\n".format(self.queens,self.iterations))
print("Total time required " + str(round(sum(times), 2)) + " seconds")
print("Average time required: " + str(m) + " seconds")
print("Time taken by the worst iteration: " + str(round(max(times), 2)) + " seconds")
print("Standard deviation (corrected) of the results " + str(round(var**0.5, 2)) + " seconds")
times.remove(max(times))
print("Average time required not considering the worst iteration: " + str(round(sum(times)/(self.iterations-1), 3)) + " seconds")
def run_ls_from_kron(self):
times = []
solutions = []
for i in range(self.iterations):
starting_time = time()
s1 = Kronecker(self.queens, 'GS')
sol1 = s1.solve()
s2 = LocalSearchSolver(sol1)
sol2 = s1.solve()
times.append(time() - starting_time)
if sol2.is_a_solution():
solutions.append(1)
else:
solutions.append(0)
m = round(sum(times) / self.iterations, 3)
var = sum([(i - m)**2 for i in times]) / (self.iterations - 1)
print("\nLOCAL SEARCH FROM KRON BENCHMARK: queens = {}, iterations = {}\n".format(self.queens,self.iterations))
print("Total time required " + str(round(sum(times), 2)) + " seconds")
print("Average time required: " + str(m) + " seconds")
print("Time taken by the worst iteration: " + str(round(max(times), 2)) + " seconds")
print("Number of actual solutions = {} out of {} ({}%)".format(sum(solutions), self.iterations, round(sum(solutions)/self.iterations * 100, 2)))
print("Standard deviation (corrected) of the results " + str(round(var**0.5, 2)) + " seconds")
times.remove(max(times))
print("Average time required not considering the worst iteration: " + str(round(sum(times)/(self.iterations-1), 3)) + " seconds")
def __init__(self, n, it):
self.queens = n
self.iterations = it
def run(self):
# cpb
self.run_cpb()
print('\n')
# local
self.run_local()
print('\n')
# global
self.run_global()
print('\n')
# kronecker with global search
self.run_kronecker()
print('\n')
# kronecker with constraint propagation
self.run_kronecker('CP')
print('\n')
# global search from kronecker solved with global search
self.run_gs_from_kron()
print('\n')
# local search from kronecker solved with global search
self.run_ls_from_kron()
print('\n')