-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode.h
288 lines (251 loc) · 7.29 KB
/
node.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
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
#ifndef _NODE_H
#define _NODE_H
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cfloat>
#include <initializer_list>
#include "board.h"
#include "cache.h"
struct Options {
void UpdateMinProbFromDepth() { min_prob = 1.0 / (1 << (max_depth + 4)); }
int seed = 1;
int max_depth = 7;
int iterations = 1;
double min_prob = 0.001;
bool verbose = false;
bool interactive = false;
float optimality = 0.9;
int prefill_rank = 0;
int max_rank = 0;
bool tuple_moves = true;
double save_threshold = 0.1;
};
static Options options;
class Node : public Board {
public:
Node() = default;
Node(int layout[N][N]) : Board(layout) {}
void Prefill(int max_rank) {
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x) board[x][y] = 1;
for (int y = 0; y < 2; ++y)
for (int x = 0; x < 3; ++x) board[x][y] = max_rank - x * 2 - y;
board[2][1] = 4;
}
static int TileScore(int r) { return r << r; }
static int Tile(int r) { return r ? 1 << r : 0; }
int Tile(int x, int y) const { return Tile(board[x][y]); }
int MaxTile() const { return Tile(MaxRank()); }
int SumTile() const {
int sum_tile = 0;
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x) sum_tile += Tile(x, y);
return sum_tile;
}
int GameScore() const {
int game_score = 0;
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x) {
auto rank = board[x][y];
if (rank > 1) game_score += (rank - 1) << rank;
}
return game_score - num_4_tiles * 4;
}
void Line() const {
for (int x = 0; x < N; ++x) printf("--------");
puts("-");
}
void Show() const {
Line();
for (int y = 0; y < N; ++y) {
putchar('|');
for (int x = 0; x < N; ++x) {
auto tile = Tile(x, y);
if (x == rand_x && y == rand_y)
printf(" > %d <", tile);
else if (tile)
printf("%6d", tile);
else
printf(" ");
putchar(' ');
putchar('|');
}
putchar('\n');
Line();
}
printf("score %d max %d sum %d\n", GameScore(), MaxTile(), SumTile());
}
int CountEmptyTiles() const {
int num_empty_tiles = 0;
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x)
if (board[x][y] == 0) num_empty_tiles++;
return num_empty_tiles;
}
int CountLargeTiles(int large_rank) const {
int count = 0;
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x)
if (board[x][y] >= large_rank) ++count;
return count;
}
bool GenerateRandomTile() {
int num_empty_tiles = 0;
int empty_tiles[N * N];
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x)
if (board[x][y] == 0) empty_tiles[num_empty_tiles++] = x + y * N;
if (num_empty_tiles == 0) return false;
int r = rand() % num_empty_tiles;
rand_x = empty_tiles[r] % N;
rand_y = empty_tiles[r] / N;
board[rand_x][rand_y] = 1 + (rand() % 10 == 0);
if (board[rand_x][rand_y] == 2) ++num_4_tiles;
return true;
}
void AddTile(int x, int y, bool is_four) {
assert(board[x][y] == 0);
rand_x = x;
rand_y = y;
board[x][y] = is_four ? 2 : 1;
if (is_four) ++num_4_tiles;
}
int Row(int y) const {
int v = 0;
for (int x = 0; x < N; ++x) v = v * 32 + board[x][y];
return v;
}
int Col(int x) const {
int v = 0;
for (int y = 0; y < N; ++y) v = v * 32 + board[x][y];
return v;
}
int Evaluate() const {
if (options.interactive) {
int score_left = 0, score_right = 0;
for (int y = 0; y < N; ++y) {
score_left += score_map[Row(y)].descending;
score_right += score_map[Row(y)].ascending;
}
int score_up = 0, score_down = 0;
for (int x = 0; x < N; ++x) {
score_up += score_map[Col(x)].descending;
score_down += score_map[Col(x)].ascending;
}
return std::max(score_left, score_right) + std::max(score_up, score_down);
} else {
int score = 0;
for (int y = 0; y < N; ++y) score += score_map[Row(y)].descending;
for (int x = 0; x < N; ++x) score += score_map[Col(x)].descending;
return score;
}
}
int TryAllTiles(int depth, float prob) {
int score = Evaluate();
if (score < pass_score || depth < 0 || prob < options.min_prob)
return score;
unsigned long long compact_board = 0;
int empty_tiles = 0;
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x) {
compact_board = compact_board * 16 + board[x][y];
empty_tiles += board[x][y] == 0;
}
if (!skip_cache && cache.Lookup(compact_board, prob, depth, &score))
return score;
float tile2_prob = prob / empty_tiles * 0.9;
float tile4_prob = prob / empty_tiles * 0.1;
int total_score = 0;
for (int y = 0; y < N; ++y) {
for (int x = 0; x < N; ++x) {
if (board[x][y]) continue;
board[x][y] = 1;
total_score += 0.9 * TryAllMoves(depth, tile2_prob);
board[x][y] = 2;
total_score += 0.1 * TryAllMoves(depth, tile4_prob);
board[x][y] = 0;
}
}
cache.Update(compact_board, prob, depth, total_score / empty_tiles);
return total_score / empty_tiles;
}
int TryAllMoves(int depth, float prob, int* best_move = nullptr) {
int best_score = game_over_score;
if (best_move) *best_move = -1;
for (int i = 0; i < 4; i++) {
Node m = *this;
if (!(m.*moves[i])()) continue;
int score = m.TryAllTiles(depth - 1, prob);
if (best_score == game_over_score || best_score < score) {
best_score = score;
if (best_move) *best_move = i;
}
}
return best_score;
}
int Search(int depth, int* best_move) {
int max_tile_score = TileScore(MaxRank());
int score = Evaluate();
if (options.tuple_moves) {
#ifdef BIG_TUPLES
pass_score = score < max_tile_score * 2 ? -INT_MAX : max_tile_score * 2;
#else
pass_score = score < 0 ? -INT_MAX : max_tile_score * 2;
#endif
game_over_score = -std::max(1 << 17, max_tile_score * 2);
} else {
pass_score = -INT_MAX;
game_over_score = -1 << 22;
}
skip_cache = false;
auto h_score = TryAllMoves(depth, 1, best_move);
#ifdef BIG_TUPLES
if (h_score < 0) {
pass_score = -INT_MAX;
game_over_score = -1 << 22;
skip_cache = true;
h_score = TryAllMoves(depth, 1, best_move);
}
#endif
return h_score;
return TryAllMoves(depth, 1, best_move);
}
static void BuildScoreMap();
static Cache cache;
private:
struct Scores {
int ascending, descending;
};
static Scores score_map[1 << 20];
int num_4_tiles = 0;
int rand_x = -1;
int rand_y = -1;
int pass_score;
int game_over_score;
bool skip_cache;
};
// static
Node::Scores Node::score_map[1 << 20];
Cache Node::cache;
// static
void Node::BuildScoreMap() {
int line[N];
for (int i = 0; i < 1 << 20; ++i) {
for (int j = 0; j < N; ++j) line[j] = (i >> (j * 5)) & 0x1f;
int score = TileScore(line[0]);
for (int x = 0; x < N - 1; ++x) {
int a = TileScore(line[x]), b = TileScore(line[x + 1]);
score += a >= b ? a + b : (a - b) * 12;
score += a == b ? a : 0;
}
int k = 0;
for (int j = 0; j < N; ++j) k = k * 32 + line[j];
score_map[k].descending = score_map[i].ascending = score;
}
}
#endif