-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
521 lines (441 loc) · 17.6 KB
/
cpu.c
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include "cpu.h"
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashmap.h"
#include "mapio.h"
#include "mapmanager.h"
#include "tools.h"
#define CHILD_NODES_MAX 64
#define CACHE_HIT_BONUS 1000
typedef struct {
Board *board;
uint64_t put;
int move_ordering_value;
} ChildNode;
typedef int (*search_func_t)(Board *, TransposeTables **,
int16_t[YSIZE][UINT8_MAX], int, int, int, bool);
typedef void (*store_node_t)(Board *, ChildNode *, uint64_t, int);
// https://note.com/nyanyan_cubetech/n/n17c169271832?magazine_key=m54104c8d2f12
const int8_t SCORE_MATRIX[YSIZE][XSIZE] = {
{30, -12, 0, -1, -1, 0, -12, 30}, {-12, -15, -3, -3, -3, -3, -15, -12},
{0, -3, 0, -1, -1, 0, -3, 0}, {-1, -3, -1, -1, -1, -1, -3, -1},
{-1, -3, -1, -1, -1, -1, -3, -1}, {0, -3, 0, -1, -1, 0, -3, 0},
{-12, -15, -3, -3, -3, -3, -15, -12}, {30, -12, 0, -1, -1, 0, -12, 30}};
unsigned long long visited_nodes = 0;
unsigned long long cache_hitted = 0;
void precompute_score_matrix(int16_t dst[YSIZE][UINT8_MAX],
int8_t score_matrix[YSIZE][XSIZE]) {
// score_matrix の組み合わせを全て試す
for (size_t y = 0; y < YSIZE; y++) {
for (uint8_t bit = 0; bit < UINT8_MAX; bit++) {
int16_t score = 0;
for (size_t x = 0; x < XSIZE; x++) {
if (bit & (1 << x)) {
score += score_matrix[y][x];
}
}
dst[y][bit] = score;
}
}
}
static inline int evaluate(Board *board,
int16_t prepared_score_matrix[YSIZE][UINT8_MAX]) {
int score = 0;
uint8_t *current_player_row_value = (uint8_t *)&(CURRENT_MODE_BOARD(board));
uint8_t *opposite_player_row_value =
(uint8_t *)&(OPPOSITE_MODE_BOARD(board));
for (size_t y = 0; y < YSIZE; y++) {
score += prepared_score_matrix[y][current_player_row_value[y]] -
prepared_score_matrix[y][opposite_player_row_value[y]];
}
return score;
}
static inline int get_score_from_table(hashmap_t *table, Board *board) {
cache_hitted++;
int res = hashmap_get(table, board);
return (board->mode == BLACK) ? res : -res;
}
static inline void regist_score_to_table(hashmap_t *table, Board *board,
int score) {
(board->mode == BLACK) ? hashmap_set(table, board, score)
: hashmap_set(table, board, -score);
}
static inline int calulate_move_ordering_value(
TransposeTables **tables, Board *board,
int16_t prepared_score_matrix[YSIZE][UINT8_MAX]) {
int score = 0;
TransposeTables *before_tables = tables[1];
if (before_tables == NULL) {
// 前回の探索がない
return -evaluate(board, prepared_score_matrix);
}
if (hashmap_exist(before_tables->upper, board)) {
// 前回の探索で上限値が格納された
score =
CACHE_HIT_BONUS - get_score_from_table(before_tables->upper, board);
} else if (hashmap_exist(before_tables->lower, board)) {
// 前回の探索で下限値が格納された
score =
CACHE_HIT_BONUS - get_score_from_table(before_tables->lower, board);
} else {
// 枝刈りされた
score = -evaluate(board, prepared_score_matrix);
}
return score;
}
static inline int compare_child_node(const void *a, const void *b) {
// 降順にソート
ChildNode *node_a = (ChildNode *)a;
ChildNode *node_b = (ChildNode *)b;
if (node_a->move_ordering_value > node_b->move_ordering_value)
return -1;
else if (node_a->move_ordering_value < node_b->move_ordering_value)
return 1;
else
return 0;
}
static inline int search_function_process_if_passed(
Board *board, TransposeTables **tables,
int16_t precompute_score_matrix[YSIZE][UINT8_MAX], int depth, int alpha,
int beta, bool is_pass, search_func_t search_func) {
if (is_pass)
return evaluate(board, precompute_score_matrix);
else {
Board tmp_board = *board;
tmp_board.mode = -tmp_board.mode;
return -search_func(&tmp_board, tables, precompute_score_matrix, depth,
-beta, -alpha, true);
}
}
static inline size_t enumerate_nodes(
Board *board, Validcoords *validcoords, ChildNode *child_nodes,
TransposeTables **tables,
int16_t precomputed_score_matrix[YSIZE][UINT8_MAX],
store_node_t store_func) {
uint64_t coords = validcoords->coords;
size_t child_nodes_count = 0;
for (size_t y = 0; y < YSIZE; y++) {
for (size_t x = 0; x < XSIZE; x++, coords >>= 1) {
if (!(coords & 1)) continue;
if (child_nodes_count >= CHILD_NODES_MAX) {
fprintf(stderr, "child_nodes_count >= CHILD_NODES_MAX\n");
exit(EXIT_FAILURE);
}
uint64_t put = coord_to_bit(y, x);
Board *tmp_board = (Board *)malloc(sizeof(Board));
if (tmp_board == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
*tmp_board = *board;
reverse_stones(tmp_board, validcoords, put);
tmp_board->mode = -tmp_board->mode;
store_func(tmp_board, &(child_nodes[child_nodes_count]), put,
calulate_move_ordering_value(tables, tmp_board,
precomputed_score_matrix));
child_nodes_count++;
}
}
return child_nodes_count;
}
static inline void store_board_and_ordering_value(Board *board,
ChildNode *child_node,
uint64_t unused_put,
int move_ordering_value) {
child_node->board = board;
child_node->move_ordering_value = move_ordering_value;
UNUSED(unused_put);
}
static inline void store_board_and_put(Board *board, ChildNode *child_node,
uint64_t put,
int unused_move_ordering_value) {
child_node->board = board;
child_node->put = put;
UNUSED(unused_move_ordering_value);
}
// hashmap には黒目線の値を格納する
static inline void free_child_nodes(ChildNode child_nodes[], size_t length) {
for (size_t i = 0; i < length; i++) {
if (child_nodes[i].board != NULL) {
free(child_nodes[i].board);
child_nodes[i].board = NULL;
}
}
}
static int nega_alpha_transpose(
Board *board, TransposeTables **tables,
int16_t precomputed_score_matrix[YSIZE][UINT8_MAX], int depth, int alpha,
int beta, bool is_pass) {
Validcoords *validcoords;
uint64_t coords;
int max_score = -INT_MAX;
ChildNode child_nodes[CHILD_NODES_MAX];
size_t child_nodes_count = 0;
int score_upper = INT_MAX, score_lower = -INT_MAX;
TransposeTables *current_tables = tables[0];
visited_nodes++;
if (depth == 0) {
return evaluate(board, precomputed_score_matrix);
}
// 置換表に値がある場合はそれを用いる
if (hashmap_exist(current_tables->upper, board)) {
score_upper = get_score_from_table(current_tables->upper, board);
// fprintf(stderr, "nat(): upper cache hit value=%d\n", score_upper);
}
if (hashmap_exist(current_tables->lower, board)) {
score_lower = get_score_from_table(current_tables->lower, board);
// fprintf(stderr, "nat(): lower cache hit value=%d\n", score_lower);
}
if (score_upper == score_lower) {
// fprintf(stderr, "nat(): minimax value found, alpha=%d\n",
// score_upper);
return score_upper;
}
validcoords = get_validcoords(board);
coords = validcoords->coords;
// パスの場合
if (coords == 0) {
free(validcoords);
return search_function_process_if_passed(
board, tables, precomputed_score_matrix, depth, alpha, beta,
is_pass, nega_alpha_transpose);
}
// 置換表の値を用いてalphaとbetaの範囲を狭める
alpha = MAX(alpha, score_lower);
beta = MIN(beta, score_upper);
// 探索する
child_nodes_count = enumerate_nodes(board, validcoords, child_nodes, tables,
precomputed_score_matrix,
store_board_and_ordering_value);
free(validcoords);
qsort(child_nodes, child_nodes_count, sizeof(ChildNode),
compare_child_node);
for (size_t i = 0; i < child_nodes_count; i++) {
Board *tmp_board = child_nodes[i].board;
Validcoords *tmp_validcoords = get_validcoords(tmp_board);
int score;
score =
-nega_alpha_transpose(tmp_board, tables, precomputed_score_matrix,
depth - 1, -beta, -alpha, false);
free(tmp_validcoords);
if (score >= beta) {
// fprintf(stderr, "fall high\n");
if (score > score_lower)
regist_score_to_table(current_tables->lower, board, score);
free_child_nodes(child_nodes, child_nodes_count);
return score;
}
alpha = MAX(alpha, score);
max_score = MAX(max_score, score);
}
if (max_score < alpha) {
// fprintf(stderr, "nat(): fall low, alpha=%d\n", alpha);
regist_score_to_table(current_tables->upper, board, max_score);
} else {
// fprintf(stderr, "nat(): minimax value found, alpha=%d\n", alpha);
regist_score_to_table(current_tables->lower, board, max_score);
regist_score_to_table(current_tables->upper, board, max_score);
}
free_child_nodes(child_nodes, child_nodes_count);
return max_score;
}
static int nega_scout(Board *board, TransposeTables **tables,
int16_t precomputed_score_matrix[YSIZE][UINT8_MAX],
int depth, int alpha, int beta, bool is_pass) {
Validcoords *validcoords;
uint64_t coords;
int score;
int max_score = -INT_MAX;
ChildNode child_nodes[CHILD_NODES_MAX];
size_t child_nodes_count = 0;
TransposeTables *current_tables = tables[0];
int score_upper = INT_MAX, score_lower = -INT_MAX;
visited_nodes++;
if (depth == 0) {
return evaluate(board, precomputed_score_matrix);
}
// 置換表に値がある場合はそれを用いる
if (hashmap_exist(current_tables->upper, board)) {
score_upper = get_score_from_table(current_tables->upper, board);
// fprintf(stderr, "ns(): upper cache hit value=%d\n",
// score_upper);
}
if (hashmap_exist(current_tables->lower, board)) {
score_lower = get_score_from_table(current_tables->lower, board);
// fprintf(stderr, "ns(): lower cache hit value=%d\n", score_lower);
}
if (score_upper == score_lower) {
// fprintf(stderr, "ns(): minimax value found, alpha=%d\n",
// score_upper);
return score_upper;
}
validcoords = get_validcoords(board);
coords = validcoords->coords;
// パスの場合
if (coords == 0) {
free(validcoords);
return search_function_process_if_passed(
board, tables, precomputed_score_matrix, depth, alpha, beta,
is_pass, nega_scout);
}
// 置換表の値を用いてalphaとbetaの範囲を狭める
alpha = MAX(alpha, score_lower);
beta = MIN(beta, score_upper);
// 探索する
child_nodes_count = enumerate_nodes(board, validcoords, child_nodes, tables,
precomputed_score_matrix,
store_board_and_ordering_value);
free(validcoords);
qsort(child_nodes, child_nodes_count, sizeof(ChildNode),
compare_child_node);
// 最善手候補を通常窓で探索
score = -nega_scout(child_nodes[0].board, tables, precomputed_score_matrix,
depth - 1, -beta, -alpha, false);
if (score >= beta) {
// fprintf(stderr, "fall high\n");
if (score > score_lower)
regist_score_to_table(current_tables->lower, board, score);
free_child_nodes(child_nodes, child_nodes_count);
return score;
}
alpha = MAX(alpha, score);
max_score = MAX(max_score, score);
// 残りの候補を Null Window Search で探索
for (size_t i = 1; i < child_nodes_count; i++) {
score = -nega_alpha_transpose(child_nodes[i].board, tables,
precomputed_score_matrix, depth - 1,
-alpha - 1, -alpha, false);
if (score >= beta) {
if (score > score_lower)
regist_score_to_table(current_tables->lower, board, score);
free_child_nodes(child_nodes, child_nodes_count);
return score;
}
// child_nodes[0] よりも良い手が見つかったら再探索
if (score > alpha) {
alpha = score;
score = -nega_scout(child_nodes[i].board, tables,
precomputed_score_matrix, depth - 1, -beta,
-alpha, false);
if (score >= beta) {
if (score > score_lower)
regist_score_to_table(current_tables->lower, board, score);
free_child_nodes(child_nodes, child_nodes_count);
return score;
}
}
alpha = MAX(alpha, score);
max_score = MAX(max_score, score);
}
if (max_score < alpha) {
// fprintf(stderr, "ns(): fall low, alpha=%d\n", alpha);
regist_score_to_table(current_tables->lower, board, max_score);
} else {
// fprintf(stderr, "ns(): minimax value found, alpha=%d\n", alpha);
regist_score_to_table(current_tables->lower, board, max_score);
regist_score_to_table(current_tables->upper, board, max_score);
}
free_child_nodes(child_nodes, child_nodes_count);
return max_score;
}
static void clear_tables(TransposeTables *tables) {
if (tables == NULL) return;
hashmap_clear(tables->upper);
hashmap_clear(tables->lower);
}
static void free_tables(TransposeTables **tables) {
if (tables == NULL) return;
for (size_t i = 0; i < 2; i++) {
hashmap_destroy(tables[i]->upper);
hashmap_destroy(tables[i]->lower);
free(tables[i]);
}
free(tables);
}
static TransposeTables **create_new_tables(void) {
TransposeTables **tables;
tables = (TransposeTables **)malloc(sizeof(TransposeTables *) * 2);
if (tables == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
tables[0] = (TransposeTables *)malloc(sizeof(TransposeTables));
tables[1] = (TransposeTables *)malloc(sizeof(TransposeTables));
if ((tables[0] == NULL) || (tables[1] == NULL)) {
perror("malloc");
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < 2; i++) {
tables[i]->upper = hashmap_create();
tables[i]->lower = hashmap_create();
}
return tables;
}
static void swap_tables(TransposeTables **tables) {
TransposeTables *tmp = tables[0];
tables[0] = tables[1];
tables[1] = tmp;
}
uint64_t search(Board *board, Validcoords *validcoords,
int16_t precompute_score_matrix[YSIZE][UINT8_MAX],
size_t depth) {
visited_nodes = 0;
cache_hitted = 0;
TransposeTables **tables;
uint64_t max_coord = 0;
ChildNode child_nodes[CHILD_NODES_MAX];
size_t child_nodes_count = 0;
int alpha = -INT_MAX;
int beta = INT_MAX;
size_t start_depth = MAX(1, depth - 3);
tables = create_new_tables();
child_nodes_count =
enumerate_nodes(board, validcoords, child_nodes, tables,
precompute_score_matrix, store_board_and_put);
// 1手ずつ探索
for (size_t search_depth = start_depth; search_depth <= depth;
++search_depth) {
int score;
alpha = -INT_MAX;
beta = INT_MAX;
for (size_t i = 0; i < child_nodes_count; ++i) {
child_nodes[i].move_ordering_value = calulate_move_ordering_value(
tables, child_nodes[i].board, precompute_score_matrix);
}
qsort(child_nodes, child_nodes_count, sizeof(ChildNode),
compare_child_node);
// 最善手候補を通常窓で探索
score =
-nega_scout(child_nodes[0].board, tables, precompute_score_matrix,
search_depth - 1, -beta, -alpha, false);
alpha = score;
max_coord = child_nodes[0].put;
// 残りの候補を Null Window Search で探索
for (size_t i = 1; i < child_nodes_count; i++) {
score = -nega_alpha_transpose(
child_nodes[i].board, tables, precompute_score_matrix,
search_depth - 1, -alpha - 1, -alpha, false);
// child_nodes[0] よりも良い手が見つかったら再探索
if (score > alpha) {
alpha = score;
score = -nega_scout(child_nodes[i].board, tables,
precompute_score_matrix, search_depth - 1,
-beta, -alpha, false);
max_coord = child_nodes[i].put;
}
alpha = MAX(alpha, score);
}
fprintf(stderr, "depth: %zu, nodes: %llu\n", search_depth,
visited_nodes);
fprintf(stderr, "cache hit: %llu\n", cache_hitted);
clear_tables(tables[1]);
swap_tables(tables);
}
free_tables(tables);
free_child_nodes(child_nodes, child_nodes_count);
return max_coord;
}