Passed
Pull Request — master (#23)
by
unknown
01:10
created

Ackley.function()   B

Complexity

Conditions 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 24
rs 8.9713
cc 3
1
"""Implementation of Ackley function.
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \; was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \l was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \s was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \e was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

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