Passed
Pull Request — master (#233)
by Grega
11:33
created

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