-
Notifications
You must be signed in to change notification settings - Fork 2
/
cec2013.cpp
273 lines (250 loc) · 7.82 KB
/
cec2013.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
/******************************************************************************
* Version: 1.0
* Last modified on: 21 January, 2013
* Developers: Michael G. Epitropakis, Xiaodong Li.
* email: mge_(AT)_cs_(DOT)_stir_(DOT)_ac_(DOT)_uk
* : xiaodong_(DOT)_li_(AT)_rmit_(DOT)_edu_(DOT)_au
* ***************************************************************************/
#include "cec2013.h"
#include "sortidx.h"
using namespace std;
CEC2013::CEC2013(const int &nfunc) :
nfunc_(0), cfunc_(NULL)
{
assert(nfunc>0 && nfunc<=20);
nfunc_ = nfunc;
init_vars_();
if (nfunc_ == 11) {
cfunc_ = new CF1(dimensions_[nfunc_-1]);
} else if (nfunc_ == 12) {
cfunc_ = new CF2(dimensions_[nfunc_-1]);
} else if (nfunc_ == 13 || nfunc_ == 14 || nfunc_ == 16 || nfunc_ == 18 ) {
cfunc_ = new CF3(dimensions_[nfunc_-1]);
} else if (nfunc_ == 15 || nfunc_ == 17 || nfunc_ == 19 || nfunc_ == 20 ) {
cfunc_ = new CF4(dimensions_[nfunc_-1]);
}
}
CEC2013::~CEC2013()
{
if (cfunc_) delete cfunc_;
}
void CEC2013::init_vars_()
{
/* specific dimensions for the competition */
dimensions_[0] = dimensions_[1] = dimensions_[2] = 1;
dimensions_[3] = dimensions_[4] = dimensions_[5] = dimensions_[6] = 2;
dimensions_[7] = dimensions_[8] = 3;
dimensions_[9] = dimensions_[10] = dimensions_[11] = dimensions_[12] = 2;
dimensions_[13] = dimensions_[14] = 3;
dimensions_[15] = dimensions_[16] = 5;
dimensions_[17] = dimensions_[18] = 10;
dimensions_[19] = 20;
fopt_[0] = 200.0; //1
fopt_[1] = 1.0; //1
fopt_[2] = 1.0; //1
fopt_[3] = 200.0; //2
fopt_[4] = 1.03163; //2
fopt_[5] = 186.7309088310240; //2
fopt_[6] = 1.0; //2
fopt_[7] = 2709.0935; //3
fopt_[8] = 1.0; //3
fopt_[9] = -2.0; //2
for (int i=10; i<20; ++i) { fopt_[i] = 0;}
rho_[0] = rho_[1] = rho_[2] = rho_[3] = 0.01;
rho_[4] = rho_[5] = 0.5;
rho_[6] = 0.2;
rho_[7] = 0.5;
rho_[8] = 0.2;
rho_[9] = rho_[10] = rho_[11] = rho_[12] = rho_[13] = 0.01;
rho_[14] = rho_[15] = rho_[16] = rho_[17] = rho_[18] = rho_[19] = 0.01;
nopt_[0] = 2; nopt_[1] = 5; nopt_[2] = 1; nopt_[3] = 4; nopt_[4] = 2;
nopt_[5] = 18; nopt_[6] = 36; nopt_[7] = 81; nopt_[8] = 216;
nopt_[9] = 12; nopt_[10] = 6; nopt_[11] = 8; nopt_[12] = 6;
nopt_[13] = 6; nopt_[14] = 8; nopt_[15] = 6; nopt_[16] = 8;
nopt_[17] = 6; nopt_[18] = 8; nopt_[19] = 8;
maxfes_[0] = maxfes_[1] = maxfes_[2] = maxfes_[3] = maxfes_[4] = 50000;
maxfes_[5] = maxfes_[6] = maxfes_[9] = maxfes_[10] = maxfes_[11] = maxfes_[12] = 200000;
maxfes_[7] = maxfes_[8] = maxfes_[13] = maxfes_[14] = maxfes_[15] = 400000;
maxfes_[16] = maxfes_[17] = maxfes_[18] = maxfes_[19] = 400000;
for(int i=0; i<5; ++i) pops_[i] = 10;//f1-f5
pops_[5] = 40;//f6
for(int i=6; i<9; ++i) pops_[i] = 400;//f7-f9
for(int i=9; i<13; ++i) pops_[i] = 40; //f10-f13
for(int i=13; i<15; ++i) pops_[i] = 400;//f14-f15
for(int i=15; i<20; ++i) pops_[i] = 200;//f16-f20
/*for(int i=5; i<9; ++i) pops_[i] = 900;
for(int i=9; i<15; ++i) pops_[i] = 120;
for(int i=15; i<20; ++i)pops_[i] = 600;*/
}
double CEC2013::get_lbound(const int &n) const
{
assert(n>=0 && n<dimensions_[nfunc_-1]);
double result(0);
if (nfunc_ == 1 || nfunc_ == 2 || nfunc_ == 3) {
result = 0;
} else if (nfunc_ == 4) {
result = -6;
} else if (nfunc_ == 5) {
double tmp[2];
tmp[0] = -1.9;
tmp[1] = -1.1;
result = tmp[n];
} else if (nfunc_ == 6 || nfunc_ == 8) {
result = -10;
} else if (nfunc_ == 7 || nfunc_ == 9) {
result = 0.25;
} else if (nfunc_ == 10) {
result = 0;
} else if (nfunc_ > 10) {
result = cfunc_->get_lbound(n);
}
return result;
}
double CEC2013::get_ubound(const int &n) const
{
assert(n>=0 && n<dimensions_[nfunc_-1]);
double result(0);
if (nfunc_ == 1) {
result = 30;
} else if (nfunc_ == 2 || nfunc_ == 3) {
result = 1;
} else if (nfunc_ == 4) {
result = 6;
} else if (nfunc_ == 5) {
double tmp[2];
tmp[0] = 1.9;
tmp[1] = 1.1;
result = tmp[n];
} else if (nfunc_ == 6 || nfunc_ == 8) {
result = 10;
} else if (nfunc_ == 7 || nfunc_ == 9) {
result = 10;
} else if (nfunc_ == 10) {
result = 1;
} else if (nfunc_ > 10) {
result = cfunc_->get_ubound(n);
}
return result;
}
tFitness CEC2013::evaluate(const double *x)
{
/* function number: */
const int dim = dimensions_[nfunc_-1];
double result(0);
if (nfunc_ == 1) {
result = five_uneven_peak_trap(x, dim);
} else if (nfunc_ == 2) {
result = equal_maxima(x, dim);
} else if (nfunc_ == 3) {
result = uneven_decreasing_maxima(x, dim);
} else if (nfunc_ == 4) {
result = himmelblau(x, dim);
} else if (nfunc_ == 5) {
result = six_hump_camel_back(x, dim);
} else if (nfunc_ == 6 || nfunc_ == 8) {
result = shubert(x, dim);
} else if (nfunc_ == 7 || nfunc_ == 9) {
result = vincent(x, dim);
} else if (nfunc_ == 10) {
result = modified_rastrigin_all(x, dim);
} else if (nfunc_ > 10) {
result = cfunc_->evaluate(x);
}
return result;
}
tFitness CEC2013::evaluate(std::vector<double> x)
{
const int dim = x.size();
assert(dim == dimensions_[nfunc_-1]);
double *tmpx = new double[dim];
for (int i=0; i<dim; ++i) { tmpx[i] = x[i]; }
double result = evaluate(tmpx);
delete [] tmpx;
return result;
}
int how_many_goptima(std::vector< std::vector<double> > pop,
std::vector< std::vector<double> > &seeds, CEC2013 *pFunc,
const double &accuracy, const double &radius)
{
std::vector<std::vector<double> > sorted_pop;
/* Evaluate pop */
vector<double> fits;//(pop.size());
//int i=0;
for (std::vector< std::vector<double> >::iterator it = pop.begin(); it != pop.end(); ++it) {
fits.push_back( pFunc->evaluate(*it) );
//cout << "fits: " << fits[i] << endl;
//++i;
}
/* sorted_pop: sort population by fitness */
vector<size_t> idx;
sortIdx(idx, fits, DESCEND);
for (size_t i=0; i< pop.size(); ++i) {
sorted_pop.push_back( pop[ idx[i] ] );
}
/* find seeds in the temp population */
get_seeds(sorted_pop, seeds, radius);
/* Based on the accuracy: check which seeds are global optimizers */
double count(0);
for (std::vector< std::vector<double> >::iterator sit = seeds.begin(); sit != seeds.end(); ++sit) {
/* evaluate seed */
double seed_fitness = pFunc->evaluate(*sit);
/* |F_seed - F_goptimum| <= accuracy */
//if ( fabs(seed_fitness - pFunc->get_fitness_goptima()) <= accuracy) {
if ( pFunc->get_fitness_goptima()-seed_fitness <= accuracy) {
++count;
}
/* save time */
if (count == pFunc->get_no_goptima()) break;
}
return (int)count;
}
void print_pop(std::vector< std::vector<double> > pop)
{
for (std::vector< std::vector<double> >::iterator it = pop.begin();
it != pop.end(); ++it) {
for (std::vector<double>::iterator jt = it->begin();
jt != it->end(); ++jt) {
cout << *jt << "\t";
}
cout << endl;
}
}
/**
* Input: population vector of vectors,
* Output: seeds
*/
void get_seeds(std::vector< std::vector<double> > &pop, std::vector< std::vector<double> > &seeds,
const double &radius)
{
/* remove any forgoten items in the seed list */
seeds.clear();
/* Determine the species seeds:
* iterate through sorted population */
for (std::vector< std::vector<double> >::iterator it = pop.begin(); it != pop.end(); ++it) {
bool found(false);
/* Iterate seeds */
//cout << "-----" << endl;
for (std::vector< std::vector<double> >::iterator sit = seeds.begin(); sit != seeds.end(); ++sit) {
/* Calculate distance from seeds */
double dist = get_eudist(*it, *sit);
//cout << "DIST: " << dist << endl;
/* If the Euclidean distance is less than the radius */
if (dist <= radius) {
found = true;
break;
}
} //seeds
/* If it is not similar to any other seed, then it is a new seed */
if (!found) { seeds.push_back(*it); }
} //pop
}
double get_eudist(std::vector<double> &v1, std::vector<double> &v2)
{
assert(v1.size() == v2.size());
//if (v1.size() != v2.size()) { exit(-1);}
double res(0);
for (size_t i=0; i < v1.size(); i++) {
res += (v1[i] - v2[i]) * (v1[i] - v2[i]);
}
return sqrt(res);
}