-
Notifications
You must be signed in to change notification settings - Fork 0
/
AStar.h
178 lines (138 loc) · 4.99 KB
/
AStar.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
//Author Faizan
#include <set>
#include "node.h"
#define H2 2 // manhatten cost
#define H1 1 // hamming cost
#define DEPTH_LIMIT 1000 // maximum allowed depth
#define NODES_LIMIT 200000000 // nodes limit (no more than that)
#define cost_ cost
#define parent_ parent
struct NodeData {
bool isDead; // further can't explored
int cost; // g cost paramets
int parent; // parent cost
bool operator==(const NodeData &node) const {
return parent == node.parent &&
cost == node.cost;
}
bool operator!=(const NodeData &node) const {
return !(node == *this);
}
};
class aStarSearchAlgorithm {
public:
map<Node, NodeData> explored;//
size_t openedTotal;
int maximum_depth;
int nInserted;
int heuristicKind = 2;
bool isCorrect(int w, int h) { return w >= 0 && h >= 0 && w< Node::boardSqSize && h < Node::boardSqSize; }
static int h1(const Node ¤t_state, const Node &goal_state) {
int dissmilarity = 0;
for (int m = 0; m < Node::boardSqSize; m++)
for (int n = 0; n < Node::boardSqSize; n++)
if (current_state.A[m][n] && current_state.A[m][n] != goal_state.A[m][n]) dissmilarity++;
return dissmilarity;
}
static int h2(const Node ¤t_state, const Node &goal_state) {
int cost = 0;
puzzle_t pR[(Node::boardSqSize * Node::boardSqSize) + 1];
puzzle_t pC[(Node::boardSqSize * Node::boardSqSize) + 1];
for (int i = 0; i < Node::boardSqSize; i++) {
for (int j = 0; j < Node::boardSqSize; j++) {
pR[current_state.A[i][j]] = static_cast<puzzle_t>(i);
pC[current_state.A[i][j]] = static_cast<puzzle_t>(j);
}
}
for (int i = 0; i < Node::boardSqSize; i++)
for (int j = 0; j < Node::boardSqSize; j++)
if (goal_state.A[i][j])
cost += abs(pR[goal_state.A[i][j]] - i) + abs(pC[goal_state.A[i][j]] - j);
// cout<<"cost: "<< cost <<endl;
return cost;
}
// int h2(const Node ¤t_state, const Node &goal_state)
// {
// int manhatten_dis = 0;
// for(int i = 0; i < Node::boardSqSize; i++)
// {
// for (int j = 0; j < Node::boardSqSize; j++)
// {
// int number = current_state.A[i][j]; // return an element to number variable...
// // cout<<"num: "<<number<<endl;
// for (int m = 0; m < Node::boardSqSize; m++)
// {
// int n = 0;
// while (n < Node::boardSqSize)
// {
// int goal_number = goal_state.A[m][n];
// if (goal_number == number)
// {
// manhatten_dis += abs(m - i) + abs(n - j);
// }
// n+=1;
// }}}}
// cout<<"h2: "<<manhatten_dis<<endl;
// return manhatten_dis;
// }
double heuristicSelection(const Node ¤t_state, const Node &goal_state) {
if (heuristicKind == H1) return h1(current_state, goal_state);
if (heuristicKind == H2) return h2(current_state, goal_state);
return 0;
}
int AStarSearch(const Node &intial_state, const Node &goal_state) {
int nExplored = 0;
maximum_depth = 0;
nInserted = 0;
priority_queue<pair<double, Node>> priorityList;
priorityList.push({0, intial_state}); // insert initial state into priority queue
explored[intial_state] = {false, 0, EOF};
while (!priorityList.empty()) {
Node current_state = priorityList.top().second;
priorityList.pop();
++nExplored;
NodeData ¤tInfo = explored[current_state];
currentInfo.isDead = true;
maximum_depth = max(maximum_depth, explored[current_state].cost);
if (current_state == goal_state) {
break;
}
if (currentInfo.cost > DEPTH_LIMIT) {
cout << "depth limit reached" << endl << current_state;
break;
}
if (explored.size() > NODES_LIMIT) {
cout << "Nodes limit reached" << endl << current_state;
break;
}
int blankX = -1, blankY = -1;
Node::getZeroPos(current_state, blankX, blankY);
for (direction_t action = 0; action < 4; action++) {
int blankXUpdated = blankX + dirX[action]; // (1 + 0) = 1
int blankYUpdated = blankY + dirY[action]; // (1 + 1) = 2 (1, 2) --> right
if (isCorrect(blankXUpdated, blankYUpdated)) {
Node current_state_copy = current_state;
swap(current_state_copy.A[blankX][blankY], current_state_copy.A[blankXUpdated][blankYUpdated]);
bool isVisited = explored.find(current_state_copy) != explored.end();
if (isVisited && explored[current_state_copy].isDead) continue;
double newCost = currentInfo.cost + 1;
if (!isVisited || newCost < explored[current_state_copy].cost) {
++nInserted;
explored[current_state_copy] = {false, static_cast<int>(newCost), Node::oppositeDirection(action)};
double priority = newCost + heuristicSelection(current_state_copy, goal_state);
priorityList.push({-priority, current_state_copy});
}
}
}
}
openedTotal = explored.size();
return nInserted;
}
void setHeuristic(int heuristic = H1) {
heuristicKind = heuristic;
}
virtual ~aStarSearchAlgorithm() {
heuristicKind = 0;
explored.clear();
}
};