-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
211 lines (183 loc) · 3.95 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
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
#include "time.h"
#include "string.h"
#include "math.h"
int main()
{
int iteretion = 100;
int popSize = 20;
float Pc = 0.75;
float Pm = 0.05;
int kromozonLength = 33;
int minX1 = -3;
float maxX1 = 12.1;
float minX2 = -4.1;
float maxX2 = 5.8;
int a,b,c,d,e,f,g,h,k,i,j,l,m,n,o,p,r,s,v,t;
int populasyon[kromozonLength];
int kromozonArray[popSize][kromozonLength];
float decimalValue[popSize];
float decimalValue_x2[popSize];
float costFunction[popSize];
float totalCostFunction=0;
float naturalSelectionProbability[popSize];
float cumulativeProba[popSize];
float randomNumberRulet[popSize];
int newKromozonArray[popSize][kromozonLength];
int newKro=0;
float randomCrossNumber[popSize];
int crossList[popSize];
int crosCount=0;
srand((unsigned)time(NULL));
// Try to create the log file
/*
FILE* fp = fopen("gaExample.txt", "w");
if(fp == NULL)
{
printf("File create problem!\n");
return -1;
}*/
// Rastgele kromozonların üretilmesi
for ( a = 0; a < popSize; a++)
{
for ( b = 0; b < kromozonLength; b++)
{
int r = rand() % 2;
//populasyon[i]= r;
kromozonArray[a][b]=r;
//fprintf(fp, "%d",kromozonArray[a][b]);
printf("%d",kromozonArray[a][b]);
}
printf("\n");
//fprintf(fp,"\n");
}
// Bütün kromozomların binary to dec dönüşümü
for ( c = 0; c < popSize; ++c)
{
int value=0;
for ( d = 0; d < 18; ++d)
{
value += kromozonArray[c][d] * pow(2,d);
}
decimalValue[c] = minX1 + ((value) * ((maxX1-(minX1))/(pow(2,18)-1)));
value=0;
}
for ( e = 0; e < popSize; ++e)
{
int value=0;
for ( f = 0; f < 15; ++f)
{
value += kromozonArray[e][f] * pow(2,f);
}
decimalValue_x2[e] = minX2 + ((value) * ((maxX2-(minX2))/(pow(2,15)-1)));
value=0;
}
for (int i = 0; i < popSize; ++i)
{
printf("%f\n", decimalValue[i]);
}
printf("\n");
for (int i = 0; i < popSize; ++i)
{
printf("%f\n", decimalValue_x2[i]);
}
//Amaç fonksiyonlarının hesaplanması
for ( g = 0; g < popSize; ++g)
{
costFunction[g] = 21.5 + (decimalValue[g]*sin(4*3.14*decimalValue[g])) + (decimalValue_x2[g]*sin(20*3.14*decimalValue_x2[g]));
}
printf("\n");
for ( h = 0; h < popSize; ++h)
{
printf("%f\n", costFunction[h]);
totalCostFunction += costFunction[h];
}
printf("%f\n",totalCostFunction);
printf("\n");
//Doğal seçim ihtimalinin hesaplanması
for ( i = 0; i < popSize; ++i)
{
naturalSelectionProbability[i] = costFunction[i] / totalCostFunction;
printf("%f\n", naturalSelectionProbability[i]);
}
printf("\n");
//Kümülatif ihtimallerin hesaplanması
for ( j = 0; j < popSize; ++j)
{
if (j==0)
{
cumulativeProba[j] = naturalSelectionProbability[j];
}
else
{
for ( k = 0; k <= j; ++k)
{
cumulativeProba[j] += naturalSelectionProbability[k];
}
}
printf("%f\n",cumulativeProba[j]);
}
printf("\n");
// Seleksiyon için rulet tekerleğinin 20 kez döndürülmesi
for ( l = 0; l < popSize; ++l)
{
randomNumberRulet[l] = (float)rand()/RAND_MAX;
printf("%f\n", randomNumberRulet[l]);
}
for ( m = 0; m < popSize; ++m)
{
for ( n = 0; n < popSize; ++n)
{
if (randomNumberRulet[m] <= cumulativeProba[n])
{
for ( o = 0; o < kromozonLength; ++o)
{
newKromozonArray[newKro][o] = kromozonArray[n][o];
}
break;
}
}
newKro++;
}
newKro = 0;
printf("\n");
for ( p = 0; p < popSize; ++p)
{
for (r = 0; r < kromozonLength; ++r)
{
printf("%d", newKromozonArray[p][r]);
}
printf("\n");
}
//Tek nokta çaprazlama operatörünün uygulanması
for ( s = 0; s < popSize; s++)
{
randomCrossNumber[s] = (float)rand()/RAND_MAX;
printf("%f\n", randomCrossNumber[s]);
}
/*
for (t = 0; t < popSize; ++t)
{
if (randomCrossNumber[t] < Pc)
{
crossList[crosCount] = t;
crosCount++;
}
}*/
/*
if (crosCount %2 == 0)
{
}
else
{
}
for (v = 0; v < crosCount; ++v)
{
printf("%d\n",crossList[v]);
}
crosCount = 0;*/
//fclose(fp);
return 0;
}