-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_algorithm.cpp
More file actions
620 lines (524 loc) · 14.3 KB
/
genetic_algorithm.cpp
File metadata and controls
620 lines (524 loc) · 14.3 KB
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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ctime>
#include <math.h>
#include <iomanip>
#include <conio.h>
#include <fstream>
#include <chrono>
/*****************************************************
* This is a Minimization optimization problem.
* Our fitness function is :
* *** Fitness function : 1 / Object function
*
* We change Gene in mutation insteed of revers or exchange
* strategy, because these strategy doesn't give us a new
* chromosom.
*
* ________________________________________________________________
* | # | Variables desc | Vars | search key |
* ________________________________________________________________
* | 1 | Population size | POPSIZE | POPSIZE |
* ________________________________________________________________
* | 2 | Max Generation | MAXGENS | MAXGENS |
* ________________________________________________________________
* | 3 | Number of Genes | NGENS | NGENS |
* ________________________________________________________________
* | 4 | Crossover rate | PXOVER | PXOVER |
* ________________________________________________________________
* | 5 | Mutation rate | PMUTATION | PMUTATION |
* ________________________________________________________________
* | 6 | Final answer of obF | final_answer | final_answer |
* ________________________________________________________________
* | 7 | Lower bound of alleles | lbound | lbound |
* ________________________________________________________________
* | 8 | Upper bound of alleles | ubound | ubound |
* ________________________________________________________________
* | 9 | popluation | population | population |
* ________________________________________________________________
* | 10| new popultaion | newpopulation | newpopulation |
* | | (Copy of new population)| | |
* ________________________________________________________________
*
*
*
*
* _______________________________________________________________________________________
* | # | Functions desc | Function name | search key |
* _______________________________________________________________________________________
* | 1 | Generate random int | random_integer_generator | #random_integer |
* | | between two integer | | |
* | | number | | |
* _______________________________________________________________________________________
* | 2 | Generate random double | random_double_generator | #random_double |
* _______________________________________________________________________________________
* | 3 | Show current time | timestamp | #timestamp |
* _______________________________________________________________________________________
* | 4 | Show chromosom with | report | #report_chromosom |
* | | Custom index | | |
* _______________________________________________________________________________________
* | 5 | Log chromosom into file | insert_into_file | #file_logger |
* | | with custom index | | |
* _______________________________________________________________________________________
* | 6 | Generate start pop | initialize | #starter_population |
* _______________________________________________________________________________________
* | 7 | Evaluate chromosom fit | evaluate | #fitness_calc |
* _______________________________________________________________________________________
* | 8 | Keep best of starter pop| keep_the_best | #first_elitism |
* | | Like we run elitism for | | |
* | | our first population | | |
* _______________________________________________________________________________________
* | 9 | Reproduction the new | selector | #reproduction |
* | | new generation | | |
* | | Roulette Wheel strategy | | |
* _______________________________________________________________________________________
* | 10| Crossover function | crossover | #crossover |
* | | Single point strategy | | |
* | | Swap left side of point | | |
* _______________________________________________________________________________________
* | 11| Do crossover on two | combination | #combination |
* | | selected parent. | | |
* | | It's used inside | | |
* | | crossover function | | |
* _______________________________________________________________________________________
* | 12| Elitism on population | elitist | #elitism |
* | | Change with a worst | | |
* | | member of population | | |
* _______________________________________________________________________________________
* | 12| Mutate on population | mutate | #mutation |
* | | Change genes value with | | |
* | | new random generation | | |
* _______________________________________________________________________________________
*
*
*
*
*
******************************************************/
using namespace std;
#define POPSIZE 5
#define MAXGENS 5000
#define NGENS 12
#define PXOVER 0.15
#define PMUTATION 0.05
double const final_answer = 150;
/***
* Our alleles for genes must be unsigned numbers because genes are a quantity of an order
* 0 <= Gene[i] <= final_answer
* But we knows that if one gene value equals to 0 or final answer, the others should equal to final answer or 0. But we don't want Gene with 0 value.
* Because we don't need order with 0 quantity.
*/
double lbound = 0.01;
double ubound = final_answer / 2;
struct Chromosom
{
double gene[NGENS];
double fitness;
double rfitness;
double cfitness;
};
struct Chromosom population[POPSIZE + 1];
struct Chromosom newpopulation[POPSIZE + 1];
// #random_integer
int random_integer_generator(int a, int b);
// #random_double
double random_double_generator(double a, double b);
// #timestamp
void timestamp();
// #report_chromosom
void report(int generation);
// #file_logger
void insert_into_file(int generation);
// #starter_population
void initialize();
// #fitness_calc
void evaluate();
// #first_elitism
void keep_the_best();
// #reproduction
void selector();
// #crossover
void crossover();
// #combination
void combination(int one, int two);
// #elitism
void elitist();
// #mutation
void mutate();
int main()
{
int generation;
int i;
timestamp();
auto start = chrono::high_resolution_clock::now();
srand((unsigned)time(NULL));
cout << "Σx[i] = N : " << final_answer << endl;
//Initialize the starter population
initialize();
evaluate();
keep_the_best();
//insert_into_file(0);
for (generation = 0; generation < MAXGENS; generation++)
{
selector();
crossover();
mutate();
//report(generation);
//insert_into_file(generation);
evaluate();
elitist();
}
auto stop = chrono::high_resolution_clock::now();
cout << "Exec time after genetic : " << (stop - start).count() << "\n";
cout << "\n";
cout << "Best member after" << MAXGENS << "generations:\n";
cout << "\n";
for (i = 0; i < NGENS; i++)
{
cout << fixed << "var(" << i << ") = " << population[POPSIZE].gene[i] << "\n";
}
cout << "\n";
cout << "Best fitness =" << population[POPSIZE].fitness << "\n";
cout << "Normal end of execution.\n";
cout << "\nΣx[i] : ";
double sum = 0;
for (i = 0; i < NGENS; i++)
{
sum += population[POPSIZE].gene[i];
}
cout << sum << endl<<endl;
timestamp();
_getch();
return 0;
}
// #timestamp
void timestamp()
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct tm* tm;
size_t len;
time_t now;
now = time(NULL);
tm = localtime(&now);
len = strftime(time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm);
cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}
// #starter_population
void initialize()
{
int i;
int j;
//Initialize genes within the bounds
for (i = 0; i < NGENS; i++)//Foreach Chromosom
{
for (j = 0; j < POPSIZE; j++)//Foreach Genes
{
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].gene[i] = random_double_generator(lbound, ubound);
}
}
return;
}
// #random_double
double random_double_generator(double a, double b)
{
double tmp;
tmp = a + (static_cast<double>(rand()) / RAND_MAX) * (b - a);
return tmp;
}
// #fitness_calc
void evaluate()
{
int member;
int i;
//The current function is: Σ x[i]
for (member = 0; member < POPSIZE; member++)
{
double sum = 0;
for (i = 0; i < NGENS; i++)
{
sum += population[member].gene[i];
}
population[member].fitness = 1 / (1 + abs(sum - final_answer)); //fitness function = 1 / (1 + object function)
}
}
// #first_elitism
void keep_the_best()
{
int best_member;
int mem;
int i;
best_member = 0;
for (mem = 0; mem < POPSIZE; mem++)
{
if (population[POPSIZE].fitness < population[mem].fitness)//minimize fitness_1 < fitness_2 then fitness_2 is better
{
best_member = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
for (i = 0; i < NGENS; i++)
{
population[POPSIZE].gene[i] = population[best_member].gene[i];
}
return;
}
// #reproduction
void selector()
{
const double a = 0.0;
const double b = 1.0;
int i;
int j;
int mem;
double p;
double total_fitness;
/*
* Find the total fitness of the population.
*/
total_fitness = 0.0;
for (mem = 0; mem < POPSIZE; mem++)
{
total_fitness = total_fitness + population[mem].fitness;
}
/*
* Calculate the relative fitness of each member.
*/
for (mem = 0; mem < POPSIZE; mem++)
{
population[mem].rfitness = population[mem].fitness / total_fitness;
}
/*
* Calculate the cumulative fitness.
*/
population[0].cfitness = population[0].rfitness;
for (mem = 1; mem < POPSIZE; mem++)
{
population[mem].cfitness = population[mem - 1].cfitness +
population[mem].rfitness;
}
/*
* Select survivors using cumulative fitness. Rollet wheel
*/
for (i = 0; i < POPSIZE; i++)
{
p = random_double_generator(a, b);
if (p < population[0].cfitness) // if p is lower than first population we pick first
{
newpopulation[i] = population[0];
}
else // else we should check every population the population[j].cfitness >= p && p <population[j + 1].cfitness
{
for (j = 0; j < POPSIZE; j++)
{
if (population[j].cfitness >= p && p < population[j + 1].cfitness)
{
newpopulation[i] = population[j + 1];
break;
}
}
}
}
/*
* Overwrite the old population with the new one.
*/
for (i = 0; i < POPSIZE; i++)
{
population[i] = newpopulation[i];
}
return;
}
// #crossover
void crossover()
{
const double a = 0.0;
const double b = 1.0;
int mem;
int one;
int first = 0;
double p;
for (mem = 0; mem < POPSIZE; ++mem)
{
p = random_double_generator(a, b);
if (p < PXOVER)
{
++first;
if (first % 2 == 0) // we need 2 parents for combination. if first is even this means we pick first one and need the second one
{
combination(one, mem);
}
else
{
one = mem;
}
}
}
return;
}
// #combination
void combination(int one, int two)
{
int i;
int point;
double t;
//Select the crossover point.(single point)
point = random_integer_generator(0, NGENS - 1);
//
//Swap genes in positions 0 through POINT-1. (Left side of point)
//
for (i = 0; i < point; i++)
{
t = population[one].gene[i];
population[one].gene[i] = population[two].gene[i];
population[two].gene[i] = t;
}
return;
}
// #random_integer
int random_integer_generator(int a, int b)
{
int tmp;
tmp = (rand() % (b - a + 1)) + a;
return tmp;
}
// #mutation
void mutate()
{
const double a = 0.0;
const double b = 1.0;
int i;
int j;
double x;
for (i = 0; i < POPSIZE; i++)
{
for (j = 0; j < NGENS; j++)
{
x = random_double_generator(a, b);
if (x < PMUTATION)
{
population[i].gene[j] = random_double_generator(lbound, ubound);
}
}
}
return;
}
// #report_chromosom
void report(int generation)
{
double avg;
double best_val;
int i;
double square_sum;
double stddev;
double sum;
double sum_square;
if (generation == 0)
{
cout << "\n";
cout << "Generation Best Average Standard/n";
cout << "number value fitness deviation/n";
cout << "\n";
}
sum = 0.0;
sum_square = 0.0;
for (i = 0; i < POPSIZE; i++)
{
sum = sum + population[i].fitness;
sum_square = sum_square + population[i].fitness * population[i].fitness;
}
avg = sum / (double)POPSIZE;
square_sum = avg * avg * POPSIZE;
stddev = sqrt((sum_square - square_sum) / (POPSIZE - 1));
best_val = population[POPSIZE].fitness;
cout << "" << setw(8) << generation
<< "" << setw(14) << best_val
<< "" << setw(14) << avg
<< "" << setw(14) << stddev << "\n";
return;
}
// #elitism
void elitist()
{
int i;
double best;
int best_mem;
double worst;
int worst_mem;
best = population[0].fitness;
worst = population[0].fitness;
for (i = 0; i < POPSIZE - 1; ++i)
{
if (population[i + 1].fitness < population[i].fitness)
{
if (best <= population[i].fitness)
{
best = population[i].fitness;
best_mem = i;
}
if (population[i + 1].fitness <= worst)
{
worst = population[i + 1].fitness;
worst_mem = i + 1;
}
}
else
{
if (population[i].fitness <= worst)
{
worst = population[i].fitness;
worst_mem = i;
}
if (best <= population[i + 1].fitness)
{
best = population[i + 1].fitness;
best_mem = i + 1;
}
}
}
//
//If the best individual from the new population is better than
//the best individual from the previous population, then
//copy the best from the new population; else replace the
//worst individual from the current population with the
//best one from the previous generation
//
if (population[POPSIZE].fitness <= best)
{
for (i = 0; i < NGENS; i++)
{
population[POPSIZE].gene[i] = population[best_mem].gene[i];
}
population[POPSIZE].fitness = population[best_mem].fitness;
}
else
{
for (i = 0; i < NGENS; i++)
{
population[worst_mem].gene[i] = population[POPSIZE].gene[i];
}
population[worst_mem].fitness = population[POPSIZE].fitness;
}
return;
}
// #file_logger
void insert_into_file(int generation) {
fstream MyFile;
MyFile.open("filename.txt", std::ios_base::app | std::ios_base::in);
MyFile << "#" << generation << " : \n";
for (int i = 0; i < POPSIZE; i++)
{
double sum = 0;
for (int j = 0; j < NGENS; j++)
{
MyFile << "[" << population[i].gene[j] << "] ";
sum += population[i].gene[j];
}
MyFile << " ==> " << sum << "\n";
}
MyFile.close();
}