-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solver.cpp
351 lines (332 loc) · 8.89 KB
/
Solver.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//Cory Fleitas
//CSCE 420-500
//Due: April 28, 2016
//Solver.cpp
#include "Solver.h"
//Contructor sets the max limit of possible solutions to test out
Solver::Solver(int gl)
{
gateLimit = gl;
solutionLoc = 0;
}
//Adds in all problems to solve and keeps track of potential bits that need to be used
void Solver::addProblem(Problem p)
{
problems.push_back(p);
vector<int> temp = p.getLimits();
for(int i=0;i<temp.size();++i)
{
limits.push_back(temp[i]);
}
sort(limits.begin(),limits.end());
limits.erase(unique(limits.begin(),limits.end()),limits.end());
}
//Runs the genetic algorithm
//Note: Longer than 24 lines due to extensive commenting needed
void Solver::runGenetic()
{
//Uses top 20% of children
int keep = gateLimit/5;
for(int i=0;i<keep;++i)
{
circuits.push_back(Circuit());
}
addNewGates(keep);
bool go = true;
int loc = -1;
unsigned long long int generation=0;
do
{
++generation;
//Randomly deletes a gate every 100 generations
if(generation%100==0)
{
randomDelete();
}
//Gets all crosses for top 20% of children every 20 generations
if(generation%20==0)
{
crossover(keep);
}
//Mutates a Random Gate every 10 generations
if(generation%10==0)
{
mutate();
}
//Adds a new random Gate every generation
addNewGates(keep);
calcFitness();
runSortFitness();
shrink();
cull();
loc = checkSolution();
if(generation%1000 == 0)
{
cout<<"Generation: "<<generation<<"\n";
cout<<"Num Gates: "<<circuits[0].getGateNum()<<"\n";
}
if(loc != -1)
{
solutionLoc = loc;
cout<<"Generation: "<<generation<<"\n";
go = false;
}
}while(go);
}
//Runs the genetic algorithm
void Solver::runAStar()
{
for(int i=0;i<gateLimit;++i)
{
circuits.push_back(Circuit());
}
bool go = true;
int loc = -1;
unsigned long long int generation=0;
do
{
//algorithm adds new random gate every generation, and reverts if the fitness value goes lower
++generation;
addGatesAstar();
calcFitness();
revert();
runSortFitness();
cull();
loc = checkSolution();
if(generation%1000 == 0)
{
cout<<"Generation: "<<generation<<"\n";
cout<<"Num Gates: "<<circuits[0].getGateNum()<<"\n";
}
if(loc != -1)
{
solutionLoc = loc;
cout<<"Generation: "<<generation<<"\n";
go = false;
}
}while(go);
}
//Adds new random gate with a controlled bit, 0 1 or 2 controller bits, and inverted bool
void Solver::addNewGates(int k)
{
for(int i=0;i<k;++i)
{
Circuit tempCircuit = circuits[i];
int nl = limits[rand()%limits.size()];
vector<int> c;
int temp = rand()%3;
for(int i=0;i<temp;++i)
{
c.push_back(limits[rand()%limits.size()]);
}
temp = rand()%2;
Gate g(nl,c,temp);
tempCircuit.addGate(g);
circuits.push_back(tempCircuit);
}
}
//Randomly changes the controlled bit of a gate
void Solver::mutate()
{
for(int i=0;i<circuits.size();++i)
{
int loc = rand()%circuits[i].getGateNum();
int temp = rand()%2;
circuits[i].mutateRandomGate(loc,temp);
}
}
//Crosses over all pairs in the top 20% of solutions
//EX: 1 and 2, 3 and 4, etc.
//Note longer than 24 lines due to the extensive data modification needed to collect all
//10 possible crosses resulting from 2 solutions
void Solver::crossover(int k)
{
if(k%2 == 0)
{
++k;
}
for(int i=0;i<k;i+=2)
{
vector<Gate> g1 = circuits[i].getGates();
vector<Gate> g2 = circuits[i+1].getGates();
if(g1.size() >= 2 && g2.size() >= 2)
{
vector<Gate> g1h1;
vector<Gate> g1h2;
vector<Gate> g2h1;
vector<Gate> g2h2;
for(int j=0;j<g1.size()/2;++j)
{
g1h1.push_back(g1[j]);
}
for(int j=g1.size()-1;j>=g1.size()/2;--j)
{
g1h2.push_back(g1[j]);
}
for(int j=0;j<g2.size()/2;++j)
{
g2h1.push_back(g2[j]);
}
for(int j=g2.size()-1;j>=g2.size()/2;--j)
{
g2h2.push_back(g2[j]);
}
vector<vector<Gate>> crosses;
crosses.push_back(vector<Gate>(g1h1));
crosses.push_back(vector<Gate>(g2h1));
crosses.push_back(vector<Gate>(g2h1));
crosses.push_back(vector<Gate>(g1h1));
crosses.push_back(vector<Gate>(g2h2));
crosses.push_back(vector<Gate>(g2h1));
crosses.push_back(vector<Gate>(g1h1));
crosses.push_back(vector<Gate>(g1h2));
crosses.push_back(vector<Gate>(g2h2));
crosses.push_back(vector<Gate>(g1h2));
for(int j=0;j<g1h1.size();++j)
{
crosses[5].push_back(g1h1[j]);
crosses[9].push_back(g1h1[j]);
}
for(int j=0;j<g1h2.size();++j)
{
crosses[1].push_back(g1h2[j]);
crosses[2].push_back(g1h2[j]);
crosses[4].push_back(g1h2[j]);
}
for(int j=0;j<g2h1.size();++j)
{
crosses[6].push_back(g2h1[j]);
crosses[8].push_back(g2h1[j]);
}
for(int j=0;j<g2h2.size();++j)
{
crosses[0].push_back(g2h2[j]);
crosses[3].push_back(g2h2[j]);
crosses[7].push_back(g2h2[j]);
}
for(int j=0;j<crosses.size();++j)
{
circuits.push_back(Circuit(crosses[j]));
}
}
}
}
//Randomly deletes a gate
void Solver::randomDelete()
{
for(int i=0;i<circuits.size();++i)
{
int loc = rand()%circuits[i].getGateNum();
circuits[i].deleteRandomGate(loc);
}
}
//Shrinks the solution vector back to the limit set at beginining
void Solver::shrink()
{
if(circuits.size() > gateLimit)
{
circuits.erase(circuits.begin()+gateLimit,circuits.end());
}
}
//Adds a random new gate to all the possible solutions
void Solver::addGatesAstar()
{
for(int i=0;i<circuits.size();++i)
{
int nl = limits[rand()%limits.size()];
vector<int> c;
int temp = rand()%3;
for(int i=0;i<temp;++i)
{
c.push_back(limits[rand()%limits.size()]);
}
temp = rand()%2;
Gate g(nl,c,temp);
circuits[i].addGate(g);
}
}
//If the most recent gate dropped the fitness score, throw it away
void Solver::revert()
{
for(int i=0;i<circuits.size();++i)
{
circuits[i].revert();
}
}
//Removes all gates that do not flip a bit after running through all the pairs of numbers
void Solver::cull()
{
for(int i=0;i<circuits.size();++i)
{
circuits[i].cullUsed();
}
}
//Sets the fitness score for each solution
void Solver::calcFitness()
{
for(int i=0;i<circuits.size();++i)
{
long long int temp=0;
circuits[i].resetFlip();
for(int j=0;j<problems.size();++j)
{
bitset<30> n = circuits[i].runGates(problems[j].getPQ());
temp+=problems[j].getFitness(n);
}
circuits[i].setFitness(temp);
}
}
//Sorts the solution list and puts the best ones in the front
void Solver::runSortFitness()
{
sort(circuits.begin(),circuits.end(),sortFitness);
}
int Solver::checkSolution()
{
for(int i=0;i<circuits.size();++i)
{
bool solution = true;
circuits[i].resetFlip();
for(int j=0;j<problems.size();++j)
{
bitset<30> n = circuits[i].runGates(problems[j].getPQ());
solution = (solution && problems[j].isSolution(n));
}
if(solution)
{
return i;
}
}
return -1;
}
//Gets the location of a solution if one is found
int Solver::getSolutionLocation()
{
return solutionLoc;
}
//Prints solution found to a text file
void Solver::print(string file)
{
ofstream out(file);
circuits[solutionLoc].resetFlip();
out<<problems.size()<<"\n";
for(int i=0;i<problems.size();++i)
{
bitset<30> n = circuits[solutionLoc].runGates(problems[i].getPQ());
out<<n<<"\n";
}
circuits[solutionLoc].print(out);
out.close();
}
//Prints out solution to the command line
void Solver::print()
{
circuits[solutionLoc].resetFlip();
for(int i=0;i<problems.size();++i)
{
problems[i].print();
bitset<30> n = circuits[solutionLoc].runGates(problems[i].getPQ());
cout<<"Calculated N: "<<n<<"\n";
}
circuits[solutionLoc].print();
}