-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
595 lines (554 loc) · 17.2 KB
/
main.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <chrono>
using namespace std;
using namespace std::chrono;
void DFS();
void DLS();
void IDS();
void rebuild_path();
int wrongtile();
int manhattan();
void A_star_wrongtile(string startstate, string goalstate);
void A_star_manhattan(string startstate, string goalstate);
class node
{
public:
string state;
string action_string;
string parent;
int path_cost;
string child_node(string state_temp, string action_string)
{
//Action for right
if(action_string=="right")
{
int null_pos;
for(int i=0;i<9;i++)
{
if(state_temp.substr(i,1)=="0")
{
null_pos=i;
break;
}
}
// cout<<null_pos<<endl;
if (null_pos!=0&&null_pos!=3&&null_pos!=6)
{
int temp=state_temp.at(null_pos-1);
state_temp.at(null_pos-1)=state_temp.at(null_pos);
state_temp.at(null_pos)=temp;
return state_temp;
}
else
{
//cout<<"Operation not possible";
return "no";
}
}
//Action for left
if(action_string=="left")
{
int null_pos;
for(int i=0;i<9;i++)
{
if(state_temp.substr(i,1)=="0")
{
null_pos=i;
break;
}
}
if (null_pos!=2&&null_pos!=5&&null_pos!=8)
{
int temp=state_temp.at(null_pos+1);
state_temp.at(null_pos+1)=state_temp.at(null_pos);
state_temp.at(null_pos)=temp;
return state_temp;
}
else
{
//cout<<"Operation not possible";
return "no";
}
}
//Action for up
if(action_string=="up")
{
int null_pos;
for(int i=0;i<9;i++)
{
if(state_temp.substr(i,1)=="0")
{
null_pos=i;
break;
}
}
if (null_pos!=6&&null_pos!=7&&null_pos!=8)
{
int temp=state_temp.at(null_pos+3);
state_temp.at(null_pos+3)=state_temp.at(null_pos);
state_temp.at(null_pos)=temp;
return state_temp;
}
else
{
//cout<<"Operation not possible";
return "no";
}
}
//Action for down
if(action_string=="down")
{
int null_pos;
for(int i=0;i<9;i++)
{
if(state_temp.substr(i,1)=="0")
{
null_pos=i;
break;
}
}
if (null_pos!=0&&null_pos!=1&&null_pos!=2)
{
int temp=state_temp.at(null_pos-3);
state_temp.at(null_pos-3)=state_temp.at(null_pos);
state_temp.at(null_pos)=temp;
return state_temp;
}
else
{
//cout<<"Operation not possible";
return "no";
}
}
return "ignore";
}
};
void visualise(string state); //Visualise function will take input of state as a string, and output will be a 3*3 matrix
string input_state; //User will give input state using command line
string goal_state="123804765"; //As defined in the problem statement
float time_DLS;
float time_IDS;
float time_A_wrongtile;
float time_A_manhattan;
int main()
{
cout<<"Enter the input state: "<<endl;
getline(cin,input_state);
cout<<"Input State Is: "<<input_state<<endl;
cout<<"Goal State Is: "<<goal_state<<endl;
node root;
root.state=input_state;
visualise(root.state);
//DFS();
// cout<<"--------------------Depth Limited Search--------------------"<<endl;
// DLS();
// IDS();
cout<<"--------------------A* with Wrong Tiles--------------------"<<endl;
A_star_wrongtile(input_state,goal_state);
cout<<"--------------------A* with Manhattan--------------------"<<endl;
A_star_manhattan(input_state,goal_state);
// cout<<"Time of execution for Depth Limited Search is : "<<(time_DLS)/1000<<" milliseconds. "<<endl;
// cout<<"Time of execution for Iterative Deepening Search is : "<<(time_IDS)/1000<<" milliseconds. "<<endl;
cout<<"Time of execution for A* search with wrong tile is : "<<(time_A_wrongtile)/1000<<" milliseconds. "<<endl;
cout<<"Time of execution for A* search with manhattan distance is : "<<(time_A_manhattan)/1000<<" milliseconds. "<<endl;
return 0;
}
void visualise(string state)
{
cout<<"***************************************"<<endl;
for(int i=0;i<9;i++)
{
cout<<"|"<<state.substr(i,1)<<"|"<<state.substr(i+1,1)<<"|"<<state.substr(i+2,1)<<"|"<<endl;
i=i+2;
}
}
void DFS()
{
stack<node> frontier;
node root;
root.state=input_state;
root.parent= "";
root.action_string= "";
root.path_cost=1;
stack<string> direction;
node current_node;
string action[4]={"left","right","up","down"};
frontier.push(root);
int flag=-1;
while (frontier.size()>0&&flag==-1)
{
current_node=frontier.top();
frontier.pop();
direction.push(current_node.action_string);
if (current_node.state==goal_state)
{
flag=1;
break;
}
for(int i=0;i<4;i++)
{
string child_state= current_node.child_node(current_node.state,action[i]);
if(child_state!="no")
{
node child_node;
child_node.state=child_state;
current_node.action_string=action[i];
child_node.parent=current_node.state;
frontier.push(child_node);
}
}
}
if (flag==1)
{
while(direction.size()>0)
{
cout<<direction.top()<<endl;
direction.pop();
}
}
}
void DLS()
{
auto start=high_resolution_clock::now();
// cout<<"--------------------Depth Limited Search--------------------"<<endl;
int depth, depth_counter;
cout<<"Enter the depth till which you want to search."<<endl;
cin>>depth;
stack<node> frontier;
node root;
root.state=input_state;
root.parent= "";
root.action_string= "";
root.path_cost=1;
stack<string> direction;
stack<int> direction_depth;
node current_node;
string action[4]={"left","right","up","down"};
frontier.push(root);
int flag=-1;
int step_counter=0;
while (frontier.size()>0&&(flag==-1||flag==0))
{
current_node=frontier.top();
frontier.pop();
if (direction.size()>0)
{
while (true)
{
if(direction_depth.top()>=depth_counter)
{
direction.pop();
direction_depth.pop();
}
else
break;
}
}
direction.push(current_node.action_string);
direction_depth.push(depth_counter);
if (current_node.state==goal_state)
{
flag=1;
visualise(current_node.state);
cout<<"REACHED GOAL STATE!"<<endl;
break;
}
else
{
if (depth_counter>depth)
flag=0;
else
{
int temp_for_action;
for(int i=0;i<4;i++)
{
string child_state= current_node.child_node(current_node.state,action[i]);
if(child_state!="no")
{
node child_node;
child_node.state=child_state;
current_node.action_string=action[i];
child_node.parent=current_node.state;
frontier.push(child_node);
depth_counter=depth_counter+1;
temp_for_action=i;
}
}
visualise(current_node.state);
}
}
}
if (flag==1)
{
while(direction.size()>0)
{
cout<<direction.top()<<endl;
direction.pop();
}
}
auto stop=high_resolution_clock::now();
auto duration= duration_cast<microseconds>(stop - start);
time_DLS=duration.count();
}
void IDS()
{
auto start=high_resolution_clock::now();
int depth, depth_counter;
cout<<"Enter the depth till which you want to search."<<endl;
cin>>depth;
stack<node> frontier;
node root;
root.state=input_state;
root.parent= "";
root.action_string= "";
root.path_cost=1;
stack<string> direction;
stack<int> direction_depth;
node current_node;
string action[4]={"left","right","up","down"};
frontier.push(root);
int flag=-1;
int step_counter=0;
while (frontier.size()>0&&(flag==-1||flag==0))
{
current_node=frontier.top();
frontier.pop();
if (direction.size()>0)
{
while (true)
{
if(direction_depth.top()>=depth_counter)
{
direction.pop();
direction_depth.pop();
}
else
break;
}
}
direction.push(current_node.action_string);
direction_depth.push(depth_counter);
if (current_node.state==goal_state)
{
flag=1;
visualise(current_node.state);
cout<<"REACHED GOAL STATE!"<<endl;
break;
}
else
{
if (depth_counter>depth)
flag=0;
else
{
int temp_for_action;
for(int i=0;i<4;i++)
{
string child_state= current_node.child_node(current_node.state,action[i]);
if(child_state!="no")
{
node child_node;
child_node.state=child_state;
current_node.action_string=action[i];
child_node.parent=current_node.state;
frontier.push(child_node);
depth_counter=depth_counter+1;
temp_for_action=i;
}
}
visualise(current_node.state);
}
}
}
if (flag==1)
{
while(direction.size()>0)
{
cout<<direction.top()<<endl;
direction.pop();
}
}
auto stop=high_resolution_clock::now();
auto duration= duration_cast<microseconds>(stop - start);
time_IDS=duration.count();
}
int wrongtile(string parent_state, string child_state)
{
int wrong_tile=0;
for(int i=0;i<9;i++)
{
if(child_state[i]!=parent_state[i])
wrong_tile=wrong_tile+1;
}
return wrong_tile;
}
int manhattan(string parent_state, string child_state)
{
int manhattan_dist=0;
for (int i=0;i<9;i++)
{
for (int j=0; j<9;j++)
{
if (parent_state[i]==child_state[j])
{
manhattan_dist=manhattan_dist+abs(i-j);
}
}
}
return manhattan_dist;
}
struct Prior_Node{
int priority;
node state;
};
struct Direction_Node{
string child;
string action;
};
struct CompareNode {
bool operator()(Prior_Node const& p1, Prior_Node const& p2)
{
// return "true" if "p1" is ordered
// before "p2", for example:
return p1.priority > p2.priority;
}
};
void A_star_wrongtile(string startstate, string goalstate)
{
auto start=high_resolution_clock::now();
priority_queue<Prior_Node, vector<Prior_Node>, CompareNode> frontier;
Prior_Node priority_root = Prior_Node();
node root;
root.state=startstate;
root.parent= "";
root.action_string= "";
root.path_cost=0;
priority_root.priority = wrongtile(startstate, goalstate);
priority_root.state = root;
frontier.push(priority_root);
set<string> visited;
map<string, int> fScore;
map<string, string> directions;
fScore.insert(pair<string,int>(startstate,wrongtile(startstate, goalstate)));
directions.insert(pair<string,string>(startstate,""));
int flag=0;
while( frontier.size()>0&&flag==0)
{
Prior_Node current_node = frontier.top();
frontier.pop();
if(current_node.state.state==goalstate)
{
flag=1;
string current;
current = current_node.state.state;
while(current != "")
{
visualise(current);
cout<<"\n";
current = directions.at(current);
}
cout<<endl;
cout<<"Reached goal state!"<<endl;
break;
}
else
{
string action[4]={"left","right","up","down"};
for (int i=0; i<4;i++)
{
string child_state= current_node.state.child_node(current_node.state.state,action[i]);
if(child_state!="no" && !(visited.find(child_state) != visited.end()))
{
int child_heuristic_1 = current_node.state.path_cost + 1;
int child_heuristic_2 = wrongtile(child_state, goalstate);
node child_node;
child_node.state=child_state;
child_node.parent= current_node.state.state;
child_node.action_string= action[i];
child_node.path_cost=child_heuristic_1;
fScore.insert(pair<string,int>(child_state,child_heuristic_1 + child_heuristic_2));
Prior_Node new_priority_node = Prior_Node();
new_priority_node.priority = child_heuristic_1 + child_heuristic_2;
new_priority_node.state = child_node;
directions.insert(pair<string,string>(child_node.state,current_node.state.state));
frontier.push(new_priority_node);
}
}
}
}
auto stop=high_resolution_clock::now();
auto duration= duration_cast<microseconds>(stop - start);
time_A_wrongtile=duration.count();
}
void A_star_manhattan(string startstate, string goalstate)
{
auto start=high_resolution_clock::now();
priority_queue<Prior_Node, vector<Prior_Node>, CompareNode> frontier;
Prior_Node priority_root = Prior_Node();
node root;
root.state=startstate;
root.parent= "";
root.action_string= "";
root.path_cost=0;
priority_root.priority = manhattan(startstate, goalstate);
priority_root.state = root;
frontier.push(priority_root);
set<string> visited;
map<string, int> fScore;
map<string, string> directions;
fScore.insert(pair<string,int>(startstate,manhattan(startstate, goalstate)));
directions.insert(pair<string,string>(startstate,""));
int flag=0;
while( frontier.size()>0&&flag==0)
{
Prior_Node current_node = frontier.top();
frontier.pop();
if(current_node.state.state==goalstate)
{
flag=1;
string current;
current = current_node.state.state;
while(current != "")
{
visualise(current);
cout<<"\n";
current = directions.at(current);
}
cout<<endl;
cout<<"Reached goal state!"<<endl;
break;
}
else
{
string action[4]={"left","right","up","down"};
for (int i=0; i<4;i++)
{
string child_state= current_node.state.child_node(current_node.state.state,action[i]);
if(child_state!="no" && !(visited.find(child_state) != visited.end()))
{
int child_heuristic_1 = current_node.state.path_cost + 1;
int child_heuristic_2 = manhattan(child_state, goalstate);
node child_node;
child_node.state=child_state;
child_node.parent= current_node.state.state;
child_node.action_string= action[i];
child_node.path_cost=child_heuristic_1;
fScore.insert(pair<string,int>(child_state,child_heuristic_1 + child_heuristic_2));
Prior_Node new_priority_node = Prior_Node();
new_priority_node.priority = child_heuristic_1 + child_heuristic_2;
new_priority_node.state = child_node;
directions.insert(pair<string,string>(child_node.state,current_node.state.state));
frontier.push(new_priority_node);
}
}
}
}
auto stop=high_resolution_clock::now();
auto duration= duration_cast<microseconds>(stop - start);
time_A_manhattan=duration.count();
}