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

Utility.get_benchmark()   F

Complexity

Conditions 27

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 27
c 3
b 2
f 0
dl 0
loc 42
rs 2.6181

How to fix   Complexity   

Complexity

Complex classes like Utility.get_benchmark() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""Implementation of benchmarks utility function."""
2
3
from . import Rastrigin, Rosenbrock, Griewank, Sphere, Ackley
4
5
__all__ = ['Utility']
6
7
8
# pylint: disable=too-many-return-statements
9
class Utility:
10
11
    # pylint: disable=too-many-return-statements
12
    @staticmethod
13
    def get_benchmark(benchmark, LowerBound=None, UpperBound=None):
14
        if not isinstance(benchmark, ''.__class__):
15
            return benchmark
16
        else:
17
            if benchmark == 'rastrigin':
18
                if UpperBound is None and LowerBound is None:
19
                    return Rastrigin()
20
                elif UpperBound is not None and LowerBound is not None:
21
                    return Rastrigin(LowerBound, UpperBound)
22
                else:
23
                    raise TypeError('Upper and Lower value must be defined!')
24
            elif benchmark == 'rosenbrock':
25
                if UpperBound is None and LowerBound is None:
26
                    return Rosenbrock()
27
                elif UpperBound is not None and LowerBound is not None:
28
                    return Rosenbrock(LowerBound, UpperBound)
29
                else:
30
                    raise TypeError('Upper and Lower value must be defined!')
31
            elif benchmark == 'griewank':
32
                if UpperBound is None and LowerBound is None:
33
                    return Griewank()
34
                elif UpperBound is not None and LowerBound is not None:
35
                    return Griewank(LowerBound, UpperBound)
36
                else:
37
                    raise TypeError('Upper and Lower value must be defined!')
38
            elif benchmark == 'sphere':
39
                if UpperBound is None and LowerBound is None:
40
                    return Sphere()
41
                elif UpperBound is not None and LowerBound is not None:
42
                    return Sphere(LowerBound, UpperBound)
43
                else:
44
                    raise TypeError('Upper and Lower value must be defined!')
45
            elif benchmark == 'ackley':
46
                if UpperBound is None and LowerBound is None:
47
                    return Ackley()
48
                elif UpperBound is not None and LowerBound is not None:
49
                    return Ackley(LowerBound, UpperBound)
50
                else:
51
                    raise TypeError('Upper and Lower value must be defined!')
52
            else:
53
                raise TypeError('Passed benchmark is not defined!')
54