forked from FredyR4zox/15-Puzzle-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchMethods.h
373 lines (286 loc) · 13 KB
/
SearchMethods.h
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*Search Functions for the 15-Puzzle*/
#include <iostream>
#include <array>
#include <queue>
#include <stack>
#include <unordered_set>
#include "Heuristics.h"
using namespace std;
//Change value to change the maximum depth of DFS, BFS, ASTAR and GREEDY
#define MAX_DEPTH 80
//Global function declarations
unsigned int generatedNodes;
unsigned int visitedNodes;
//Prototype declarations
unsigned int inversions(Config& initialConfig);
bool solutionExists(Config& initialConfig, Config& finalConfig);
string GENERAL_SEARCH_DFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth);
string GENERAL_SEARCH_LDFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth);
string GENERAL_SEARCH_BFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth);
string GENERAL_SEARCH_GREEDY(Config& initialConfig, Config& finalConfig, unsigned int maxDepth, unsigned int heuristicsFlag);
string GENERAL_SEARCH_ASTAR(Config& initialConfig, Config& finalConfig, unsigned int maxDepth, unsigned int heuristicsFlag);
void DFS(Config& initialConfig, Config& finalConfig);
void LDFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth);
void IDFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth);
void BFS(Config& initialConfig, Config& finalConfig);
void ASTAR(Config& initialConfig, Config& finalConfig, unsigned int heuristicsFlag);
void GREEDY(Config& initialConfig, Config& finalConfig, unsigned int heuristicsFlag);
//Function to check if we can reach a solution from the initial configuration
unsigned int inversions(Config& initialConfig){
unsigned int n=0;
array< array<int, 4>, 4> matrix = initialConfig.getMatrix();
array<int, 16> arr;
for(unsigned int i=0; i < 4; i++){
for(unsigned int j=0; j < 4; j++)
arr[i*4 + j] = matrix[i][j];
}
for(unsigned int i=0; i < 16; i++){
n += arr[i];
for(unsigned int j=0; j <= i; j++){
if(arr[i] >= arr[j] && arr[i]!=0 && arr[j]!=0)
n--;
}
}
return n;
}
bool solutionExists(Config& initialConfig, Config& finalConfig){
return ((inversions(initialConfig)%2 == 0) == (abs((int)initialConfig.getEmptyRowIndex()-4)%2 == 1)) ==
((inversions(finalConfig)%2 == 0) == (abs((int)finalConfig.getEmptyRowIndex()-4)%2 == 1));
}
//General Search Algorithm to search for a solution
string GENERAL_SEARCH_DFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth){
Node *initialNode = new Node(initialConfig);
generatedNodes++;
stack<Node*> q;
q.push(initialNode);
unordered_set<Config> hashSet;
while(!q.empty()){
Node *removed = q.top();
q.pop();
visitedNodes++;
if(removed->getConfig() == finalConfig){
cout << "Solution found on depth: " << removed->getDepth() << endl;
string str = removed->makePath();
delete initialNode;
return str;
}
if(removed->getDepth() < maxDepth){
array<Node*, 4> descendantList = removed->makeDescendants(&hashSet);
for(unsigned int i=0; i<4 && descendantList[i]!=NULL; i++){
generatedNodes++;
q.push(descendantList[i]);
}
}
}
delete initialNode;
return "Solution not found";
}
//General Search Algorithm to search for a solution
string GENERAL_SEARCH_LDFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth){
Node *initialNode = new Node(initialConfig);
generatedNodes++;
stack<Node*> q;
q.push(initialNode);
while(!q.empty()){
Node *removed = q.top();
q.pop();
visitedNodes++;
if(removed->getConfig() == finalConfig){
cout << "Solution found on depth: " << removed->getDepth() << endl;
string str = removed->makePath();
delete initialNode;
return str;
}
if(removed->getDepth() < maxDepth){
array<Node*, 4> descendantList = removed->makeDescendants(NULL);
for(unsigned int i=0; i<4 && descendantList[i]!=NULL; i++){
generatedNodes++;
q.push(descendantList[i]);
}
}
}
delete initialNode;
return "Solution not found";
}
//General Search Algorithm to search for a solution
string GENERAL_SEARCH_BFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth){
Node *initialNode = new Node(initialConfig);
generatedNodes++;
queue<Node*> q;
q.push(initialNode);
unordered_set<Config> hashSet;
unsigned int depth = 0;
while(!q.empty()){
Node *removed = q.front();
q.pop();
if(removed->getDepth() > depth){
cout << "Depth: " << removed->getDepth() << "\tTotal of generated nodes: " << generatedNodes << " \tTotal of visited nodes: " << visitedNodes << endl;
depth = removed->getDepth();
}
visitedNodes++;
if(removed->getConfig() == finalConfig){
cout << endl << "Solution found on depth: " << removed->getDepth() << endl;
string str = removed->makePath();
delete initialNode;
return str;
}
if(removed->getDepth() < maxDepth){
array<Node*, 4> descendantList = removed->makeDescendants(&hashSet);
for(unsigned int i=0; i<4 && descendantList[i]!=NULL; i++){
generatedNodes++;
q.push(descendantList[i]);
}
}
}
delete initialNode;
return "Solution not found";
}
//General Search Algorithm to search for a solution
string GENERAL_SEARCH_ASTAR(Config& initialConfig, Config& finalConfig, unsigned int maxDepth, unsigned int heuristicsFlag){
Node *initialNode = new Node(initialConfig);
generatedNodes++;
priority_queue<Node*, vector<Node*>, compareNodes> q;
q.push(initialNode);
while(!q.empty()){
Node *removed = q.top();
q.pop();
visitedNodes++;
if(removed->getConfig() == finalConfig){
cout << "Solution found on depth: " << removed->getDepth() << endl;
string str = removed->makePath();
delete initialNode;
return str;
}
if(removed->getDepth() < maxDepth){
array<Node*, 4> descendantList = removed->makeDescendants(NULL);
calcPathCostASTAR(descendantList, finalConfig, heuristicsFlag); //calculates the pathCost for each of the children Nodes
for(unsigned int i=0; i<4 && descendantList[i]!=NULL; i++){
generatedNodes++;
q.push(descendantList[i]);
}
}
}
delete initialNode;
return "Solution not found";
}
//General Search Algorithm to search for a solution
string GENERAL_SEARCH_GREEDY(Config& initialConfig, Config& finalConfig, unsigned int maxDepth, unsigned int heuristicsFlag){
Node *initialNode = new Node(initialConfig);
generatedNodes++;
priority_queue<Node*, vector<Node*>, compareNodes> q;
q.push(initialNode);
unordered_set<Config> hashSet;
while(!q.empty()){
Node *removed = q.top();
q.pop();
visitedNodes++;
if(removed->getConfig() == finalConfig){
cout << "Solution found on depth: " << removed->getDepth() << endl;
string str = removed->makePath();
delete initialNode;
return str;
}
if(removed->getDepth() < maxDepth){
array<Node*, 4> descendantList = removed->makeDescendants(&hashSet);
calcPathCostGREEDY(descendantList, finalConfig,heuristicsFlag); //calculates the pathCost for each of the children Nodes
for(unsigned int i=0; i<4 && descendantList[i]!=NULL; i++){
generatedNodes++;
q.push(descendantList[i]);
}
}
}
delete initialNode;
return "Solution not found";
}
void DFS(Config& initialConfig, Config& finalConfig){ /*Depth first search function*/
if(!solutionExists(initialConfig, finalConfig)){
cout << "It's not possible to reach to the final configuration starting from the initial configuration" << endl << endl; //There is no solution
return;
}
generatedNodes = visitedNodes = 0;
string str = GENERAL_SEARCH_DFS(initialConfig, finalConfig, MAX_DEPTH);
if(str != "Solution not found")
cout << "Movements: " << str << endl << endl;
else
cout << "Solution not found" << endl << endl;
cout << "Generated Nodes: " << generatedNodes << endl;
cout << "Visited Nodes: " << visitedNodes << endl;
cout << "Maximum of memory spent on nodes: " << (generatedNodes*sizeof(Node))/1024 << " KB" << endl << endl;
}
void LDFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth){ /*Limited Breadth first search function*/
if(!solutionExists(initialConfig, finalConfig)){
cout << "It's not possible to reach to the final configuration starting from the initial configuration" << endl << endl; //There is no solution
return;
}
generatedNodes = visitedNodes = 0;
string str = GENERAL_SEARCH_LDFS(initialConfig, finalConfig, maxDepth);
if(str != "Solution not found")
cout << "Movements: " << str << endl << endl;
else
cout << "Solution not found" << endl << endl;
cout << "Generated Nodes: " << generatedNodes << endl;
cout << "Visited Nodes: " << visitedNodes << endl;
cout << "Maximum of memory spent on nodes: " << (generatedNodes*sizeof(Node))/1024 << " KB" << endl << endl;
}
void IDFS(Config& initialConfig, Config& finalConfig, unsigned int maxDepth){ /*Iterative Depth first search function*/
if(!solutionExists(initialConfig, finalConfig)){
cout << "It's not possible to reach to the final configuration starting from the initial configuration" << endl << endl; //There is no solution
return;
}
for(unsigned int i=1; i<=maxDepth; i++){
generatedNodes = visitedNodes = 0;
string str = GENERAL_SEARCH_LDFS(initialConfig, finalConfig, i);
cout << "Depth: " << i << "\tGenerated nodes: " << generatedNodes << " \tVisited nodes: " << visitedNodes << endl;
cout << "Maximum of memory spent on nodes: " << (generatedNodes*sizeof(Node))/1024 << " KB" << endl << endl;
if(str != "Solution not found"){
cout << "Movements: " << str << endl << endl;
return;
}
}
cout << "Solution not found" << endl << endl;
}
void BFS(Config& initialConfig, Config& finalConfig){ /*Breadth first search function*/
if(!solutionExists(initialConfig, finalConfig)){
cout << "It's not possible to reach to the final configuration starting from the initial configuration" << endl << endl; //There is no solution
return;
}
generatedNodes = visitedNodes = 0;
string str = GENERAL_SEARCH_BFS(initialConfig, finalConfig, MAX_DEPTH);
if(str != "Solution not found")
cout << "Movements: " << str << endl << endl;
else
cout << "Solution not found" << endl << endl;
cout << "Generated Nodes: " << generatedNodes << endl;
cout << "Visited Nodes: " << visitedNodes << endl;
cout << "Maximum of memory spent on nodes: " << (generatedNodes*sizeof(Node))/1024 << " KB" << endl << endl;
}
void ASTAR(Config& initialConfig, Config& finalConfig, unsigned int heuristicsFlag){ /*A* search function calls overloaded 2nd version of BFS*/
if(!solutionExists(initialConfig, finalConfig)){
cout << "It's not possible to reach to the final configuration starting from the initial configuration" << endl << endl; //There is no solution
return;
}
generatedNodes = visitedNodes = 0;
string str = GENERAL_SEARCH_ASTAR(initialConfig, finalConfig, MAX_DEPTH, heuristicsFlag);
if(str != "Solution not found")
cout << "Movements: " << str << endl << endl;
else
cout << "Solution not found" << endl << endl;
cout << "Generated Nodes: " << generatedNodes << endl;
cout << "Visited Nodes: " << visitedNodes << endl;
cout << "Maximum of memory spent on nodes: " << (generatedNodes*sizeof(Node))/1024 << " KB" << endl << endl;
}
void GREEDY(Config& initialConfig, Config& finalConfig, unsigned int heuristicsFlag){ /*Greedy with Heuristics search function*/
if(!solutionExists(initialConfig, finalConfig)){
cout << "It's not possible to reach to the final configuration starting from the initial configuration" << endl << endl; //There is no solution
return;
}
generatedNodes = visitedNodes = 0;
string str = GENERAL_SEARCH_GREEDY(initialConfig, finalConfig, MAX_DEPTH, heuristicsFlag);
if(str != "Solution not found")
cout << "Movements: " << str << endl << endl;
else
cout << "Solution not found" << endl << endl;
cout << "Generated Nodes: " << generatedNodes << endl;
cout << "Visited Nodes: " << visitedNodes << endl;
cout << "Maximum of memory spent on nodes: " << (generatedNodes*sizeof(Node))/1024 << " KB" << endl << endl;
}