-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem02.cpp
320 lines (244 loc) · 6 KB
/
problem02.cpp
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
#include <bits/stdc++.h>
#define SIZE 6
using namespace std;
int gene1[SIZE];//={2, 4, 6, 8, 10, 12};
int gene2[SIZE];//={1, 3, 5, 7, 9, 11};
int gene3[SIZE];//={3, 7, 11,15, 13, 5 };
void assignRandomNumtoGenes();
int getPredictedValueForGene(int gene[], int x);
int getBadnessValue(int y1, int y2, int predy1, int predy2);
string generateBinary(int gene[]);
string generateNewGene(string binary, int r);
void display(int gene[]);
int getMinimum(int value1, int value2);
void printBinary(string binary);
int *getGene(int lowValue, int badness_1, int badness_2, int badness_3);
string getC(string binaryA, string binaryB);
string getD(string binaryA, string binaryB);
int main()
{
int size=3;
long long int array[size];
int *geneA;
int *geneB;
// 2 Data Points
int x1=1, x2=3, y1=2, y2=4;
long long int pred_y1=0, pred_y2=0;
long long int badness_1=0, badness_2=0, badness_3=0;
long long int lowValue1=0, lowValue2=0;
assignRandomNumtoGenes();
cout<<"Genetic Algorithm by using Crossover\n"<<endl;
for(int i=0; i<100; i++)
{
//Badness For Gene1
pred_y1= getPredictedValueForGene( gene1, x1); // For x1=1
pred_y2= getPredictedValueForGene( gene1, x2); // For x2=3
badness_1=getBadnessValue( y1, y2, pred_y1, pred_y2);
array[0]=badness_1;
cout<<"Iteration "<<i+1<<"\n\n";
cout<<"Badness Value for Gene1 ";
display( gene1);
cout<<": "<<badness_1<<endl;
//Badness For Gene2
pred_y1= getPredictedValueForGene( gene2, x1); // For x1=1
pred_y2= getPredictedValueForGene( gene2, x2); // For x2=3
badness_2=getBadnessValue( y1, y2, pred_y1, pred_y2);
array[1]=badness_2;
cout<<"Badness Value for Gene2 ";
display( gene2);
cout<<": "<<badness_2<<endl;
//Badness For Gene3
pred_y1= getPredictedValueForGene( gene3, x1); // For x1=1
pred_y2= getPredictedValueForGene( gene3, x2); // For x2=3
badness_3=getBadnessValue( y1, y2, pred_y1, pred_y2);
array[2]=badness_3;
cout<<"Badness Value for Gene3 ";
display( gene3);
cout<<": "<<badness_3<<endl;
//Sort function
sort(array, array+size );
//Two minimum badness value
lowValue1= array[0];
lowValue2= array[1];
geneA= getGene(lowValue1, badness_1, badness_2, badness_3);
geneB= getGene(lowValue2, badness_1, badness_2, badness_3);
cout<<"LowValue1 : "<<lowValue1<<endl;
display( geneA);
cout<<"\nLowValue2 : "<<lowValue2<<endl;
display( geneB);
// For Solution if badness =0
if(badness_2 == 0)
{
cout<<"Solution is Gene2 ";
display( gene2);
cout<<endl;
break;
}
else if(badness_1 == 0)
{
cout<<"Solution is Gene1 ";
display( gene1);
cout<<endl;
break;
}
else if(badness_3 == 0)
{
cout<<"Solution is Gene3 ";
display( gene3);
cout<<endl;
break;
}
//Binary Representation
cout<<"\n\nGene A and B Binary "<<endl;
string binaryA=generateBinary(geneA);
printBinary(binaryA);
string binaryB=generateBinary(geneB);
printBinary(binaryB);
cout<<"After CrossOver\n";
cout<<"GeneC"<<endl;
string binaryC=getC( binaryA, binaryB);
printBinary(binaryC);
cout<<"GeneD"<<endl;
string binaryD=getD( binaryA, binaryB);
printBinary(binaryD);
cout<<"\nNew GeneC { ";
int start=0;
string str="";
for(int i=0; i<SIZE; i++)
{
str=binaryC.substr(start,4);
start=start+4;
unsigned long decimal =bitset<4>(str).to_ulong();
gene1[i]= decimal;
cout<<gene1[i]<<" ";
}
cout<<"}"<<endl;
cout<<"\nNew GeneD { ";
int start1=0;
string str1="";
for(int i=0; i<SIZE; i++)
{
str1=binaryD.substr(start1,4);
start1=start1+4;
unsigned long decimal =bitset<4>(str1).to_ulong();
gene2[i]= decimal;
cout<<gene2[i]<<" ";
}
cout<<"}"<<endl;
int r=0;
//FOR GENE3
for(int i=0; i<SIZE; i++)
{
r = (rand() % 4) ;
gene3[i]=r;
}
}
return 0;
}
int getPredictedValueForGene(int gene[], int x)
{
int ans=0, index=0;
for(int i=5; i>=1; i--)
{
ans= ans + ( gene[index] * pow(x , i)) ;
index++;
}
ans=ans+gene[5];
return (ans);
}
int getBadnessValue(int y1, int y2, int predy1, int predy2)
{
int ans=0;
ans= pow(y1 - predy1, 2) + pow(y2 - predy2, 2) ;
return (ans);
}
string generateBinary(int gene[])
{
string binary="";
for(int i=0; i<SIZE; i++)
{
//string binary = bitset<4>(15).to_string(); //to binary
binary=binary + bitset<4>(gene[i]).to_string(); // 4 bit binary
}
return binary;
}
string generateNewGene(string binary, int r)
{
if(binary[r]== '1')
{
binary[r]= '0';
}
else if(binary[r]== '0')
{
binary[r]= '1';
}
return (binary);
}
void printBinary(string binary)
{
int start=0;
cout<<"\n\n{ ";
for(int i=0; i<6; i++)
{
cout<<binary.substr(start,4)<<" ";
start=start+4;
}
cout<<" }"<<endl;
}
void display(int gene[])
{
cout<<"{ ";
for(int i=0; i<SIZE; i++)
{
cout<<gene[i]<<" ";
}
cout<<" }\t";
}
int getMinimum(int value1, int value2)
{
if(value1 < value2)
{
return value1;
}
return value2;
}
int *getGene(int lowValue, int badness_1, int badness_2, int badness_3)
{
if(lowValue == badness_1)
{
return (gene1);
}
else if(lowValue == badness_2)
{
return (gene2);
}
else if(lowValue == badness_3)
{
return (gene3);
}
}
string getC(string binaryA, string binaryB)
{
string result="";
result=binaryA.substr(0, 12) + binaryB.substr(12, 12);
return result;
}
string getD(string binaryA, string binaryB)
{
string result="";
result=binaryB.substr(0, 12) + binaryA.substr(12, 12);
return result;
}
void assignRandomNumtoGenes()
{
int r=0;
for(int i=0; i<SIZE; i++)
{
r = (rand() % 4 ) +1;
gene1[i]=r;
r = (rand() % 1) +1;
gene2[i]=r;
r = (rand() % 2)+1 ;
gene3[i]=r;
}
}