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

NiaPy.benchmarks.utility.Utility.__init__()   B

Complexity

Conditions 1

Size

Total Lines 56
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 52
nop 1
dl 0
loc 56
rs 8.5709
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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