Completed
Push — master ( 71dc45...9b6f89 )
by Grega
9s
created

Rastrigin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 19
rs 10
wmc 4
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