-
Notifications
You must be signed in to change notification settings - Fork 0
/
problems.py
157 lines (125 loc) · 4.27 KB
/
problems.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import numpy as np
def sphere(x):
return np.sum(x**2)
class NoisySphere:
def __init__(self, dim, seed, noisevar):
self.dim = dim
self.seed = seed
np.random.seed(seed)
self.noisevar = noisevar
def __call__(self, x):
return sphere(x) + np.random.normal(0.0, np.sqrt(self.noisevar))
def truefval(self, x):
return sphere(x)
def ellipsoid(x):
n = len(x)
if len(x) < 2:
raise ValueError("dimension must be greater one")
Dell = np.diag([10 ** (3 * i / (n - 1)) for i in range(n)])
return sphere(Dell @ x)
class NoisyEllipsoid:
def __init__(self, dim, seed, noisevar):
self.dim = dim
self.seed = seed
np.random.seed(seed)
self.noisevar = noisevar
def __call__(self, x):
return ellipsoid(x) + np.random.normal(0.0, np.sqrt(self.noisevar))
def truefval(self, x):
return ellipsoid(x)
def rosenbrockchain(x):
if len(x) < 2:
raise ValueError("dimension must be greater one")
# return np.sum([100*(x[i+1] - x[i]**2)**2 + (x[i] - 1)**2 for i in range(self.n-1)])
return np.sum(100.0 * (x[1:] - x[:-1] ** 2) ** 2 + (1 - x[:-1]) ** 2)
def rastrigin(x):
n = len(x)
if n < 2:
raise ValueError("dimension must be greater one")
return 10 * n + sum(x**2 - 10 * np.cos(2 * np.pi * x))
class NoisyRastrigin:
def __init__(self, dim, seed, noisevar):
self.dim = dim
self.seed = seed
np.random.seed(seed)
self.noisevar = noisevar
def __call__(self, x):
return rastrigin(x)[0] + np.random.normal(0.0, np.sqrt(self.noisevar))
def truefval(self, x):
return rastrigin(x)[0]
def ackley(x):
n = len(x)
f_value = 20.0
tmp1 = np.sum([pow(x[i], 2.0) for i in range(n)])
tmp2 = np.sum([np.cos(2.0 * np.pi * x[i]) for i in range(n)])
f_value -= 20.0 * np.exp(-0.2 * np.sqrt(tmp1 / n))
f_value += np.exp(1.0)
f_value -= np.exp(tmp2 / n)
# additional bound
# see: Hansen(PPSN'04), Evaluating the CMA Evolution Strategy on Multimodal Test Functions
bound_deviation = np.array([np.abs(x[i]) - 30 for i in range(n)])
bound_deviation = np.where(bound_deviation > 0.0, 1.0, 0.0)
penalty = (10**4) * np.sum(bound_deviation * np.square(x))
return f_value + penalty
def bohachevsky(x):
n = len(x)
f_value = 0.0
for i in range(n - 1):
f_value += pow(x[i], 2.0)
f_value += 2 * pow(x[i + 1], 2.0)
f_value -= 0.3 * np.cos(3 * np.pi * x[i])
f_value -= 0.4 * np.cos(4 * np.pi * x[i + 1])
f_value += 0.7
return f_value
def schaffer(x):
n = len(x)
return np.sum(
[
pow(pow(x[i], 2.0) + pow(x[i + 1], 2.0), 0.25)
* (
pow(np.sin(50 * pow(pow(x[i], 2.0) + pow(x[i + 1], 2.0), 0.1)), 2.0)
+ 1.0
)
for i in range(n - 1)
]
)
def griewank(x):
n = len(x)
term_one = np.sum(x**2) / 4000.0
term_two = np.prod([np.cos(x[i] / np.sqrt(i + 1)) for i in range(n)])
return term_one - term_two + 1.0
def get_problem(problem, dim=None, seed=None, noisevar=None):
if problem == "sphere":
obj_func = sphere
elif problem == "ellipsoid":
obj_func = ellipsoid
elif problem == "rosen":
obj_func = rosenbrockchain
elif problem == "rastrigin":
obj_func = rastrigin
elif problem == "ackley":
obj_func = ackley
elif problem == "bohachevsky":
obj_func = bohachevsky
elif problem == "schaffer":
obj_func = schaffer
elif problem == "griewank":
obj_func = griewank
elif problem == "noisysphere":
assert dim is not None
assert seed is not None
assert noisevar is not None
obj_func = NoisySphere(dim=dim, seed=seed, noisevar=noisevar)
elif problem == "noisyellipsoid":
assert dim is not None
assert seed is not None
assert noisevar is not None
obj_func = NoisyEllipsoid(dim=dim, seed=seed, noisevar=noisevar)
elif problem == "noisyrastrigin":
assert dim is not None
assert seed is not None
assert noisevar is not None
obj_func = NoisyRastrigin(dim=dim, seed=seed, noisevar=noisevar)
else:
raise NotImplementedError
return obj_func