-
Notifications
You must be signed in to change notification settings - Fork 77
/
runner.py
268 lines (210 loc) · 9.06 KB
/
runner.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
"""Implementation of Runner utility class."""
import datetime
import json
import os
import logging
import xlsxwriter
from numpy import (
amin,
median,
amax,
mean,
std
)
from NiaPy.algorithms import AlgorithmUtility
logging.basicConfig()
logger = logging.getLogger('NiaPy.runner.Runner')
logger.setLevel('INFO')
__all__ = ["Runner"]
class Runner:
r"""Runner utility feature.
Feature which enables running multiple algorithms with multiple benchmarks.
It also support exporting results in various formats (e.g. LaTeX, Excel, JSON)
Attributes:
D (int): Dimension of problem
NP (int): Population size
nFES (int): Number of function evaluations
nRuns (int): Number of repetitions
useAlgorithms (list of Algorithm): List of algorithms to run
useBenchmarks (list of Benchmarks): List of benchmarks to run
Returns:
results (Dict[str, Dict]): Returns the results.
"""
def __init__(self, D=10, nFES=1000000, nRuns=1, useAlgorithms='ArtificialBeeColonyAlgorithm', useBenchmarks='Ackley', **kwargs):
r"""Initialize Runner.
Args:
D (int): Dimension of problem
nFES (int): Number of function evaluations
nRuns (int): Number of repetitions
useAlgorithms (list of Algorithm): List of algorithms to run
useBenchmarks (list of Benchmarks): List of benchmarks to run
"""
self.D = D
self.nFES = nFES
self.nRuns = nRuns
self.useAlgorithms = useAlgorithms
self.useBenchmarks = useBenchmarks
self.results = {}
def benchmark_factory(self, name):
r"""Create optimization task.
Args:
name (str): Benchmark name.
Returns:
Task: Optimization task to use.
"""
from NiaPy.task import StoppingTask, OptimizationType
return StoppingTask(D=self.D, nFES=self.nFES, optType=OptimizationType.MINIMIZATION, benchmark=name)
@classmethod
def __create_export_dir(cls):
r"""Create export directory if not already createed."""
if not os.path.exists("export"):
os.makedirs("export")
@classmethod
def __generate_export_name(cls, extension):
r"""Generate export file name.
Args:
extension (str): File format.
Returns:
"""
return "export/" + str(datetime.datetime.now()).replace(":", ".") + "." + extension
def __export_to_log(self):
r"""Print the results to terminal."""
print(self.results)
def __export_to_json(self):
r"""Export the results in the JSON form.
See Also:
* :func:`NiaPy.Runner.__createExportDir`
"""
self.__create_export_dir()
with open(self.__generate_export_name("json"), "w") as outFile:
json.dump(self.results, outFile)
logger.info("Export to JSON completed!")
def __export_to_xlsx(self):
r"""Export the results in the xlsx form.
See Also:
:func:`NiaPy.Runner.__generateExportName`
"""
self.__create_export_dir()
workbook = xlsxwriter.Workbook(self.__generate_export_name("xlsx"))
worksheet = workbook.add_worksheet()
row, col, nRuns = 0, 0, 0
for alg in self.results:
_, col = worksheet.write(row, col, alg), col + 1
for bench in self.results[alg]:
worksheet.write(row, col, bench)
nRuns = len(self.results[alg][bench])
for i in range(len(self.results[alg][bench])):
_, row = worksheet.write(row, col, self.results[alg][bench][i]), row + 1
row, col = row - len(self.results[alg][bench]), col + 1
row, col = row + 1 + nRuns, col - 1 + len(self.results[alg])
workbook.close()
logger.info("Export to XLSX completed!")
def __export_to_latex(self):
r"""Export the results in the form of latex table.
See Also:
:func:`NiaPy.Runner.__createExportDir`
:func:`NiaPy.Runner.__generateExportName`
"""
self.__create_export_dir()
metrics = ["Best", "Median", "Worst", "Mean", "Std."]
def only_upper(s):
return "".join(c for c in s if c.isupper())
with open(self.__generate_export_name("tex"), "a") as outFile:
outFile.write("\\documentclass{article}\n")
outFile.write("\\usepackage[utf8]{inputenc}\n")
outFile.write("\\usepackage{siunitx}\n")
outFile.write("\\sisetup{\n")
outFile.write("round-mode=places,round-precision=3}\n")
outFile.write("\\begin{document}\n")
outFile.write("\\begin{table}[h]\n")
outFile.write("\\centering\n")
begin_tabular = "\\begin{tabular}{cc"
for alg in self.results:
for _i in range(len(self.results[alg])):
begin_tabular += "S"
firstLine = " &"
for benchmark in self.results[alg].keys():
firstLine += " & \\multicolumn{1}{c}{\\textbf{" + benchmark + "}}"
firstLine += " \\\\"
break
begin_tabular += "}\n"
outFile.write(begin_tabular)
outFile.write("\\hline\n")
outFile.write(firstLine + "\n")
outFile.write("\\hline\n")
for alg in self.results:
for metric in metrics:
line = ""
if metric != "Worst":
line += " & " + metric
else:
shortAlg = ""
if alg.endswith("Algorithm"):
shortAlg = only_upper(alg[:-9])
else:
shortAlg = only_upper(alg)
line += "\\textbf{" + shortAlg + "} & " + metric
for benchmark in self.results[alg]:
if metric == "Best":
line += " & " + str(amin(self.results[alg][benchmark]))
elif metric == "Median":
line += " & " + str(median(self.results[alg][benchmark]))
elif metric == "Worst":
line += " & " + str(amax(self.results[alg][benchmark]))
elif metric == "Mean":
line += " & " + str(mean(self.results[alg][benchmark]))
else:
line += " & " + str(std(self.results[alg][benchmark]))
line += " \\\\"
outFile.write(line + "\n")
outFile.write("\\hline\n")
outFile.write("\\end{tabular}\n")
outFile.write("\\end{table}\n")
outFile.write("\\end{document}")
logger.info("Export to Latex completed!")
def run(self, export="log", verbose=False):
"""Execute runner.
Arguments:
export (str): Takes export type (e.g. log, json, xlsx, latex) (default: "log")
verbose (bool: Switch for verbose logging (default: {False})
Raises:
TypeError: Raises TypeError if export type is not supported
Returns:
dict: Returns dictionary of results
See Also:
* :func:`NiaPy.Runner.useAlgorithms`
* :func:`NiaPy.Runner.useBenchmarks`
* :func:`NiaPy.Runner.__algorithmFactory`
"""
for alg in self.useAlgorithms:
if not isinstance(alg, "".__class__):
alg_name = str(type(alg).__name__)
else:
alg_name = alg
self.results[alg_name] = {}
if verbose:
logger.info("Running %s...", alg_name)
for bench in self.useBenchmarks:
if not isinstance(bench, "".__class__):
bench_name = str(type(bench).__name__)
else:
bench_name = bench
if verbose:
logger.info("Running %s algorithm on %s benchmark...", alg_name, bench_name)
benchmark_stopping_task = self.benchmark_factory(bench)
self.results[alg_name][bench_name] = []
for _ in range(self.nRuns):
algorithm = AlgorithmUtility().get_algorithm(alg)
self.results[alg_name][bench_name].append(algorithm.run(benchmark_stopping_task))
if verbose:
logger.info("---------------------------------------------------")
if export == "log":
self.__export_to_log()
elif export == "json":
self.__export_to_json()
elif export == "xlsx":
self.__export_to_xlsx()
elif export == "latex":
self.__export_to_latex()
else:
raise TypeError("Passed export type is not supported!")