|
1
|
|
|
from __future__ import print_function # for backward compatibility purpose |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import logging |
|
5
|
|
|
import json |
|
6
|
|
|
import datetime |
|
7
|
|
|
from NiaPy import algorithms, benchmarks |
|
8
|
|
|
|
|
9
|
|
|
__all__ = ['algorithms', 'benchmarks'] |
|
10
|
|
|
__project__ = 'NiaPy' |
|
11
|
|
|
__version__ = '0.0.0' |
|
12
|
|
|
|
|
13
|
|
|
VERSION = "{0} v{1}".format(__project__, __version__) |
|
14
|
|
|
|
|
15
|
|
|
logging.basicConfig() |
|
16
|
|
|
logger = logging.getLogger('NiaPy') |
|
17
|
|
|
logger.setLevel('INFO') |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class Runner(object): |
|
21
|
|
|
# pylint: disable=too-many-instance-attributes, too-many-locals |
|
22
|
|
|
def __init__(self, D, NP, nFES, nRuns, useAlgorithms, useBenchmarks, |
|
23
|
|
|
A=0.5, r=0.5, Qmin=0.0, Qmax=2.0, F=0.5, CR=0.9, alpha=0.5, |
|
24
|
|
|
betamin=0.2, gamma=1.0, p=0.5): |
|
25
|
|
|
self.D = D |
|
26
|
|
|
self.NP = NP |
|
27
|
|
|
self.nFES = nFES |
|
28
|
|
|
self.nRuns = nRuns |
|
29
|
|
|
self.useAlgorithms = useAlgorithms |
|
30
|
|
|
self.useBenchmarks = useBenchmarks |
|
31
|
|
|
self.A = A |
|
32
|
|
|
self.r = r |
|
33
|
|
|
self.Qmin = Qmin |
|
34
|
|
|
self.Qmax = Qmax |
|
35
|
|
|
self.F = F |
|
36
|
|
|
self.CR = CR |
|
37
|
|
|
self.alpha = alpha |
|
38
|
|
|
self.betamin = betamin |
|
39
|
|
|
self.gamma = gamma |
|
40
|
|
|
self.p = p |
|
41
|
|
|
self.results = {} |
|
42
|
|
|
|
|
43
|
|
|
def __algorithmFactory(self, name, benchmark): |
|
44
|
|
|
bench = benchmarks.utility.Utility().get_benchmark(benchmark) |
|
45
|
|
|
algorithm = None |
|
46
|
|
|
|
|
47
|
|
|
if name == 'BatAlgorithm': |
|
48
|
|
|
algorithm = algorithms.basic.BatAlgorithm( |
|
49
|
|
|
self.D, self.NP, self.nFES, self.A, self.r, self.Qmin, self.Qmax, bench) |
|
50
|
|
|
elif name == 'DifferentialEvolutionAlgorithm': |
|
51
|
|
|
algorithm = algorithms.basic.DifferentialEvolutionAlgorithm( |
|
52
|
|
|
self.D, self.NP, self.nFES, self.F, self.CR, bench) |
|
53
|
|
|
elif name == 'FireflyAlgorithm': |
|
54
|
|
|
algorithm = algorithms.basic.FireflyAlgorithm( |
|
55
|
|
|
self.D, self.NP, self.nFES, self.alpha, self.betamin, self.gamma, bench) |
|
56
|
|
|
elif name == 'FlowerPollinationAlgorithm': |
|
57
|
|
|
algorithm = algorithms.basic.FlowerPollinationAlgorithm( |
|
58
|
|
|
self.D, self.NP, self.nFES, self.p, bench) |
|
59
|
|
|
elif name == 'GreyWolfOptimizer': |
|
60
|
|
|
algorithm = algorithms.basic.GreyWolfOptimizer( |
|
61
|
|
|
self.D, self.NP, self.nFES, bench) |
|
62
|
|
|
elif name == 'ArtificialBeeColonyAlgorithm': |
|
63
|
|
|
algorithm = algorithms.basic.ArtificialBeeColonyAlgorithm(self.D, self.NP, self.nFES, bench) |
|
64
|
|
|
elif name == 'HybridBatAlgorithm': |
|
65
|
|
|
algorithm = algorithms.modified.HybridBatAlgorithm( |
|
66
|
|
|
self.D, self.NP, self.nFES, self.A, self.r, self.F, self.CR, self.Qmin, self.Qmax, bench) |
|
67
|
|
|
else: |
|
68
|
|
|
raise TypeError('Passed benchmark is not defined!') |
|
69
|
|
|
|
|
70
|
|
|
return algorithm |
|
71
|
|
|
|
|
72
|
|
|
@classmethod |
|
73
|
|
|
def __createExportDir(cls): |
|
74
|
|
|
if not os.path.exists('export'): |
|
75
|
|
|
os.makedirs('export') |
|
76
|
|
|
|
|
77
|
|
|
def __exportToLog(self): |
|
78
|
|
|
print(self.results) |
|
79
|
|
|
|
|
80
|
|
|
def __exportToJson(self): |
|
81
|
|
|
self.__createExportDir() |
|
82
|
|
|
with open('export/' + str(datetime.datetime.now()) + '.json', 'w') as outFile: |
|
83
|
|
|
json.dump(self.results, outFile) |
|
84
|
|
|
logger.info('Export to JSON completed!') |
|
85
|
|
|
|
|
86
|
|
|
def __exportToXls(self): |
|
87
|
|
|
# TODO: implement export to XLS |
|
88
|
|
|
pass |
|
89
|
|
|
|
|
90
|
|
|
def __exportToLatex(self): |
|
91
|
|
|
# TODO: implement export to Latex |
|
92
|
|
|
pass |
|
93
|
|
|
|
|
94
|
|
|
def run(self, export='log', verbose=False): |
|
95
|
|
|
for alg in self.useAlgorithms: |
|
96
|
|
|
self.results[alg] = {} |
|
97
|
|
|
if verbose: |
|
98
|
|
|
logger.info('Running %s...', alg) |
|
99
|
|
|
for bench in self.useBenchmarks: |
|
100
|
|
|
benchName = '' |
|
101
|
|
|
# check if passed benchmark is class |
|
102
|
|
|
if not isinstance(bench, ''.__class__): |
|
103
|
|
|
# set class name as benchmark name |
|
104
|
|
|
benchName = str(type(bench).__name__) |
|
105
|
|
|
else: |
|
106
|
|
|
benchName = bench |
|
107
|
|
|
|
|
108
|
|
|
if verbose: |
|
109
|
|
|
logger.info('Running %s algorithm on %s benchmark...', alg, benchName) |
|
110
|
|
|
|
|
111
|
|
|
self.results[alg][benchName] = [] |
|
112
|
|
|
|
|
113
|
|
|
for _i in range(self.nRuns): |
|
114
|
|
|
algorithm = self.__algorithmFactory(alg, bench) |
|
115
|
|
|
self.results[alg][benchName].append(algorithm.run()) |
|
116
|
|
|
|
|
117
|
|
|
if verbose: |
|
118
|
|
|
logger.info('---------------------------------------------------') |
|
119
|
|
|
|
|
120
|
|
|
if export == 'log': |
|
121
|
|
|
self.__exportToLog() |
|
122
|
|
|
elif export == 'json': |
|
123
|
|
|
self.__exportToJson() |
|
124
|
|
|
elif export == 'xls': |
|
125
|
|
|
self.__exportToXls() |
|
126
|
|
|
elif export == 'latex': |
|
127
|
|
|
self.__exportToLatex() |
|
128
|
|
|
else: |
|
129
|
|
|
raise TypeError('Passed export type is not supported!') |
|
130
|
|
|
|
|
131
|
|
|
return self.results |
|
132
|
|
|
|