-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnQueens-GA.CPP
259 lines (228 loc) · 5.21 KB
/
nQueens-GA.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
#include<iostream>
#include<stdlib.h>
using namespace std;
int size=8;
int getrandom()
{
return rand() % size;
}
int factorial(int n)
{
int fact=1;
while(n>=1)
{
fact=fact*n;
n--;
}
return fact;
}
int total(int t)
{
return (factorial(t))/(factorial(t-2)*2);
}
int evaluate(int current[])
{
int count=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if((current[i]!=current[j])&&(abs(current[i]-current[j])!=j-i))
{
count++;
}
}
}
return count;
}
int main()
{
int n;
int c=-1;
cout<<"Enter the size of the n*n board\n";
cin>>size;
if(size<=0)
{
cout<<"Invalid Size Entered\n";
}
cout<<"\nEnter number of Readings : ";
cin>>n;
if(n<=0)
{
cout<<"Valid number not entered.Better luck next time\n";
return 0;
}
int readings[n][size];
int evalres[n];
int threshold=total(size);
/*Getting Initial Population*/
for(int i=0;i<n;i++)
{
cout<<"\nEnter the "<<i<<"th reading :\n";
for(int j=0;j<size;j++)
{
cout<<"\nEnter position "<<j+1<<" : ";
cin>>readings[i][j];
if((readings[i][j])<=0 || (readings[i][j])>(size))
{
cout<<"You entered an invalid number\n";
j--;
}
}
}
/*end of Initial Population*/
repeat:
if(c>1000000)
{
cout<<"1 Lakh iterations reached\n";
return 0;
}
c++;
/*Fitness Evaluation*/
for(int i=0;i<n;i++)
{
int temp[size];
for(int j=0;j<size;j++)
{
temp[j]=readings[i][j];
}
evalres[i]=evaluate(temp);
}
int solpoint=size;
cout<<"\nThe values generated are :\n";
for(int i=0;i<n;i++)
{
cout<<"\n"<<i+1<<" : "<<evalres[i];
if(evalres[i]==threshold)
{
solpoint=i;
}
}
if(solpoint!=size)
{
cout<<"\n\nSolution found\n";
for(int j=0;j<size;j++)
{
cout<<readings[solpoint][j]<<"\t";
}
cout<<"\n\n\nSolution : \n";
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(readings[solpoint][j]-1==i)
{
cout<<"Q ";
}
else
{
cout<<"X ";
}
}
cout<<"\n";
}
cout<<"\nNumber of iterations required : "<<c<<endl;
return 0;
}
//Arranging as per fitness
cout<<"\n\nArranging as per Fitness Function\n";
for(int i=0;i<n-1;i++)
{
int max=i;
for(int j=i+1;j<n;j++)
{
if(evalres[j]>=evalres[max])
{
int temp;
temp=evalres[max];
evalres[max]=evalres[j];
evalres[j]=temp;
max=j;
}
}
for(int j=0;j<size;j++)
{
int temp;
temp=readings[max][j];
readings[max][j]=readings[i][j];
readings[i][j]=temp;
}
}
for(int i=0;i<n;i++)
{
int temp[size];
for(int j=0;j<size;j++)
{
temp[j]=readings[i][j];
}
evalres[i]=evaluate(temp);
}
cout<<"\n\nReadings after Arranging as per value of Fitness Function : \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<size;j++)
{
cout<<readings[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"\nCorresponding Fitness Values : \n";
for(int i=0;i<n;i++)
{
cout<<"\n"<<i+1<<" : "<<evalres[i];
}
//return 0;
/*end of Fitness Evaluation*/
/*Selection*/
cout<<"\n\nInput before selection is : \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<size;j++)
{
cout<<readings[i][j]<<"\t";
}
cout<<"\n";
}
/*end of Selection*/
/*Crossover*/
for(int i=0;i<n;i+=2)
{
int selpoint=getrandom();
cout<<"\nPoint for readings "<<i+1<<" and "<<i+2<<" : "<<selpoint+1;
for(int j=0;j<=selpoint;j++)
{
int temp;
temp=readings[i+1][j];
readings[i+1][j]=readings[i][j];
readings[i][j]=temp;
}
}
cout<<"\n\nInput after selection and crossover is : \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<size;j++)
{
cout<<readings[i][j]<<"\t";
}
cout<<"\n";
}
/*end of Crossover*/
/*Mutation*/
for(int i=0;i<n;i++)
{
int mutpoint=getrandom();
readings[i][mutpoint]=getrandom()+1;
}
cout<<"\n\nInput after Mutation is : \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<size;j++)
{
cout<<readings[i][j]<<"\t";
}
cout<<"\n";
}
/*end of Mutation*/
goto repeat;
return 0;
}