-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_algorithm.c
376 lines (326 loc) · 11.9 KB
/
genetic_algorithm.c
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
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Genetic Algorithm Example
//
// Author: John Zalles
// Date: September 19th, 2020
// Quote of the day: "Defeat is not the worst of failures. Not to have tried is the true failure."
// - George Edward Woodberry
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------------------------------
// Required libraries
//---------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//---------------------------------------------------------------------------------------------------
// Define constants
//---------------------------------------------------------------------------------------------------
#define STATES 10 // no. of US states inside an individual (value can be modified)
#define BORDERS 17 // no. of border pairs (value can be modified after adjusting borderPairs variable)
#define INDIV 100 // no. of individuals in population (value can be modified)
#define COLORS 4 // no. of possible colors (value can be modified after adjusting colors variable)
#define ITERATIONS 1000 // no. of iterations (value can be modified)
//---------------------------------------------------------------------------------------------------
// Variable declarations
//---------------------------------------------------------------------------------------------------
char stateNames[30] = "NC,SC,VA,TN,KY,WV,GA,AL,MS,FL";
char colors[COLORS+1] = { 'R', 'G', 'B', 'W'}; // Red, Green, Blue, White
char initialPopulation[INDIV][STATES+1];
double fitness[INDIV];
int chosenOnes[INDIV];
char crossPopulation[INDIV][STATES+1];
char bestIndividual[STATES+1];
_Bool globalMax = 0;
double maxFitness = 0.0;
int counter = 0;
//---------------------------------------------------------------------------------------------------
// Border pairs (would have to be manually modified to include more states)
//---------------------------------------------------------------------------------------------------
int borderPairs[BORDERS][2] =
{
{0, 1},
{0, 2},
{0, 3},
{0, 6},
{1, 6},
{2, 3},
{2, 4},
{2, 5},
{3, 4},
{3, 6},
{3, 7},
{3, 8},
{4, 5},
{6, 7},
{6, 9},
{7, 8},
{7, 9},
};
//---------------------------------------------------------------------------------------------------
// Function: randNum - generates a random number within bounds
//---------------------------------------------------------------------------------------------------
int randNum(int lower, int upper)
{
int num = (rand() % (upper - lower + 1)) + lower;
return num;
}
//---------------------------------------------------------------------------------------------------
// Step 1: create initial population
//---------------------------------------------------------------------------------------------------
void createPopulation()
{
// randomly generate a population of n individuals
// each "row" represents an individual
// each "column" represents a state and stores one of k colors
// create individuals
for(int j = 0; j < INDIV; ++j)
{
for(int i = 0; i < STATES; ++i)
{
initialPopulation[j][i] = colors[randNum(0, COLORS-1)];
}
}
return;
}
//---------------------------------------------------------------------------------------------------
// Step 2: calculate fitness function for each individual
//---------------------------------------------------------------------------------------------------
void fitnessFunction()
{
// fitness function is percentage of non-conflicting pairs of states
// add up number of conflicts
for(int j = 0; j < INDIV; ++j)
{
for(int i = 0; i < BORDERS; ++i)
{
if(initialPopulation[j][borderPairs[i][0]] == initialPopulation[j][borderPairs[i][1]])
fitness[j] += 1.0;
}
}
// transform result into percentage of non-conflicting pairs of states
for(int i = 0; i < INDIV; ++i)
{
fitness[i] = ((BORDERS - fitness[i])/BORDERS);
}
return;
}
//---------------------------------------------------------------------------------------------------
// Step 3: return best individual after last iteration or if a global maximum is found
//---------------------------------------------------------------------------------------------------
void returnBest()
{
// find individual with the best fitness function
for(int i = 0; i < INDIV; ++i)
{
if(fitness[i] > maxFitness)
{
maxFitness = fitness[i];
strcpy(bestIndividual, initialPopulation[i]);
if(maxFitness == 1.0)
{
globalMax = 1;
}
}
}
return;
}
//---------------------------------------------------------------------------------------------------
// Step 4: select top individuals based on fitness function
//---------------------------------------------------------------------------------------------------
void chooseIndividuals()
{
double rank[INDIV-1];
for(int i = 0; i < INDIV; ++i)
{
for(int j = 0; j < (INDIV-1); ++j)
{
if(fitness[i] > rank[j])
{
// move rank
for(int k = 0; k < (INDIV-2-j); ++k)
{
rank[INDIV-2-k] = rank[INDIV-3-k];
}
// copy rank
rank[j] = fitness[i];
// move chosen
for(int k = 0; k < (INDIV-2-j); ++k)
{
chosenOnes[INDIV-2-k] = chosenOnes[INDIV-3-k];
}
// copy chosen
chosenOnes[j] = i;
break;
}
}
}
return;
}
//---------------------------------------------------------------------------------------------------
// Step 5: crossover
//---------------------------------------------------------------------------------------------------
void crossover()
{
int crossPoint;
int k, n;
for(int j = 0; j < INDIV; ++j)
{
if(j % 2 == 0)
{
crossPoint = randNum(1, STATES-2); // choose a random crossover point
k = j / 2;
n = k + 1;
}
else
{
k = (j + 1) / 2;
n = k - 1;
}
// crossover individuals
for(int i = 0; i < crossPoint; ++i)
{
crossPopulation[j][i] = initialPopulation[chosenOnes[k]][i];
}
for(int i = crossPoint; i < STATES; ++i)
{
crossPopulation[j][i] = initialPopulation[chosenOnes[n]][i];
}
}
return;
}
//---------------------------------------------------------------------------------------------------
// Step 6: mutation
//---------------------------------------------------------------------------------------------------
void mutation()
{
// choose a random US state, and mutate its color
for(int i = 0; i < INDIV; ++i)
{
crossPopulation[i][randNum(0, STATES-1)] = colors[randNum(0, COLORS-1)];
}
// copy new population
for(int j = 0; j < INDIV; ++j)
{
strcpy(initialPopulation[j], crossPopulation[j]);
}
return;
}
//---------------------------------------------------------------------------------------------------
// Step 7: print results
//---------------------------------------------------------------------------------------------------
void printResults()
{
printf("-------------------------------------------------------\n");
printf("After %d iterations, a %s maximum has been found.\n", counter, globalMax == 1 ? "global" : "local");
printf("Best individual: %s\n", bestIndividual);
printf("Fitness function: %g\n", maxFitness);
printf("Number of iterations: %d\n", counter);
printf("Individuals in population: %d\n", INDIV);
printf("Number of colors: %d\n", COLORS);
printf("-------------------------------------------------------\n");
for(int i = 1; i < (STATES+1); ++i)
{
printf("%c%c -- %c\n", stateNames[(i*3)-3], stateNames[(i*3)-2], bestIndividual[i-1]);
}
printf("-------------------------------------------------------\n");
return;
}
//---------------------------------------------------------------------------------------------------
// Step 8: create dot file
//---------------------------------------------------------------------------------------------------
void dotFile()
{
// declare and initialize variables
FILE *filePointer = NULL;
char colorName[10];
char usState[3];
// create file "results.dot"
filePointer = fopen("results.dot", "w+");
// write first line of the dot file
fputs("graph {\n", filePointer);
// write state colors according to best individual
for(int i = 1; i < (STATES+1); ++i)
{
fputs(" ", filePointer);
usState[0] = stateNames[(i*3)-3];
usState[1] = stateNames[(i*3)-2];
usState[2] = '\0';
fputs(usState, filePointer);
fputs(" [fillcolor=", filePointer);
switch(bestIndividual[i-1])
{
case 'R':
strcpy(colorName, "red");
break;
case 'G':
strcpy(colorName, "green");
break;
case 'B':
strcpy(colorName, "blue");
break;
case 'W':
strcpy(colorName, "white");
break;
}
fputs(colorName, filePointer);
fputs(", style=filled]\n", filePointer);
}
// write borders
for(int j = 0; j < BORDERS; ++j)
{
fputs(" ", filePointer);
usState[0] = stateNames[(((borderPairs[j][0])+1)*3)-3];
usState[1] = stateNames[(((borderPairs[j][0])+1)*3)-2];
usState[2] = '\0';
fputs(usState, filePointer);
fputs(" -- ", filePointer);
usState[0] = stateNames[(((borderPairs[j][1])+1)*3)-3];
usState[1] = stateNames[(((borderPairs[j][1])+1)*3)-2];
usState[2] = '\0';
fputs(usState, filePointer);
fputs("\n", filePointer);
}
// write the last line of the dot file
fputs("}", filePointer);
// close the file
fclose(filePointer);
filePointer = NULL;
// print comments
printf("File 'results.dot' created with results.\n");
return;
}
//---------------------------------------------------------------------------------------------------
// Function: main
//---------------------------------------------------------------------------------------------------
int main()
{
// use current time as seed for random number generator
srand(time(0));
// Step 1: create initial population
createPopulation();
for(counter = 0; counter < ITERATIONS && globalMax == 0; ++counter)
{
// Step 2: calculate fitness function for each individual
fitnessFunction();
// Step 3: return best individual after last iteration or if a global maximum is reached
returnBest();
if(globalMax != 1)
{
// Step 4: select top individuals based on fitness function
chooseIndividuals();
// Step 5: crossover
crossover();
// Step 6: mutation
mutation();
}
}
// Step 7: print results
printResults();
// Step 8: create dot file
dotFile();
// Finalize program
return 0;
}