-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cc
More file actions
653 lines (536 loc) · 18.1 KB
/
search.cc
File metadata and controls
653 lines (536 loc) · 18.1 KB
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Copyright (C) 2016 Tetsui Ohkubo.
#include "./search.h"
#include <gflags/gflags.h>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include "./timer.h"
#include "./trax.h"
Move RandomSearcher::SearchBestMove(const Position& position, Timer* timer) {
std::vector<Move> legal_moves;
for (Move move : position.GenerateMoves()) {
Position next_position;
if (position.DoMove(move, &next_position)) {
// The move is proved to be legal.
legal_moves.push_back(move);
}
}
assert(legal_moves.size() > 0);
return legal_moves[Random() % legal_moves.size()];
}
// Return the best move from the perspective of position.red_to_move().
template<typename Evaluator>
Move SimpleSearcher<Evaluator>::SearchBestMove(const Position& position,
Timer *timer) {
assert(!position.finished());
int best_score = -kInf;
std::vector<ScoredMove> moves;
for (Move move : position.GenerateMoves()) {
Position next_position;
if (!position.DoMove(move, &next_position)) {
// This is illegal move.
continue;
}
// next_position.red_to_move() == !position.red_to_move() holds.
// Evaluate() evaluates from the perspective of next_position.
// Therefore, position that is good for next_position.red_to_move() is
// bad for position.red_to_move().
const int score = -Evaluator::Evaluate(next_position);
#if 0
std::cerr << score << " " << move.notation() << std::endl;
#endif
best_score = std::max(best_score, score);
moves.emplace_back(score, move);
}
std::vector<Move> best_moves;
for (ScoredMove& move : moves) {
if (move.score == best_score) {
best_moves.push_back(move);
}
}
assert(best_moves.size() > 0);
return best_moves[Random() % best_moves.size()];
}
// Return the best move from the perspective of position.red_to_move().
template<typename Evaluator>
Move NegaMaxSearcher<Evaluator>::SearchBestMove(const Position& position,
Timer* timer) {
assert(!position.finished());
Move book_move;
if (book_.Select(position, &book_move)) {
return book_move;
}
transposition_table_.NewSearch();
if (iterative_) {
std::vector<Move> possible_moves = position.GenerateMoves();
Move best_move;
for (int current_depth = 0; current_depth <= max_depth_; ++current_depth) {
int best_score = -kInf;
std::vector<ScoredMove> moves;
bool aborted = false;
for (Move move : possible_moves) {
Position next_position;
if (!position.DoMove(move, &next_position)) {
// This is illegal move.
continue;
}
// next_position.red_to_move() == !position.red_to_move() holds.
// NegaMax() evaluates from the perspective of next_position.
// Therefore, position that is good for next_position.red_to_move() is
// bad for position.red_to_move().
const int score = -NegaMax(next_position, timer, current_depth);
best_score = std::max(best_score, score);
moves.emplace_back(score, move);
if (current_depth > 0 && timer->CheckTimeout()) {
aborted = true;
break;
}
}
if (aborted) {
// Drop the result of that iteration.
break;
}
std::vector<Move> best_moves;
for (ScoredMove& move : moves) {
if (move.score == best_score) {
best_moves.push_back(move);
}
}
assert(best_moves.size() > 0);
best_move = best_moves[Random() % best_moves.size()];
timer->set_completed_depth(current_depth);
}
return best_move;
} else {
int best_score = -kInf;
std::vector<ScoredMove> moves;
for (Move move : position.GenerateMoves()) {
Position next_position;
if (!position.DoMove(move, &next_position)) {
// This is illegal move.
continue;
}
// next_position.red_to_move() == !position.red_to_move() holds.
// NegaMax() evaluates from the perspective of next_position.
// Therefore, position that is good for next_position.red_to_move() is
// bad for position.red_to_move().
const int score = -NegaMax(next_position, timer, max_depth_);
best_score = std::max(best_score, score);
moves.emplace_back(score, move);
}
std::vector<Move> best_moves;
for (ScoredMove& move : moves) {
if (move.score == best_score) {
best_moves.push_back(move);
}
}
timer->set_completed_depth(max_depth_);
assert(best_moves.size() > 0);
return best_moves[Random() % best_moves.size()];
}
}
// Decrement the value in a way its absolute value will desrease.
int AbsoluteDecrement(int x) {
if (x > 0) {
return x - 1;
} else if (x < 0) {
return x + 1;
} else {
return 0;
}
}
// Score the move from the perspective of position.red_to_move().
// Larger is better.
template<typename Evaluator>
int NegaMaxSearcher<Evaluator>::NegaMax(
const Position& position, Timer* timer, int depth, int alpha, int beta) {
const int original_alpha = alpha;
TranspositionTable::Entry entry;
// At that point of time, we don't care about conflicts.
const PositionHash key = position.Hash();
const bool found = transposition_table_.Probe(key, &entry);
if (found && entry.depth >= depth) {
if (entry.bound == BOUND_EXACT) {
return entry.score;
} else if (entry.bound == BOUND_LOWER) {
alpha = std::max(alpha, entry.score);
} else if (entry.bound == BOUND_UPPER) {
beta = std::min(beta, entry.score);
}
if (alpha >= beta) {
return entry.score;
}
}
entry.score = -kInf;
entry.best_move = Move();
if (position.finished() || depth <= 0) {
// Evaluate the position, from the perspective of position.red_to_move(),
// and this is same as NegaMax().
// Thus, there is no need for sign flip.
entry.score = Evaluator::Evaluate(position);
timer->IncrementNodeCounter();
} else {
for (Move move : position.GenerateMoves()) {
Position next_position;
if (!position.DoMove(move, &next_position)) {
// This is illegal move.
continue;
}
// next_position.red_to_move() == !position.red_to_move() holds.
// NegaMax() evaluates from the perspective of next_position.
// Therefore, position that is good for next_position.red_to_move() is
// bad for position.red_to_move().
const int score = AbsoluteDecrement(
-NegaMax(next_position, timer, depth - 1, -beta, -alpha));
// The reason why we used AbsoluteDecrement here is to finish the game
// as early as possible.
// This not only reduces unexpected behavior to human players, but also
// works as very good pruning.
if (entry.score < score) {
entry.score = score;
entry.best_move = move;
}
alpha = std::max(alpha, score);
if (alpha >= beta) {
break;
}
if (timer->CheckTimeout()) {
return 0;
}
}
}
entry.depth = depth;
if (entry.score <= original_alpha) {
entry.bound = BOUND_UPPER;
} else if (entry.score >= beta) {
entry.bound = BOUND_LOWER;
} else {
entry.bound = BOUND_EXACT;
}
transposition_table_.Store(
key, entry.best_move, entry.score, entry.depth, entry.bound);
return entry.score;
}
template<typename Evaluator>
Move ThreadedIterativeSearcher<Evaluator>::SearchBestMove(
const Position& position, Timer* timer) {
assert(!position.finished());
Move book_move;
if (book_.Select(position, &book_move)) {
return book_move;
}
transposition_table_.NewSearch();
return ThreadedSearcher::SearchBestMove(position, timer);
}
static const std::vector<int> kDepthDensityMatrix[] = {
{1},
{0, 1},
{1, 0},
{0, 0, 1, 1},
{0, 1, 1, 0},
{1, 1, 0, 0},
{1, 0, 0, 1},
{0, 0, 0, 1, 1, 1},
{0, 0, 1, 1, 1, 0},
{0, 1, 1, 1, 0, 0},
{1, 1, 1, 0, 0, 0},
{1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 1}
};
template<typename Evaluator>
void ThreadedIterativeSearcher<Evaluator>::DoSearchBestMove(
const Position& position, int thread_index, int num_threads,
Timer* timer, Move* best_move, int* best_score, int* completed_depth) {
std::vector<Move> possible_moves = position.GenerateMoves();
for (int current_depth = 0; ; ++current_depth) {
// Skip different depths for each thread using density matrix.
// auto& row = kDepthDensityMatrix[thread_index];
// if (!row[current_depth % row.size()]) {
// continue;
// }
*best_score = -kInf;
std::vector<ScoredMove> moves;
bool aborted = false;
for (Move move : possible_moves) {
Position next_position;
if (!position.DoMove(move, &next_position)) {
// This is illegal move.
continue;
}
// next_position.red_to_move() == !position.red_to_move() holds.
// NegaMax() evaluates from the perspective of next_position.
// Therefore, position that is good for next_position.red_to_move() is
// bad for position.red_to_move().
const int score = -NegaMax(next_position, timer, current_depth);
*best_score = std::max(*best_score, score);
moves.emplace_back(score, move);
if (current_depth > 0 && timer->CheckTimeout()) {
aborted = true;
break;
}
}
if (aborted) {
// Drop the result of that iteration.
break;
}
std::vector<Move> best_moves;
for (ScoredMove& move : moves) {
if (move.score == *best_score) {
best_moves.push_back(move);
}
}
assert(best_moves.size() > 0);
*best_move = best_moves[Random() % best_moves.size()];
timer->set_completed_depth(current_depth);
*completed_depth = current_depth;
}
}
// Score the move from the perspective of position.red_to_move().
// Larger is better.
template<typename Evaluator>
int ThreadedIterativeSearcher<Evaluator>::NegaMax(
const Position& position, Timer* timer, int depth, int alpha, int beta) {
const int original_alpha = alpha;
TranspositionTable::Entry entry;
// At that point of time, we don't care about conflicts.
const PositionHash key = position.Hash();
const bool found = transposition_table_.Probe(key, &entry);
if (found && entry.depth >= depth) {
if (entry.bound == BOUND_EXACT) {
return entry.score;
} else if (entry.bound == BOUND_LOWER) {
alpha = std::max(alpha, entry.score);
} else if (entry.bound == BOUND_UPPER) {
beta = std::min(beta, entry.score);
}
if (alpha >= beta) {
return entry.score;
}
}
entry.score = -kInf;
entry.best_move = Move();
if (position.finished() || depth <= 0) {
// Evaluate the position, from the perspective of position.red_to_move(),
// and this is same as NegaMax().
// Thus, there is no need for sign flip.
entry.score = Evaluator::Evaluate(position);
timer->IncrementNodeCounter();
} else {
for (Move move : position.GenerateMoves()) {
Position next_position;
if (!position.DoMove(move, &next_position)) {
// This is illegal move.
continue;
}
// next_position.red_to_move() == !position.red_to_move() holds.
// NegaMax() evaluates from the perspective of next_position.
// Therefore, position that is good for next_position.red_to_move() is
// bad for position.red_to_move().
const int score = AbsoluteDecrement(
-NegaMax(next_position, timer, depth - 1, -beta, -alpha));
// The reason why we used AbsoluteDecrement here is to finish the game
// as early as possible.
// This not only reduces unexpected behavior to human players, but also
// works as very good pruning.
if (entry.score < score) {
entry.score = score;
entry.best_move = move;
}
alpha = std::max(alpha, score);
if (alpha >= beta) {
break;
}
if (timer->CheckTimeout(/* allow_false_negative = */true)) {
return 0;
}
}
}
entry.depth = depth;
if (entry.score <= original_alpha) {
entry.bound = BOUND_UPPER;
} else if (entry.score >= beta) {
entry.bound = BOUND_LOWER;
} else {
entry.bound = BOUND_EXACT;
}
transposition_table_.Store(
key, entry.best_move, entry.score, entry.depth, entry.bound);
return entry.score;
}
// Instantiation.
#define INSTANTIATE_TEMPLATES_FOR(CLASS) \
template Move SimpleSearcher<CLASS>::SearchBestMove( \
const Position& position, Timer* timer); \
template Move NegaMaxSearcher<CLASS>::SearchBestMove( \
const Position& position, Timer* timer); \
template int NegaMaxSearcher<CLASS>::NegaMax( \
const Position& position, Timer* timer, \
int depth, int alpha, int beta); \
template Move ThreadedIterativeSearcher<CLASS>::SearchBestMove( \
const Position& position, Timer* timer); \
template void ThreadedIterativeSearcher<CLASS>::DoSearchBestMove( \
const Position& position, int thread_index, int num_threads, \
Timer* timer, Move* best_move, int* best_score, int* completed_depth); \
template int ThreadedIterativeSearcher<CLASS>::NegaMax( \
const Position& position, Timer* timer, int depth, int alpha, int beta)
INSTANTIATE_TEMPLATES_FOR(LeafAverageEvaluator);
INSTANTIATE_TEMPLATES_FOR(MonteCarloEvaluator);
INSTANTIATE_TEMPLATES_FOR(FactorEvaluator);
INSTANTIATE_TEMPLATES_FOR(NoneEvaluator);
INSTANTIATE_TEMPLATES_FOR(AdvancedFactorEvaluator);
INSTANTIATE_TEMPLATES_FOR(LoopFactorEvaluator);
namespace {
int CountEdgeColors(const Position& position) {
int red = 0;
int white = 0;
for (int i_x = 0; i_x < position.max_x(); ++i_x) {
for (int j_y = 0; j_y < position.max_y(); ++j_y) {
if (position.at(i_x, j_y) == PIECE_EMPTY) {
continue;
}
for (int k = 0; k < 4; ++k) {
const int nx = i_x + kDx[k];
const int ny = j_y + kDy[k];
if (position.at(nx, ny) != PIECE_EMPTY) {
continue;
}
if (kPieceColors[position.at(i_x, j_y)][k] == 'R') {
++red;
} else {
++white;
}
}
}
}
return red - white;
}
double AverageColor(const std::vector<double>& v) {
double red_numerator = 0.0;
double white_numerator = 0.0;
int red_denominator = 0;
int white_denominator = 0;
for (double x : v) {
if (x > 0) {
red_numerator += x;
++red_denominator;
} else {
white_numerator += x;
++white_denominator;
}
}
if (red_denominator > 0) {
red_numerator /= red_denominator;
}
if (white_denominator > 0) {
white_numerator /= white_denominator;
}
return red_numerator + white_numerator;
}
} // namespace
void GenerateFactors(const Position& position,
std::vector<std::pair<std::string, double>> *factors) {
double leaf_average = LeafAverageEvaluator::Evaluate(position);
double factor_evaluator = FactorEvaluator::Evaluate(position);
if (!position.red_to_move()) {
leaf_average *= -1.0;
factor_evaluator *= -1.0;
}
// double longest_line = position.red_longest() - position.white_longest();
double edge_color = CountEdgeColors(position);
std::vector<Line> lines;
position.EnumerateLines(&lines);
if (lines.size() == 0) {
std::cerr << "gah!";
exit(EXIT_FAILURE);
}
std::vector<double> endpoints;
std::vector<double> sum_edges;
std::vector<double> max_edges;
double total_count = 0.0;
double inner_count = 0.0;
for (Line& line : lines) {
if (line.is_inner) {
if (line.is_red) {
inner_count += 1.0;
} else {
inner_count -= 1.0;
}
}
if (line.is_red) {
total_count += 1.0;
} else {
total_count -= 1.0;
}
double endpoint = 1.0 / (1.0 + line.endpoint_distance);
double sum_edge = (1.0 / (1.0 + line.edge_distances[0]) +
1.0 / (1.0 + line.edge_distances[1]));
double max_edge = std::max(
1.0 / (1.0 + line.edge_distances[0]),
1.0 / (1.0 + line.edge_distances[1]));
if (!line.is_red) {
endpoint *= -1.0;
sum_edge *= -1.0;
max_edge *= -1.0;
}
endpoints.push_back(endpoint);
sum_edges.push_back(sum_edge);
max_edges.push_back(max_edge);
}
factors->emplace_back("leaf_average", leaf_average);
factors->emplace_back("factor_evaluator", factor_evaluator);
factors->emplace_back("inner_count", inner_count);
factors->emplace_back("total_count", total_count);
// factors->emplace_back("longest_line", longest_line);
factors->emplace_back("edge_color", edge_color);
factors->emplace_back("endpoint_factor",
std::accumulate(endpoints.begin(), endpoints.end(), 0.0));
factors->emplace_back("sum_edge_factor",
std::accumulate(sum_edges.begin(), sum_edges.end(), 0.0));
factors->emplace_back("max_edge_factor",
std::accumulate(max_edges.begin(), max_edges.end(), 0.0));
factors->emplace_back("endpoint_factor_max_min",
*std::max_element(endpoints.begin(), endpoints.end()) +
*std::min_element(endpoints.begin(), endpoints.end()));
factors->emplace_back("sum_edge_factor_max_min",
*std::max_element(sum_edges.begin(), sum_edges.end()) +
*std::min_element(sum_edges.begin(), sum_edges.end()));
factors->emplace_back("max_edge_factor_max_min",
*std::max_element(max_edges.begin(), max_edges.end()) +
*std::min_element(max_edges.begin(), max_edges.end()));
factors->emplace_back("endpoint_factor_average", AverageColor(endpoints));
factors->emplace_back("sum_edge_factor_average", AverageColor(sum_edges));
factors->emplace_back("max_edge_factor_average", AverageColor(max_edges));
double shortcut = 0.0;
int red_mates = 0;
int white_mates = 0;
for (Line& line : lines) {
if (line.is_mate()) {
if (line.is_red) {
++red_mates;
} else {
++white_mates;
}
}
}
if (position.red_to_move()) {
if (red_mates > 0) {
shortcut = 1.0;
}
if (white_mates >= 2) {
shortcut = -1.0;
}
} else {
if (white_mates > 0) {
shortcut = -1.0;
}
if (red_mates >= 2) {
shortcut = 1.0;
}
}
factors->emplace_back("shortcut", shortcut);
factors->emplace_back("min_edge_size",
std::min<double>(position.max_x(), position.max_y()));
factors->emplace_back("max_edge_size",
std::max<double>(position.max_x(), position.max_y()));
}