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