-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmark_Functions.py
More file actions
360 lines (266 loc) · 11.6 KB
/
Benchmark_Functions.py
File metadata and controls
360 lines (266 loc) · 11.6 KB
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
import numpy as np
# Benchmark Functions for Optimization Algorithms
def sphere(pos, dimensions):
"""Sphere Function
The Sphere function is a simple benchmark function for optimization.
It is defined as the sum of the squares of its input variables.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 3 to 256 (commonly used: 30)
Common Domain: [-5.12, 5.12]
"""
return sum(x**2 for x in pos)
def rosenbrock(pos, dimensions):
"""Rosenbrock Function
The Rosenbrock function is a non-convex function used as a performance test problem for optimization algorithms.
It is characterized by a narrow, curved valley.
Global Minimum:
f(1, 1, ..., 1) = 0
Dimension Range: 5 to 100 (commonly used: 30)
Common Domain: [-5, 10]"""
res = 0
for i in range(dimensions-1):
res += (pos[i] - 1)**2 + 100 * (pos[i+1] + pos[i]**2)**2
return res
def quartic(pos, dimensions):
"""Quartic Function without Noise
The Quartic function without noise is a benchmark function for optimization that includes a deterministic component.
It is defined as the sum of the fourth powers of its input variables.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 10 to 100 (commonly used: 30)
Common Domain: [-1.28, 1.28]"""
res = 0
for i in range(dimensions):
res += (i + 1) * (pos[i] ** 4)
return res
def step(pos, dimensions):
"""Step Function
The Step function is a simple benchmark function for optimization.
It is defined as the sum of the squares of the ceiling of its input variables.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 5 to 100 (commonly used: 30)
Common Domain: [-100, 100]"""
return sum(int(x + 0.5)**2 for x in pos)
def schwefel_2point22(pos, dimensions):
"""Schwefel 2.22 Function
The Schwefel 2.22 function is a benchmark function for optimization.
It is defined as the sum of the absolute values of its input variables plus the product of the absolute values of its input variables.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 10 to 100 (commonly used: 30)
Common Domain: [-10, 10]"""
sum_abs = sum(abs(x) for x in pos)
prod_abs = 1
for x in pos:
prod_abs *= abs(x)
return sum_abs + prod_abs
def sumsquare(pos, dimensions):
"""Sum Square Function
The Sum Square function is a benchmark function for optimization.
It is defined as the sum of the squares of its input variables multiplied by their respective indices.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 10 to 100 (commonly used: 30)
Common Domain: [-10, 10]"""
res = 0
for i in range(dimensions):
res += (i + 1) * (pos[i] ** 2)
return res
def elliptic(pos, dimensions):
"""Elliptic Function
The Elliptic function is a benchmark function for optimization.
It is defined as the sum of the squares of its input variables, each multiplied by a scaling factor that increases exponentially with the index.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 10 to 100 (commonly used: 30)
Common Domain: [-100, 100]"""
res = 0
for i in range(dimensions):
res += (pos[i] ** 2) * (10**6)**(i / (dimensions - 1))
return res
def rastrigin(pos, dimensions):
"""Rastrigin Function
The Rastrigin function is a non-convex function used as a performance test problem for optimization algorithms.
It is characterized by a large search space and many local minima.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 2 to 100 (commonly used: 30)
Common Domain: [-5.12, 5.12]"""
res = 0
for i in range(dimensions):
res += 10 + pos[i]**2 - 10 * np.cos(2 * np.pi * pos[i])
return res
def griewank(pos, dimensions):
"""Griewank Function
The Griewank function is a complex benchmark function for optimization.
It is defined as the sum of the squares of its input variables divided by 4000 minus the product of the cosines of its input variables divided by the square root of their indices, plus one.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 2 to 100 (commonly used: 30)
Common Domain: [-600, 600]"""
res = sum(x**2 / 4000 for x in pos)
prod = 1
for i, x in enumerate(pos):
prod *= np.cos(x / np.sqrt(i + 1))
return res - prod + 1
def ackley(pos, dimensions):
"""Ackley Function
The Ackley function is a widely used benchmark function for optimization.
It is characterized by a nearly flat outer region and a large hole at the center.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 2 to 100 (commonly used: 30)
Common Domain: [-32, 32]"""
sum1 = sum(x**2 for x in pos)
sum2 = sum(np.cos(2 * np.pi * x) for x in pos)
term1 = -20 * np.exp(-0.2 * np.sqrt(sum1 / dimensions))
term2 = -np.exp(sum2 / dimensions)
return term1 + term2 + 20 + np.e
def michalewicz(pos, dimensions):
"""Michalewicz Function
The Michalewicz function is a challenging benchmark function for optimization.
It is defined as the negative sum of the sine of its input variables multiplied by the sine of the square of its input variables divided by pi, raised to the power of 2m.
Global Minimum:
f(2.20, 1.57, ..., 1.57) ≈ -1.8013 for 2D
f(2.20, 1.57, ..., 1.57) ≈ -4.6876 for 5D
f(2.20, 1.57, ..., 1.57) ≈ -9.6602 for 10D
Dimension Range: 2 to 100 (commonly used: 10)
Common Domain: [0, π]"""
m = 10
res = 0
for i in range(dimensions):
res += np.sin(pos[i]) * (np.sin((i + 1) * pos[i]**2 / np.pi))**(2 * m)
return -res
def penalized1(pos, dimensions):
"""Penalized Function 1
The Penalized Function 1 is a benchmark function for optimization that includes penalty terms for constraint violations.
It is defined as a combination of a base function and penalty terms based on the input variables.
Global Minimum:
f(1, 1, ..., 1) = 0
Dimension Range: 2 to 100 (commonly used: 30)
Common Domain: [-50, 50]"""
k = 100
m = 4
a = 10
def u(x, a, k, m):
if x > a:
return k * (x - a) ** m
elif x < -a:
return k * (-x - a) ** m
else:
return 0
def y(x):
return 1 + (x + 1) / 4
term1 = 10 * np.sin(np.pi * y(pos[0]))**2
term2 = sum((y(pos[i]) - 1)**2 * (1 + 10 * np.sin(np.pi * y(pos[i + 1]))**2) for i in range(dimensions - 1))
term3 = (y(pos[-1]) - 1)**2
penalty = sum(u(pos[i], a, k, m) for i in range(dimensions))
res = (term1 + term2 + term3) * np.pi / dimensions
return res + penalty
def schwefel(pos, dimensions):
"""Schwefel Function
The Schwefel function is a complex benchmark function for optimization.
It is defined as the sum of the negative product of its input variables and the sine of the square root of their absolute values, plus a constant term.
Global Minimum:
f(421, 421, ..., 421) = -418.9829 * dimensions
Dimension Range: 10 to 100 (commonly used: 30)
Common Domain: [-500, 500]"""
return sum(-x * np.sin(np.sqrt(abs(x))) for x in pos)
def weierstrass(pos, dimensions):
"""Weierstrass Function
The Weierstrass function is a continuous but nowhere differentiable function used as a benchmark for optimization algorithms.
It is defined as the sum of cosine terms with exponentially decreasing amplitudes and frequencies.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 2 to 100 (commonly used: 30)
Common Domain: [-0.5, 0.5]"""
a = 0.5
b = 3
k_max = 20
sum1 = sum(sum(a**k * np.cos(2 * np.pi * b**k * (pos[i] + 0.5)) for k in range(k_max + 1)) for i in range(dimensions))
sum2 = dimensions * sum(a**k * np.cos(np.pi * b**k) for k in range(k_max + 1))
return sum1 - sum2
def non_continuous_rastrigin(pos, dimensions):
"""Non-Continuous Rastrigin Function
The Non-Continuous Rastrigin function is a variant of the Rastrigin function that introduces discontinuities.
It is defined as the sum of the squares of its input variables (rounded to the nearest integer if greater than 0.5) minus 10 times the cosine of 2π times its input variables, plus 10 times the number of dimensions.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 2 to 100 (commonly used: 30)
Common Domain: [-5.12, 5.12]"""
res = 0
for i in range(dimensions):
xi = pos[i]
if abs(xi) > 0.5:
xi = round(xi * 2) / 2
res += xi**2 - 10 * np.cos(2 * np.pi * xi)
return res + 10 * dimensions
def penalized2(pos, dimensions):
"""Penalized Function 2
The Penalized Function 2 is a benchmark function for optimization that includes penalty terms for constraint violations.
It is defined as a combination of a base function and penalty terms based on the input variables.
Global Minimum:
f(1, 1, ..., 1) = 0
Dimension Range: 2 to 100 (commonly used: 30)
Common Domain: [-5.12, 5.12]"""
k = 100
m = 4
a = 5
def u(x, a, k, m):
if x > a:
return k * (x - a) ** m
elif x < -a:
return k * (-x - a) ** m
else:
return 0
term1 = 0.1 * ((np.sin(3 * np.pi * pos[0]))**2 + sum((pos[i] - 1)**2 * (1 + (np.sin(3 * np.pi * pos[i + 1]))**2) for i in range(dimensions - 1)) + (pos[-1] - 1)**2)
penalty = sum(u(pos[i], a, k, m) for i in range(dimensions))
return term1 + penalty
def schwefel_2point26(pos, dimensions):
"""Schwefel 2.26 Function
The Schwefel 2.26 function is a benchmark function for optimization.
It is defined as the sum of the input variables multiplied by the sine of the square root of their absolute values.
Global Minimum:
f(420.9687, 420.9687, ..., 420.9687) = -418.9829 * dimensions
Dimension Range: 10 to 100 (commonly used: 30)
Common Domain: [-500, 500]"""
return sum(x * np.sin(np.sqrt(abs(x))) for x in pos) - 418.9829 * dimensions
def schaffer(pos, dimensions):
"""Schaffer Function N. 2
The Schaffer function N. 2 is a benchmark function for optimization.
It is defined as 0.5 plus the square of the sine of the sum of the squares of its input variables minus 0.5, divided by (1 + 0.001 times the sum of the squares of its input variables) squared.
Global Minimum:
f(0, 0) = 0
Dimension Range: 2 (fixed)
Common Domain: [-100, 100]"""
sum_sq = sum(x**2 for x in pos)
numerator = (np.sin(sum_sq)**2 - 0.5)
denominator = (1 + 0.001 * sum_sq)**2
return 0.5 + numerator / denominator
def alpine(pos, dimensions):
"""Alpine Function
The Alpine function is a benchmark function for optimization.
It is defined as the sum of the absolute values of its input variables multiplied by the sine of their square roots.
Global Minimum:
f(0, 0, ..., 0) = 0
Dimension Range: 10 to 100 (commonly used: 30)
Common Domain: [-10, 10]"""
return sum(abs(x * np.sin(np.sqrt(abs(x)))) for x in pos)
def himmelblau(pos, dimensions):
"""Himmelblau's Function
Himmelblau's function is a multi-modal benchmark function for optimization.
It is defined as the sum of the squares of two expressions involving its input variables.
Global Minima:
f(3.0, 2.0) = 0
f(-2.805118, 3.131312) = 0
f(-3.779310, -3.283186) = 0
f(3.584428, -1.848126) = 0
Dimension Range: 2 (fixed)
Common Domain: [-6, 6]"""
x = pos[0]
y = pos[1]
term1 = (x**2 + y - 11)**2
term2 = (x + y**2 - 7)**2
return term1 + term2