-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
354 lines (313 loc) · 8.86 KB
/
main.cpp
File metadata and controls
354 lines (313 loc) · 8.86 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
/* Author: Nick Nesbit
* Date: 4/4/2018
* CS4300: Project 3
* Simple GA: main.cpp
*/
//#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "individual.h"
using namespace std;
//Start of main program
int main() {
//Seed the random number generator
srand(time(0));
//Initialize the population size and string size
int popSize = 10;
int stringSize = 0;
//Declare upperBound and lowerBound varibles
int upperBound = 81920;
int lowerBound = 10;
//Pass variable determines if the global optimum was found
int pass;
//Get the string size from user
do {
cin.clear();
cout << "Enter the size of the string (2 - 50): ";
cin >> stringSize;
cin.ignore();
} while ( stringSize < 2 || stringSize > 50 );
//Termination loop
bool fail = true;
while ( popSize < 81920 && fail == true ) {
//Display population size
cout << endl << "Population size: " << popSize << endl;
//Set pass to 0
pass = 0;
//Procedural loop
for ( int count = 0; count < 5; count++ ) {
//Display the round number
cout << endl << "Round: " << count+1 << endl;
//Randomly initialize the population:
Individual* pop = new Individual[popSize];
cout << " Original population: " << endl;
for ( int i = 0; i < popSize; i++ ) {
pop[i].pattern = "";
pop[i].fitness = 0;
for ( int j = 0; j < stringSize; j++) {
if ( (rand()%2) == 0 ) {
pop[i].pattern += '0';
}
else {
pop[i].pattern += '1';
pop[i].fitness++;
}
}
}
//Declare utility variables
int totalFitness;
int lowFitness;
int highFitness;
float avgFitness;
float maxAvgFitness = 0.0;
int numGens = 0;
int itr = 0;
//Generational loop
while ( itr < 3 ) {
//Increment number of generations
numGens++;
//Initialize fitness values
totalFitness = 0;
highFitness = 0;
lowFitness = stringSize + 1;
//Calculate fitness information
for ( int i = 0; i < popSize; i++ ) {
totalFitness += pop[i].fitness;
if ( pop[i].fitness > highFitness )
highFitness = pop[i].fitness;
if ( pop[i].fitness < lowFitness)
lowFitness = pop[i].fitness;
}
avgFitness = ((float)totalFitness) / popSize;
//Handle termination details
if ( avgFitness > maxAvgFitness ) {
maxAvgFitness = avgFitness;
itr = 0;
}
else {
itr++;
}
//Display fitness information
cout << " Highest fitness: " << highFitness << endl;
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness == highFitness)
cout << " " << pop[i].pattern << endl;
}
cout << " Lowest fitness: " << lowFitness << endl;
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness == lowFitness)
cout << " " << pop[i].pattern << endl;
}
cout << " Average fitness: " << std::setprecision(3) << avgFitness << endl << endl;
//Create a new array of parents
int parentSize = (popSize - 2) / 2;
Individual* parents = new Individual[parentSize];
//Selection:
for ( int i = 0; i < parentSize; i++ ) {
int a = rand() % popSize;
int b = rand() % popSize;
while (b == a)
b = rand() % popSize;
if (pop[a].fitness >= pop[b].fitness)
parents[i] = pop[a];
else
parents[i] = pop[b];
}
//Create a new array of children
int childrenSize = popSize - 2;
Individual* children = new Individual[childrenSize];
//Recombination:
for ( int i = 0; i < childrenSize; i += 2 ) {
int p = rand() % parentSize;
int q = rand() % parentSize;
while ( q == p )
q = rand() % parentSize;
//Direct Copy
if ( (rand()%10 + 1) < 5 ) {
children[i].pattern = "";
children[i].fitness = 0;
children[i+1].pattern = "";
children[i+1].fitness = 0;
for (int j = 0; j < stringSize; j++ ) {
//Generate child 1
int mutation = rand() % stringSize;
if ( mutation == 0 && parents[p].pattern[j] == '0' ) {
children[i].pattern += '1';
children[i].fitness++;
}
else if ( mutation == 0 && parents[p].pattern[j] == '1' ) {
children[i].pattern += '0';
}
else if ( mutation > 0) {
children[i].pattern += parents[p].pattern[j];
if ( parents[p].pattern[j] == '1')
children[i].fitness++;
}
//Generate child 2
mutation = rand() % stringSize;
if ( mutation == 0 && parents[q].pattern[j] == '0' ) {
children[i+1].pattern += '1';
children[i+1].fitness++;
}
else if ( mutation == 0 && parents[q].pattern[j] == '1' ) {
children[i+1].pattern += '0';
}
else if ( mutation > 0) {
children[i+1].pattern += parents[q].pattern[j];
if ( parents[q].pattern[j] == '1')
children[i+1].fitness++;
}
}
}
//Crossover
else {
children[i].pattern = "";
children[i].fitness = 0;
children[i+1].pattern = "";
children[i+1].fitness = 0;
for ( int j = 0; j < stringSize; j++ ) {
//Generate child 1
int mutation = rand() % stringSize;
if ( (rand()%2) == 0 ) {
if ( mutation == 0 && parents[p].pattern[j] == '0' ) {
children[i].pattern += '1';
children[i].fitness++;
}
else if ( mutation == 0 && parents[p].pattern[j] == '1') {
children[i].pattern += '0';
}
else if ( mutation > 0 ) {
children[i].pattern += parents[p].pattern[j];
if ( parents[p].pattern[j] == '1' )
children[i].fitness++;
}
}
else {
if ( mutation == 0 && parents[q].pattern[j] == '0' ) {
children[i].pattern += '1';
children[i].fitness++;
}
else if ( mutation == 0 && parents[q].pattern[j] == '1' ) {
children[i].pattern += '0';
}
else if ( mutation > 0 ) {
children[i].pattern += parents[q].pattern[j];
if ( parents[q].pattern[j] == '1' )
children[i].fitness++;
}
}
//Generate child 2
mutation = rand() % stringSize;
if ( children[i].pattern[j] == '1') {
if ( mutation != 0 ) {
children[i+1].pattern += '0';
}
else {
children[i+1].pattern += '1';
children[i+1].fitness++;
}
}
else {
if ( mutation != 0 ) {
children[i+1].pattern += '1';
children[i+1].fitness++;
}
else {
children[i+1].pattern += '0';
}
}
}
}
}
//Replacement:
//temps hold the most fit individuals in the current population
Individual temp1;
Individual temp2;
//max and next hold the two highest fitness values in the current population
int max = 0;
int next = 0;
//first and second hold the index values of the most fit individuals
int first = -1;
int second = -1;
//Find the most fit individual
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness >= max ) {
max = pop[i].fitness;
temp1 = pop[i];
first = i;
}
}
//Find the next fit individual
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness == max && i != first) {
temp2 = pop[i];
second = i;
}
}
if ( second == -1) {
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness > next && pop[i].fitness < max ) {
next = pop[i].fitness;
temp2 = pop[i];
second = i;
}
}
}
//Update the population
for ( int i = 0; i < childrenSize; i++ )
pop[i] = children[i];
pop[popSize-2] = temp1;
pop[popSize-1] = temp2;
cout << " Generation: " << numGens << endl;
//Delete the allocated memory
delete [] children;
delete [] parents;
}
//End of generational loop
//Display fitness information
cout << " Highest fitness: " << highFitness << endl;
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness == highFitness)
cout << " " << pop[i].pattern << endl;
}
cout << " Lowest fitness: " << lowFitness << endl;
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness == lowFitness)
cout << " " << pop[i].pattern << endl;
}
cout << " Average fitness: " << std::setprecision(4) << avgFitness << endl << endl;
//Check if global optimum was found
for ( int i = 0; i < popSize; i++ ) {
if ( pop[i].fitness == stringSize ) {
pass++;
break;
}
}
//Delete allocated memory
delete [] pop;
}
//End of procedural loop
if ( pass != 5) {
lowerBound = popSize;
popSize *= 2;
}
else {
upperBound = popSize;
fail = false;
}
}
//End termination loop
//Display results
cout << "Process terminating at population size of " << popSize << endl;
if ( fail == false )
cout << "Status: Success";
else if ( popSize >= 81920)
cout << "Status: Fail";
else
cout << "Status: Unknown";
//End program
return 0;
}
//End of main program