-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
392 lines (289 loc) · 11.1 KB
/
functions.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# source : https://github.com/P-N-Suganthan/CEC2017-BoundContrained/blob/master/Definitions%20of%20%20CEC2017%20benchmark%20suite%20final%20version%20updated.pdf
import numpy as np
class Ackley: ## verified
name = "Ackley"
separable = True
def __init__(self, d, a=20, b=0.2, c=2 * np.pi):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
self.a = a
self.b = b
self.c = c
def get_param(self):
return {"a": self.a, "b": self.b, "c": self.c}
def get_global_minimum(self):
X = np.array([0 for _ in range(d)])
return (X, self(X))
def __call__(self, X):
res = -self.a * np.exp(-self.b * np.sqrt(np.mean(X**2)))
res = res - np.exp(np.mean(np.cos(self.c * X))) + self.a + np.exp(1)
return res
class Rosenbrock: # verified
name = "Rosenbrock"
separable = False
def __init__(self, d, a=1, b=100):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
self.a = a
self.b = b
def get_param(self):
return {"a": self.a, "b": self.b}
def get_global_minimum(self):
X = np.array([1 for _ in range(self.d)])
return (X, self(X))
def __call__(self, X):
return np.sum(self.b * (X[1:] - X[:-1] ** 2) ** 2 + (self.a - X[:-1]) ** 2)
class Rastrigin: # verified
name = "Rastrigin"
separable = True
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([0 for _ in range(self.d)])
return (X, self(X))
def __call__(self, X): return 10 * self.d + np.sum(X**2 - 10 * np.cos(2 * np.pi * X))
class PermZeroDBeta: # verified
name = "Perm 0, d, beta"
separable = False
def __init__(self, d, beta=10):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
self.beta = beta
def get_param(self):
return {"beta": self.beta}
def get_global_minimum(self):
X = np.array([1 / (i + 1) for i in range(self.d)])
return (X, self(X))
def __call__(self, X):
j = np.arange(1, self.d + 1)
res = np.sum(
[
np.sum((j + self.beta) * (X**i - 1/j**i)) ** 2 for i in range(1, self.d + 1)
]
)
return res
class Zakharov: ### Verified
name = 'Zakharov'
separable = False
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([0 for i in range(self.d)])
return (X, self(X))
def __call__(self, X):
i = np.arange(1, self.d + 1)
return np.sum(X**2) + np.sum(0.5 * i * X) ** 2 + np.sum(0.5 * i * X) ** 4
class Schwefel: # verified
name = 'Schwefel'
separable = True
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([420.9687 for i in range(self.d)])
return (X, self(X))
def __call__(self, X): return 418.9829*self.d - np.sum(X*np.sin(np.sqrt(np.abs(X))))
class Modified_Schwefel: #### Verified
'''same as Schwefel function when evaluating on [-100,100]^d'''
name = 'Modified Schwefel'
separable = True
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([0 for i in range(self.d)])
return (X, self(X))
def __call__(self, X):
def g(z):
if np.abs(z) <= 500:
return z*np.sin(np.sqrt(np.abs(z)))
elif z > 500:
return (500-z%500) * np.sin(np.sqrt(np.abs(500-z%500))) - (z-500)**2/(10000*self.d)
elif z < -500:
return (z%500-500) * np.sin(np.sqrt(np.abs(z%500-500))) - (z+500)**2/(10000*self.d)
Z = X + 420.9687462275036
return 418.9829*self.d - np.sum([g(z) for z in Z])
# source : http://www5.zzu.edu.cn/__local/A/69/BC/D3B5DFE94CD2574B38AD7CD1D12_C802DAFE_BC0C0.pdf
class Bent_Cigar: # verified
name = 'Bent Cigar'
separable = True
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([420.968746 for i in range(self.d)])
return (X, self(X))
def __call__(self, X): return X[0]**2 * 10**6 * np.sum(X[1:]**2)
class Expanded_Schaffer_f6: # verified
name = 'Expanded Scaffer f6'
separable = True
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([0 for i in range(self.d)])
return (X, self(X))
def __call__(self, x):
x_next = np.roll(x, -1)
tmp = x ** 2 + x_next ** 2
val = 0.5 + (np.sin(np.sqrt(tmp)) ** 2 - 0.5) / (1 + 0.001 * tmp) ** 2
return np.sum(val)
class Levy: # verified
name = 'Levy'
separable = False
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([1 for i in range(self.d)])
return (X, self(X))
def __call__(self, X):
W = 1 + (X-1)/4
return np.sin(np.pi*W[0])**2 + np.sum((W[:-1]-1)**2 * (1+10*np.sin(np.pi*W[:-1]+1)**2)) + (W[-1]-1)**2 * (1+np.sin(2*np.pi*W[-1])**2)
class High_Conditioned_Elliptic: # verified
name = 'High Conditioned Elliptic'
separable = True
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([420.968746 for i in range(self.d)])
return (X, self(X))
def __call__(self, X): return np.sum((10**6) ** (np.arange(self.d) / (self.d - 1)) * X**2)
class Discus: # verified
name = 'Discus'
separable = True
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([420.968746 for i in range(self.d)])
return (X, self(X))
def __call__(self, X): return 10**6 * X[0]**2 + np.sum(X[1:]**2)
class Weierstrass: ################### VERIFIED
name = 'Weierstrass'
separable = False
def __init__(self, d, a=0.5, b=3, kmax=20):
self.d = d
self.a = a
self.b = b
self.kmax = kmax
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {'a': self.a, 'b': self.b, 'kmax': self.kmax}
def get_global_minimum(self):
X = np.array([420.968746 for i in range(self.d)])
return (X, self(X))
def __call__(self, X):
return np.sum(np.sum([self.a**k * np.cos(2*np.pi*self.b**k*(X+0.5)) for k in range(self.kmax+1)])) - self.d * np.sum([self.a**k * np.cos(2*np.pi*0.5) for k in range(self.kmax+1)])
class Griewank: # verified
name = 'Griewank'
separable = False
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([0 for i in range(self.d)])
return (X, self(X))
def __call__(self, X): return np.sum(X**2)/4000 - np.prod([np.cos(X[i]/np.sqrt(i+1)+1) for i in range(self.d)])
# source : https://niapy.org/en/stable/_modules/niapy/problems/katsuura.html
class Katsuura: # verified
name = 'Katsuura'
separable = False
def __init__(self, d):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([420.968746 for i in range(self.d)])
return (X, self(X))
def __call__(self, X):
val = 1.0
for i in range(self.d):
val_t = 1.0
for j in range(1, 33):
val_t += np.abs(2 ** j * X[i] - round(2 ** j * X[i])) / 2 ** j
val *= (1 + (i + 1) * val_t) ** (10 / self.d ** 1.2) - (10 / self.d ** 2)
return 10 / self.d ** 2 * val
class Happy_Cat: # verified
name = 'Happy Cat'
separable = False
def __init__(self, d, alpha=0.25):
self.d = d
self.alpha = alpha
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {'alpha': self.alpha}
def get_global_minimum(self):
X = np.array([420.968746 for i in range(self.d)])
return (X, self(X))
def __call__(self, X): return np.abs(np.sum(X**2 - self.d))**self.alpha + 0.5*np.sum((X**2 + X))/self.d + 0.5
class HGBat:
name = 'HGBat'
separable = False
def __init__(self, d, alpha=0.25):
self.d = d
self.alpha = alpha
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {'alpha': self.alpha}
def get_global_minimum(self):
X = np.array([420.968746 for i in range(self.d)])
return (X, self(X))
def __call__(self, X): return np.abs(np.sum(X**2)**2 - np.sum(X)**2)**0.5 + 0.5*np.sum((X**2 + X))/self.d + 0.5
class Lunacek_bi_Rastrigin:
name = 'Lunacek-bi-Rastrigin'
separable = False
def __init__(self, d, a=0.5, b=2.0, h=1.0):
self.d = d
self.a = a
self.b = b
self.h = h
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {'a': self.a, 'b': self.b, 'd': self.d}
def get_global_minimum(self):
X = np.array([0 for i in range(self.d)]) # UNKNOWN
return (X, self(X))
def __call__(self, X):
s1 = np.sum((X-self.a)**2 - 10*np.cos(np.pi*(X-self.a)))
s2 = np.sum((X-self.a)**2 - 10*np.cos(np.pi*(X+self.a)))
return self.h*np.abs(s1-s2) + self.b*np.sqrt(self.d) + 10*self.d
class expanded_griewank_plus_rosenbrock:
name = 'expanded Griewank plus Rosenbrock'
separable = False
def __init__(self, d, a=0.5, b=2.0, h=1.0):
self.d = d
self.input_domain = np.array([[-100, 100] for _ in range(d)])
def get_param(self):
return {}
def get_global_minimum(self):
X = np.array([0 for i in range(self.d)]) # UNKNOWN
return (X, self(X))
def __call__(self, X):
f_7 = Lunacek_bi_Rastrigin(1)
f_4 = Rosenbrock(2)
return np.sum([f_7(f_4(np.array([X[i],X[i+1]]))) for i in range(self.d-2)]) + f_7(f_4(np.array([X[-1],X[0]])))