-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem01.cpp
More file actions
239 lines (186 loc) · 4.55 KB
/
Copy pathproblem01.cpp
File metadata and controls
239 lines (186 loc) · 4.55 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
/*
#include <iostream>
#include <math.h>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
#include <bitset>
*/
#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 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[]);
void assignRandomNumtoGenes();
void printBinary(string binary);
int main()
{
assignRandomNumtoGenes();
// 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;
cout<<"Genetic Algorithm by using mutation\n"<<endl;
for(int i=0; i<60; 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);
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);
cout<<"Badness Value for Gene2 ";
display( gene2);
cout<<": "<<badness_2<<endl;
// 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;
}
//For Binary and mutation
if(badness_2 <= badness_1)
{
int r=0;
//srand(time(0)); // Initialize random number generator.
r = (rand() % 24) ;
//Binary
string binary=generateBinary(gene2);
printBinary(binary);
//Mutation
cout<<"\nAfter Mutation and random number to toggle "<<r<<endl;
string newbinaryGene= generateNewGene(binary, r-1);
printBinary(newbinaryGene);
cout<<"\nNew Gene { ";
int start=0;
string str="";
for(int i=0; i<SIZE; i++)
{
str=newbinaryGene.substr(start,4);
start=start+4;
unsigned long decimal =bitset<4>(str).to_ulong();
gene1[i]= decimal;
cout<<gene1[i]<<" ";
}
cout<<"}"<<endl;
}
else // if badness_1<badness2
{
int r=0;
//srand(time(0)); // Initialize random number generator.
r = (rand() % 24) ;
//Binary
string binary=generateBinary(gene1);
printBinary(binary);
//Mutation
cout<<"\nAfter Mutation and random number to toggle "<<r<<endl;
string newbinaryGene= generateNewGene(binary, r-1);
printBinary(newbinaryGene);
cout<<"\nNew Gene { ";
int start=0;
string str="";
for(int i=0; i<SIZE; i++)
{
str=newbinaryGene.substr(start,4);
start=start+4;
unsigned long decimal =bitset<4>(str).to_ulong();
gene2[i]= decimal;
cout<<gene2[i]<<" ";
}
cout<<"}"<<endl;
}
cout<<"\n";
}
cout<<"Solution is : Gene ";
display(gene2);
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";
}
void assignRandomNumtoGenes()
{
int r=0;
for(int i=0; i<SIZE; i++)
{
r = (rand() % 5) +1;
gene1[i]=r;
r = (rand() % 3) +1;
gene2[i]=r;
}
}