StyblinskiTang.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 3
dl 11
loc 11
rs 10
c 0
b 0
f 0
1
# encoding=utf8
2
3
"""Stybliski Tang benchmark."""
4
5
import math
6
from NiaPy.benchmarks.benchmark import Benchmark
7
8
__all__ = ['StyblinskiTang']
9
10 View Code Duplication
class StyblinskiTang(Benchmark):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
11
    r"""Implementation of Styblinski-Tang functions.
12
13
    Date: 2018
14
15
    Authors: Lucija Brezočnik
16
17
    License: MIT
18
19
    Function: **Styblinski-Tang function**
20
21
        :math:`f(\mathbf{x}) = \frac{1}{2} \sum_{i=1}^D \left(
22
        x_i^4 - 16x_i^2 + 5x_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, 5]`, for all :math:`i = 1, 2,..., D`.
27
28
        **Global minimum:** :math:`f(x^*) = -78.332`, at :math:`x^* = (-2.903534,...,-2.903534)`
29
30
    LaTeX formats:
31
        Inline:
32
                $f(\mathbf{x}) = \frac{1}{2} \sum_{i=1}^D \left(
33
                x_i^4 - 16x_i^2 + 5x_i \right) $
34
35
        Equation:
36
                \begin{equation}f(\mathbf{x}) =
37
                \frac{1}{2} \sum_{i=1}^D \left( x_i^4 - 16x_i^2 + 5x_i \right) \end{equation}
38
39
        Domain:
40
                $-5 \leq x_i \leq 5$
41
42
    Reference paper:
43
        Jamil, M., and Yang, X. S. (2013).
44
        A literature survey of benchmark functions for global optimisation problems.
45
        International Journal of Mathematical Modelling and Numerical Optimisation,
46
        4(2), 150-194.
47
    """
48
    Name = ['StyblinskiTang']
49
50
    def __init__(self, Lower=-5.0, Upper=5.0):
51
        r"""Initialize of Styblinski Tang benchmark.
52
53
        Args:
54
            Lower (Optional[float]): Lower bound of problem.
55
            Upper (Optional[float]): Upper bound of problem.
56
57
        See Also:
58
            :func:`NiaPy.benchmarks.Benchmark.__init__`
59
        """
60
        Benchmark.__init__(self, Lower, Upper)
61
62
    @staticmethod
63
    def latex_code():
64
        r"""Return the latex code of the problem.
65
66
        Returns:
67
            str: Latex code
68
        """
69
        return r'''$f(\mathbf{x}) = \frac{1}{2} \sum_{i=1}^D \left(
70
                x_i^4 - 16x_i^2 + 5x_i \right) $'''
71
72
    def function(self):
73
        r"""Return benchmark evaluation function.
74
75
        Returns:
76
            Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
77
        """
78
        def evaluate(D, sol):
79
            r"""Fitness function.
80
81
            Args:
82
                D (int): Dimensionality of the problem
83
                sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
84
85
            Returns:
86
                float: Fitness value for the solution.
87
            """
88
            val = 0.0
89
90
            for i in range(D):
91
                val += (math.pow(sol[i], 4) - 16.0 * math.pow(sol[i], 2) + 5.0 * sol[i])
92
93
            return 0.5 * val
94
95
        return evaluate
96