Passed
Pull Request — master (#206)
by Grega
01:38
created

NiaPy.benchmarks.utility   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 113
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
B Utility.__init__() 0 56 1
A Utility.get_benchmark() 0 17 3
A Utility.__raiseLowerAndUpperNotDefined() 0 10 1
1
"""Implementation of benchmarks utility function."""
2
3
from NiaPy.benchmarks import (
4
    Ackley,
5
    Alpine1,
6
    Alpine2,
7
    BentCigar,
8
    ChungReynolds,
9
    CosineMixture,
10
    Csendes,
11
    Discus,
12
    DixonPrice,
13
    Elliptic,
14
    ExpandedGriewankPlusRosenbrock,
15
    ExpandedSchaffer,
16
    Griewank,
17
    HappyCat,
18
    HGBat,
19
    Infinity,
20
    Katsuura,
21
    Levy,
22
    Michalewichz,
23
    ModifiedSchwefel,
24
    Perm,
25
    Pinter,
26
    Powell,
27
    Qing,
28
    Quintic,
29
    Rastrigin,
30
    Ridge,
31
    Rosenbrock,
32
    Salomon,
33
    SchafferN2,
34
    SchafferN4,
35
    SchumerSteiglitz,
36
    Schwefel,
37
    Schwefel221,
38
    Schwefel222,
39
    Sphere,
40
    Sphere2,
41
    Sphere3,
42
    Step,
43
    Step2,
44
    Step3,
45
    Stepint,
46
    StyblinskiTang,
47
    SumSquares,
48
    Trid,
49
    Weierstrass,
50
    Whitley,
51
    Zakharov,
52
    Benchmark
53
)
54
55
56
class Utility:
57
    r"""Base class with string mappings to benchmarks.
58
59
    Attributes:
60
        classes (Dict[str, Benchmark]): Mapping from stings to benchmark.
61
62
    """
63
64
    def __init__(self):
65
        r"""Initialize benchmarks."""
66
67
        self.benchmark_classes = {
68
            "ackley": Ackley,
69
            "alpine1": Alpine1,
70
            "alpine2": Alpine2,
71
            "bentcigar": BentCigar,
72
            "chungReynolds": ChungReynolds,
73
            "cosinemixture": CosineMixture,
74
            "csendes": Csendes,
75
            "discus": Discus,
76
            "dixonprice": DixonPrice,
77
            "conditionedellptic": Elliptic,
78
            "elliptic": Elliptic,
79
            "expandedgriewankplusrosenbrock": ExpandedGriewankPlusRosenbrock,
80
            "expandedschaffer": ExpandedSchaffer,
81
            "griewank": Griewank,
82
            "happyCat": HappyCat,
83
            "hgbat": HGBat,
84
            "infinity": Infinity,
85
            "katsuura": Katsuura,
86
            "levy": Levy,
87
            "michalewicz": Michalewichz,
88
            "modifiedscwefel": ModifiedSchwefel,
89
            "perm": Perm,
90
            "pinter": Pinter,
91
            "powell": Powell,
92
            "qing": Qing,
93
            "quintic": Quintic,
94
            "rastrigin": Rastrigin,
95
            "ridge": Ridge,
96
            "rosenbrock": Rosenbrock,
97
            "salomon": Salomon,
98
            "schaffer2": SchafferN2,
99
            "schaffer4": SchafferN4,
100
            "schumerSteiglitz": SchumerSteiglitz,
101
            "schwefel": Schwefel,
102
            "schwefel221": Schwefel221,
103
            "schwefel222": Schwefel222,
104
            "sphere": Sphere,
105
            "sphere2": Sphere2,
106
            "sphere3": Sphere3,
107
            "step": Step,
108
            "step2": Step2,
109
            "step3": Step3,
110
            "stepint": Stepint,
111
            "styblinskiTang": StyblinskiTang,
112
            "sumSquares": SumSquares,
113
            "trid": Trid,
114
            "weierstrass": Weierstrass,
115
            "whitley": Whitley,
116
            "zakharov": Zakharov
117
        }
118
119
        self.algorithm_classes = {}
120
121
    def get_benchmark(self, benchmark):
122
        r"""Get the optimization problem.
123
124
        Arguments:
125
            benchmark (Union[str, Benchmark]): String or class that represents the optimization problem.
126
127
        Returns:
128
            Benchmark: Optimization function with limits.
129
130
        """
131
132
        if issubclass(type(benchmark), Benchmark):
133
            return benchmark
134
        elif benchmark in self.benchmark_classes.keys():
135
            return self.benchmark_classes[benchmark]()
136
        else:
137
            raise TypeError("Passed benchmark is not defined!")
138
139
    @classmethod
140
    def __raiseLowerAndUpperNotDefined(cls):
141
        r"""Trow exception if lower and upper bounds are not defined in benchmark.
142
143
        Raises:
144
            TypeError: Type error.
145
146
        """
147
148
        raise TypeError("Upper and Lower value must be defined!")
149