-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarkFunctions.cpp
392 lines (327 loc) · 12.5 KB
/
BenchmarkFunctions.cpp
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
/**
* @file BenchmarkFunctions.cpp
* @author Al Timofeyev
* @date April 17, 2019
* @brief A library of benchmark functions.
*/
#include "BenchmarkFunctions.h"
using namespace std;
// **********************************************************************************
// **************************** Benchmark Functions Below ***************************
// **********************************************************************************
/**
* @brief Performs the Schefel's Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double schefelsFunc(vector<double> &vect, int size)
{
double answer;
double summedUp = 0;
// perform function calculations.
for(int i = 0; i < size; ++i)
summedUp += (-vect[i]) * sin(sqrt(abs(vect[i])));
answer = (418.9829 * size) - summedUp;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the 1st De Jong’s Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double deJongsFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size; ++i)
answer += pow(vect[i], 2);
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Rosenbrock Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double rosenbrockFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
answer += 100 * pow((pow(vect[i], 2) - vect[i+1]), 2) + pow((1-vect[i]), 2);
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Rastrigin Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double rastriginFunc(vector<double> &vect, int size)
{
double answer;
double summedUp = 0;
// perform function calculations.
for(int i = 0; i < size; ++i)
summedUp += pow(vect[i], 2) - (10 * cos(2*M_PI*vect[i]));
answer = 10 * size * summedUp;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Griewangk Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double griewangkFunc(vector<double> &vect, int size)
{
double answer;
double summedUp = 0;
double productUp = 1;
// perform function calculations.
for(int i = 0; i < size; ++i)
summedUp += pow(vect[i], 2) / 4000;
for(int i = 0; i < size; ++i)
productUp *= cos(vect[i] / sqrt(i+1));
answer = 1 + summedUp - productUp;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Sine Envelope Sine Wave Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double sineEnvelopeSineWaveFunc(vector<double> &vect, int size)
{
double answer;
double summedUp = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
summedUp += 0.5 + pow(sin(pow(vect[i], 2) + pow(vect[i+1], 2) - 0.5), 2) / pow((1 + 0.001*(pow(vect[i], 2) + pow(vect[i+1], 2))), 2);
answer = -summedUp;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Stretched V Sine Wave Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double stretchedVSineWaveFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
answer += pow((pow(vect[i], 2) + pow(vect[i+1], 2)), 1.0/4) * pow(sin(50 * pow((pow(vect[i], 2) + pow(vect[i+1], 2)), 1.0/10)), 2) + 1;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Ackley’s One Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double ackleysOneFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
answer += (1 / pow(exp(1.0), 0.2)) * sqrt(pow(vect[i] ,2) + pow(vect[i+1], 2)) + 3 * (cos(2*vect[i]) + sin(2*vect[i+1]));
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Ackley’s Two Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double ackleysTwoFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
answer += 20 + exp(1.0) - (20 / pow(exp(1.0), (0.2 * sqrt((pow(vect[i], 2) + pow(vect[i+1], 2)) / 2))))
- pow(exp(1.0), (0.5 * (cos(2*M_PI*vect[i]) + cos(2*M_PI*vect[i+1]))));
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Egg Holder Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double eggHolderFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
answer += (-vect[i]) * sin(sqrt(abs(vect[i] - vect[i+1] - 47)))
- (vect[i+1] + 47) * sin(sqrt(abs(vect[i+1] + 47 + (vect[i] / 2))));
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Rana Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double ranaFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
answer += vect[i] * sin(sqrt(abs(vect[i+1] - vect[i] + 1))) * cos(sqrt(abs(vect[i+1] + vect[i] + 1)))
+ (vect[i+1] + 1) * cos(sqrt(abs(vect[i+1] - vect[i] + 1))) * sin(sqrt(abs(vect[i+1] + vect[i] + 1)));
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Pathological Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double pathologicalFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
answer += 0.5 + (pow(sin(sqrt(100 * pow(vect[i], 2) + pow(vect[i+1], 2))), 2) - 0.5)
/ (1 + 0.001*pow((pow(vect[i], 2) - 2*vect[i] * vect[i+1] + pow(vect[i+1], 2)), 2));
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Michalewicz Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double michalewiczFunc(vector<double> &vect, int size)
{
double answer;
double summedUp = 0;
// perform function calculations.
for(int i = 0; i < size; ++i)
summedUp += sin(vect[i]) * pow(sin(((i+1) * pow(vect[i], 2)) / M_PI), 20);
answer = -summedUp;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Masters Cosine Wave Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double mastersCosWaveFunc(vector<double> &vect, int size)
{
double answer;
double summedUp = 0;
// perform function calculations.
for(int i = 0; i < size-1; ++i)
summedUp += pow(exp(1.0), ((-1.0/8.0) * (pow(vect[i], 2) + pow(vect[i+1], 2) + 0.5 * vect[i+1] * vect[i])))
* cos(4 * sqrt(pow(vect[i], 2) + pow(vect[i+1], 2) + 0.5 * vect[i] * vect[i+1]));
answer = -summedUp;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Quartic Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double quarticFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size; ++i)
answer += (i+1) * pow(vect[i], 4);
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Levy Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double levyFunc(vector<double> &vect, int size)
{
double answer;
double summedUp = 0;
double w1 = vect[0];
double wi;
double wn = vect[size-1];
// perform function calculations.
for(int i = 0; i < size-1; ++i)
{
wi = 1 + (vect[i] - 1) / 4;
summedUp += pow((wi - 1), 2) * (1 + 10 * pow(sin(M_PI*wi + 1), 2))
+ pow((wn - 1), 2) * (1 + pow(sin(2*M_PI*wn), 2));
}
answer = pow(sin(M_PI * w1), 2) + summedUp;
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Step Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double stepFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size; ++i)
answer += pow((abs(vect[i]) + 0.5), 2);
return answer;
}
// ----------------------------------------------------------------------------------
/**
* @brief Performs the Alpine Function on a vector of elements.
*
* @param vect The vector of elements on which to perform calculations.
* @param size The number of elements in vector.
* @return The results of the calculations (fitness).
*/
double alpineFunc(vector<double> &vect, int size)
{
double answer = 0;
// perform function calculations.
for(int i = 0; i < size; ++i)
answer += abs(vect[i] * sin(vect[i]) + 0.1 * vect[i]);
return answer;
}