-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsga.cpp
653 lines (575 loc) · 16.9 KB
/
nsga.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
// random seed from clock for the random engine
unsigned seed = std::chrono::steady_clock::now().time_since_epoch().count();
// engine for producing random numbers using the Mersenne twister algorithm
std::mt19937 rand_engine(seed);
const std::string nsga_file = "nsga_populations";
const std::string feasible_file = "feasible_population";
struct
limits {
// lower limit of search interval
double XL;
// upper limit of search interval
double XU;
};
struct
parameters {
// number of variables
int NVAR;
// number of bits in encoding of a variable
std::vector<int> NS;
// search interval for a variable
std::vector<limits> LIMS;
// step size in the search interval (calculated from N)
std::vector<double> STEPS;
// number of members in a population
int NP;
// number of generations to stop after
int MAX_GEN;
// probability of crossover
double PC;
// probability of mutation
double PM;
// number of objectives
int NOBJ;
// number of constraints
int NCON;
// whether to write the feasible population to a file or not
// the feasible population will be written to STDOUT regardless
bool TOPLOT;
};
// display column width and precision parameters
int COLW = 16, COLP = 8;
// parameters for NSGA (provided by user)
parameters params;
// ---------------------------------------------------------------------------80
bool
gen_prob(double p) {
// generates true with given probability p
std::bernoulli_distribution d(p);
return d(rand_engine);
}
int
get_xb(double x, double xl, double step) {
// returns the xb value for a given value of x (in integer form)
double xb_d = (x - xl) / step;
int xb = static_cast<int>(round(xb_d));
return xb;
}
double
get_x(int xb, double xl, double step) {
// returns the x value for a given value of xb (in double form)
double x = xl + (xb * step);
return x;
}
void
get_params() {
params.TOPLOT = true;
std::cout
<< "Do you want the feasible population in a file for plotting? (Y/n): ";
char ans = 0;
std::cin >> ans;
if(ans == 'n' || ans == 'N') {
params.TOPLOT = false;
}
std::cout << "Number of variables: ";
std::cin >> params.NVAR;
for(int i=0; i<params.NVAR; i++) {
std::cout << "Number of bits in encoding of var" << (i+1) << ": ";
int n;
std::cin >> n;
params.NS.push_back(n);
limits lim;
std::cout << "Lower limit for var" << (i+1) << ": ";
std::cin >> lim.XL;
std::cout << "Upper limit for var" << (i+1) << ": ";
std::cin >> lim.XU;
params.LIMS.push_back(lim);
// step size in the interval if the binary encoding is n-bits
double step = (lim.XU - lim.XL) / ((1UL << n) - 1);
params.STEPS.push_back(step);
}
std::cout << "Number of members in a population: ";
std::cin >> params.NP;
std::cout << "Number of generations to stop after: ";
std::cin >> params.MAX_GEN;
std::cout << "Probability of crossover: ";
std::cin >> params.PC;
std::cout << "Probability of mutation: ";
std::cin >> params.PM;
std::cout << "Number of objectives: ";
std::cin >> params.NOBJ;
std::cout << "Number of constraints: ";
std::cin >> params.NCON;
}
// ---------------------------------------------------------------------------80
class
NSGAIndividual
{
public:
// the values of variables
std::vector<double> vars;
// the values of objective functions
std::vector<double> objs;
// the values of constraint penalties
std::vector<double> cons;
// rank of the individual
int rank = 0;
// crowding distance of the individual
double crowding_dist = 0.0;
// sum of absolute values of constraint violations
// note that constraints are to be converted to the form:
// g(x1...xn) >= 0 for the program to work correctly
double cons_violation = 0.0;
NSGAIndividual();
NSGAIndividual(const std::vector<double>& vars);
void calc_objs();
void flip_bit(int v, int i);
bool dominates(const NSGAIndividual& j) const;
bool wins_against(const NSGAIndividual& j) const;
void display(std::ostream& os) const;
bool operator == (const NSGAIndividual& j) const;
};
NSGAIndividual::NSGAIndividual() {
vars = std::vector<double>(params.NVAR);
objs = std::vector<double>(params.NOBJ);
cons = std::vector<double>(params.NCON);
}
NSGAIndividual::NSGAIndividual(const std::vector<double>& vars) {
this->vars = vars;
calc_objs();
}
void
NSGAIndividual::calc_objs() {
objs = std::vector<double>(params.NOBJ);
double x = vars[0];
if(x <= 1) {
objs[0] = -x;
}
else if(x <= 3) {
objs[0] = x - 2;
}
else if(x <= 4) {
objs[0] = 4 - x;
}
else {
objs[0] = x - 4;
}
objs[1] = (x - 5) * (x - 5);
for(auto& c : cons) {
if(c < 0) cons_violation += std::abs(c);
}
}
void
NSGAIndividual::flip_bit(int v, int i) {
// flips bit at i'th position
double xl = params.LIMS[v].XL;
double step = params.STEPS[v];
int xb = get_xb(vars[v], xl, step);
xb = xb ^ (1UL << i);
vars[v] = get_x(xb, xl, step);
}
bool
NSGAIndividual::dominates(const NSGAIndividual& j) const {
// returns true if this individual dominates given individual j
// from two infeasible solutions, the one with lower
// constraint violation is preferred
if(cons_violation > 0 && j.cons_violation > 0) {
return (cons_violation > j.cons_violation);
}
// a feasible solution is preferred to an infeasible solution
if(cons_violation == 0 && j.cons_violation > 0) {
return true;
}
if(cons_violation > 0 && j.cons_violation == 0) {
return false;
}
// from two feasible solutions, the one with better
// objective functions is preferred
bool worse = false, better = false;
for(int o=0; o<params.NOBJ; o++) {
// i is better than j in atleast one objective
if(objs[o] < j.objs[o]) {
better = true;
}
// i is worse than j in atleast one objective
else if(objs[o] > j.objs[o]) {
worse = true;
}
}
// this individual dominates given individual j if this individual is
// not worse than j in any of the objectives, and is better than j in
// atleast one objective
return (!worse && better);
}
bool
NSGAIndividual::wins_against(const NSGAIndividual& j) const {
// returns true if this individual wins a tournament against given
// individual j
// crowding distance is considered only when ranks are same
if(rank == j.rank) {
// if the ranks are same and this individual has a higher crowding
// distance, then it wins a tournament against the given individual j
return (crowding_dist > j.crowding_dist);
}
// if this individual has a higher rank, it wins a tournament
return (rank < j.rank);
}
void
NSGAIndividual::display(std::ostream& os) const {
// prints the individual's variables and objective function values
// to the output stream provided (can be a file or stdout)
std::ios init(nullptr);
init.copyfmt(os);
for(auto& v : vars) {
os << std::fixed
<< std::setprecision(COLP)
<< std::setw(COLW)
<< std::right << v;
}
for(auto& f : objs) {
os << std::fixed
<< std::setprecision(COLP)
<< std::setw(COLW)
<< std::right << f;
}
os << std::endl;
os.copyfmt(init);
}
bool
NSGAIndividual::operator == (const NSGAIndividual& j) const {
// this operator is overloaded so that the std::find method works
// two individuals are considered equal
// if all their variables are equal
for(int v=0; v<params.NVAR; v++) {
if(vars[v] != j.vars[v]) return false;
}
return true;
}
// ---------------------------------------------------------------------------80
class
NSGAPopulation
{
public:
std::vector<NSGAIndividual> popn;
int size;
NSGAPopulation();
void initialize();
void add(const NSGAIndividual& individual);
void add(const NSGAPopulation& individuals);
void sort();
void resize();
void copy_from(const NSGAPopulation& p);
int select_parent_index(std::vector<int>& tmp_wts, std::vector<int>& wts);
void crossover(int p1_ind, int p2_ind, NSGAPopulation& child_popn);
void mutate(int ind);
void calc_ranks_crowding();
void gen_child_popn(NSGAPopulation& child_popn);
void gen_next_popn();
void display(std::ostream& os) const;
};
NSGAPopulation::NSGAPopulation() {
size = 0;
}
void
NSGAPopulation::initialize() {
// initialize NP random individuals
while(size < params.NP) {
std::vector<double> vars(params.NVAR);
for(int v=0; v<params.NVAR; v++) {
std::uniform_int_distribution<> d(0, 1UL << params.NS[v]);
// choose random value of xb
int xb = d(rand_engine);
double xl = params.LIMS[v].XL;
double step = params.STEPS[v];
vars[v] = get_x(xb, xl, step);
}
add(NSGAIndividual(vars));
}
}
void
NSGAPopulation::add(const NSGAIndividual& individual) {
popn.push_back(individual);
size++;
}
void
NSGAPopulation::add(const NSGAPopulation& individuals) {
// adds individuals from given population
popn.insert(
popn.end(),
std::make_move_iterator(individuals.popn.begin()),
std::make_move_iterator(individuals.popn.end())
);
size = popn.size();
}
void
NSGAPopulation::sort() {
// sorts the population using the crowding tournament operator, placing
// better individuals first.
struct sort_comp {
bool operator() (const NSGAIndividual& i, const NSGAIndividual& j) {
return i.wins_against(j);
}
};
std::sort(popn.begin(), popn.end(), sort_comp());
}
void
NSGAPopulation::resize() {
// resize the population to NP individuals
// takes top NP individuals, so sort before resizing
popn.resize(params.NP);
size = params.NP;
}
void
NSGAPopulation::copy_from(const NSGAPopulation& p) {
popn = p.popn;
size = p.size;
}
int
NSGAPopulation::select_parent_index(std::vector<int>& tmp_wts,
std::vector<int>& wts) {
// Selects a prent index based on weights provided.
// Epoch: All parents are randomly selected for exactly two tournaments.
// Round: Four parents are randomly selected and the two winners of the
// tournaments are used for crossover.
std::discrete_distribution<> d(tmp_wts.begin(), tmp_wts.end());
int p_ind = d(rand_engine);
// reduce the temporary weight to 0
// so that this parent won't get selected in the same round
tmp_wts[p_ind] = 0;
// reduce the weight in further iterations
// if this weight goes to zero, it will not get
// selected for further tournaments in this epoch
wts[p_ind]--;
return p_ind;
}
void
NSGAPopulation::crossover(int p1_ind, int p2_ind, NSGAPopulation& child_popn) {
std::vector<double> c1vars(params.NVAR), c2vars(params.NVAR);
// crossover all variables
for(int v=0; v<params.NVAR; v++) {
int N = params.NS[v];
// generate random crossover point in interval (1, N-2)
// the positions before 1st bit and after last bit
// are not in the selection pool
std::uniform_int_distribution<> d(1, N - 1);
int xpt = d(rand_engine);
double xl = params.LIMS[v].XL;
double step = params.STEPS[v];
int p1_xb = get_xb(popn[p1_ind].vars[v], xl, step);
int p2_xb = get_xb(popn[p2_ind].vars[v], xl, step);
// mask for first part
int fm = ((1UL << N) - 1) << (N - xpt);
// mask for second part
int sm = ((1UL << N) - 1) >> xpt;
// first and second parts of parent 1
int p1_f = (p1_xb & fm), p1_s = (p1_xb & sm);
// first and second parts of parent 1
int p2_f = (p2_xb & fm), p2_s = (p2_xb & sm);
// compbine first part of parent 1 with second part of parent 2
int c1 = p1_f | p2_s;
c1vars[v] = get_x(c1, xl, step);
// compbine first part of parent 2 with second part of parent 1
int c2 = p2_f | p1_s;
c2vars[v] = get_x(c2, xl, step);
}
child_popn.add(NSGAIndividual(c1vars));
child_popn.add(NSGAIndividual(c2vars));
}
void
NSGAPopulation::mutate(int ind) {
// mutates the individual at given index
// iterate through the variables
for(int v=0; v<params.NVAR; v++) {
// iterate through the bits of the variable
for(int i=0; i<params.NS[v]; i++) {
// if true is generated (with probability PM), flip the bit
if(gen_prob(params.PM)) {
popn[ind].flip_bit(v, i);
}
}
}
}
void
NSGAPopulation::calc_ranks_crowding() {
// calculates ranks and crowding distances for individuals
double inf = std::numeric_limits<double>::infinity();
// maximum and minimum objectives in the population
std::vector<double> max_objs(params.NOBJ, -inf);
std::vector<double> min_objs(params.NOBJ, inf);
for(int o=0; o<params.NOBJ; o++) {
for(auto& i : popn) {
if(i.objs[o] > max_objs[o]) max_objs[o] = i.objs[o];
if(i.objs[o] < min_objs[o]) min_objs[o] = i.objs[o];
}
}
// whether i'th individual is deleted or not
std::vector<bool> deleted(size, false);
// total number of deleted individuals
int c_deleted = 0;
NSGAPopulation rankedpopn;
// non-dominated fronts are made till all individuals are deleted
for(int rank = 1; c_deleted < size; rank++) {
// current non-dominated front
NSGAPopulation ndf;
std::vector<int> ndf_inds;
for(int i=0; i < size; i++) {
if(deleted[i]) continue;
bool dominated = false;
for(int j=0; j < size; j++) {
if((j == i) || deleted[j]) continue;
if(popn[j].dominates(popn[i])) {
dominated = true;
break;
}
}
if(!dominated) {
popn[i].rank = rank;
ndf_inds.push_back(i);
}
}
for(auto& i : ndf_inds) {
ndf.add(popn[i]);
deleted[i] = true;
c_deleted++;
}
// for every objective
for(int o=0; o<params.NOBJ; o++) {
// sort the non-dominated front in worse order
std::sort(ndf.popn.begin(), ndf.popn.end(),
[o](const auto& i, const auto& j) {
return (i.objs[o] < j.objs[o]);
});
// most separated individuals are given highest crowding distance
ndf.popn[0].crowding_dist = inf;
ndf.popn[ndf.size - 1].crowding_dist = inf;
// calculate crowding distances for rest of the individuals
for(int i=1; i<ndf.size - 1; i++) {
if(std::isinf(ndf.popn[i].crowding_dist)) continue;
// numerator
double num = ndf.popn[i+1].objs[o] - ndf.popn[i-1].objs[o];
// denominator
double den = max_objs[o] - min_objs[o];
// update crowding distance
ndf.popn[i].crowding_dist += (num / den);
}
}
// and add the individuals to ranked population
rankedpopn.add(ndf);
}
// replace this population by ranked population
copy_from(rankedpopn);
// sort the population according to crowding tournament operator
sort();
}
void
NSGAPopulation::gen_child_popn(NSGAPopulation& child_popn) {
// overall weights according to which parents are selected in an epoch
// this is maintained so that we can keep track of how many tournaments
// an individual has participated in
// if this weight reduces to zero (individual has participated in two
// tournaments), it will not get selected for another tournament
std::vector<int> wts(size, 2);
// note that we are selecting 4 parents in a round
// so the population size NP should be divisible by 4
for(int num_tmnts=0; num_tmnts < size/2; num_tmnts++) {
// copy the epoch weights as temporary weights for each round
std::vector<int> tmp_wts = wts;
// select four parents for two tournaments
int ind1, ind2, ind3, ind4;
int p1, p2;
ind1 = select_parent_index(tmp_wts, wts);
ind2 = select_parent_index(tmp_wts, wts);
ind3 = select_parent_index(tmp_wts, wts);
ind4 = select_parent_index(tmp_wts, wts);
// use the winners for crossover
if(popn[ind1].wins_against(popn[ind2])) p1 = ind1;
else p1 = ind2;
if(popn[ind3].wins_against(popn[ind4])) p2 = ind3;
else p2 = ind4;
// crossover
bool cross = gen_prob(params.PC);
if(!cross) continue;
crossover(p1, p2, child_popn);
// mutate the newly added children
child_popn.mutate(child_popn.size - 1);
child_popn.mutate(child_popn.size - 2);
}
}
void
NSGAPopulation::gen_next_popn() {
// generates child population Qt
NSGAPopulation q, r;
gen_child_popn(q);
// combined population Rt
r.add(*this);
r.add(q);
r.calc_ranks_crowding();
r.sort();
r.resize();
// replace this population by Rt for next iteration (t+1)
copy_from(r);
}
void
NSGAPopulation::display(std::ostream& os) const {
// displays the population to the given output stream
std::ios init(nullptr);
init.copyfmt(os);
os << std::setfill('#')
<< std::setw((params.NOBJ + params.NVAR) * COLW) << ""
<< std::endl;
os << std::setfill(' ');
for(int v=1; v<=params.NVAR; v++) {
os << std::setw(COLW)
<< std::left << "var" + std::to_string(v);
}
for(int i=1; i<=params.NOBJ; i++) {
os << std::setw(COLW)
<< std::left << "f" + std::to_string(i);
}
os << std::endl;
os << std::setfill('#')
<< std::setw((params.NOBJ + params.NVAR) * COLW) << ""
<< std::endl;
os << std::setfill(' ');
for(auto& i : popn) {
i.display(os);
}
os.copyfmt(init);
}
// ---------------------------------------------------------------------------80
int
main() {
std::ofstream of;
std::cout << std::endl << "NOTE: The number of members in a "
<< "population should be divisible by 4" << std::endl;
std::cout << std::endl;
get_params();
of.open(nsga_file);
NSGAPopulation p, q;
p.initialize();
p.calc_ranks_crowding();
of << "Generation 1" << std::endl;
p.display(of);
for(int i=2; i<=params.MAX_GEN; i++) {
p.gen_next_popn();
of << "Generation " << i << std::endl;
p.display(of);
}
of.close();
std::cout << std::endl;
std::cout << "Feasible Population" << std::endl;
p.display(std::cout);
if(params.TOPLOT) {
of.open(feasible_file);
p.display(of);
of.close();
}
return 0;
}