/
Rastrigin.py
86 lines (68 loc) · 2.07 KB
/
Rastrigin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""Implmentation of Rastrigin
"""
# Author: Wenjie Li <li3549@purdue.edu>
# License: MIT
import numpy as np
from PyXAB.synthetic_obj.Objective import Objective
class Rastrigin(Objective):
"""
Rastrigin objective implementation, with the domain [-1, 1]^p
"""
def __init__(self):
"""
Initialization with fmax = 0
"""
self.fmax = 0
def f(self, x):
"""
Evaluation of the chosen point in Rastrigin function
Parameters
----------
x: list
one input point in the form of x = [x1, x2,...,xp]
Returns
-------
y: float
Evaluated value of the function at the particular point x = [x1, x2,...,xp], returns
\sum_i^p - 10 - (x[i] ** 2 - 10 * cos(2 * \pi * x[i]))
"""
x = np.array(x)
S = 0
for i in range(x.size):
S = S - 10 - (x[i] ** 2 - 10 * np.cos(2 * np.pi * x[i]))
return S
class Rastrigin_Normalized(Objective):
"""
Normalized Rastrigin objective implementation, with the domain [-1, 1]^p
"""
def __init__(self, k=20):
"""
Initialization with fmax = 0, normalization constant k
Parameters
----------
k: float
Normalization constant k
"""
self.fmax = 0
self.k = k # Normalization constant
def f(self, x):
"""
Evaluation of the chosen point in normalized Rastrigin function
Parameters
----------
x: list
one input point in the form of x = [x1, x2,...,xp]
Returns
-------
y: float
Evaluated value of the function at the particular point x = [x1, x2,...,xp], returns
(1/pk)\sum_i^p - 10 - (x[i] ** 2 - 10 * cos(2 * \pi * x[i]))
"""
x = np.array(x)
S = 0
for i in range(x.size):
S = S - 10 - (x[i] ** 2 - 10 * np.cos(2 * np.pi * x[i]))
S = S / (self.k * x.size)
# Only devided by an upper bound in [-1, 1]^n
return S