-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
163 lines (127 loc) · 4.02 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
#include <iostream>
#include "State.h"
#include <algorithm>
using namespace std;
template <typename myState>
bool BFS(const myState &goal,queue<myState> &agenda, set<myState> &closed,myState &solution)
{
myState s;
while (!(agenda.empty()))
{
s=agenda.front();
agenda.pop();
if (s==goal)
{
solution = s;
return true;
}
if (closed.size()==0 || (closed.find(s)==closed.end()))// if closed set is empty or set does NOT contain s,
{
vector<myState> children;
children=s.expand();
closed.insert(s);
for (unsigned int i=0;i<children.size();i++)
agenda.push(children.at(i));
}
}
return false;
}
template <typename myState>
bool DFS(const myState &goal,stack<myState> &agenda, set<myState> &closed,myState &solution)
{
myState s;
while (!(agenda.empty()))
{
s=agenda.top();
agenda.pop();
if (s==goal)
{
solution = s;
return true;
}
if (closed.size()==0 || (closed.find(s)==closed.end()))
{
vector<myState> children;
children=s.expand();
closed.insert(s);
for (unsigned int i=0;i<children.size();i++)
agenda.push(children.at(i));
}
}
return false;
}
template <typename myState>
bool BestFS(const myState &goal, vector<myState> &agenda, vector<myState> &closed, myState &solution)
{
myState s;
if (agenda.empty())
return false;
s = getBestRoute(agenda);
if (s==goal)
{
solution = s;
return true;
}
if (closed.size()==0 || (find(closed.begin(), closed.end(), s) == closed.end()))
// if closed set is empty or set does NOT contain s,
{
vector<myState> children;
children=s.expand();
closed.push_back(s);
for (unsigned int i=0;i<children.size();i++)
agenda.push_back(children.at(i));
}
return BestFS(goal,agenda,closed,solution);
}
State getBestRoute(vector<State> &agenda) //finding best State to follow and deleting that State from the agenda
{
State bestState = agenda.at(0);
int pos = 0;
unsigned i;
for (i=1;i<agenda.size()-1;i++)
{
if (agenda.at(i).getManhattan() < bestState.getManhattan())
{
bestState = agenda.at(i);
pos = i;
}
}
agenda.erase(agenda.begin() + pos);
return bestState;
}
int main()
{
int startingBoard[3][3] = {
{6, 7, 1} , /* initializers for row indexed by 0 */
{0, 3, 2} , /* initializers for row indexed by 1 */
{8, 5, 4} /* initializers for row indexed by 2 */
};
int goalBoard[3][3] = {
{1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6} , /* initializers for row indexed by 1 */
{7, 8, 0} /* initializers for row indexed by 2 */
};
State initial(1,0,startingBoard);//Creates the starting state
State goal(2,2,goalBoard);//Sets a goal
stack <State> agenda;
queue <State> Qagenda;
vector <State> bestFSAgenda;
set <State> closed; //Closed set
set <State> Qclosed;
vector <State> bestFSClosed;
agenda.push(initial);
Qagenda.push(initial);
bestFSAgenda.push_back(initial);
cout<<"Starting State: \n"<<initial.toString()<<"\n"<<"\nGoal State: \n"<<goal.toString()<<endl;//Prints the starting to final state
State solution;
BestFS(goal,bestFSAgenda,bestFSClosed,solution);
cout<<"BestFS Solution: "<<endl;
cout<<solution.getPath()<<endl;;
BFS(goal,Qagenda,Qclosed,solution);
cout<<"BFS Solution: "<<endl;
cout<<solution.getPath()<<endl;;
DFS(goal,agenda,closed,solution);
cout<<"DFS Solution: "<<endl;
cout<<solution.getPath()<<endl;;
return 0;
}