NiaPy.benchmarks.ackley   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 27
dl 0
loc 110
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Ackley.function() 0 36 2
A Ackley.latex_code() 0 8 1
A Ackley.__init__() 0 11 1
1
# encoding=utf8
2
3
"""Implementation of Ackley benchmark."""
4
5
from numpy import exp, pi, cos, sqrt
6
7
from NiaPy.benchmarks.benchmark import Benchmark
8
9
__all__ = ['Ackley']
10
11
class Ackley(Benchmark):
12
    r"""Implementation of Ackley function.
13
14
    Date: 2018
15
16
    Author: Lucija Brezočnik
17
18
    License: MIT
19
20
    Function: **Ackley function**
21
22
        :math:`f(\mathbf{x}) = -a\;\exp\left(-b \sqrt{\frac{1}{D}\sum_{i=1}^D x_i^2}\right)
23
        - \exp\left(\frac{1}{D}\sum_{i=1}^D \cos(c\;x_i)\right) + a + \exp(1)`
24
25
        **Input domain:**
26
        The function can be defined on any input domain but it is usually
27
        evaluated on the hypercube :math:`x_i ∈ [-32.768, 32.768]`, for all :math:`i = 1, 2,..., D`.
28
29
        **Global minimum:** :math:`f(\textbf{x}^*) = 0`, at  :math:`x^* = (0,...,0)`
30
31
    LaTeX formats:
32
        Inline:
33
                $f(\mathbf{x}) = -a\;\exp\left(-b \sqrt{\frac{1}{D}
34
                \sum_{i=1}^D x_i^2}\right) - \exp\left(\frac{1}{D}
35
                \sum_{i=1}^D cos(c\;x_i)\right) + a + \exp(1)$
36
37
        Equation:
38
                \begin{equation}f(\mathbf{x}) =
39
                -a\;\exp\left(-b \sqrt{\frac{1}{D} \sum_{i=1}^D x_i^2}\right) -
40
                \exp\left(\frac{1}{D} \sum_{i=1}^D \cos(c\;x_i)\right) +
41
                a + \exp(1) \end{equation}
42
43
        Domain:
44
                $-32.768 \leq x_i \leq 32.768$
45
46
    Reference:
47
        https://www.sfu.ca/~ssurjano/ackley.html
48
    """
49
    Name = ['Ackley']
50
51
    def __init__(self, Lower=-32.768, Upper=32.768):
52
        r"""Initialize of Ackley benchmark.
53
54
        Args:
55
            Lower (Optional[float]): Lower bound of problem.
56
            Upper (Optional[float]): Upper bound of problem.
57
58
        See Also:
59
            :func:`NiaPy.benchmarks.Benchmark.__init__`
60
        """
61
        Benchmark.__init__(self, Lower, Upper)
62
63
    @staticmethod
64
    def latex_code():
65
        r"""Return the latex code of the problem.
66
67
        Returns:
68
            str: Latex code
69
        """
70
        return r'''$f(\mathbf{x}) = -a\;\exp\left(-b \sqrt{\frac{1}{D}
71
                \sum_{i=1}^D x_i^2}\right) - \exp\left(\frac{1}{D}
72
                \sum_{i=1}^D \cos(c\;x_i)\right) + a + \exp(1)$'''
73
74
    def function(slef):
75
        r"""Return benchmark evaluation function.
76
77
        Returns:
78
            Callable[[int, Union[int, float, List[int, float], numpy.ndarray]], float]: Fitness function
79
        """
80
        def evaluate(D, sol):
81
            r"""Fitness function.
82
83
            Args:
84
                D (int): Dimensionality of the problem
85
                sol (Union[int, float, List[int, float], numpy.ndarray]): Solution to check.
86
87
            Returns:
88
                float: Fitness value for the solution.
89
            """
90
            a = 20  # Recommended variable value
91
            b = 0.2  # Recommended variable value
92
            c = 2 * pi  # Recommended variable value
93
94
            val = 0.0
95
            val1 = 0.0
96
            val2 = 0.0
97
98
            for i in range(D):
99
                val1 += sol[i] ** 2
100
                val2 += cos(c * sol[i])
101
102
            temp1 = -b * sqrt(val1 / D)
103
            temp2 = val2 / D
104
105
            val = -a * exp(temp1) - exp(temp2) + a + exp(1)
106
107
            return val
108
109
        return evaluate
110