-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkFunction.h
More file actions
337 lines (271 loc) · 9.77 KB
/
BenchmarkFunction.h
File metadata and controls
337 lines (271 loc) · 9.77 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
//
// Created by Sippawit Thammawiset on 29/1/2024 AD.
//
#ifndef BENCHMARK_FUNCTION_H
#define BENCHMARK_FUNCTION_H
#include <random>
namespace Benchmark
{
typedef int FUNCTION_NAME;
enum
{
SPHERE = 0,
SCHWEFEL_S_2_22 = 1,
SCHWEFEL_S_1_20 = 2,
ROSENBROCK = 3,
STEP = 4,
QUARTIC_NOISE = 5,
SCHWEFEL_S_2_26 = 6,
RASTRIGIN = 7,
ACKLEY = 8,
GRIEWANK = 9
};
double GenerateRandom (double LowerBound = 0.0, double UpperBound = 1.0)
{
std::random_device Engine;
std::uniform_real_distribution<double> RandomDistribution(0.0, 1.0);
return LowerBound + RandomDistribution(Engine) * (UpperBound - LowerBound);
}
namespace Function
{
double Sphere (const std::vector<double> &Position)
{
double Sum = 0.0;
for (const double &i : Position)
{
Sum += i * i;
}
return Sum;
}
double Schwefel_s_2_22 (const std::vector<double> &Position)
{
double Term1 = 0.0;
double Term2 = 1.0;
for (const double &i : Position)
{
Term1 += std::abs(i);
Term2 *= std::abs(i);
}
return Term1 + Term2;
}
double Schwefel_s_1_20 (const std::vector<double> &Position)
{
double Sum = 0.0;
for (int i = 0; i < Position.size(); ++i)
{
double InnerSum = 0.0;
for (int j = 0; j <= i; ++j)
{
InnerSum += Position[j];
}
Sum += InnerSum * InnerSum;
}
return Sum;
}
double Rosenbrock (const std::vector<double> &Position)
{
double Sum = 0.0;
for (int i = 0; i < Position.size() - 1; ++i)
{
double Term1 = 100 * (Position[i + 1] - Position[i] * Position[i]) * (Position[i + 1] - Position[i] * Position[i]);
double Term2 = (Position[i] - 1) * (Position[i] - 1);
Sum += Term1 + Term2;
}
return Sum;
}
double Step (const std::vector<double> &Position)
{
double Sum = 0.0;
for (const double &i : Position)
{
Sum += (i + 0.5) * (i + 0.5);
}
return Sum;
}
double QuarticNoise (const std::vector<double> &Position)
{
double Sum = 0.0f;
for (int i = 0; i < Position.size(); ++i)
{
Sum += i * Position[i] * Position[i] * Position[i] * Position[i];
}
return Sum + GenerateRandom(0.0, 1.0);
}
double Schwefel_s_2_26 (const std::vector<double> &Position)
{
double Sum = 0.0;
for (const double &i : Position)
{
Sum += i * sin(sqrt(abs(i)));
}
return -Sum;
}
double Rastrigin (const std::vector<double> &Position)
{
double Sum = 0.0;
for (const double &i : Position)
{
Sum += i * i - 10 * cos(2.0 * M_PI * i) + 10.0;
}
return Sum;
}
double Ackley (const std::vector<double> &Position)
{
int Dimension = static_cast<int>(Position.size());
double SumSquare = 0.0;
double SumCosine = 0.0;
for (const double &i : Position)
{
SumSquare += i * i;
SumCosine += cos(2.0 * M_PI * i);
}
double Term1 = -20.0 * exp(-0.2 * sqrt(SumSquare / Dimension));
double Term2 = -exp(SumCosine / Dimension);
return Term1 + Term2 + 20.0 + expf(1.0);
}
double Griewank (const std::vector<double> &Position)
{
double SumSquare = 0.0;
double ProductCosine = 1.0;
for (int i = 0; i < Position.size(); i++)
{
SumSquare += Position[i] * Position[i];
ProductCosine *= cos(Position[i] / sqrt(i + 1));
}
return SumSquare / 4000 - ProductCosine + 1;
}
} // Benchmark::Function
namespace Property
{
void Sphere (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -100);
UpperBound = std::vector<double> (NVariable, 100);
}
void Schwefel_s_2_22 (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -10);
UpperBound = std::vector<double> (NVariable, 10);
}
void Schwefel_s_1_20 (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -100);
UpperBound = std::vector<double> (NVariable, 100);
}
void Rosenbrock (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -30);
UpperBound = std::vector<double> (NVariable, 30);
}
void Step (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -100);
UpperBound = std::vector<double> (NVariable, 100);
}
void QuarticNoise (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -1.28);
UpperBound = std::vector<double> (NVariable, 1.28);
}
void Schwefel_s_2_26 (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -500);
UpperBound = std::vector<double> (NVariable, 500);
}
void Rastrigin (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -5.12);
UpperBound = std::vector<double> (NVariable, 5.12);
}
void Ackley (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -32);
UpperBound = std::vector<double> (NVariable, 32);
}
void Griewank (int &NVariable, std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
NVariable = 30;
LowerBound = std::vector<double> (NVariable, -600);
UpperBound = std::vector<double> (NVariable, 600);
}
} // Benchmark::Property
double BenchmarkFunction (FUNCTION_NAME FUNCTION, const std::vector<double> &Position)
{
switch (FUNCTION)
{
case SPHERE:
return Function::Sphere(Position);
case SCHWEFEL_S_2_22:
return Function::Schwefel_s_2_22(Position);
case SCHWEFEL_S_1_20:
return Function::Schwefel_s_1_20(Position);
case ROSENBROCK:
return Function::Rosenbrock(Position);
case STEP:
return Function::Step(Position);
case QUARTIC_NOISE:
return Function::QuarticNoise(Position);
case SCHWEFEL_S_2_26:
return Function::Schwefel_s_2_26(Position);
case RASTRIGIN:
return Function::Rastrigin(Position);
case ACKLEY:
return Function::Ackley(Position);
case GRIEWANK:
return Function::Griewank(Position);
default:
return Function::Sphere(Position);
}
return -1;
}
void BenchmarkProperty (FUNCTION_NAME FUNCTION,
int &NVariable,
std::vector<double> &LowerBound, std::vector<double> &UpperBound)
{
switch (FUNCTION)
{
case SPHERE:
Property::Sphere(NVariable, LowerBound, UpperBound);
break;
case SCHWEFEL_S_2_22:
Property::Schwefel_s_2_22(NVariable, LowerBound, UpperBound);
break;
case SCHWEFEL_S_1_20:
Property::Schwefel_s_1_20(NVariable, LowerBound, UpperBound);
break;
case ROSENBROCK:
Property::Rosenbrock(NVariable, LowerBound, UpperBound);
break;
case STEP:
Property::Step(NVariable, LowerBound, UpperBound);
break;
case QUARTIC_NOISE:
Property::QuarticNoise(NVariable, LowerBound, UpperBound);
break;
case SCHWEFEL_S_2_26:
Property::Schwefel_s_2_26(NVariable, LowerBound, UpperBound);
break;
case RASTRIGIN:
Property::Rastrigin(NVariable, LowerBound, UpperBound);
break;
case ACKLEY:
Property::Ackley(NVariable, LowerBound, UpperBound);
break;
case GRIEWANK:
Property::Griewank(NVariable, LowerBound, UpperBound);
break;
default:
Property::Sphere(NVariable, LowerBound, UpperBound);
}
}
}
#endif //BENCHMARK_FUNCTION_H