Passed
Pull Request — master (#23)
by Grega
01:00
created

Ackley   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 30
rs 10
wmc 4
1
# pylint: disable=anomalous-backslash-in-string
2
"""Implementation of Ackley function.
3
4
Date: 2018
5
6
Author: Lucija Brezočnik
7
8
License: MIT
9
10
Function: Ackley function
11
12
Input domain:
13
    The function can be defined on any input domain but it is usually
14
    evaluated on the hypercube x_i ∈ [-32.768, 32.768], for all i = 1, 2,..., D.
15
16
Global minimum:
17
    f(x*) = 0, at x* = (0,...,0)
18
19
LaTeX formats:
20
    Inline: $f(x) = -a\;exp\left(-b \sqrt{\frac{1}{D}
21
             \sum_{i=1}^D x_i^2}\right) - exp\left(\frac{1}{D}
22
             \sum_{i=1}^D cos(c\;x_i)\right) + a + exp(1)$
23
    Equation:  \begin{equation}f(x) =
24
               -a\;exp\left(-b \sqrt{\frac{1}{D} \sum_{i=1}^D x_i^2}\right) -
25
               exp\left(\frac{1}{D} \sum_{i=1}^D cos(c\;x_i)\right) +
26
               a + exp(1) \end{equation}
27
    Domain: $-32.768 \leq x_i \leq 32.768$
28
29
Reference: https://www.sfu.ca/~ssurjano/ackley.html
30
"""
31
32
import math
33
34
__all__ = ['Ackley']
35
36
37
class Ackley(object):
38
39
    def __init__(self, Lower=-32.768, Upper=32.768):
40
        self.Lower = Lower
41
        self.Upper = Upper
42
43
    @classmethod
44
    def function(cls):
45
        def evaluate(D, sol):
46
47
            a = 20  # Recommended variable value
48
            b = 0.2  # Recommended variable value
49
            c = 2 * math.pi  # Recommended variable value
50
51
            val = 0.0
52
            val1 = 0.0
53
            val2 = 0.0
54
55
            for i in range(D):
56
                val1 += math.pow(sol[i], 2)
57
                val2 += math.cos(c * sol[i])
58
59
            temp1 = -b * math.sqrt(val1 / D)
60
            temp2 = val2 / D
61
62
            val = -a * math.exp(temp1) - math.exp(temp2) + a + math.exp(1)
63
64
            return val
65
66
        return evaluate
67