-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUCTNode.cpp
707 lines (599 loc) · 19.1 KB
/
UCTNode.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
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#include "config.h"
#include <cassert>
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <cmath>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <vector>
#include "UCTNode.h"
#include "FastBoard.h"
#include "FastState.h"
#include "GTP.h"
#include "GameState.h"
#include "Network.h"
#include "distance.h"
#include "Utils.h"
#include <iostream>
#define forEach4Nbr(v_origin,v_nbr,block) \
int v_nbr; \
v_nbr = v_origin + 1; block; \
v_nbr = v_origin - 1; block; \
v_nbr = v_origin + EBSIZE; block; \
v_nbr = v_origin - EBSIZE; block;
bool japanese_rule = false;
using namespace Utils;
UCTNode::UCTNode(int vertex, float score) : m_move(vertex), m_score(score) {
}
bool UCTNode::first_visit() const {
return m_visits == 0;
}
SMP::Mutex& UCTNode::get_mutex() {
return m_nodemutex;
}
bool UCTNode::create_children(std::atomic<int>& nodecount,
GameState& state,
float& eval,
float min_psa_ratio) {
// check whether somebody beat us to it (atomic)
if (!expandable(min_psa_ratio)) {
return false;
}
// acquire the lock
LOCK(get_mutex(), lock);
// no successors in final state
if (state.get_passes() >= 2) {
return false;
}
// check whether somebody beat us to it (after taking the lock)
if (!expandable(min_psa_ratio)) {
return false;
}
// Someone else is running the expansion
if (m_is_expanding) {
return false;
}
// We'll be the one queueing this node for expansion, stop others
m_is_expanding = true;
lock.unlock();
const auto raw_netlist = Network::get_scored_moves(
&state, Network::Ensemble::RANDOM_SYMMETRY);
// DCNN returns winrate as side to move
m_net_eval = raw_netlist.winrate;
const auto to_move = state.board.get_to_move();
// our search functions evaluate from black's point of view
if (state.board.white_to_move()) {
m_net_eval = 1.0f - m_net_eval;
}
eval = m_net_eval;
std::vector<Network::ScoreVertexPair> nodelist;
auto legal_sum = 0.0f;
for (auto i = 0; i < BOARD_SQUARES; i++) {
const auto x = i % BOARD_SIZE;
const auto y = i / BOARD_SIZE;
const auto vertex = state.board.get_vertex(x, y);
if (state.is_move_legal(to_move, vertex)) {
nodelist.emplace_back(raw_netlist.policy[i], vertex);
legal_sum += raw_netlist.policy[i];
}
}
nodelist.emplace_back(raw_netlist.policy_pass, FastBoard::PASS);
legal_sum += raw_netlist.policy_pass;
if (legal_sum > std::numeric_limits<float>::min()) {
// re-normalize after removing illegal moves.
for (auto& node : nodelist) {
node.first /= legal_sum;
}
} else {
// This can happen with new randomized nets.
auto uniform_prob = 1.0f / nodelist.size();
for (auto& node : nodelist) {
node.first = uniform_prob;
}
}
link_nodelist(nodecount, nodelist, min_psa_ratio);
return true;
}
void UCTNode::link_nodelist(std::atomic<int>& nodecount,
std::vector<Network::ScoreVertexPair>& nodelist,
float min_psa_ratio) {
assert(min_psa_ratio < m_min_psa_ratio_children);
if (nodelist.empty()) {
return;
}
// Use best to worst order, so highest go first
std::stable_sort(rbegin(nodelist), rend(nodelist));
LOCK(get_mutex(), lock);
const auto max_psa = nodelist[0].first;
const auto old_min_psa = max_psa * m_min_psa_ratio_children;
const auto new_min_psa = max_psa * min_psa_ratio;
if (new_min_psa > 0.0f) {
m_children.reserve(
std::count_if(cbegin(nodelist), cend(nodelist),
[=](const auto& node) { return node.first >= new_min_psa; }
)
);
} else {
m_children.reserve(nodelist.size());
}
auto skipped_children = false;
for (const auto& node : nodelist) {
if (node.first < new_min_psa) {
skipped_children = true;
} else if (node.first < old_min_psa) {
m_children.emplace_back(node.second, node.first);
++nodecount;
}
}
m_min_psa_ratio_children = skipped_children ? min_psa_ratio : 0.0f;
m_is_expanding = false;
}
const std::vector<UCTNodePointer>& UCTNode::get_children() const {
return m_children;
}
int UCTNode::get_move() const {
return m_move;
}
void UCTNode::virtual_loss() {
m_virtual_loss += VIRTUAL_LOSS_COUNT;
}
void UCTNode::virtual_loss_undo() {
m_virtual_loss -= VIRTUAL_LOSS_COUNT;
}
void UCTNode::update(float eval,float rollout) {
m_visits++;
accumulate_eval(eval);
accumulate_rollouts(rollout);
if(rollout==1)
accumulate_rolloutwin();
}
bool UCTNode::has_children() const {
return m_min_psa_ratio_children <= 1.0f;
}
bool UCTNode::expandable(const float min_psa_ratio) const {
return min_psa_ratio < m_min_psa_ratio_children;
}
float UCTNode::get_score() const {
return m_score;
}
void UCTNode::set_score(float score) {
m_score = score;
}
int UCTNode::get_visits() const {
return m_visits;
}
float UCTNode::get_nn_eval(int tomove) const {
// Due to the use of atomic updates and virtual losses, it is
// possible for the visit count to change underneath us. Make sure
// to return a consistent result to the caller by caching the values.
auto virtual_loss = int{m_virtual_loss};
auto visits = get_visits() + virtual_loss;
assert(visits > 0);
auto blackeval = get_blackevals();
if (tomove == FastBoard::WHITE) {
blackeval += static_cast<double>(virtual_loss);
}
auto score = static_cast<float>(blackeval / double(visits));
if (tomove == FastBoard::WHITE) {
score = 1.0f - score;
}
//myprintf(" v= %d ",get_visits());
return score;
}
float UCTNode::get_rollouts(int tomove) const {
// Due to the use of atomic updates and virtual losses, it is
// possible for the visit count to change underneath us. Make sure
// to return a consistent result to the caller by caching the values.
auto virtual_loss = int{m_virtual_loss};
auto visits = get_visits() + virtual_loss;
assert(visits > 0);
auto blackrollouts = get_blackrollouts();
if (tomove == FastBoard::WHITE) {
blackrollouts += static_cast<double>(virtual_loss);
}
auto score = static_cast<float>(blackrollouts / double(visits));
if (tomove == FastBoard::WHITE) {
if(cfg_rollout_mode)
score = -score;
else
score = 1.0f - score;
}
//myprintf(" r= %d \n",get_visits());
return score;
}
float UCTNode::get_rollout_winrate(int tomove) const {
// Due to the use of atomic updates and virtual losses, it is
// possible for the visit count to change underneath us. Make sure
// to return a consistent result to the caller by caching the values.
auto visits = get_visits();
assert(visits > 0);
auto blackrollouts = get_rolloutwin();
auto score = static_cast<float>(blackrollouts / double(visits));
if (tomove == FastBoard::WHITE) {
score = 1.0 - score;
}
return score;
}
float UCTNode::get_net_eval(int tomove) const {
if (tomove == FastBoard::WHITE) {
return 1.0f - m_net_eval;
}
return m_net_eval;
}
double UCTNode::get_blackevals() const {
return m_blackevals;
}
double UCTNode::get_blackrollouts() const {
return m_blackrollouts;
}
int UCTNode::get_rolloutwin() const{
return m_rolloutwin;
}
void UCTNode::accumulate_eval(float eval) {
atomic_add(m_blackevals, double(eval));
}
void UCTNode::accumulate_rollouts(float rollout) {
atomic_add(m_blackrollouts, double(rollout));
}
void UCTNode::accumulate_rolloutwin(){
atomic_add(m_rolloutwin, 1);
}
UCTNode* UCTNode::uct_select_child(int color, bool is_root) {
LOCK(get_mutex(), lock);
// Count parentvisits manually to avoid issues with transpositions.
auto total_visited_policy = 0.0f;
auto parentvisits = size_t{0};
for (const auto& child : m_children) {
if (child.valid()) {
parentvisits += child.get_visits();
if (child.get_visits() > 0) {
total_visited_policy += child.get_score();
}
}
}
auto numerator = std::sqrt(double(parentvisits));
auto fpu_reduction = 0.0f;
// Lower the expected eval for moves that are likely not the best.
// Do not do this if we have introduced noise at this node exactly
// to explore more.
if (!is_root || !cfg_noise) {
fpu_reduction = cfg_fpu_reduction * std::sqrt(total_visited_policy);
}
// Estimated eval for unknown nodes = original parent NN eval - reduction
auto fpu_eval = get_net_eval(color) - fpu_reduction;
auto best = static_cast<UCTNodePointer*>(nullptr);
auto best_value = std::numeric_limits<double>::lowest();
for (auto& child : m_children) {
if (!child.active()) {
continue;
}
auto winrate = fpu_eval;
if (child.get_visits() > 0) {
//if(cfg_use_rollout)
winrate = child.get_nn_eval(color)*lambda+child.get_rollouts(color)*(1-lambda);
//else
//winrate = child.get_nn_eval(color);
}
auto psa = child.get_score();
auto denom = 1.0 + child.get_visits();
auto puct = cfg_puct * psa * (numerator / denom);
auto value = winrate + puct;
assert(value > std::numeric_limits<double>::lowest());
if (value > best_value) {
best_value = value;
best = &child;
}
}
assert(best != nullptr);
best->inflate();
return best->get();
}
UCTNode* UCTNode::max_p_child() {
LOCK(get_mutex(), lock);
auto best = static_cast<UCTNodePointer*>(nullptr);
auto best_value = std::numeric_limits<double>::lowest();
for (auto& child : m_children) {
if (!child.active()) {
continue;
}
auto value = child.get_score();
assert(value > std::numeric_limits<double>::lowest());
if(value > 0.5){
best = &child;
break;
}
if (value > best_value) {
best_value = value;
best = &child;
}
}
//myprintf("%.4f ",best_value);
assert(best != nullptr);
best->inflate();
return best->get();
}
class NodeComp : public std::binary_function<UCTNodePointer&,
UCTNodePointer&, bool> {
public:
NodeComp(int color) : m_color(color) {};
bool operator()(const UCTNodePointer& a,
const UCTNodePointer& b) {
// if visits are not same, sort on visits
if (a.get_visits() != b.get_visits()) {
return a.get_visits() < b.get_visits();
}
// neither has visits, sort on prior score
if (a.get_visits() == 0) {
return a.get_score() < b.get_score();
}
// both have same non-zero number of visits
//if(cfg_use_rollout)
return (a.get_nn_eval(m_color)*lambda+a.get_rollouts(m_color)*(1-lambda)) < (b.get_nn_eval(m_color)*lambda+b.get_rollouts(m_color)*(1-lambda));
//else
//return a.get_nn_eval(m_color) < b.get_nn_eval(m_color);
}
private:
int m_color;
};
void UCTNode::sort_children(int color) {
LOCK(get_mutex(), lock);
std::stable_sort(rbegin(m_children), rend(m_children), NodeComp(color));
}
UCTNode& UCTNode::get_best_root_child(int color) {
LOCK(get_mutex(), lock);
assert(!m_children.empty());
auto ret = std::max_element(begin(m_children), end(m_children),
NodeComp(color));
ret->inflate();
return *(ret->get());
}
size_t UCTNode::count_nodes() const {
auto nodecount = size_t{0};
nodecount += m_children.size();
for (auto& child : m_children) {
if (child.get_visits() > 0) {
nodecount += child->count_nodes();
}
}
return nodecount;
}
void UCTNode::invalidate() {
m_status = INVALID;
}
void UCTNode::set_active(const bool active) {
if (valid()) {
m_status = active ? ACTIVE : PRUNED;
}
}
bool UCTNode::valid() const {
return m_status != INVALID;
}
bool UCTNode::active() const {
return m_status == ACTIVE;
}
////////////////////////////////////////////
float UCTNode::calculateRollout(GameState& state) {
//float rollout_value=0;
FullBoard board_cpy;
board_cpy=state.board;
//rollout_value+=playout(board_cpy, lgr, state.m_komi);
return Playout(board_cpy, state.m_komi);
}
int UCTNode::Playout(FullBoard& b, double komi) {
int next_move;
int prev_move = VNULL;
int pl = b.my;
while (b.move_cnt <= 720) {
next_move = b.SelectMove();
// 2��A���Ńp�X�̏ꍇ�A�I�ǁD
// Break in case of 2 consecutive pass.
if (next_move==PASS_AQ && prev_move==PASS_AQ) break;
prev_move = next_move;
}
prev_move = VNULL;
while (b.move_cnt <= 720) {
next_move = b.SelectRandomMove();
if (next_move==PASS_AQ && prev_move==PASS_AQ) break;
prev_move = next_move;
}
// Return the result.
return Win(b, pl, komi);
}
/*
int UCTNode::playout(FullBoard& b, LGR& lgr, double komi){
///*
int next_move;
int prev_move = VNULL;
int pl = b.my;
std::array<int, 4> lgr_seed;
std::vector<std::array<int, 3>> lgr_rollout_add[2];
std::array<int, 3> lgr_rollout_seed;
int update_v[2] = {VNULL, VNULL};
double update_p[2] = {100, 25};
while (b.move_cnt <= 720) {
lgr_seed[0] = b.prev_ptn[0].bf;
lgr_seed[1] = b.prev_move[b.her];
lgr_seed[2] = b.prev_ptn[1].bf;
lgr_seed[3] = b.prev_move[b.my];
// �i�J�f�Ő���ꂽ�Ƃ��A�}���ɑł�
// Forced move if removed stones is Nakade.
if (b.response_move[0] != VNULL) {
next_move = b.response_move[0];
b.PlayLegal(next_move);
}
else{
// lgr.policy�Ɋ܂܂��
// Check whether lgr_seed is included in lgr.policy.
auto itr = lgr.policy[b.my].find(lgr_seed);
int v = VNULL;
if (itr != lgr.policy[b.my].end()){
v = itr->second;
if(v < PASS_AQ && b.IsLegal(b.my, v) && !b.IsEyeShape(b.my, v) && !b.IsSeki(v)){
if(b.prob[b.my][v] != 0){
b.ReplaceProb(b.my, v, b.prob[b.my][v] * update_p[0]);
update_v[0] = v;
}
}
}
v = VNULL;
if(lgr_seed[1] < PASS_AQ && lgr_seed[3] < PASS_AQ){
v = lgr.rollout[b.my][lgr_seed[1]][lgr_seed[3]];
if(v < PASS_AQ){
if(b.prob[b.my][v] != 0){
b.ReplaceProb(b.my, v, b.prob[b.my][v] * update_p[1]);
update_v[1] = v;
}
}
}
next_move = b.SelectMove();
//printf(" selectmove ");
// update_v�̎�̊m����ɖ߂�
// Restore probability.
for(int i=0;i<2;++i){
if(update_v[i] != VNULL){
if(b.prob[b.her][update_v[i]] != 0){
b.ReplaceProb(b.her, update_v[i], b.prob[b.her][update_v[i]] / update_p[i]);
}
update_v[i] = VNULL;
}
}
}
if(lgr_seed[1] < PASS_AQ && lgr_seed[3] < PASS_AQ && next_move < PASS_AQ){
lgr_rollout_seed[0] = lgr_seed[1];
lgr_rollout_seed[1] = lgr_seed[3];
lgr_rollout_seed[2] = next_move;
lgr_rollout_add[b.her].push_back(lgr_rollout_seed);
}
// 2��A���Ńp�X�̏ꍇ�A�I��
// Break in case of 2 consecutive pass.
if (next_move==PASS_AQ && prev_move==PASS_AQ) break;
prev_move = next_move;
}
prev_move = VNULL;
while (b.move_cnt <= 720) {
//printf(" randommove ");
next_move = b.SelectRandomMove();
if (next_move==PASS_AQ && prev_move==PASS_AQ) break;
prev_move = next_move;
}
int win = Win(b, pl, komi);
int win_pl = int(win == 1);
int lose_pl = int(win != 1);
for(auto& i:lgr_rollout_add[win_pl]){
lgr.rollout[win_pl][i[0]][i[1]] = i[2];
}
for(auto& i:lgr_rollout_add[lose_pl]){
if(lgr.rollout[lose_pl][i[0]][i[1]] == i[2]){
lgr.rollout[lose_pl][i[0]][i[1]] = VNULL;
}
}
// �I�ǐ}�̏��s��Ԃ�
// Return the result.
//std::cout<<win<<" ";
return win;
}*/
int UCTNode::Win(FullBoard& b, int pl, double komi) {
double score[2] = {0.0, 0.0};
std::array<bool, EBVCNT> visited;
std::fill(visited.begin(), visited.end(), false);
// �Z�L�����邩�m�F. Check Seki.
for(int i=0,i_max=b.empty_cnt;i<i_max;++i){
int v = b.empty[i];
if(b.IsSeki(v) && !visited[v]){
// ���̋Ȃ���l�ڂ��m�F.
// Check whether it is corner bent fours.
int ren_idx[2] = {0,0};
forEach4Nbr(v, v_nbr2, {
if(b.color[v_nbr2] > 1){
ren_idx[b.color[v_nbr2] - 2] = b.ren_idx[v_nbr2];
}
});
bool is_bent4 = false;
for(int j=0;j<2;++j){
if(b.ren[ren_idx[j]].size == 3){
int v_tmp = ren_idx[j];
bool is_edge = true;
bool is_conner = false;
do{
is_edge &= (DistEdge(v_tmp) == 1);
if(!is_edge) break;
if ( v_tmp == rtoe[0] ||
v_tmp == rtoe[BSIZE - 1] ||
v_tmp == rtoe[BSIZE * (BSIZE - 1)] ||
v_tmp == rtoe[BVCNT - 1] ){
bool is_not_bnt = false;
forEach4Nbr(v_tmp, v_nbr1, {
// �G�̂Ƃ��A�Ȃ���4�ڂł͂Ȃ�
// If the neighboring stone is an opponnent's one.
is_not_bnt |= (b.color[v_nbr1] == int(j==0) + 2);
});
is_conner = !is_not_bnt;
}
v_tmp = b.next_ren_v[v_tmp];
}while(v_tmp != ren_idx[j]);
if(is_edge && is_conner){
// �Ȃ���l�ڂ̂Ƃ��A4�ڑ��̒n�Ƃ���
// Count all stones as that of the player of the bent fours.
score[j] += b.ren[ren_idx[0]].size + b.ren[ren_idx[1]].size + 2.0;
is_bent4 = true;
}
}
}
// visited��X�V. Update visited.
int64 lib_bit;
for(int i=0;i<6;++i){
lib_bit = b.ren[ren_idx[0]].lib_bits[i];
while(lib_bit != 0){
int ntz = NTZ(lib_bit);
int lib = rtoe[ntz + i * 64];
visited[lib] = true;
lib_bit ^= (0x1ULL << ntz);
}
}
// �Ȃ���l�ڂ̂Ƃ�. If it bent fours exist.
if(is_bent4){
int v_tmp = ren_idx[0];
do{
visited[v_tmp] = true;
v_tmp = b.next_ren_v[v_tmp];
}while(v_tmp != ren_idx[0]);
v_tmp = ren_idx[1];
do{
visited[v_tmp] = true;
v_tmp = b.next_ren_v[v_tmp];
}while(v_tmp != ren_idx[1]);
}
}
}
for (auto i: rtoe) {
int stone_color = b.color[i] - 2;
if (!visited[i] && (stone_color >= 0)) {
visited[i] = true;
++score[stone_color];
forEach4Nbr(i, v_nbr, {
if (!visited[v_nbr] && b.color[v_nbr] == 0) {
visited[v_nbr] = true;
++score[stone_color];
}
});
}
}
// �����p�X�̍��A�����Ō�ɒ����+1
// Correction factor of PASS. Add one if the last move is black.
int pass_corr = b.pass_cnt[0] - b.pass_cnt[1] + int((b.move_cnt%2)!=0);
double abs_score = score[1] - score[0] - komi - pass_corr * int(japanese_rule);
// ������->0, ���ԍ�����->1�A���ԍ�����-> -1��Ԃ�
// Return 0 if white wins, 1 if black wins and it's black's turn and else -1.
//if(cfg_rollout_mode)
//return int(abs_score > 0)*(int(pl == 1) - int(pl == 0));
//else
//return int(abs_score > 0);
if(abs_score > 0)
return 1;
else
return 0;
}