-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathga.cpp
More file actions
267 lines (198 loc) · 6.38 KB
/
Copy pathga.cpp
File metadata and controls
267 lines (198 loc) · 6.38 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
#include <string>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define POP_SIZE 100
#define LENGTH 150
#define MAX_GENERATIONS 20000000
// Defined a data structure for type
struct type
{
string bits;
double fitness;
type(): bits(""), fitness(0.0){};
type(string bts, double ftns): bits(bts), fitness(ftns){}
};
// Function defination
string InitializePop(int length);
double GetFitness(string bits);
void PrintBits(string bits);
string Roulette(int total_fitness, type* Population);
void Mutate(string &bits);
void Crossover(string &child1, string &child2);
double eval(int *pj);
// main
int main()
{
// Seed for rand
srand((int)time(NULL));
type Population[POP_SIZE];
type globalmax;
type localmax;
cout << endl << endl;
// Initializing Parent population.
for (int i=0; i<POP_SIZE; i++)
{
Population[i].bits = InitializePop(LENGTH);
Population[i].fitness = 0.0;
}
// Initializing global max
for (int i=0; i<POP_SIZE; i++)
{
globalmax.bits = InitializePop(LENGTH);
globalmax.fitness = 0.0;
}
int GenerationCount = 0;
//GA loop
while(true)
{
double TotalFitness = 0.0;
for (int i=0; i<POP_SIZE; i++)
{
localmax.bits = InitializePop(LENGTH);
localmax.fitness = 0.0;
}
// Check the fitness of this population and update Local max
for (int i=0; i<POP_SIZE; i++)
{
Population[i].fitness = GetFitness(Population[i].bits);
if(localmax.fitness < Population[i].fitness){
localmax = type(Population[i].bits, Population[i].fitness);
// cout<<"Current chromosome ="<<Population[i].fitness <<endl;
//cout<<"Local maximum ="<<localmax.fitness <<endl;
// PrintBits(localmax.bits);
}
TotalFitness += Population[i].fitness;
}
// Check the local max fitness and update global max
if(globalmax.fitness <= localmax.fitness){
globalmax = type(localmax.bits, localmax.fitness);
cout<<"Global maximum ="<< globalmax.fitness <<endl;
PrintBits(globalmax.bits);
}
//Temporary array of individuals for storing children
type temp[POP_SIZE];
int cPop = 0;
//Creating Population sized children
while (cPop < POP_SIZE)
{
string child1 = Roulette(TotalFitness, Population);
string child2 = Roulette(TotalFitness, Population);
//Do crossover
Crossover(child1, child2);
//mutate
Mutate(child1);
Mutate(child2);
//Storing children in temp array
temp[cPop++] = type(child1, 0.0);
temp[cPop++] = type(child2, 0.0);
}//end loop
//Make children new parents
for (int i=0; i<POP_SIZE; i++)
{
Population[i] = temp[i];
}
++GenerationCount;
// exit within the maximum allowable generations and return Global max
if (GenerationCount > MAX_GENERATIONS)
{
cout << "Solution found is"<<endl;
cout<<"Global maximum ="<< globalmax.fitness <<endl;
PrintBits(globalmax.bits);
return 0;
}
}
return 0;
}
// This function initializes parent population with random bits
string InitializePop(int length)
{
string bits;
double randomNum;
for (int i=0; i<length; i++)
{
randomNum = ((double)rand()/(RAND_MAX));
if (randomNum > 0.5)
bits += "1";
else
bits += "0";
}
return bits;
}
// This function converts string to integer array and returns fitness from eval
double GetFitness(string bits)
{
//cout<<"String = "<<bits<<endl;
int buffer[LENGTH];
double tempfitness;
//cout<< "Array =";
std::transform( bits.begin(), bits.end(), buffer,[]( char c ) -> int { return ( c - '0' ); } );
// cout<<endl;
tempfitness = eval(buffer);
//cout<<"Fitness = "<<tempfitness<<endl;
return tempfitness;
// return result;
}
// prints bits of individual to screen
void PrintBits(string bits)
{
cout<< "Individual = "<<bits <<endl;
return;
}
// Mutates based on the MUTATION_RATE
void Mutate(string &bits)
{
double randomNum;
for (int i=0; i<bits.length(); i++)
{
randomNum = ((double)rand()/(RAND_MAX));
if (randomNum < MUTATION_RATE)
{
if (bits.at(i) == '1')
bits.at(i) = '0';
else
bits.at(i) = '1';
}
}
return;
}
// Do crossover between parent based on crossover probability
void Crossover(string &child1, string &child2)
{
double randomNum = ((double)rand()/(RAND_MAX));
//cout<<"Entering crossover func"<<endl;
//dependent on the crossover rate
if (randomNum < CROSSOVER_RATE)
{
//create a random crossover point
int crossover = (int) (randomNum * LENGTH);
string t1 = child1.substr(0, crossover) + child2.substr(crossover, LENGTH);
string t2 = child2.substr(0, crossover) + child1.substr(crossover, LENGTH);
child1 = t1; child2 = t2;
}
//cout<<"Exiting crossover func"<<endl;
}
// Roulette wheel selection
string Roulette(int total_fitness, type* Population)
{
double randomNum = ((double)rand()/(RAND_MAX));
//generate a random number between 0 & total fitness count
double Slice = (double)(randomNum * total_fitness);
// cout << "Slice" << Slice<<endl;
//go through the chromosones adding up the fitness so far
double FitnessSoFar = 0.0;
for (int i=0; i<POP_SIZE; i++)
{
FitnessSoFar += Population[i].fitness;
//cout << "FitnessSoFar" << FitnessSoFar<<endl;
//if the fitness so far > random number return the chromo at this point
if (FitnessSoFar >= Slice)
return Population[i].bits;
}
return "";
}