-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmct.cpp
373 lines (286 loc) · 7.33 KB
/
mct.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
#include "mct.hpp"
#include <random>
#include <chrono>
#include <tuple>
#include <climits>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <fstream>
#include <queue>
#define INF 1'000'000'000
MCT :: MCT() {
}
MCT :: MCT(const char* filename, int table[][8]) {
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(table[i][j] != -1)
board.put(i, j, table[i][j]);
}
}
std :: ifstream fs(filename, std :: ios_base :: binary);
if(fs.is_open()) {
struct RECORD {
int result;
int x;
int y;
int piece;
int points;
int firstChild;
int sibling;
};
/*
[result | x | y | piece | points | firstChild | sibling]
*/
auto loadRecord = [&fs](int u)->RECORD {
unsigned recordBytes = 7 * 4;
RECORD record;
fs.seekg(recordBytes * u, std :: ios_base :: beg);
fs.read((char*)&record.result, 4);
fs.read((char*)&record.x, 4);
fs.read((char*)&record.y, 4);
fs.read((char*)&record.piece, 4);
fs.read((char*)&record.points, 4);
fs.read((char*)&record.firstChild, 4);
fs.read((char*)&record.sibling, 4);
return record;
};
auto dfs = [&](auto& self, int u)->std :: shared_ptr<Position> {
auto r = loadRecord(u);
std :: shared_ptr<Position> ptr(new Position {r.result, {}});
for(int v = r.firstChild; v != -1; ) {
auto rC = loadRecord(v);
ptr->children.push_back({rC.x, rC.y, rC.piece, rC.points, self(self, v)});
v = rC.sibling;
}
return ptr;
};
root = dfs(dfs, 0);
fs.close();
} else {
build(10000);
persist("data/start_tree.dat");
system("cp -f data/start_tree.dat data/cache_tree.dat");
}
}
MCT :: MCT(int iter) {
build(iter);
}
int MCT :: p1(int s, int a, int b) {
int p = board.getPositions();
if(!p) return max[p][board] = 0;
auto it = max[p].find(board);
if(it != max[p].end()) return it->second;
int result = -INF;
for(int x = 0; x < 8; ++x) {
for(int y = 0; y < 8; ++y) {
if(!board.can(x, y)) continue;
for(int pi = 0; pi < 2; ++pi) {
int pts = board.put(x, y, pi);
int score = pts + p2(s + pts, a, b);
board.rem(x, y);
if(b < score + s) return INF;
if(result < score) result = score;
if(a < score + s) a = score + s;
}
}
}
if(result <= 4 - INF) return INF;
return max[p][board] = result;
}
int MCT :: p2(int s, int a, int b) {
int p = board.getPositions();
if(!p) return min[p][board] = 0;
auto it = min[p].find(board);
if(it != min[p].end()) return it->second;
int result = INF;
for(int x = 0; x < 8; ++x) {
for(int y = 0; y < 8; ++y) {
if(!board.can(x, y)) continue;
for(int pi = 0; pi < 2; ++pi) {
int pts = board.put(x, y, pi);
int score = -pts + p1(s - pts, a, b);
board.rem(x, y);
if(a > score + s) return -INF;
if(result > score) result = score;
if(b > score + s) b = score + s;
}
}
}
if(result >= INF - 4) return -INF;
return min[p][board] = result;
}
MCT :: ~MCT() {
}
void MCT :: print(std :: shared_ptr<Position>& p) {
if(!p) return;
board.print();
std::cout<<'\n';
for(auto [x, y, piece, points, next] : p->children) {
board.put(x, y, piece);
print(next);
board.rem(x, y);
}
}
void MCT :: print() {
board.print();
}
void MCT :: build(std :: shared_ptr<Position>& p, int max) {
if(board.getPositions() == 0) {
p->result = 0;
return;
}
p->result = max ? -INF : INF;
int x, y, pi;
board.nextMove(x, y, pi);
int points = board.put(x, y, pi);
auto& chlist = p->children;
std :: shared_ptr<Position> ch;
auto it = std :: find_if(chlist.begin(), chlist.end(), [x, y, pi](const Edge& a) {
return a.x == x && a.y == y && a.piece == pi;
});
if(it == chlist.end()) {
ch.reset(new Position);
ch->result = max ? INF : -INF;
chlist.push_back({x, y, pi, points, ch});
} else ch = it->next;
build(ch, max ^ 1);
board.rem(x, y);
for(auto [r, c, piece, points, next] : chlist) {
if(max) p->result = std :: max(p->result, next->result + points);
else p->result = std :: min(p->result, next->result - points);
}
}
void MCT :: build(int iter) {
root.reset(new Position);
root->result = -INF;
while(iter--) build(root, 1);
}
int MCT :: do_next_move(int& x, int& y, int& pi) {
int p = board.getPositions();
if(p <= MINIMAXN) {
#ifdef DEBUG
int64_t A = std :: chrono :: steady_clock :: now().time_since_epoch().count();
#endif
int result = p1(0, -INF, INF);
#ifdef DEBUG
int64_t B = std :: chrono :: steady_clock :: now().time_since_epoch().count();
std :: cout.setf(std :: ios_base :: fixed);
std :: cout << "tempo do minimax = " << double(B - A) * std :: chrono :: system_clock :: period :: num / std :: chrono :: system_clock :: period :: den << " s\n";
if(p == MINIMAXN || p == MINIMAXN - 1) {
int tot = 0;
for(int i = 0; i <= 64; ++i) tot += max[i].size() + min[i].size();
std :: cout << "ht size = " << tot << '\n';
}
#endif
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(!board.can(i, j)) continue;
for(int p = 0; p < 2; ++p) {
int pts = board.put(i, j, p);
auto& htab = min[board.getPositions()];
auto it = htab.find(board);
board.rem(i, j);
if(it != htab.end() && it->second + pts == result) {
x = i, y = j, pi = p;
return board.put(i, j, p);
}
}
}
}
std :: cout << result << ' ' << "erro: do_next_move não encontrou o próximo movimento\n";
exit(1);
}
auto& chlist = root->children;
auto it = std :: find_if(chlist.begin(), chlist.end(), [&](Edge a) {
return a.points + a.next->result == root->result;
});
assert(it != chlist.end());
x = it->x, y = it->y, pi = it->piece;
auto cur = it->next;
chlist.erase(it);
root = cur;
return board.put(x, y, pi);
}
int MCT :: move_to(int x, int y, int pi) {
int score = board.put(x, y, pi);
if(board.getPositions() > MINIMAXN) {
auto& chlist = root->children;
#ifdef DEBUG
std :: cout << "tamanho da lista = " << chlist.size() << '\n';
#endif
auto it = std :: find_if(chlist.begin(), chlist.end(), [x, y, pi](Edge a) {
return a.x == x && a.y == y && a.piece == pi;
});
if(it == chlist.end()) {
#ifdef DEBUG
std :: cout << "tive que construir de novo\n";
#endif
build(936);
} else {
auto cur = it->next;
chlist.erase(it);
root = cur;
}
build(64);
} else
root.reset();
return score;
}
void MCT :: persist(const char* filename) {
if(!root) {
#ifdef DEBUG
std :: cout << "a raíz é nula\n";
#endif
return;
}
std :: ofstream fs(filename, std :: ios_base :: binary);
int cur = 1;
assert(fs.is_open());
struct RECORD {
std :: shared_ptr<Position> no;
int x;
int y;
int piece;
int points;
int sibling;
};
std :: queue<RECORD> q;
q.push({
root,
-1,
-1,
-1,
-1,
-1
});
/*
[result | x | y | piece | points | firstChild | sibling]
*/
while(!q.empty()) {
auto [ptr, x, y, piece, points, sibling] = q.front();
q.pop();
fs.write((const char*)&ptr->result, 4);
fs.write((const char*)&x, 4);
fs.write((const char*)&y, 4);
fs.write((const char*)&piece, 4);
fs.write((const char*)&points, 4);
auto& adj = ptr->children;
int firstChild = adj.empty() ? -1 : cur;
fs.write((const char*)&firstChild, 4);
fs.write((const char*)&sibling, 4);
for(int i = 0; i < (int)adj.size(); ++i) {
auto& [x, y, piece, points, child] = adj[i];
q.push({
child,
x,
y,
piece,
points,
i + 1 < (int)adj.size() ? cur + 1 : -1
});
++cur;
}
}
fs.close();
}