-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.h
220 lines (166 loc) · 5.53 KB
/
puzzle.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
#ifndef PUZZLE_H
#define PUZZLE_H
#include <iostream>
#include <queue>
#include "node.h"
#include <vector>
#include <cmath>
using namespace std;
class puzzle {
private:
//queue of nodes to visit (last one is next in line), ordered in rank of heuristic
vector<node *> queue;
vector<node *> visited; // already visited nodes (vector)
node goal = node(); //goal state (default constructor gives 0 1 2 3 4 5 6 7 8)
int heuristic;
public:
puzzle(){
heuristic = 1;
}
// void general_search(problem, QUEUEING-FUNCTION){
node * general_search(node * input){
//measure nodes traversed + max queue size for writeup
int nodes_traversed = 0;
int max_queue_size = 0;
//nodes = MAKE-QUEUE(MAKE-NODE(problem.INITIAL-STATE))
queue.push_back(input);
// loop do
while(queue.size() > 0){
// node = REMOVE-FRONT(nodes)
node * curnode = queue.at(queue.size() - 1);
queue.pop_back();
// cout << " " << curnode->depth << " " << curnode->get_h() << " " << queue.size() << endl;
// ! TODO HERE IS THE DEPTH cout
nodes_traversed++;
if(max_queue_size < queue.size()){
max_queue_size = queue.size();
}
// if problem.GOAL-TEST(node.STATE) succeeds then return node
if(curnode->compare_nodes(goal)){
cout << "HEURISTIC: " << heuristic;
cout << endl;
cout << "ANSWER HAS BEEN FOUND AT DEPTH: " << curnode->depth <<endl << "NODES CHECKED :" << nodes_traversed << endl << "MAX QUEUE SIZE: " << max_queue_size << endl;
return curnode;
}
visited.push_back(curnode);
//nodes = QUEUEING-FUNCTION(nodes, EXPAND(node, problem.OPERATORS))
queueing_function(curnode);
}
//if EMPTY(nodes) then return "failure"
cout << "FAILURE";
return nullptr;
};
void add_visited(node * input){
visited.push_back(input);
};
void set_heuristic(int h){
heuristic = h;
}
//pass in node, add children to the queue in order, pass in heuristic assign them heuristic and add them to queue in order
// 1 = UCS, 2 = MANHATTAN DISTANCE, 3 = MISPLACED TILES
void queueing_function(node * input){
vector<node *> children = create_children(input);
// h = 1 and put everything in front
if(heuristic == 1){
for(int i = 0; i < children.size(); i++){
queue.insert(queue.begin(), children.at(i));
// children.at(i).get_parent()->print_node();
}
}
//manhattan distance
else if(heuristic == 2){
for(int i = 0; i < children.size(); i++){
compute_manhattan(children.at(i));
// queue.insert(queue.begin(), children.at(i));
int j = 0;
while(j < queue.size() && queue.at(j)->get_h() > children.at(i)->get_h()){
j++;
}
std::vector<node*>::iterator it = queue.begin() + j;
queue.insert(it, children.at(i));
// cout << j << " ; " << queue.size() << " : " << children.at(i)->get_h() << " : " << queue.at(0)->get_h() << " : " << queue.at(queue.size()-1)->get_h() << endl;
}
}
//misplaced tile heuristic
else{
for(int i = 0; i < children.size(); i++){
compute_misplaced(children.at(i));
// queue.insert(queue.begin(), children.at(i));
std::vector<node*>::iterator it = queue.begin();
int j = 0;
while(j < queue.size() && queue.at(j)->get_h() > children.at(i)->get_h()){
j++;
it++;
}
queue.insert(it, children.at(i));
}
}
}
//returns a vector of nodes (children of the input node)
vector<node*> create_children(node * input){
vector<node*> children;
if(input->can_createD()){
node* b = new node(input->data_toVector());
b->set_parent(input);
b->set_depth(input->get_depth() + 1);
b->create_childD();
children.push_back(b);
}
if(input->can_createU()){
node* b = new node(input->data_toVector());
b->set_parent(input);
b->set_depth(input->get_depth() + 1);
b->create_childU();
children.push_back(b);
}
if(input->can_createR()){
node* b = new node(input->data_toVector());
b->set_parent(input);
b->set_depth(input->get_depth() + 1);
b->create_childR();
children.push_back(b);
}
if(input->can_createL()){
node* b = new node(input->data_toVector());
b->set_parent(input);
b->set_depth(input->get_depth() + 1);
b->create_childL();
children.push_back(b);
}
//checking for repeated states
vector<node*> children2;
for(int i = 0; i < children.size(); i++ ){
bool is_repeat = false;
for(int j = 0; j < visited.size();j++){
if(visited.at(j)->compare_nodes(*children.at(i))){
is_repeat = true;
}
}
if(is_repeat == false){
children2.push_back(children.at(i));
}
}
return children2;
}
void compute_manhattan(node* input){
int h = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
h+= abs(goal.findX(input->data[i][j]) - j) + abs(goal.findY(input->data[i][j]) - i);
}
}
input->set_h(h + input->get_depth());
}
void compute_misplaced(node* input){
int h = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(input->data[i][j] != goal.data[i][j]){
h++;
}
}
}
input->set_h(h + input->get_depth());
}
};
#endif