-
Notifications
You must be signed in to change notification settings - Fork 0
/
recomb.h
327 lines (301 loc) · 9.59 KB
/
recomb.h
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
#ifndef _RECOMBINATION_H_
#define _RECOMBINATION_H_
#include "global.h"
#include "individual.h"
/* Routine for real polynomial mutation of an T */
void realmutation(CIndividual &ind, double rate)
{
double rnd, delta1, delta2, mut_pow, deltaq;
double y, yl, yu, val, xy;
double eta_m = etam;
int id_rnd = int(rnd_uni(&rnd_uni_init)*nvar);
for (int j=0; j<nvar; j++)
{
if (rnd_uni(&rnd_uni_init)<=rate)
{
y = ind.x_var[j];
yl = lowBound[j];
yu = uppBound[j];
delta1 = (y-yl)/(yu-yl);
delta2 = (yu-y)/(yu-yl);
rnd = rnd_uni(&rnd_uni_init);
mut_pow = 1.0/(eta_m+1.0);
if (rnd <= 0.5)
{
xy = 1.0-delta1;
val = 2.0*rnd+(1.0-2.0*rnd)*(pow(xy,(eta_m+1.0)));
deltaq = pow(val,mut_pow) - 1.0;
}
else
{
xy = 1.0-delta2;
val = 2.0*(1.0-rnd)+2.0*(rnd-0.5)*(pow(xy,(eta_m+1.0)));
deltaq = 1.0 - (pow(val,mut_pow));
}
y = y + deltaq*(yu-yl);
if (y<yl)
y = yl;
if (y>yu)
y = yu;
ind.x_var[j] = y;
}
}
return;
}
/* Routine for real variable SBX crossover */
template <class T>
void real_sbx_xoverA(CIndividual &parent1, CIndividual &parent2, CIndividual &child1, CIndividual &child2)
{
double rand;
double y1, y2, yl, yu;
double c11, c22;
double alpha, beta, betaq;
double eta_c = etax;
if (rnd_uni(&rnd_uni_init) <= 1.0)
{
for (int i=0; i<nvar; i++)
{
if (rnd_uni(&rnd_uni_init)<=0.5 )
{
if (fabs(parent1.x_var[i]-parent2.x_var[i]) > EPS)
{
if (parent1.x_var[i] < parent2.x_var[i])
{
y1 = parent1.x_var[i];
y2 = parent2.x_var[i];
}
else
{
y1 = parent2.x_var[i];
y2 = parent1.x_var[i];
}
yl = lowBound[i];
yu = uppBound[i];
rand = rnd_uni(&rnd_uni_init);
beta = 1.0 + (2.0*(y1-yl)/(y2-y1));
alpha = 2.0 - pow(beta,-(eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(eta_c+1.0)));
}
c11 = 0.5*((y1+y2)-betaq*(y2-y1));
beta = 1.0 + (2.0*(yu-y2)/(y2-y1));
alpha = 2.0 - pow(beta,-(eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(eta_c+1.0)));
}
c22 = 0.5*((y1+y2)+betaq*(y2-y1));
if (c11<yl)
c11=yl;
if (c22<yl)
c22=yl;
if (c11>yu)
c11=yu;
if (c22>yu)
c22=yu;
if (rnd_uni(&rnd_uni_init)<=0.5)
{
child1.x_var[i] = c22;
child2.x_var[i] = c11;
}
else
{
child1.x_var[i] = c11;
child2.x_var[i] = c22;
}
}
else
{
child1.x_var[i] = parent1.x_var[i];
child2.x_var[i] = parent2.x_var[i];
}
}
else
{
child1.x_var[i] = parent1.x_var[i];
child2.x_var[i] = parent2.x_var[i];
}
}
}
else
{
for (int i=0; i<nvar; i++)
{
child1.x_var[i] = parent1.x_var[i];
child2.x_var[i] = parent2.x_var[i];
}
}
return;
}
void real_sbx_xoverB (CIndividual &parent1, CIndividual &parent2, CIndividual &child)
{
double rand;
double y1, y2, yl, yu;
double c11, c22;
double alpha, beta, betaq;
double eta_c = etax;
eta_c = 0.5;
if (rnd_uni(&rnd_uni_init) <= 1.0)
{
for (int i=0; i<nvar; i++)
{
if (rnd_uni(&rnd_uni_init)<=0.5 )
{
if (fabs(parent1.x_var[i]-parent2.x_var[i]) > EPS)
{
if (parent1.x_var[i] < parent2.x_var[i])
{
y1 = parent1.x_var[i];
y2 = parent2.x_var[i];
}
else
{
y1 = parent2.x_var[i];
y2 = parent1.x_var[i];
}
yl = lowBound[i];
yu = uppBound[i];
rand = rnd_uni(&rnd_uni_init);
beta = 1.0 + (2.0*(y1-yl)/(y2-y1));
alpha = 2.0 - pow(beta,-(eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(eta_c+1.0)));
}
c11 = 0.5*((y1+y2)-betaq*(y2-y1));
beta = 1.0 + (2.0*(yu-y2)/(y2-y1));
alpha = 2.0 - pow(beta,-(eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(eta_c+1.0)));
}
c22 = 0.5*((y1+y2)+betaq*(y2-y1));
if (c11<yl)
{
double rnd = rnd_uni(&rnd_uni_init);
c11=yl+rnd*(y1-yl);
}
if (c22<yl)
{
double rnd = rnd_uni(&rnd_uni_init);
c22=yl+rnd*(y2-yl);
}
if (c11>yu)
{
double rnd = rnd_uni(&rnd_uni_init);
c11=yu-rnd*(yu-y1);
}
if (c22>yu)
{
double rnd = rnd_uni(&rnd_uni_init);
c22=yu-rnd*(yu-y2);
}
if (rnd_uni(&rnd_uni_init)<=0.5)
{
child.x_var[i] = c22;
}
else
{
child.x_var[i] = c11;
}
}
else
{
child.x_var[i] = parent1.x_var[i];
}
}
else
{
child.x_var[i] = parent1.x_var[i];
}
}
}
else
{
for (int i=0; i<nvar; i++)
{
child.x_var[i] = parent1.x_var[i];
}
}
return;
}
void diff_evo_xoverA(CIndividual &ind0, CIndividual &ind1, CIndividual &ind2, CIndividual &ind3, CIndividual &child, double rate)
{
// Check Whether the cross-over to be performed
/*Loop over no of variables*/
int idx_rnd = int(rnd_uni(&rnd_uni_init)*nvar);
//rate = rnd_uni(&rnd_uni_init);
for(int n=0;n<nvar;n++)
{
double rnd = rnd_uni(&rnd_uni_init);
if(rnd<1||n==idx_rnd)
child.x_var[n] = ind1.x_var[n] + rate*(ind2.x_var[n] - ind3.x_var[n]);
else
child.x_var[n] = ind0.x_var[n];
if(child.x_var[n]<lowBound[n]) child.x_var[n] = lowBound[n];
if(child.x_var[n]>uppBound[n]) child.x_var[n] = uppBound[n];
}
}
void diff_evo_xoverB(CIndividual &ind0, CIndividual &ind1, CIndividual &ind2, CIndividual &child, double rate)
{
int idx_rnd = int(rnd_uni(&rnd_uni_init)*nvar);
rate = rnd_uni(&rnd_uni_init);
for(int n=0;n<nvar;n++)
{
/*Selected Two Parents*/
// strategy one
// child.x_var[n] = ind0.x_var[n] + rate*(ind2.x_var[n] - ind1.x_var[n]);
//*
// strategy two
double rnd1 = rnd_uni(&rnd_uni_init);
double CR = 1.0;
if(rnd1<CR||n==idx_rnd)
child.x_var[n] = ind0.x_var[n] + rate*(ind2.x_var[n] - ind1.x_var[n]);
else
child.x_var[n] = ind0.x_var[n];
//*/
// handle the boundary voilation
if(child.x_var[n]<lowBound[n]){
double rnd = rnd_uni(&rnd_uni_init);
child.x_var[n] = lowBound[n] + rnd*(ind0.x_var[n] - lowBound[n]);
}
if(child.x_var[n]>uppBound[n]){
double rnd = rnd_uni(&rnd_uni_init);
child.x_var[n] = uppBound[n] - rnd*(uppBound[n] - ind0.x_var[n]);
}
//if(child.x_var[n]<lowBound) child.x_var[n] = lowBound;
//if(child.x_var[n]>uppBound) child.x_var[n] = uppBound;
}
}
void diff_evo_xoverC(CIndividual &ind0, CIndividual &ind1, CIndividual &ind2, vector<double> &xdiff, CIndividual &child, double rate)
{
double rnd = rnd_uni(&rnd_uni_init), rnd2 = rnd_uni(&rnd_uni_init);
for(int n=0;n<nvar;n++)
{
/*Selected Two Parents*/
if(rnd<1)
child.x_var[n] = ind0.x_var[n] + rate*(ind2.x_var[n] - ind1.x_var[n]);
else
child.x_var[n] = ind0.x_var[n] + rnd2*xdiff[n];
if(child.x_var[n]<lowBound[n]) child.x_var[n] = lowBound[n];
if(child.x_var[n]>uppBound[n]) child.x_var[n] = uppBound[n];
}
}
#endif