-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathExpectiMax.cpp
360 lines (316 loc) · 11.1 KB
/
ExpectiMax.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
/*
Copyright (c) 2014 Maarten Baert <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
*/
#include "ExpectiMax.h"
#include "RandomSeed.h"
#include <iostream>
#include <random>
#define SEARCH_DEPTH 4
#define SCORE_MULTIPLIER 4
#define TRANSPOSITION_TABLE_SIZE 0x1000
#define COLLECT_STATS 0
#if COLLECT_STATS
#include <atomic>
#include <chrono>
#include <iomanip>
#endif
const unsigned int PARAMETERS_MIN[PARAM_COUNT] = {0};
const unsigned int PARAMETERS_MAX[PARAM_COUNT] = {
1000000,
1000000,
10000,
10000,
10000,
1000,
};
const unsigned int PARAMETERS_STEP[PARAM_COUNT] = {
816,
11,
3,
31,
35,
13,
};
void GetDefaultHeuristicParameters(HeuristicParameters* parameters) {
// normal tuning
/*parameters->m_values[PARAM_STILLALIVE] = 19354;
parameters->m_values[PARAM_FREECELL] = 94;
parameters->m_values[PARAM_CENTEROFMASS1] = 41;
parameters->m_values[PARAM_CENTEROFMASS2] = 309;
parameters->m_values[PARAM_CENTEROFMASS3] = 383;
parameters->m_values[PARAM_CENTEROFMASS4] = 143;*/
// rollback tuning
parameters->m_values[PARAM_STILLALIVE] = 7260;
parameters->m_values[PARAM_FREECELL] = 125;
parameters->m_values[PARAM_CENTEROFMASS1] = 41;
parameters->m_values[PARAM_CENTEROFMASS2] = 351;
parameters->m_values[PARAM_CENTEROFMASS3] = 328;
parameters->m_values[PARAM_CENTEROFMASS4] = 151;
}
#if COLLECT_STATS
std::atomic<uint64_t> g_tt_hit[SEARCH_DEPTH], g_tt_miss[SEARCH_DEPTH], g_tt_collision[SEARCH_DEPTH], g_tt_time[SEARCH_DEPTH];
struct AtomicInit {
inline AtomicInit() {
for(unsigned int i = 0; i < SEARCH_DEPTH; ++i) {
g_tt_hit[i] = 0;
g_tt_miss[i] = 0;
g_tt_collision[i] = 0;
g_tt_time[i] = 0;
}
}
} g_atomic_init;
#endif
void PrintExpectiMaxStats() {
#if COLLECT_STATS
std::cout << "**** ExpectiMax Stats ****" << std::endl;
std::cout << std::setw(15) << "MovesLeft";
std::cout << std::setw(15) << "Hit";
std::cout << std::setw(15) << "Miss";
std::cout << std::setw(15) << "Hit/Miss%";
std::cout << std::setw(15) << "Collisions";
std::cout << std::setw(15) << "Coll/Miss%";
std::cout << std::setw(15) << "Time(ms)";
std::cout << std::setw(15) << "Time(ns)/Miss";
std::cout << std::endl;
for(unsigned int i = 0; i < SEARCH_DEPTH; ++i) {
uint64_t hit = g_tt_hit[i], miss = g_tt_miss[i], collision = g_tt_collision[i];
uint64_t total_time = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::duration(g_tt_time[i])).count();
std::cout << std::setw(15) << i;
std::cout << std::setw(15) << hit;
std::cout << std::setw(15) << miss;
std::cout << std::setw(15) << ((miss == 0)? 0 : 100 * hit / miss);
std::cout << std::setw(15) << collision;
std::cout << std::setw(15) << ((miss == 0)? 0 : 100 * collision / miss);
std::cout << std::setw(15) << total_time / 1000000;
std::cout << std::setw(15) << ((miss == 0)? 0 : total_time / miss);
std::cout << std::endl;
}
std::cout << "----------------------------------------" << std::endl;
#endif
}
struct Transposition {
Board m_board;
unsigned int m_score;
Transposition() : m_board{0}, m_score{0} {}
};
struct ExpectiMaxContext {
HeuristicParameters m_parameters;
std::minstd_rand m_rng;
unsigned int m_weight_table[16];
Transposition m_transposition_tables[SEARCH_DEPTH][TRANSPOSITION_TABLE_SIZE];
#if COLLECT_STATS
uint64_t m_tt_hit[SEARCH_DEPTH], m_tt_miss[SEARCH_DEPTH], m_tt_collision[SEARCH_DEPTH], m_tt_time[SEARCH_DEPTH];
#endif
};
template<unsigned int moves_left>
unsigned int ExpectiMaxPlayer(Board board, ExpectiMaxContext* context);
template<unsigned int moves_left>
inline __attribute__((always_inline))
void TurnComputer(Board board, ExpectiMaxContext* context, unsigned int& score_sum, unsigned int& score_div, unsigned int location, unsigned int value) {
assert(GetCell(board, location) == 0);
Board newboard = SetCell(board, location, value);
score_sum += ExpectiMaxPlayer<moves_left>(newboard, context);
++score_div;
}
template<unsigned int moves_left>
unsigned int ExpectiMaxComputer(Board board, ExpectiMaxContext* context) {
// normalize the board
board = Normalize(board);
// check the transposition table
unsigned int bucket = Hash1(board) & (TRANSPOSITION_TABLE_SIZE - 1);
Transposition &tt = context->m_transposition_tables[moves_left][bucket];
if(tt.m_board.m_data == board.m_data) {
#if COLLECT_STATS
++context->m_tt_hit[moves_left];
#endif
return tt.m_score;
}
#if COLLECT_STATS
++context->m_tt_miss[moves_left];
if(tt.m_board.m_data != 0) {
++context->m_tt_collision[moves_left];
}
auto t1 = std::chrono::high_resolution_clock::now();
#endif
// get all free locations
unsigned int locations[16], location_count = 0;
for(unsigned int location = 0; location < 16; ++location) {
if(GetCell(board, location) == 0) {
locations[location_count] = location;
++location_count;
}
}
assert(location_count > 0);
// should we do an exhaustive search?
unsigned int score;
if(location_count < 4) {
// check 2-tiles and 4-tiles
unsigned int score1_sum = 0, score1_div = 0;
unsigned int score2_sum = 0, score2_div = 0;
for(unsigned int k = 0; k < location_count; ++k) {
unsigned int location = locations[k];
TurnComputer<moves_left>(board, context, score1_sum, score1_div, location, 1);
TurnComputer<moves_left>(board, context, score2_sum, score2_div, location, 2);
}
unsigned int score_sum = score1_sum * 9 + score2_sum, score_div = score1_div * 9 + score2_div;
assert(score_div != 0);
score = (score_sum + score_div / 2) / score_div;
} else {
// check only 2-tiles, ignore 4-tiles
unsigned int score_sum = 0, score_div = 0;
for(unsigned int k = 0; k < location_count; ++k) {
unsigned int location = locations[k];
TurnComputer<moves_left>(board, context, score_sum, score_div, location, 1); // (context->m_rng() % 10 == 0)? 2 : 1
}
assert(score_div != 0);
score = (score_sum + score_div / 2) / score_div;
}
tt.m_board = board;
tt.m_score = score;
#if COLLECT_STATS
auto t2 = std::chrono::high_resolution_clock::now();
context->m_tt_time[moves_left] += (t2 - t1).count();
#endif
return score;
}
template<>
unsigned int ExpectiMaxComputer<0>(Board board, ExpectiMaxContext* context) {
// check the transposition table
unsigned int bucket = Hash1(board) & (TRANSPOSITION_TABLE_SIZE - 1);
Transposition &tt = context->m_transposition_tables[0][bucket];
if(tt.m_board.m_data == board.m_data) {
#if COLLECT_STATS
++context->m_tt_hit[moves_left];
#endif
return tt.m_score;
}
#if COLLECT_STATS
++context->m_tt_miss[moves_left];
if(tt.m_board.m_data != 0) {
++context->m_tt_collision[moves_left];
}
auto t1 = std::chrono::high_resolution_clock::now();
#endif
unsigned int score = context->m_parameters.m_values[PARAM_STILLALIVE];
unsigned int freecell = context->m_parameters.m_values[PARAM_FREECELL];
int wx = 0, wy = 0;
for(unsigned int j = 0; j < 4; ++j) {
for(unsigned int i = 0; i < 4; ++i) {
unsigned int value = GetCell(board, i, j);
if(value == 0) {
score += freecell;
freecell >>= 1;
} else {
unsigned int weight = context->m_weight_table[value];
wx += ((int) i * 2 - 3) * (int) weight;
wy += ((int) j * 2 - 3) * (int) weight;
}
}
}
score += (abs(wx) + abs(wy)) >> 10;
tt.m_board = board;
tt.m_score = score;
#if COLLECT_STATS
auto t2 = std::chrono::high_resolution_clock::now();
context->m_tt_time[moves_left] += (t2 - t1).count();
#endif
return score;
}
template<unsigned int moves_left>
unsigned int ExpectiMaxPlayer(Board board, ExpectiMaxContext* context) {
unsigned int best_score = 0;
/*for(unsigned int direction = 0; direction < 4; ++direction) {
BoardScore result = Collapse(board, (enum_direction) direction);
if(result.m_board.m_data != board.m_data) {
unsigned int score = result.m_score * SCORE_MULTIPLIER + ExpectiMaxComputer<moves_left - 1>(result.m_board, context);
if(score > best_score) {
best_score = score;
}
}
}*/
{
BoardScore result = CollapseLeft(board);
if(result.m_board.m_data != board.m_data) {
unsigned int score = result.m_score * SCORE_MULTIPLIER + ExpectiMaxComputer<moves_left - 1>(result.m_board, context);
if(score > best_score)
best_score = score;
}
}
{
BoardScore result = CollapseRight(board);
if(result.m_board.m_data != board.m_data) {
unsigned int score = result.m_score * SCORE_MULTIPLIER + ExpectiMaxComputer<moves_left - 1>(result.m_board, context);
if(score > best_score)
best_score = score;
}
}
board = Transpose(board);
{
BoardScore result = CollapseLeft(board);
if(result.m_board.m_data != board.m_data) {
unsigned int score = result.m_score * SCORE_MULTIPLIER + ExpectiMaxComputer<moves_left - 1>(result.m_board, context);
if(score > best_score)
best_score = score;
}
}
{
BoardScore result = CollapseRight(board);
if(result.m_board.m_data != board.m_data) {
unsigned int score = result.m_score * SCORE_MULTIPLIER + ExpectiMaxComputer<moves_left - 1>(result.m_board, context);
if(score > best_score)
best_score = score;
}
}
return best_score;
}
std::pair<enum_direction, unsigned int> FindBestMove(Board board, const HeuristicParameters& parameters) {
ExpectiMaxContext context;
context.m_parameters = parameters;
context.m_rng.seed(RandomSeed());
for(unsigned int i = 0; i < 16; ++i) {
unsigned int weight3 = i * parameters.m_values[PARAM_CENTEROFMASS4];
unsigned int weight2 = i * (parameters.m_values[PARAM_CENTEROFMASS3] + weight3);
unsigned int weight1 = i * (parameters.m_values[PARAM_CENTEROFMASS2] + weight2);
unsigned int weight0 = i * (parameters.m_values[PARAM_CENTEROFMASS1] + weight1);
context.m_weight_table[i] = weight0;
}
#if COLLECT_STATS
for(unsigned int i = 0; i < SEARCH_DEPTH; ++i) {
context.m_tt_hit[i] = 0;
context.m_tt_miss[i] = 0;
context.m_tt_collision[i] = 0;
context.m_tt_time[i] = 0;
}
#endif
unsigned int best_score = 0;
enum_direction best_move = DIRECTION_NONE;
for(unsigned int direction = 0; direction < 4; ++direction) {
BoardScore result = Collapse(board, (enum_direction) direction);
if(result.m_board.m_data != board.m_data) {
unsigned int score = result.m_score * SCORE_MULTIPLIER + ExpectiMaxComputer<SEARCH_DEPTH - 1>(result.m_board, &context);
if(score > best_score || best_move == DIRECTION_NONE) {
best_score = score;
best_move = (enum_direction) direction;
}
}
}
#if COLLECT_STATS
for(unsigned int i = 0; i < SEARCH_DEPTH; ++i) {
g_tt_hit[i] += context.m_tt_hit[i];
g_tt_miss[i] += context.m_tt_miss[i];
g_tt_collision[i] += context.m_tt_collision[i];
g_tt_time[i] += context.m_tt_time[i];
}
#endif
return std::make_pair(best_move, best_score);
}