-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCrossover.hpp
443 lines (335 loc) · 14.1 KB
/
Crossover.hpp
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//
// Crossover.hpp
// parallel-nsgaII-backend
//
// Created by Jeffrey Newman on 19/11/2015.
// Copyright © 2015 University of Adelaide and Bushfire and Natural Hazards CRC. All rights reserved.
//
/*this file contains the crossover operator, which apply one point crossover to
the integer chromosome chrom1 and SBX to the real-coded chromsome chrom2.*/
// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.33.7291&rep=rep1&type=pdf
#ifndef Crossover_h
#define Crossover_h
#include <cmath>
#include <chrono>
#include "Population.hpp"
#include <boost/serialization/nvp.hpp>
template <typename RNG = std::mt19937>
class CrossoverBase
{
public:
CrossoverBase():
uniform_rand_distributn(0.0, 1.0),
seed_crossover(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_crossover(seed_crossover),
random_number_gen(default_rng_crossover)
{
}
CrossoverBase(RNG & rng):
uniform_rand_distributn(0.0, 1.0),
seed_crossover(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_crossover(seed_crossover),
random_number_gen(rng)
{
}
virtual void operator()(Individual & individal_1, Individual & individal_2) = 0;
protected:
std::uniform_real_distribution<double> uniform_rand_distributn;
unsigned seed_crossover;
RNG default_rng_crossover;
RNG & random_number_gen;
};
template <typename RNG = std::mt19937>
class DebsSBXCrossover : public CrossoverBase<RNG>
{
double default_eps;
double default_eta;
double eps;
double eta_c;
public:
DebsSBXCrossover() :
default_eps(0.00001),
default_eta(20.0),
eps(default_eps),
eta_c(default_eta)
{
}
DebsSBXCrossover(RNG & rng) :
CrossoverBase<RNG>(rng),
default_eps(0.00001),
default_eta(20.0),
eps(default_eps),
eta_c(default_eta)
{
}
DebsSBXCrossover(RNG & rng, double & eta, double & _eps) :
CrossoverBase<RNG>(rng),
default_eps(0.00001),
default_eta(20.0),
eps(_eps),
eta_c(eta)
{
}
DebsSBXCrossover & setEps(double & _eps)
{
eps = _eps;
return (*this);
}
DebsSBXCrossover & setEtaC(double & _eta_c)
{
eta_c = _eta_c;
return (*this);
}
virtual void operator()(Individual & individal_1, Individual & individal_2)
{
// individal_1.crossovered = true;
// individal_2.crossovered = true;
for (Individual::RealDVsT::size_type j=0;j < individal_1.numOfRealDVs(); ++j)
{//for each real in chrom
double dv_val_parent1 = individal_1.getRealDV(j);
double dv_val_parent2 = individal_2.getRealDV(j);
const double & lower_bound_val = individal_1.getRealLowerBound(j);//lower limt of variable j of ind i
const double & upper_bound_val = individal_1.getRealUpperBound(j);//upper limt of variable j of ind i
//Sanity check. If dv vals are out of bounds, NaNs are produced.
if (dv_val_parent1 < lower_bound_val)
{
std::cerr << "real decision variable " << dv_val_parent1 << " at place " << j << " out of bounds; setting to lower bound which is " << lower_bound_val << std::endl;
dv_val_parent1 = lower_bound_val;
}
if (dv_val_parent1 > upper_bound_val)
{
std::cerr << "input real decision variable " << dv_val_parent1 << " at place " << j << " out of bounds; setting to upper bound which is " << upper_bound_val << std::endl;
dv_val_parent1 = upper_bound_val;
}
if (dv_val_parent2 < lower_bound_val)
{
std::cerr << "real decision variable " << dv_val_parent2 << " at place " << j << " out of bounds; setting to lower bound which is " << lower_bound_val << std::endl;
dv_val_parent2 = lower_bound_val;
}
if (dv_val_parent2 > upper_bound_val)
{
std::cerr << "input real decision variable " << dv_val_parent2 << " at place " << j << " out of bounds; setting to upper bound which is " << upper_bound_val << std::endl;
dv_val_parent2 = upper_bound_val;
}
if (uniform_rand_distributn(random_number_gen)<=0.5) //only half of the bits in chrom2 will be crossed
{
double y1, y2;
if (fabs( dv_val_parent1 - dv_val_parent2) > eps) //values of two parents are different
{
if (dv_val_parent2 > dv_val_parent1)
{
y2 = dv_val_parent2; //y2 is the parent that has larger value
y1 = dv_val_parent1;
}
else
{
y2 = dv_val_parent1;
y1 = dv_val_parent2;
}
//for the first child
double beta = 1.0 + (2.0 * (y1 - lower_bound_val) / (y2 - y1));
double alpha = 2.0 - pow(beta, -(eta_c + 1.0));
double betaq;
double rand = uniform_rand_distributn(random_number_gen);
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)));
double dvar_val_child1 = 0.5 * ((y1 + y2) - betaq * (y2 - y1));
//for the second child
beta = 1.0 + (2.0 * (upper_bound_val - 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)));
double dvar_val_child2 = 0.5 * ((y1 + y2) + betaq * (y2 - y1));
if (dvar_val_child1<lower_bound_val) dvar_val_child1=lower_bound_val;
if (dvar_val_child1>upper_bound_val) dvar_val_child1=upper_bound_val;
if (dvar_val_child2<lower_bound_val) dvar_val_child2=lower_bound_val;
if (dvar_val_child2>upper_bound_val) dvar_val_child2=y2;
if (uniform_rand_distributn(random_number_gen)<=0.5)
{
individal_1.setRealDV(j, dvar_val_child2);
individal_2.setRealDV(j, dvar_val_child1);
}
else
{
individal_1.setRealDV(j, dvar_val_child1);
individal_2.setRealDV(j, dvar_val_child2);
}
}
}
}
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(eps);
ar & BOOST_SERIALIZATION_NVP(eta_c);
}
};
template <typename RNG = std::mt19937>
class OnePointCrossover : public CrossoverBase<RNG>
{
private:
public:
OnePointCrossover()
{
}
OnePointCrossover(RNG & rng) :
CrossoverBase<RNG>(rng)
{
}
virtual void operator()(Individual & individal_1, Individual & individal_2)
{
/// First we crossover the unordered DVs
//Determine location of crossover.
int location = int(std::ceil(uniform_rand_distributn(random_number_gen) * (individal_1.numOfUnorderedDVs() - 1)));
// To the right of location, the chromosones are crossovered.
for (int j=location;j < individal_1.numOfUnorderedDVs(); ++j)
{//for each real in chrom
//
int val = individal_1.getUnorderedDV(j);
individal_1.setUnorderedDV(j, individal_2.getUnorderedDV(j));
individal_2.setUnorderedDV(j, val);
}
/// Second, we crossover the ordered DVs
location = int(std::ceil(uniform_rand_distributn(random_number_gen) * (individal_1.numOfOrderedDVs() - 1)));
// To the right of location, the chromosones are crossovered.
for (int j = location; j < individal_1.numOfOrderedDVs(); ++j)
{//for each real in chrom
int val = individal_1.getOrderedDV(j);
individal_1.setOrderedDV(j, individal_2.getOrderedDV(j));
individal_2.setOrderedDV(j, val);
}
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// no persistent information required to rebuild...
}
};
template <typename RNG = std::mt19937>
class CombinedRealIntCrossover
{
std::uniform_real_distribution<double> uniform_rand_distributn;
unsigned seed_crossover;
RNG default_rng_crossover;
RNG & random_number_gen;
double default_crossoverp;
DebsSBXCrossover<> default_real_xover;
OnePointCrossover<> default_int_xover;
double probability_crossover;
CrossoverBase<RNG> & real_xover;
CrossoverBase<RNG> & int_xover;
public:
CombinedRealIntCrossover():
uniform_rand_distributn(0.0, 1.0),
seed_crossover(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_crossover(seed_crossover),
random_number_gen(default_rng_crossover),
default_crossoverp(0.9),
default_real_xover(random_number_gen),
default_int_xover(random_number_gen),
probability_crossover(default_crossoverp),
real_xover(default_real_xover),
int_xover(default_int_xover)
{
}
CombinedRealIntCrossover(double & _probability_crossover):
uniform_rand_distributn(0.0, 1.0),
seed_crossover(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_crossover(seed_crossover),
random_number_gen(default_rng_crossover),
default_crossoverp(0.9),
default_real_xover(random_number_gen),
default_int_xover(random_number_gen),
probability_crossover(_probability_crossover),
real_xover(default_real_xover),
int_xover(default_int_xover)
{
}
CombinedRealIntCrossover(double & _probability_crossover, CrossoverBase<RNG> & _real_xover, CrossoverBase<RNG> & _int_xover) :
uniform_rand_distributn(0.0, 1.0),
seed_crossover(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_crossover(seed_crossover),
random_number_gen(default_rng_crossover),
default_crossoverp(0.9),
default_real_xover(random_number_gen),
default_int_xover(random_number_gen),
probability_crossover(_probability_crossover),
real_xover(_real_xover),
int_xover(_int_xover)
{
}
CombinedRealIntCrossover(RNG & rng, double & _probability_crossover):
uniform_rand_distributn(0.0, 1.0),
seed_crossover(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_crossover(seed_crossover),
random_number_gen(rng),
default_crossoverp(0.9),
default_real_xover(random_number_gen),
default_int_xover(random_number_gen),
probability_crossover(_probability_crossover),
real_xover(default_real_xover),
int_xover(default_int_xover)
{
}
CombinedRealIntCrossover(RNG & rng, double & _probability_crossover, CrossoverBase<RNG> & _real_xover, CrossoverBase<RNG> & _int_xover) :
uniform_rand_distributn(0.0, 1.0),
seed_crossover(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_crossover(seed_crossover),
random_number_gen(rng),
default_crossoverp(0.9),
default_real_xover(random_number_gen),
default_int_xover(random_number_gen),
probability_crossover(_probability_crossover),
real_xover(_real_xover),
int_xover(_int_xover)
{
}
CombinedRealIntCrossover & setProbabilityCrossover(double & _probability_crossover)
{
probability_crossover = _probability_crossover;
return (*this);
}
void
operator()(PopulationSPtr pop)
{
pop->invalidate();
std::shuffle(pop->begin(), pop->end(), random_number_gen);
// int count = 0;
// int x_count = 0;
if (pop->size() >= 2)
{
for (int i = 0; i <= pop->size()-2; i = i + 2)
{
// std::cout << "crossover ind " << i << std::endl;
// ++count;
double rand = uniform_rand_distributn(random_number_gen);
// std::cout << rand << "\n";
if (rand <= probability_crossover)
{
// ++x_count;
IndividualSPtr parent1 = (*pop)[i];
IndividualSPtr parent2 = (*pop)[i+1];
real_xover(*parent1, *parent2);
int_xover(*parent1, *parent2);
}
}
}
// std::cout << "Crossover: " << x_count << " of " << count << " iterations underwent: " << 100 * double(x_count) / count << "%\n";
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(probability_crossover);
ar & BOOST_SERIALIZATION_NVP(real_xover);
ar & BOOST_SERIALIZATION_NVP(int_xover);
}
};
#endif /* Crossover_h */