Passed
Pull Request — master (#25)
by Grega
02:55 queued 01:24
created

Runner   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

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