-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMutation.hpp
468 lines (371 loc) · 12.5 KB
/
Mutation.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//
// Mutation.hpp
// parallel-nsgaII-backend
//
// Created by a1091793 on 21/11/2015.
// Copyright © 2015 University of Adelaide. All rights reserved.
//
#ifndef Mutation_h
#define Mutation_h
#include <random>
#include <chrono>
#include "Population.hpp"
#include <boost/serialization/nvp.hpp>
template <typename RNG = std::mt19937>
class MutationBase
{
protected:
double default_mutation_probability;
double & probability_mutation;
unsigned seed_mutation;
RNG default_rng_mutation;
RNG & random_number_gen;
std::uniform_real_distribution<double> uniform_rand_distributn;
public:
MutationBase()
: default_mutation_probability(0.10),
probability_mutation(default_mutation_probability),
seed_mutation(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_mutation(seed_mutation),
random_number_gen(default_rng_mutation),
uniform_rand_distributn(0.0, 1.0)
{
}
MutationBase(RNG & rng)
: default_mutation_probability(0.10),
probability_mutation(default_mutation_probability),
seed_mutation(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_mutation(seed_mutation),
random_number_gen(rng),
uniform_rand_distributn(0.0, 1.0)
{
}
MutationBase(double & _probability_mutation)
: default_mutation_probability(0.10),
probability_mutation(_probability_mutation),
seed_mutation(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_mutation(seed_mutation),
random_number_gen(default_rng_mutation),
uniform_rand_distributn(0.0, 1.0)
{
}
MutationBase(RNG & rng, double & _probability_mutation)
: default_mutation_probability(0.10),
probability_mutation(_probability_mutation),
seed_mutation(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_mutation(seed_mutation),
random_number_gen(rng),
uniform_rand_distributn(0.0, 1.0)
{
}
virtual void operator() (Individual & individual) = 0;
virtual MutationBase &
setMutationProbability(double _mutation_probability)
{
probability_mutation = _mutation_probability;
return (*this);
}
void
setMutationInverseDVSize(IndividualSPtr _ind_sample)
{
setMutationProbability(1 / double(_ind_sample->numberOfIntDecisionVariables() + _ind_sample->numOfRealDVs()));
}
};
template <typename RNG = std::mt19937>
class DebsPolynomialMutation : public MutationBase<RNG>
{
double default_eta_m;
double eta_m;
int gene_m_count = 0;
int gene_count = 0;
public:
//Mutation is on a per chromosone basis
DebsPolynomialMutation():
default_eta_m(20.0),
eta_m(default_eta_m)
{
}
//Mutation is on a per chromosone basis
DebsPolynomialMutation(RNG & rng):
default_eta_m(20.0),
MutationBase<RNG>(rng),
eta_m(default_eta_m)
{
}
DebsPolynomialMutation(RNG & rng, double & eta, double & _probability_mutation) :
default_eta_m(20.0),
MutationBase<RNG>(rng, _probability_mutation),
eta_m(eta)
{
}
void
operator()(Individual & individual)
{
for (Individual::RealDVsT::size_type j=0; j < individual.numOfRealDVs(); j++)
{
// ++gene_count;
if (uniform_rand_distributn(random_number_gen) <= probability_mutation)
{
// ++gene_m_count;
// individual.mutated = true;
double delta1, delta2, xy, val, deltaq;
const double & current_val = individual.getRealDV(j);
const double lower_bound_val=individual.getRealLowerBound(j);//lower limt of variable j of ind i
const double upper_bound_val=individual.getRealUpperBound(j);//upper limt of variable j of ind i
delta1 = (current_val-lower_bound_val)/(upper_bound_val-lower_bound_val);
delta2 = (upper_bound_val-current_val)/(upper_bound_val-lower_bound_val);
double rand = uniform_rand_distributn(random_number_gen);
double mut_pow = 1.0/(eta_m+1.0);
if (rand <= 0.5)
{
xy = 1.0-delta1;
val = 2.0*rand+(1.0-2.0*rand)*(pow(xy,(eta_m+1.0)));
deltaq = pow(val,mut_pow) - 1.0;
}
else
{
xy = 1.0-delta2;
val = 2.0*(1.0-rand)+2.0*(rand-0.5)*(pow(xy,(eta_m+1.0)));
deltaq = 1.0 - (pow(val,mut_pow));
}
double new_val = current_val + deltaq*(upper_bound_val-lower_bound_val);
if (new_val<lower_bound_val)
new_val = lower_bound_val;
if (new_val>upper_bound_val)
new_val = upper_bound_val;
individual.setRealDV(j, new_val);
}
}
return;
}
virtual DebsPolynomialMutation&
setMutationProbability(double _mutation_probability)
{
probability_mutation = _mutation_probability;
return (*this);
}
DebsPolynomialMutation &
setEtaM(double _eta_m)
{
eta_m = _eta_m;
return (*this);
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(eta_m);
ar & BOOST_SERIALIZATION_NVP(probability_mutation);
}
};
template <typename RNG = std::mt19937>
class UniformIntMutation : public MutationBase<RNG>
{
int gene_m_count = 0;
int gene_count = 0;
public:
//Mutation is on a per chromosone basis
UniformIntMutation()
{
}
UniformIntMutation(RNG & rng) :
MutationBase<RNG>(rng)
{
}
UniformIntMutation(RNG & rng, double & _probability_mutation) :
MutationBase<RNG>(rng, _probability_mutation)
{
}
void
operator()(Individual & individual)
{
for (unsigned long j=0; j < individual.numOfUnorderedDVs(); j++)
{
// ++gene_count;
if (uniform_rand_distributn(random_number_gen) <= probability_mutation)
{
std::uniform_int_distribution<int> distribution(individual.getUnorderedLowerBound(j),individual.getUnorderedUpperBound(j));
individual.setUnorderedDV(j, distribution(random_number_gen));
}
}
return;
}
virtual UniformIntMutation&
setMutationProbability(double _mutation_probability)
{
probability_mutation = _mutation_probability;
return (*this);
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(probability_mutation);
}
};
template <typename RNG = std::mt19937>
class CreepMutation : public MutationBase<RNG>
{
public:
CreepMutation()
{
}
/**
*
* @param _epsilon the range for uniform nutation
* @param _p_change the probability to change a given coordinate
* @param _p_change_down probability of changing to a lower value
*/
CreepMutation(double _p_change = 0.05, double _p_change_down = 0.65)
: MutationBase(_p_change), p_change_down(_p_change_down), count1(0), count2(0),
{
}
/**
* Do it!
* @param _eo The indi undergoing the mutation
*/
void
operator()(Individual& individual)
{
//bool hasChanged = false;
for (Individual::OrderedDVsT::size_type i = 0; i < individual.numOfOrderedDVs(); i++)
{
if (uniform_rand_distributn(random_number_gen) <= probability_mutation)
{
if (uniform_rand_distributn(random_number_gen) <= p_change_down)
{
individual.setOrderedDV(i,individual.getOrderedDV(i)-1);
if (individual.getOrderedDV(i) < individual.getOrderedLowerBound(i))
{
individual.setOrderedDV(i, individual.getOrderedUpperBound(i));
}
}
else
{
individual.setOrderedDV(i, individual.getOrderedDV(i) + 1);
if (individual.getOrderedDV(i) > individual.getOrderedUpperBound(i))
{
individual.setOrderedDV(i, individual.getOrderedLowerBound(i));
}
}
//hasChanged = true;
count2++;
}
}
count1++;
}
virtual CreepMutation&
setMutationProbability(double _mutation_probability)
{
probability_mutation = _mutation_probability;
return (*this);
}
CreepMutation&
setCreepDownProbability(double _p_change_down)
{
p_change_down = _p_change_down;
return (*this);
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar& BOOST_SERIALIZATION_NVP(probability_mutation);
ar& BOOST_SERIALIZATION_NVP(p_change_down);
ar& BOOST_SERIALIZATION_NVP(count1);
ar& BOOST_SERIALIZATION_NVP(count2);
}
private:
//double p_change; // the proba that each variable is modified
double p_change_down; // the proba that each variable is changed down
int count1;
int count2;
};
template <typename RNG = std::mt19937>
class CombinedRealIntMutation
{
unsigned seed_mutation;
RNG default_rng_mutation;
RNG & random_number_gen;
DebsPolynomialMutation<> default_real_mutation;
UniformIntMutation<> default_unordered_mutation;
CreepMutation<> default_ordered_mutation;
MutationBase<RNG> & real_mut;
MutationBase<RNG> & unordered_mut;
MutationBase<RNG> & ordered_mut;
public:
//Mutation is on a per chromosone basis
CombinedRealIntMutation() :
seed_mutation(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_mutation(seed_mutation),
random_number_gen(default_rng_mutation),
default_real_mutation(random_number_gen),
default_unordered_mutation(random_number_gen),
real_mut(default_real_mutation),
unordered_mut(default_unordered_mutation),
ordered_mut(default_ordered_mutation)
{
}
CombinedRealIntMutation(RNG & rng) :
seed_mutation(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_mutation(seed_mutation),
random_number_gen(rng),
default_real_mutation(random_number_gen),
default_unordered_mutation(random_number_gen),
real_mut(default_real_mutation),
unordered_mut(default_unordered_mutation),
ordered_mut(default_ordered_mutation)
{
}
CombinedRealIntMutation(MutationBase<RNG> & _real_mut, MutationBase<RNG> & _unordered_mut, MutationBase<RNG> & _ordered_mut) :
seed_mutation(std::chrono::system_clock::now().time_since_epoch().count()),
default_rng_mutation(seed_mutation),
random_number_gen(default_rng_mutation),
default_real_mutation(random_number_gen),
default_unordered_mutation(random_number_gen),
real_mut(_real_mut),
unordered_mut(_unordered_mut)
ordered_mut(_ordered_mut)
{
}
void
operator()(PopulationSPtr & parent_pop)
{
// int ind_count= 0;
// int m_count = 0;
// gene_m_count = 0;
// gene_count = 0;
parent_pop->invalidate();
for (IndividualSPtr ind : parent_pop)
{
real_mut(*ind);
unordered_mut(*ind);
ordered_mut(*ind);
}
// std::cout << "Mutation: " << m_count << " of " << ind_count << " ind underwent: " << 100 * double(m_count) / ind_count << "%\n";
// std::cout << "Mutation: " << gene_m_count << " of " << gene_count << " genes underwent: " << 100 * double(gene_m_count) / gene_count << "%\n";
}
MutationBase<RNG> &
getRealMutationOperator()
{
return (real_mut);
}
MutationBase<RNG> &
getUnorderedMutationOperator()
{
return (unordered_mut);
}
MutationBase<RNG> &
getOrderedMutationOperator()
{
return (ordered_mut);
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(real_mut);
ar & BOOST_SERIALIZATION_NVP(unordered_mut);
}
};
#endif /* Mutation_h */