-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaterSort.cpp
382 lines (343 loc) · 8.35 KB
/
WaterSort.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
#include <vector>
#include <set>
#include <algorithm>
#include <random>
#include <sstream>
#include <iostream>
#include <chrono>
#include <map>
using namespace std;
class bottle : public vector<char>
{
public:
int order = 0; // position of the bottle in a state
};
typedef vector<bottle> state;
/// <summary>
/// Represents a state together with its generation, its heuristing distance
/// to the final solution, plus a reference to its parent node.
/// </summary>
class node
{
public:
state st; // The state of the node (bottles and their content)
mutable int g; // Generation
int h; // Heuristic estimation of number of steps to reach solution
const node* parent;
/// <summary>
/// Constructor
/// </summary>
node(state state, const node* p) : st(state), parent(p)
{
g = (p == nullptr) ? 0 : p->g + 1;
compute_h();
}
int f() const
{
return g + h;
}
/// <summary>
/// Computation of a heuristic distance to the solution
/// (number of steps needed)
/// </summary>
void compute_h()
{
static map<char, int> m;
m.clear();
h = 0;
for (size_t i = 0; i < st.size(); i++)
{
if (st[i].empty()) continue;
for (size_t j = 0; j < st[i].size() - 1; j++)
{
if (st[i][j] != st[i][j + 1])
h++;
}
m[st[i][0]]++;
}
for (auto& item : m)
{
h += item.second - 1;
}
}
};
/// <summary>
/// Node comparer for the open set. Compares nodes by their f value,
/// then by distance and then by their states
/// </summary>
/// <param name="a">First node to compare</param>
/// <param name="b">Second node to compare</param>
/// <returns>Returns true if node a is "less than" node b</returns>
struct ocomp
{
bool operator() (const node& a, const node& b) const
{
int fa = a.g + a.h;
int fb = b.g + b.h;
return (fa == fb) ? ((a.g == b.g) ? a.st < b.st : a.h < b.h) : (fa < fb);
}
};
/// <summary>
/// Node comparer for the closed set. Compares nodes by their states
/// only
/// </summary>
/// <param name="a">First node to compare</param>
/// <param name="b">Second node to compare</param>
/// <returns>Returns true if node a is "less than" node b</returns>
struct ccomp
{
bool operator() (const node& a, const node& b) const
{
return a.st < b.st;
}
};
/// <summary>
/// Represents a game setting, e.g. number of colors, height of each bottle,
/// number of empty bottles
/// </summary>
class game
{
static string alphabet;
int _t; // Number of total bottles
int _n; // Number of filled bottles
int _k; // Number of empty bottles
int _h; // Height of each bottle
public:
set<node, ocomp> open;
set<node, ccomp> closed;
/// <summary>
/// Generates a new game
/// </summary>
/// <param name="n">Number of colors</param>
/// <param name="k">Number of empty bottles</param>
/// <param name="h">Height of each bottle</param>
game(int n, int k, int h) : _t(n + k), _n(n), _h(h), _k(k)
{
}
/// <summary>
/// Generates randomly a new state using a seed value.
/// </summary>
state random_state(unsigned int seed)
{
vector<char> chars;
for (int i = 0; i < _n; i++)
{
for (int j = 0; j < _h; j++)
{
chars.push_back(alphabet[i]);
}
}
auto rng = std::default_random_engine{ seed };
std::shuffle(chars.begin(), chars.end(), rng);
int idx = 0;
state state;
for (int i = 0; i < _n; i++)
{
bottle b;
b.order = i;
for (int j = 0; j < _h; j++)
{
b.push_back(chars[idx++]);
}
state.push_back(b);
}
for (int i = 0; i < _k; i++)
{
bottle b;
b.order = _n + i;
state.push_back(b);
}
sort(state.begin(), state.end());
return state;
}
/// <summary>
/// Returns a string representation of a node
/// </summary>
/// <param name="start">The node to convert to a string</param>
/// <returns>A string for the node state, generation and heuristic distance</returns>
string node_to_string(const node& n)
{
state stc(n.st);
// We sort bottles according to their initial order
sort(stc.begin(), stc.end(), [&](bottle a, bottle b) {return a.order < b.order; });
ostringstream ostr;
ostr << '|';
for (int i = 0; i < _t; i++)
{
for (int j = 0; j < _h; j++)
{
if (j < stc[i].size())
ostr << stc[i][j];
else
ostr << " ";
}
ostr << '|';
}
ostr.width(3);
ostr << n.g;
ostr.width(3);
ostr << n.h;
return ostr.str();
}
/// <summary>
/// Builds recursively a string representation of a node
/// together with all its anchestor nodes
/// </summary>
string get_history(const node& n)
{
if (n.parent == nullptr)
{
return node_to_string(n);
}
else
{
return get_history(*n.parent) + "\n" + node_to_string(n);
}
}
/// <summary>
/// Generates the child nodes of a node
/// </summary>
vector<node> get_child_nodes(const node& n)
{
vector<node> result;
const state& st = n.st;
for (size_t from = 0; from < _t; from++)
{
if (st[from].empty()) continue;
for (size_t to = 0; to < _t; to++)
{
if (to == from || st[to].size() == _h)
continue;
if (st[to].empty() || st[from].back() == st[to].back())
{
state nst(st);
do
{
nst[to].push_back(nst[from].back());
nst[from].pop_back();
} while (
(!nst[from].empty()) &&
(nst[to].size() < _h) &&
(nst[to].back() == nst[from].back())
);
sort(nst.begin(), nst.end());
result.emplace_back(nst, &n);
}
}
}
return result;
}
/// <summary>
/// Checks whether we can skip entering a node in the
/// open set
/// </summary>
/// <param name="start">The set containing other nodes</param>
/// <returns>The node to check</returns>
bool check_skip(const set<node, ocomp>& s, const node& n)
{
auto it = s.begin();
while (it != s.end() && (*it).f() < n.f())
{
if ((*it).st == n.st) return true;
it++;
}
return false;
}
/// <summary>
/// Checks whether we can skip entering a node in the
/// closed set
/// </summary>
/// <param name="start">The set containing other nodes</param>
/// <returns>The node to check</returns>
bool check_skip(const set<node, ccomp>& s, const node& n)
{
auto it = s.find(n);
return (it == s.end()) ? false : n.g >= (*it).g;
}
/// <summary>
/// Solves a Water Sort problem using a* algorithm
/// </summary>
node solve(node start)
{
// put initial node in open set
open.insert(start);
while (open.size() > 0)
{
// nodes in the open set are sorted by their f=g+h value
// here we get the node with the lower f value
auto it = open.begin();
node n = *it;
open.erase(it);
// we have reached a solution!
if (n.h == 0)
return n;
// insert the node in the closed set
auto p = closed.emplace(n);
// if insertion was successful
if (p.second)
{
// get a reference of the node in its new location
auto& parent = *p.first;
vector<node> children = get_child_nodes(parent);
// insert each child node in the open set
// if inclusion checks are met
for (node& item : children)
{
if (check_skip(open, item))
continue;
if (check_skip(closed, item))
continue;
open.insert(item);
}
}
}
// return a node with an empty state in order to indicate
return node(state(), nullptr);
}
};
// Extend this if you plan to use more than 25 colors
string game::alphabet = "0123456789ABCDEFGHIJKLMNO";
int main()
{
// Initialize a new game with 12 colors, 2 empty bottles
// Height of each bottle is 4
game game(12, 2, 4);
// Generate an initial random state
state st = game.random_state(123);
// Initialize parent node
node initial(st, nullptr);
// Solve the state and get the solution
node sol = game.solve(initial);
// Print the steps
cout << game.get_history(sol) << endl;
// Uncomment the following section in order to run
// the solver several timesand collect statistics
//int N = 100;
//int c = 0;
//double sum = 0.0;
//auto start = chrono::high_resolution_clock::now();
//for (int i = 0; i < N; i++)
//{
// game g(12, 2, 4);
// state st = g.random_state(i);
// node n(st, nullptr);
// node solution = g.solve(n);
// if (!solution.st.empty())
// {
// cout.width(3);
// cout << i;
// cout.width(4);
// cout << solution.g << endl;
// sum += solution.g;
// c++;
// }
// else
// {
// cout.width(3);
// cout << i << " Unsolved!" << endl;
// }
//}
//auto end = chrono::high_resolution_clock::now();
//chrono::duration<double> d = end - start;
//std::cout << "Average steps = " << sum / c << " Average seconds to solve = " << d.count() / N << endl;
}