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

Rastrigin.function()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 1
cc 3
1
# encoding=utf8
2
# pylint: disable=anomalous-backslash-in-string
3
"""Implementation of Rastrigin benchmark function.
4
5
Date: 2018
6
7
Authors: Lucija Brezočnik and Iztok Fister Jr.
8
9
License: MIT
10
11
Function: Rastrigin 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 ∈ [-5.12, 5.12], 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) = 10D + \sum_{i=1}^D (x_i^2 -10cos(2\pi x_i))$
22
    Equation: \begin{equation}
23
              f(x) = 10D + \sum_{i=1}^D (x_i^2 -10cos(2\pi x_i))
24
              \end{equation}
25
    Domain: $-5.12 \leq x_i \leq 5.12$
26
27
Reference: https://www.sfu.ca/~ssurjano/rastr.html
28
"""
29
30
import math
31
32
__all__ = ['Rastrigin']
33
34
35
class Rastrigin(object):
36
37
    def __init__(self, Lower=-5.12, Upper=5.12):
38
        self.Lower = Lower
39
        self.Upper = Upper
40
41
    @classmethod
42
    def function(cls):
43
        def evaluate(D, sol):
44
45
            val = 0.0
46
47
            for i in range(D):
48
                val += math.pow(sol[i], 2) - (10 * math.cos(2 * math.pi * sol[i]))
49
50
            return 10 * D + val
51
52
        return evaluate
53