NiaPy.benchmarks.rastrigin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 91
Duplicated Lines 87.91 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 80
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Rastrigin.function() 24 24 2
A Rastrigin.latex_code() 8 8 1
A Rastrigin.__init__() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# encoding=utf8
2
3
"""Implementaion of Rastrigin function."""
4
5
import math
6
from NiaPy.benchmarks.benchmark import Benchmark
7
8
__all__ = ['Rastrigin']
9
10
11 View Code Duplication
class Rastrigin(Benchmark):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
    r"""Implementation of Rastrigin benchmark function.
13
14
    Date: 2018
15
16
    Authors: Lucija Brezočnik and Iztok Fister Jr.
17
18
    License: MIT
19
20
    Function: **Rastrigin function**
21
22
        :math:`f(\mathbf{x}) = 10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)`
23
24
        **Input domain:**
25
        The function can be defined on any input domain but it is usually
26
        evaluated on the hypercube :math:`x_i ∈ [-5.12, 5.12]`, for all :math:`i = 1, 2,..., D`.
27
28
        **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (0,...,0)`
29
30
    LaTeX formats:
31
        Inline:
32
                $f(\mathbf{x}) = 10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)$
33
34
        Equation:
35
                \begin{equation} f(\mathbf{x}) =
36
                10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)
37
                \end{equation}
38
39
        Domain:
40
                $-5.12 \leq x_i \leq 5.12$
41
42
    Reference: https://www.sfu.ca/~ssurjano/rastr.html
43
    """
44
    Name = ['Rastrigin']
45
46
    def __init__(self, Lower=-5.12, Upper=5.12):
47
        r"""Initialize of Rastrigni benchmark.
48
49
        Args:
50
            Lower (Optional[float]): Lower bound of problem.
51
            Upper (Optional[float]): Upper bound of problem.
52
53
        See Also:
54
            :func:`NiaPy.benchmarks.Benchmark.__init__`
55
        """
56
        Benchmark.__init__(self, Lower, Upper)
57
58
    @staticmethod
59
    def latex_code():
60
        r"""Return the latex code of the problem.
61
62
        Returns:
63
            str: Latex code
64
        """
65
        return r'''$f(\mathbf{x}) = 10D + \sum_{i=1}^D \left(x_i^2 -10\cos(2\pi x_i)\right)$'''
66
67
    def function(self):
68
        r"""Return benchmark evaluation function.
69
70
        Returns:
71
            Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
72
        """
73
        def evaluate(D, sol):
74
            r"""Fitness function.
75
76
            Args:
77
                D (int): Dimensionality of the problem
78
                sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
79
80
            Returns:
81
                float: Fitness value for the solution.
82
            """
83
            val = 0.0
84
85
            for i in range(D):
86
                val += math.pow(sol[i], 2) - (10.0 * math.cos(2 * math.pi * sol[i]))
87
88
            return 10 * D + val
89
90
        return evaluate
91