NiaPy.benchmarks.pinter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 32
dl 0
loc 126
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Pinter.latex_code() 0 8 1
A Pinter.function() 0 42 4
A Pinter.__init__() 0 11 1
1
# encoding=utf8
2
3
"""Implementation of Pinter function."""
4
5
import math
6
from NiaPy.benchmarks.benchmark import Benchmark
7
8
__all__ = ['Pinter']
9
10
11
class Pinter(Benchmark):
12
    r"""Implementation of Pintér function.
13
14
    Date: 2018
15
16
    Author: Lucija Brezočnik
17
18
    License: MIT
19
20
    Function: **Pintér function**
21
22
        :math:`f(\mathbf{x}) =
23
        \sum_{i=1}^D ix_i^2 + \sum_{i=1}^D 20i \sin^2 A + \sum_{i=1}^D i
24
        \log_{10} (1 + iB^2);`
25
        :math:`A = (x_{i-1}\sin(x_i)+\sin(x_{i+1}))\quad \text{and} \quad`
26
        :math:`B = (x_{i-1}^2 - 2x_i + 3x_{i+1} - \cos(x_i) + 1)`
27
28
        **Input domain:**
29
        The function can be defined on any input domain but it is usually
30
        evaluated on the hypercube :math:`x_i ∈ [-10, 10]`, for all :math:`i = 1, 2,..., D`.
31
32
        **Global minimum:** :math:`f(x^*) = 0`, at :math:`x^* = (0,...,0)`
33
34
    LaTeX formats:
35
        Inline:
36
                $f(\mathbf{x}) =
37
                \sum_{i=1}^D ix_i^2 + \sum_{i=1}^D 20i \sin^2 A + \sum_{i=1}^D i
38
                \log_{10} (1 + iB^2);
39
                A = (x_{i-1}\sin(x_i)+\sin(x_{i+1}))\quad \text{and} \quad
40
                B = (x_{i-1}^2 - 2x_i + 3x_{i+1} - \cos(x_i) + 1)$
41
42
        Equation:
43
                \begin{equation} f(\mathbf{x}) = \sum_{i=1}^D ix_i^2 +
44
                \sum_{i=1}^D 20i \sin^2 A + \sum_{i=1}^D i \log_{10} (1 + iB^2);
45
                A = (x_{i-1}\sin(x_i)+\sin(x_{i+1}))\quad \text{and} \quad
46
                B = (x_{i-1}^2 - 2x_i + 3x_{i+1} - \cos(x_i) + 1) \end{equation}
47
48
        Domain:
49
                $-10 \leq x_i \leq 10$
50
51
    Reference paper:
52
        Jamil, M., and Yang, X. S. (2013).
53
        A literature survey of benchmark functions for global optimisation problems.
54
        International Journal of Mathematical Modelling and Numerical Optimisation,
55
        4(2), 150-194.
56
    """
57
    Name = ['Pinter']
58
59
    def __init__(self, Lower=-10.0, Upper=10.0):
60
        r"""Initialize of Pinter benchmark.
61
62
        Args:
63
            Lower (Optional[float]): Lower bound of problem.
64
            Upper (Optional[float]): Upper bound of problem.
65
66
        See Also:
67
            :func:`NiaPy.benchmarks.Benchmark.__init__`
68
        """
69
        Benchmark.__init__(self, Lower, Upper)
70
71
    @staticmethod
72
    def latex_code():
73
        r"""Return the latex code of the problem.
74
75
        Returns:
76
            str: Latex code
77
        """
78
        return r''' $f(\mathbf{x}) =
79
                \sum_{i=1}^D ix_i^2 + \sum_{i=1}^D 20i \sin^2 A + \sum_{i=1}^D i
80
                \log_{10} (1 + iB^2);
81
                A = (x_{i-1}\sin(x_i)+\sin(x_{i+1}))\quad \text{and} \quad
82
                B = (x_{i-1}^2 - 2x_i + 3x_{i+1} - \cos(x_i) + 1)$'''
83
84
    def function(self):
85
        r"""Return benchmark evaluation function.
86
87
        Returns:
88
            Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
89
        """
90
        def evaluate(D, sol):
91
            r"""Fitness function.
92
93
            Args:
94
                D (int): Dimensionality of the problem
95
                sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
96
97
            Returns:
98
                float: Fitness value for the solution.
99
            """
100
            val1 = 0.0
101
            val2 = 0.0
102
            val3 = 0.0
103
104
            for i in range(D):
105
106
                if i == 0:
107
                    sub = sol[D - 1]
108
                    add = sol[i + 1]
109
                elif i == D - 1:
110
                    sub = sol[i - 1]
111
                    add = sol[0]
112
                else:
113
                    sub = sol[i - 1]
114
                    add = sol[i + 1]
115
116
                A = (sub * math.sin(sol[i]) + math.sin(add))
117
                B = (math.pow(sub, 2) - 2.0 * sol[i] + 3.0 * add - math.cos(sol[i]) + 1.0)
118
119
                val1 += (i + 1.0) * math.pow(sol[i], 2)
120
                val2 += 20.0 * (i + 1.0) * math.pow(math.sin(A), 2)
121
                val3 += (i + 1.0) * math.log10(1.0 + (i + 1.0) * math.pow(B, 2))
122
123
            return val1 + val2 + val3
124
125
        return evaluate
126