-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
337 lines (310 loc) · 9.4 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Gene {
struct Gene *nextGene ;
int data ;
};
struct Chromosome {
struct Chromosome *nextChromosome ;
struct Gene *headGene ;
int fitness ;
double rank ;
};
struct Chromosome *headChromosome ;
int bestFitness ;
int worstFitness ;
struct Chromosome *bestChromosome ;
struct Gene* createGene(int data,struct Gene *head) {
struct Gene *temp1 = (struct Gene*) malloc(sizeof(struct Gene)) ;
temp1 -> data = data ;
temp1 -> nextGene = NULL ;
if(head == NULL) {
head = temp1 ;
return head ;
}
struct Gene *temp2 = head ;
while(temp2->nextGene != NULL) {
temp2 = temp2 -> nextGene ;
}
temp2 ->nextGene = temp1 ;
return head ;
}
void createChromosome(char *line) {
struct Chromosome *temp1 = (struct Chromosome*)malloc(sizeof(struct Chromosome)) ;
temp1 ->headGene = NULL ;
temp1 ->nextChromosome = NULL ;
temp1 -> fitness = 0 ;
if(headChromosome == NULL) {
headChromosome = temp1 ;
}
else {
struct Chromosome *temp2 = headChromosome ;
while(temp2->nextChromosome != NULL) {
temp2 = temp2 -> nextChromosome ;
}
temp2 -> nextChromosome = temp1 ;
}
char *token ;
token = strtok(line,":") ;
while(token != NULL) {
int genData = atoi(token) ;
temp1->headGene = createGene(genData,temp1->headGene) ;
token = strtok(NULL,":\n\r") ;
}
}
int myPow(int x,int y) {
int result = 1 ;
if(y==0) {
return 1;
}
int j ;
for(j=0;j<y;j++) {
result = result*x ;
}
return result ;
}
void fitnessCalculation(int prob_size) {
struct Chromosome *temp = headChromosome;
int i ;
while(temp != NULL) {
temp -> fitness = 0 ;
struct Gene *tempGene = temp -> headGene ;
i = prob_size-1 ;
while(tempGene != NULL) {
temp -> fitness += (tempGene->data) * (myPow(2,i)) ;
i-- ;
tempGene = tempGene ->nextGene ;
}
temp = temp ->nextChromosome;
}
}
void findBestWorstFitness() {
struct Chromosome *temp = headChromosome ;
while(temp != NULL) {
if(temp->fitness > worstFitness){
worstFitness = temp -> fitness ;
}
else if(temp->fitness < bestFitness) {
bestFitness = temp -> fitness ;
}
temp = temp -> nextChromosome ;
}
}
void rankCalculation() {
struct Chromosome *temp = headChromosome ;
while(temp != NULL) {
if(bestFitness == worstFitness) {
temp -> rank = 0 ;
}
else {
temp -> rank = (double)(temp->fitness - bestFitness) / (double)(worstFitness-bestFitness) ;
}
temp = temp -> nextChromosome ;
}
}
void swap(struct Chromosome *temp1,struct Chromosome *temp2) {
int tempFitness ;
tempFitness = temp1->fitness ;
temp1 -> fitness = temp2 -> fitness ;
temp2 -> fitness = tempFitness ;
struct Gene *tempGene ;
tempGene = temp1 ->headGene ;
temp1 ->headGene = temp2 ->headGene ;
temp2 -> headGene = tempGene ;
}
void sortPopulation() {
int i ;
do{
i = 0 ;
struct Chromosome *temp1=NULL,*temp2= headChromosome ;
while(temp2->nextChromosome != temp1) {
if(temp2->fitness > temp2->nextChromosome->fitness) {
swap(temp2,temp2->nextChromosome) ;
i = 1 ;
}
temp2 = temp2 -> nextChromosome ;
}
temp1 = temp2 ;
}while(i) ;
}
void Crossover(int chromosomeIndex1,int chromosomeIndex2,int xoverParam1,int xoverParam2,int mutateParam) {
int i ;
struct Chromosome *temp1 = headChromosome ;
struct Chromosome *temp2 = headChromosome;
/* set selected chromosomes to temp1 and temp2 */
for(i=0;i<chromosomeIndex1-1;i++) {
temp1 = temp1 ->nextChromosome ;
}
for(i=0;i<chromosomeIndex2-1;i++) {
temp2 = temp2 -> nextChromosome ;
}
struct Gene *tempGene1 = temp1->headGene ;
struct Gene *tempGene2 = temp2 -> headGene ;
/* set selected genes to tempGene1 and tempGene2 */
for(i=0;i<xoverParam1-1;i++) {
tempGene1 = tempGene1->nextGene ;
tempGene2 = tempGene2 -> nextGene ;
}
/* crossover happens here */
for(i=0;i<xoverParam2-xoverParam1+1;i++) {
int tempData = tempGene1->data ;
tempGene1->data = tempGene2->data ;
tempGene2->data = tempData ;
tempGene1 = tempGene1 ->nextGene;
tempGene2 = tempGene2 -> nextGene ;
}
tempGene1 = temp1->headGene ;
tempGene2 = temp2 -> headGene ;
for(i=0;i<mutateParam-1;i++) {
tempGene1 = tempGene1->nextGene ;
tempGene2 = tempGene2->nextGene ;
}
tempGene1->data= (tempGene1->data == 0) ? 1 : 0 ;
tempGene2->data = (tempGene2->data==0) ? 1 : 0 ;
}
void Selection(char *selectionLine,char *xoverLine,char *mutateLine,int pop_size) {
int mutateParam = atoi(mutateLine) ;
char *xoverToken = strtok(xoverLine,":\n\r") ;
int xoverParam1 = atoi(xoverToken) ;
xoverToken = strtok(NULL,":\n\r") ;
int xoverParam2 = atoi(xoverToken) ;
char *selectionToken = strtok(selectionLine," :\n\r") ;
int selectionArray[pop_size] ;
int i=0;
while(selectionToken != NULL) {
int chromosomeNumber = atoi(selectionToken) ;
selectionArray[i] = chromosomeNumber ;
i++ ;
selectionToken = strtok(NULL," :\n\r") ;
}
for(i=0;i<pop_size;i+=2) {
Crossover(selectionArray[i],selectionArray[i+1],xoverParam1,xoverParam2,mutateParam) ;
}
}
void Display(int generation,int *bestGenes,int prob_size) {
printf("GENERATION: %d\n",generation) ;
struct Chromosome *temp = headChromosome ;
while(temp != NULL) {
struct Gene *tempGene = temp -> headGene ;
while(tempGene != NULL) {
if(tempGene->nextGene != NULL) {
printf("%d:",tempGene->data) ;
tempGene = tempGene -> nextGene ;
}
else {
printf("%d",tempGene->data) ;
tempGene = tempGene->nextGene ;
}
}
printf(" -> %d\n",temp->fitness) ;
temp = temp ->nextChromosome ;
}
printf("Best chromosome found so far: ") ;
int i = 0 ;
for(;i<prob_size;i++) {
if(i != prob_size-1) {
printf("%d:",bestGenes[i]) ;
}
else {
printf("%d",bestGenes[i]) ;
}
}
printf(" -> %d\n",bestChromosome->fitness) ;
}
void copyBestGene(int *bestGenes,int prob_size) {
struct Gene *temp = headChromosome->headGene ;
int i = 0 ;
while(temp != NULL) {
bestGenes[i] = temp ->data ;
i++ ;
temp = temp ->nextGene ;
}
}
int main(int argc,char *argv[]) {
FILE *fp ;
fp = fopen("population","r") ;
int prob_size = atoi(argv[1]) ; /* number of genes in any chromosome. */
int pop_size = atoi(argv[2]) ; /* total number of chromosome in population. */
int max_gen = atoi(argv[3]) ; /* total number of iteration to terminate evolutionary process. */
int generation = 0 ;
headChromosome = NULL ;
bestChromosome = (struct Chromosome*) malloc(sizeof(struct Chromosome)) ;
int *bestGenes = malloc(sizeof(int)*prob_size) ;
/* FILE READING */
char *population[pop_size] ; /* array for store population file */
if(fp == NULL) {
return 0 ;
}
size_t len = 0;
int i ;
for(i=0;i<pop_size;i++) {
population[i] = NULL ;
len = 0 ;
getline(&population[i],&len,fp) ;
}
fclose(fp) ;
char *selection[max_gen] ; /* array for store selection file */
fp = fopen("selection","r") ;
if(fp == NULL) {
return 0 ;
}
for(i=0;i<max_gen;i++) {
selection[i] = NULL ;
len = 0 ;
getline(&selection[i],&len,fp) ;
}
fclose(fp) ;
char *xover[max_gen] ; /* array for store xover file */
fp= fopen("xover","r") ;
if(fp == NULL) {
return 0 ;
}
for(i=0;i<max_gen;i++) {
xover[i] = NULL ;
len = 0 ;
getline(&xover[i],&len,fp) ;
}
fclose(fp) ;
char *mutate[max_gen] ; /* array for mutate file */
fp = fopen("mutate","r") ;
if(fp == NULL) {
return 0 ;
}
for(i=0;i<max_gen;i++) {
mutate[i] = NULL ;
len = 0 ;
getline(&mutate[i],&len,fp) ;
}
fclose(fp) ;
/* Population initialization */
for(i=0;i<pop_size;i++) {
createChromosome(population[i]) ;
}
fitnessCalculation(prob_size) ;
bestFitness = headChromosome ->fitness ;
worstFitness = headChromosome ->fitness ;
findBestWorstFitness() ;
rankCalculation() ;
sortPopulation();
bestChromosome->fitness = headChromosome -> fitness ;
copyBestGene(bestGenes,prob_size) ;
Display(generation,bestGenes,prob_size) ;
generation++ ;
for(i=0;i<max_gen;i++) {
Selection(selection[i],xover[i],mutate[i],pop_size) ;
fitnessCalculation(prob_size) ;
bestFitness = headChromosome ->fitness ;
worstFitness = headChromosome ->fitness ;
findBestWorstFitness();
rankCalculation();
sortPopulation();
if(bestFitness < bestChromosome->fitness) {
bestChromosome->fitness = headChromosome -> fitness ;
copyBestGene(bestGenes,prob_size) ;
}
Display(generation,bestGenes,prob_size) ;
generation++ ;
}
return 0;
}