Passed
Pull Request — master (#25)
by Grega
01:09
created

Runner   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 13
Bugs 4 Features 4
Metric Value
c 13
b 4
f 4
dl 0
loc 83
rs 10
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
C __algorithmFactory() 0 24 7
D run() 0 30 8
A __init__() 0 22 1
A __exportToLog() 0 2 1
1
from __future__ import print_function
2
3
import logging
4
from NiaPy import algorithms, benchmarks
5
6
__all__ = ['algorithms', 'benchmarks']
7
__project__ = 'NiaPy'
8
__version__ = '0.0.0'
9
10
VERSION = "{0} v{1}".format(__project__, __version__)
11
12
logging.basicConfig()
13
logger = logging.getLogger('NiaPy')
14
logger.setLevel('INFO')
15
16
17
class Runner(object):
18
    # pylint: disable=too-many-instance-attributes, too-many-locals
19
    def __init__(self, D, NP, nFES, nRuns, useAlgorithms, useBenchmarks,
20
                 A=0.5, r=0.5, Qmin=0.0, Qmax=2.0, F=0.5, CR=0.9, alpha=0.5,
21
                 betamin=0.2, gamma=1.0, p=0.5, Lower=-5, Upper=5):
22
        self.D = D
23
        self.NP = NP
24
        self.nFES = nFES
25
        self.nRuns = nRuns
26
        self.useAlgorithms = useAlgorithms
27
        self.useBenchmarks = useBenchmarks
28
        self.A = A
29
        self.r = r
30
        self.Qmin = Qmin
31
        self.Qmax = Qmax
32
        self.F = F
33
        self.CR = CR
34
        self.alpha = alpha
35
        self.betamin = betamin
36
        self.gamma = gamma
37
        self.p = p
38
        self.Lower = Lower
39
        self.Upper = Upper
40
        self.results = {}
41
42
    def __algorithmFactory(self, name, benchmark):
43
        bench = benchmarks.utility.Utility().get_benchmark(
44
            benchmark, self.Lower, self.Upper)
45
46
        if name == 'BatAlgorithm':
47
            return algorithms.basic.BatAlgorithm(
48
                self.D, self.NP, self.nFES, self.A, self.r, self.Qmin, self.Qmax, bench)
49
        elif name == 'DifferentialEvolutionAlgorithm':
50
            return algorithms.basic.DifferentialEvolutionAlgorithm(
51
                self.D, self.NP, self.nFES, self.F, self.CR, bench)
52
        elif name == 'FireflyAlgorithm':
53
            return algorithms.basic.FireflyAlgorithm(
54
                self.D, self.NP, self.nFES, self.alpha, self.betamin, self.gamma, bench)
55
        elif name == 'FlowerPollinationAlgorithm':
56
            return algorithms.basic.FlowerPollinationAlgorithm(
57
                self.D, self.NP, self.nFES, self.p, bench)
58
        elif name == 'GreyWolfOptimizer':
59
            return algorithms.basic.GreyWolfOptimizer(
60
                self.D, self.NP, self.nFES, bench)
61
        elif name == 'HybridBatAlgorithm':
62
            return algorithms.modified.HybridBatAlgorithm(
63
                self.D, self.NP, self.nFES, self.A, self.r, self.F, self.CR, self.Qmin, self.Qmax, bench)
64
        else:
65
            raise TypeError('Passed benchmark is not defined!')
66
67
    def __exportToLog(self):
68
        print(self.results)
69
70
    def run(self, export='log'):
71
        for alg in self.useAlgorithms:
72
            self.results[alg] = {}
73
            for bench in self.useBenchmarks:
74
                benchName = ''
75
                # check if passed benchmark is class
76
                if not isinstance(bench, ''.__class__):
77
                    # set class name as benchmark name
78
                    benchName = str(type(bench).__name__)
79
                else:
80
                    benchName = bench
81
82
                self.results[alg][benchName] = []
83
84
                for _i in range(self.nRuns):
85
                    algorithm = self.__algorithmFactory(alg, bench)
86
                    self.results[alg][benchName].append(algorithm.run())
87
88
        if export == 'log':
89
            self.__exportToLog()
90
        elif export == 'xls':
91
            # TODO: implement export to xls
92
            pass
93
        elif export == 'latex':
94
            # TODO: implement export to latex
95
            pass
96
        else:
97
            raise TypeError('Passed export type is not supported!')
98
99
        return self.results
100