forked from QueensGambit/CrazyAra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.cpp
1106 lines (982 loc) · 30.7 KB
/
node.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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
CrazyAra, a deep learning chess variant engine
Copyright (C) 2018 Johannes Czech, Moritz Willig, Alena Beyer
Copyright (C) 2019-2020 Johannes Czech
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* @file: node.cpp
* Created on 28.08.2019
* @author: queensgambit
*/
#include "node.h"
#include <limits.h>
#include "syzygy/tbprobe.h"
#include "util/blazeutil.h" // get_dirichlet_noise()
#include "constants.h"
#include "../util/sfutil.h"
#include "../util/communication.h"
#include "evalinfo.h"
bool Node::is_sorted() const
{
return sorted;
}
Node::Node(Board *pos, bool inCheck, Node *parentNode, size_t childIdxForParent, const SearchSettings* searchSettings):
parentNode(parentNode),
key(pos->get_state_info()->key),
value(0),
d(nullptr),
childIdxForParent(childIdxForParent),
pliesFromNull(pos->get_state_info()->pliesFromNull),
isTerminal(false),
isTablebase(false),
hasNNResults(false),
sorted(false)
{
fill_child_node_moves(pos);
// specify the number of direct child nodes of this node
const int numberChildNodes = legalMoves.size();
check_for_terminal(pos, inCheck);
#ifdef MODE_CHESS
if (searchSettings->useTablebase && !isTerminal) {
check_for_tablebase_wdl(pos);
}
#endif
policyProbSmall.resize(numberChildNodes);
}
Node::Node(const Node &b)
{
set_value(b.get_value());
key = b.key;
pliesFromNull = b.plies_from_null();
const int numberChildNodes = b.legalMoves.size();
policyProbSmall.resize(numberChildNodes);
policyProbSmall = b.policyProbSmall;
legalMoves = b.legalMoves;
isTerminal = b.isTerminal;
// parentNode = // is not copied
// childIdxForParent = // is not copied
isTerminal = b.isTerminal;
isTablebase = b.isTablebase;
hasNNResults = b.hasNNResults;
sorted = b.sorted;
if (isTerminal) {
d = make_unique<NodeData>(numberChildNodes);
d->nodeType = b.d->nodeType;
return;
}
if (sorted) {
d = make_unique<NodeData>(numberChildNodes);
}
// TODO: Allow copying checkmateIndex
}
void Node::fill_child_node_moves(Board* pos)
{
// generate the legal moves and save them in the list
for (const ExtMove& move : MoveList<LEGAL>(*pos)) {
legalMoves.push_back(move);
}
}
bool Node::solved_win(const Node* childNode) const
{
if (childNode->d->nodeType == SOLVED_LOSS) {
d->checkmateIdx = childNode->get_child_idx_for_parent();
return true;
}
return false;
}
bool Node::solved_draw(const Node* childNode) const
{
if (d->numberUnsolvedChildNodes == 0 &&
(childNode->d->nodeType == SOLVED_DRAW || childNode->d->nodeType == SOLVED_WIN)) {
return at_least_one_drawn_child();
}
return false;
}
bool Node::at_least_one_drawn_child() const
{
bool atLeastOneDrawnChild = false;
for (Node* childNode : d->childNodes) {
if (!childNode->is_playout_node() || (childNode->d->nodeType != SOLVED_DRAW && childNode->d->nodeType != SOLVED_WIN)) {
return false;
}
if (childNode->d->nodeType == SOLVED_DRAW) {
atLeastOneDrawnChild = true;
}
}
if (atLeastOneDrawnChild) {
return true;
}
return false;
}
bool Node::only_won_child_nodes() const
{
for (Node* childNode : d->childNodes) {
if (childNode->d->nodeType != SOLVED_WIN) {
return false;
}
}
return true;
}
bool Node::solved_loss(const Node* childNode) const
{
if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == SOLVED_WIN) {
return only_won_child_nodes();
}
return false;
}
void Node::mark_as_loss()
{
set_value(LOSS);
d->nodeType = SOLVED_LOSS;
}
void Node::mark_as_draw()
{
set_value(DRAW);
d->nodeType = SOLVED_DRAW;
}
void Node::define_end_ply_for_solved_terminal(const Node* childNode)
{
if (d->nodeType == SOLVED_LOSS) {
// choose the longest pv line
for (const Node* curChildNode : d->childNodes) {
if (curChildNode->d->endInPly+1 > d->endInPly) {
d->endInPly = curChildNode->d->endInPly+1;
}
}
return;
}
if (d->nodeType == SOLVED_DRAW) {
// choose the shortest pv line for draws
for (const Node* curChildNode : d->childNodes) {
if (curChildNode->d->nodeType == SOLVED_DRAW && curChildNode->d->endInPly+1 < d->endInPly) {
d->endInPly = curChildNode->d->endInPly+1;
}
}
return;
}
// get the endPly for WINS
d->endInPly = childNode->d->endInPly + 1;
}
void Node::update_solved_terminal(const Node* childNode, int targetValue)
{
define_end_ply_for_solved_terminal(childNode);
set_value(targetValue);
if (parentNode != nullptr) {
parentNode->lock();
parentNode->d->numberUnsolvedChildNodes--;
parentNode->d->qValues[childIdxForParent] = targetValue;
if (targetValue == LOSS) {
parentNode->d->checkmateIdx = childIdxForParent;
}
else if (targetValue == WIN && !is_root_node() && parentNode->is_root_node()) {
parentNode->disable_move(childIdxForParent);
}
parentNode->unlock();
}
}
void Node::mcts_policy_based_on_wins(DynamicVector<float> &mctsPolicy) const
{
mctsPolicy = 0;
size_t childIdx = 0;
for (auto childNode: get_child_nodes()) {
if (childNode != nullptr && childNode->d != nullptr && childNode->d->nodeType == SOLVED_LOSS) {
mctsPolicy[childIdx] = 1.0f;
}
++childIdx;
}
mctsPolicy /= sum(mctsPolicy);
}
void Node::prune_losses_in_mcts_policy(DynamicVector<float> &mctsPolicy) const
{
// check if PV line leads to a loss
if (d->numberUnsolvedChildNodes != get_number_child_nodes() && d->nodeType != SOLVED_LOSS) {
// set all entries which lead to a WIN of the opponent to zero
for (size_t childIdx = 0; childIdx < d->noVisitIdx; ++childIdx) {
const Node* childNode = d->childNodes[childIdx];
if (childNode != nullptr && childNode->is_playout_node() && childNode->d->nodeType == SOLVED_WIN) {
mctsPolicy[childIdx] = 0;
}
}
}
}
void Node::mcts_policy_based_on_q_n(DynamicVector<float>& mctsPolicy, float qValueWeight) const
{
DynamicVector<float> qValuePruned = d->qValues;
qValuePruned = (qValuePruned + 1) * 0.5f;
const DynamicVector<float> normalizedVisits = d->childNumberVisits / get_visits();
const float quantile = get_quantile(normalizedVisits, 0.25f);
for (size_t idx = 0; idx < get_number_child_nodes(); ++idx) {
if (d->childNumberVisits[idx] < quantile) {
qValuePruned[idx] = 0;
}
}
mctsPolicy = (1.0f - qValueWeight) * normalizedVisits + qValueWeight * qValuePruned;
}
void Node::solve_for_terminal(const Node* childNode)
{
if (d->nodeType != UNSOLVED) {
// already solved
return;
}
if (!childNode->is_playout_node()) {
return;
}
if (solved_win(childNode)) {
d->nodeType = SOLVED_WIN;
update_solved_terminal(childNode, WIN);
return;
}
if (solved_loss(childNode)) {
d->nodeType = SOLVED_LOSS;
update_solved_terminal(childNode, LOSS);
return;
}
if (solved_draw(childNode)) {
d->nodeType = SOLVED_DRAW;
update_solved_terminal(childNode, DRAW);
}
}
void Node::mark_nodes_as_fully_expanded()
{
info_string("mark as fully expanded");
d->noVisitIdx = get_number_child_nodes();
}
bool Node::is_root_node() const
{
return parentNode->parentNode == nullptr;
}
Node::~Node()
{
}
void Node::sort_moves_by_probabilities()
{
auto p = sort_permutation(policyProbSmall, std::greater<float>());
apply_permutation_in_place(policyProbSmall, p);
apply_permutation_in_place(legalMoves, p);
sorted = true;
}
Move Node::get_move(size_t childIdx) const
{
return legalMoves[childIdx];
}
Node *Node::get_child_node(size_t childIdx) const
{
return d->childNodes[childIdx];
}
Move Node::get_best_move() const
{
return get_move(get_best_move_index(this, false));
}
vector<Move> Node::get_ponder_moves() const
{
vector<Move> ponderMoves;
const size_t visitThresh = 0.01 * get_visits();
for (const Node* childNode : get_child_nodes()) {
if (childNode == nullptr) {
break;
}
if (childNode->is_playout_node() && childNode->get_visits() > visitThresh) {
if (!childNode->is_terminal()) {
if (ponderMoves.size() == 0) {
ponderMoves.emplace_back(childNode->get_best_move());
}
else if (find(ponderMoves.begin(), ponderMoves.end(), childNode->get_best_move()) == ponderMoves.end()) {
ponderMoves.emplace_back(childNode->get_best_move());
}
}
}
}
return ponderMoves;
}
vector<Node*> Node::get_child_nodes() const
{
return d->childNodes;
}
bool Node::is_terminal() const
{
return isTerminal;
}
bool Node::has_nn_results() const
{
return hasNNResults;
}
void Node::apply_virtual_loss_to_child(size_t childIdx, float virtualLoss)
{
// update the stats of the parent node
// temporarily reduce the attraction of this node by applying a virtual loss /
// the effect of virtual loss will be undone if the playout is over
// virtual increase the number of visits
d->childNumberVisits[childIdx] += size_t(virtualLoss);
// make it look like if one has lost X games from this node forward where X is the virtual loss value
d->actionValues[childIdx] -= virtualLoss;
d->qValues[childIdx] = d->actionValues[childIdx] / d->childNumberVisits[childIdx];
}
Node *Node::get_parent_node() const
{
return parentNode;
}
void Node::increment_visits(size_t numberVisits)
{
parentNode->lock();
parentNode->d->childNumberVisits[childIdxForParent] += numberVisits;
parentNode->unlock();
}
void Node::subtract_visits(size_t numberVisits)
{
parentNode->lock();
parentNode->d->childNumberVisits[childIdxForParent] -= numberVisits;
parentNode->unlock();
}
float Node::get_q_value(size_t idx)
{
return d->qValues[idx];
}
void Node::set_q_value(size_t idx, float value)
{
d->qValues[idx] = value;
}
size_t Node::get_best_q_idx() const
{
return argmax(d->qValues);
}
vector<size_t> Node::get_q_idx_over_thresh(float qThresh)
{
vector<size_t> indices;
for (size_t idx = 0; idx < size(d->qValues); ++idx) {
if (d->qValues[idx] > qThresh) {
indices.emplace_back(idx); }
}
return indices;
}
void Node::reserve_full_memory()
{
const size_t numberChildNodes = get_number_child_nodes();
d->childNumberVisits.reserve(numberChildNodes);
d->actionValues.reserve(numberChildNodes);
d->qValues.reserve(numberChildNodes);
d->childNodes.reserve(numberChildNodes);
}
void Node::increment_no_visit_idx()
{
if (d->noVisitIdx < get_number_child_nodes()) {
++d->noVisitIdx;
if (d->noVisitIdx == PRESERVED_ITEMS) {
reserve_full_memory();
}
d->add_empty_node();
}
}
void Node::fully_expand_node()
{
if (d->nodeType == UNSOLVED && !is_fully_expanded()) {
reserve_full_memory();
for (size_t idx = d->noVisitIdx; idx < get_number_child_nodes(); ++idx) {
d->add_empty_node();
}
d->noVisitIdx = get_number_child_nodes();
// keep this exact order
sorted = true;
}
}
float Node::get_value() const
{
return value;
}
Key Node::hash_key() const
{
return key;
}
size_t Node::get_number_child_nodes() const
{
return legalMoves.size();
}
void Node::prepare_node_for_visits()
{
sort_moves_by_probabilities();
init_node_data();
}
uint32_t Node::get_visits() const
{
return parentNode->d->childNumberVisits[childIdxForParent];
}
void Node::backup_value(size_t childIdx, float value, float virtualLoss)
{
Node* currentNode = this;
do {
currentNode->revert_virtual_loss_and_update(childIdx, value, virtualLoss);
childIdx = currentNode->childIdxForParent;
value = -value;
currentNode = currentNode->parentNode;
} while(currentNode->parentNode != nullptr);
// revert virtual loss for root
if (virtualLoss != 1) {
currentNode->get_child_node(childIdx)->subtract_visits(virtualLoss-1);
}
}
void Node::revert_virtual_loss_and_update(size_t childIdx, float value, float virtualLoss)
{
lock();
if (virtualLoss != 1) {
d->childNumberVisits[childIdx] -= size_t(virtualLoss) - 1;
d->actionValues[childIdx] += virtualLoss + value;
}
else {
d->actionValues[childIdx] += 1 + value;
}
d->qValues[childIdx] = d->actionValues[childIdx] / d->childNumberVisits[childIdx];
if (is_terminal_value(value)) {
++d->terminalVisits;
solve_for_terminal(d->childNodes[childIdx]);
}
unlock();
}
void Node::backup_collision(size_t childIdx, float virtualLoss)
{
Node* currentNode = this;
do {
currentNode->revert_virtual_loss(childIdx, virtualLoss);
childIdx = currentNode->childIdxForParent;
currentNode = currentNode->parentNode;
} while(currentNode->parentNode != nullptr);
// revert virtual loss for root
currentNode->get_child_node(childIdx)->subtract_visits(virtualLoss);
}
void Node::revert_virtual_loss(size_t childIdx, float virtualLoss)
{
lock();
d->childNumberVisits[childIdx] -= virtualLoss;
d->actionValues[childIdx] += virtualLoss;
d->qValues[childIdx] = d->actionValues[childIdx] / d->childNumberVisits[childIdx];
unlock();
}
bool Node::is_playout_node() const
{
return d != nullptr;
}
bool Node::is_blank_root_node() const
{
return get_visits() == 0;
}
bool Node::is_solved() const
{
return d->nodeType != UNSOLVED;
}
bool Node::has_forced_win() const
{
return get_checkmate_idx() != NO_CHECKMATE;
}
void Node::set_parent_node(Node* value)
{
parentNode = value;
}
size_t Node::get_no_visit_idx() const
{
return d->noVisitIdx;
}
bool Node::is_fully_expanded() const
{
return get_number_child_nodes() == d->noVisitIdx;
}
DynamicVector<float>& Node::get_policy_prob_small()
{
return policyProbSmall;
}
void Node::set_value(float value)
{
this->value = value;
}
size_t Node::get_child_idx_for_parent() const
{
return childIdxForParent;
}
void Node::add_new_child_node(Node *newNode, size_t childIdx)
{
d->childNodes[childIdx] = newNode;
}
void Node::add_transposition_child_node(Node* newNode, size_t childIdx)
{
newNode->parentNode = this;
newNode->childIdxForParent = childIdx;
d->childNodes[childIdx] = newNode;
}
float Node::max_policy_prob()
{
return max(policyProbSmall);
}
size_t Node::max_q_child()
{
return argmax(d->qValues);
}
size_t Node::max_visits_child()
{
return argmax(d->childNumberVisits);
}
float Node::updated_value_eval() const
{
if (!is_sorted()) {
return get_value();
}
if (d == nullptr || get_visits() == 1) {
return get_value();
}
switch(d->nodeType) {
case SOLVED_WIN:
return WIN;
case SOLVED_DRAW:
return DRAW;
case SOLVED_LOSS:
return LOSS;
default: ; // UNSOLVED
}
return d->qValues[argmax(d->childNumberVisits)];
}
std::vector<Move> Node::get_legal_moves() const
{
return legalMoves;
}
int Node::get_checkmate_idx() const
{
return d->checkmateIdx;
}
DynamicVector<uint32_t> Node::get_child_number_visits() const
{
return d->childNumberVisits;
}
void Node::enable_has_nn_results()
{
hasNNResults = true;
}
int Node::plies_from_null() const
{
return pliesFromNull;
}
bool Node::is_tablebase() const
{
return isTablebase;
}
uint8_t Node::get_node_type() const
{
return d->nodeType;
}
uint16_t Node::get_end_in_ply() const
{
return d->endInPly;
}
uint32_t Node::get_terminal_visits() const
{
return d->terminalVisits;
}
void Node::init_node_data(size_t numberNodes)
{
d = make_unique<NodeData>(numberNodes);
}
void Node::init_node_data()
{
init_node_data(get_number_child_nodes());
}
void Node::mark_as_terminal()
{
isTerminal = true;
init_node_data();
}
void Node::check_for_terminal(Board* pos, bool inCheck)
{
if (get_number_child_nodes() == 0) {
mark_as_terminal();
#ifdef ANTI
if (pos->is_anti()) {
// a stalmate is a win in antichess
set_value(WIN);
return;
}
#endif
// test if we have a check-mate
if (inCheck) {
mark_as_loss();
return;
}
// we reached a stalmate
mark_as_draw();
return;
}
#ifdef ANTI
if (pos->is_anti()) {
if (pos->is_anti_win()) {
mark_as_terminal();
set_value(WIN);
return;
}
if (pos->is_anti_loss()) {
mark_as_terminal();
set_value(LOSS);
parentNode->d->checkmateIdx = childIdxForParent;
return;
}
}
#endif
if (pos->can_claim_3fold_repetition() || pos->is_50_move_rule_draw() || pos->draw_by_insufficient_material()) {
// reached 3-fold-repetition or 50 moves rule draw or insufficient material
mark_as_terminal();
mark_as_draw();
return;
}
// normal game position
// isTerminal = false; // is the default value
}
void Node::check_for_tablebase_wdl(Board *pos)
{
Tablebases::ProbeState result;
Tablebases::WDLScore wdlScore = probe_wdl(*pos, &result);
if (result != Tablebases::FAIL) {
// TODO: Change return values
isTablebase = true;
switch(wdlScore) {
case Tablebases::WDLLoss:
set_value(-0.99); //LOSS);
break;
case Tablebases::WDLWin:
set_value(0.99); //WIN);
break;
default:
set_value(0.00001); //DRAW);
}
}
// default: isTablebase = false;
}
void Node::make_to_root()
{
parentNode->parentNode = nullptr;
}
void Node::lock()
{
mtx.lock();
}
void Node::unlock()
{
mtx.unlock();
}
void Node::apply_dirichlet_noise_to_prior_policy(const SearchSettings* searchSettings)
{
DynamicVector<float> dirichlet_noise = get_dirichlet_noise(get_number_child_nodes(), searchSettings->dirichletAlpha);
policyProbSmall = (1 - searchSettings->dirichletEpsilon ) * policyProbSmall + searchSettings->dirichletEpsilon * dirichlet_noise;
}
void Node::apply_temperature_to_prior_policy(float temperature)
{
apply_temperature(policyProbSmall, temperature);
}
void Node::set_probabilities_for_moves(const float *data, unordered_map<Move, size_t>& moveLookup)
{
// allocate sufficient memory -> is assumed that it has already been done
assert(legalMoves.size() == policyProbSmall.size());
for (size_t mvIdx = 0; mvIdx < legalMoves.size(); ++mvIdx) {
// retrieve vector index from look-up table
// set the right prob value
// accessing the data on the raw floating point vector is faster
// than calling policyProb.At(batchIdx, vectorIdx)
policyProbSmall[mvIdx] = data[moveLookup[legalMoves[mvIdx]]];
}
}
void Node::apply_softmax_to_policy()
{
policyProbSmall = softmax(policyProbSmall);
}
void Node::mark_enhanced_moves(const Board* pos, const SearchSettings* searchSettings)
{
// const float numberChildNodes = get_number_child_nodes();
// if (searchSettings->enhanceChecks || searchSettings->enhanceCaptures) {
// isCheck.resize(numberChildNodes);
// isCheck = false;
// isCapture.resize(numberChildNodes);
// isCapture = false;
// for (size_t idx = 0; idx < numberChildNodes; ++idx) {
// if (pos->capture(legalMoves[idx])) {
// isCapture[idx] = true;
// }
// if (pos->gives_check(legalMoves[idx])) {
// isCheck[idx] = true;
// }
// }
// }
}
void Node::disable_move(size_t childIdxForParent)
{
policyProbSmall[childIdxForParent] = 0;
d->actionValues[childIdxForParent] = -INT_MAX;
}
void Node::enhance_moves(const SearchSettings* searchSettings)
{
// if (!searchSettings->enhanceChecks && !searchSettings->enhanceCaptures) {
// return;
// }
// bool checkUpdate = false;
// bool captureUpdate = false;
// if (searchSettings->enhanceChecks) {
// checkUpdate = enhance_move_type(min(searchSettings->threshCheck, max(policyProbSmall)*searchSettings->checkFactor),
// searchSettings->threshCheck, legalMoves, isCheck, policyProbSmall);
// }
// if (searchSettings->enhanceCaptures) {
// captureUpdate = enhance_move_type(min(searchSettings->threshCapture, max(policyProbSmall)*searchSettings->captureFactor),
// searchSettings->threshCheck, legalMoves, isCapture, policyProbSmall);
// }
// if (checkUpdate || captureUpdate) {
// policyProbSmall /= sum(policyProbSmall);
// }
}
bool enhance_move_type(float increment, float thresh, const vector<Move>& legalMoves, const DynamicVector<bool>& moveType, DynamicVector<float>& policyProbSmall)
{
bool update = false;
for (size_t i = 0; i < legalMoves.size(); ++i) {
if (moveType[i] && policyProbSmall[i] < thresh) {
policyProbSmall[i] += increment;
update = true;
}
}
return update;
}
bool is_check(const Board* pos, Move move)
{
return pos->gives_check(move);
}
bool is_capture(const Board* pos, Move move)
{
return pos->capture(move);
}
DynamicVector<float> Node::get_current_u_values(const SearchSettings* searchSettings)
{
return get_current_cput(get_visits(), searchSettings) * blaze::subvector(policyProbSmall, 0, d->noVisitIdx) * (sqrt(get_visits()) / (d->childNumberVisits + 1.0));
}
Node *Node::get_child_node(size_t childIdx)
{
return d->childNodes[childIdx];
}
void Node::get_mcts_policy(DynamicVector<float>& mctsPolicy, size_t& bestMoveIdx, float qValueWeight) const
{
// fill only the winning moves in case of a known win
if (d->nodeType == SOLVED_WIN) {
mcts_policy_based_on_wins(mctsPolicy);
return;
}
if (qValueWeight > 0) {
size_t secondArg;
float firstMax;
float secondMax;
mctsPolicy = d->childNumberVisits;
first_and_second_max(mctsPolicy, d->noVisitIdx, firstMax, secondMax, bestMoveIdx, secondArg);
if (d->qValues[secondArg]-Q_VALUE_DIFF > d->qValues[bestMoveIdx]) {
mctsPolicy[bestMoveIdx] = secondMax;
mctsPolicy[secondArg] = firstMax;
bestMoveIdx = secondArg;
}
// TODO: check if this is useful
// else {
// size_t qIdx = get_best_q_idx();
// if (bestMoveIdx != qIdx) {
// const float qDiff = 1.0f - (d->childNumberVisits[qIdx] / d->childNumberVisits[bestMoveIdx]);
// if (d->qValues[qIdx]-qDiff > d->qValues[bestMoveIdx]) {
// mctsPolicy[bestMoveIdx] = d->childNumberVisits[qIdx];
// mctsPolicy[secondArg] = firstMax;
// bestMoveIdx = qIdx;
// }
// }
// }
}
else {
mctsPolicy = d->childNumberVisits;
bestMoveIdx = argmax(d->childNumberVisits);
}
mctsPolicy /= sum(mctsPolicy);
}
void Node::get_principal_variation(vector<Move>& pv) const
{
const Node* curNode = this;
while (curNode != nullptr && curNode->is_playout_node() && !curNode->is_terminal()) {
size_t childIdx = get_best_move_index(curNode, true);
pv.push_back(curNode->get_move(childIdx));
curNode = curNode->d->childNodes[childIdx];
}
}
size_t get_best_move_index(const Node *curNode, bool fast)
{
if (curNode->get_checkmate_idx() != NO_CHECKMATE) {
// chose mating line
return curNode->get_checkmate_idx();
}
if (curNode->get_node_type() == SOLVED_LOSS) {
// choose node which delays the mate
size_t longestPVlength = 0;
size_t childIdx = 0;
for (size_t idx = 0; idx < curNode->get_number_child_nodes(); ++idx) {
if (curNode->get_child_nodes()[idx]->get_end_in_ply() > longestPVlength) {
longestPVlength = curNode->get_child_nodes()[idx]->get_end_in_ply();
childIdx = idx;
}
}
return childIdx;
}
if (fast) {
return argmax(curNode->get_child_number_visits());
}
DynamicVector<float> mctsPolicy(curNode->get_number_child_nodes());
size_t bestMoveIdx;
curNode->get_mcts_policy(mctsPolicy, bestMoveIdx);
return bestMoveIdx;
}
size_t Node::select_child_node(const SearchSettings* searchSettings)
{
if (!sorted) {
prepare_node_for_visits();
}
if (d->noVisitIdx == 1) {
return 0;
}
if (has_forced_win()) {
return d->checkmateIdx;
}
// find the move according to the q- and u-values for each move
// calculate the current u values
// it's not worth to save the u values as a node attribute because u is updated every time n_sum changes
return argmax(d->qValues + get_current_u_values(searchSettings));
}
const char* node_type_to_string(enum NodeType nodeType)
{
switch(nodeType) {
case SOLVED_WIN:
return "WIN";
case SOLVED_DRAW:
return "DRAW";
case SOLVED_LOSS:
return "LOSS";
default:
return "UNSOLVED";
}
}
NodeType flip_node_type(const enum NodeType nodeType) {
switch(nodeType) {
case SOLVED_WIN:
return SOLVED_LOSS;
case SOLVED_LOSS:
return SOLVED_WIN;
default:
return nodeType;
}
}
void generate_dtz_values(const vector<Move> legalMoves, Board& pos, DynamicVector<int>& dtzValues) {
StateListPtr states = StateListPtr(new std::deque<StateInfo>(0));
// fill dtz value vector
for (size_t idx = 0; idx < legalMoves.size(); ++idx) {
states->emplace_back();
pos.do_move(legalMoves[idx], states->back());
Tablebases::ProbeState result;
int dtzValue = -probe_dtz(pos, &result);
if (result != Tablebases::FAIL) {
dtzValues[idx] = dtzValue;
}
else {
info_string("DTZ tablebase look-up failed!");