forked from leela-zero/leela-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUCTNodeRoot.cpp
232 lines (196 loc) · 7.1 KB
/
UCTNodeRoot.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
/*
This file is part of Leela Zero.
Copyright (C) 2018-2019 Gian-Carlo Pascutto
Leela Zero 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.
Leela Zero 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 Leela Zero. If not, see <http://www.gnu.org/licenses/>.
Additional permission under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with NVIDIA Corporation's libraries from the
NVIDIA CUDA Toolkit and/or the NVIDIA CUDA Deep Neural
Network library and/or the NVIDIA TensorRT inference library
(or a modified version of those libraries), containing parts covered
by the terms of the respective license agreement, the licensors of
this Program grant you additional permission to convey the resulting
work.
*/
#include "config.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <numeric>
#include <random>
#include <utility>
#include <vector>
#include "FastBoard.h"
#include "FastState.h"
#include "GTP.h"
#include "KoState.h"
#include "Random.h"
#include "UCTNode.h"
#include "Utils.h"
/*
* These functions belong to UCTNode but should only be called on the root node
* of UCTSearch and have been seperated to increase code clarity.
*/
UCTNode* UCTNode::get_first_child() const {
if (m_children.empty()) {
return nullptr;
}
return m_children.front().get();
}
void UCTNode::kill_superkos(const GameState& state) {
UCTNodePointer* pass_child = nullptr;
size_t valid_count = 0;
for (auto& child : m_children) {
auto move = child->get_move();
if (move != FastBoard::PASS) {
KoState mystate = state;
mystate.play_move(move);
if (mystate.superko()) {
// Don't delete nodes for now, just mark them invalid.
child->invalidate();
}
} else {
pass_child = &child;
}
if (child->valid()) {
valid_count++;
}
}
if (valid_count > 1 && pass_child
&& !state.is_move_legal(state.get_to_move(), FastBoard::PASS)) {
// Remove the PASS node according to "avoid" -- but only if there are
// other valid nodes left.
(*pass_child)->invalidate();
}
// Now do the actual deletion.
m_children.erase(
std::remove_if(begin(m_children), end(m_children),
[](const auto& child) { return !child->valid(); }),
end(m_children));
}
void UCTNode::dirichlet_noise(const float epsilon, const float alpha) {
auto child_cnt = m_children.size();
auto dirichlet_vector = std::vector<float>{};
std::gamma_distribution<float> gamma(alpha, 1.0f);
for (size_t i = 0; i < child_cnt; i++) {
dirichlet_vector.emplace_back(gamma(Random::get_Rng()));
}
auto sample_sum =
std::accumulate(begin(dirichlet_vector), end(dirichlet_vector), 0.0f);
// If the noise vector sums to 0 or a denormal, then don't try to
// normalize.
if (sample_sum < std::numeric_limits<float>::min()) {
return;
}
for (auto& v : dirichlet_vector) {
v /= sample_sum;
}
child_cnt = 0;
for (auto& child : m_children) {
auto policy = child->get_policy();
auto eta_a = dirichlet_vector[child_cnt++];
policy = policy * (1 - epsilon) + epsilon * eta_a;
child->set_policy(policy);
}
}
void UCTNode::randomize_first_proportionally() {
auto accum = 0.0;
auto norm_factor = 0.0;
auto accum_vector = std::vector<double>{};
for (const auto& child : m_children) {
auto visits = child->get_visits();
if (norm_factor == 0.0) {
norm_factor = visits;
// Nonsensical options? End of game?
if (visits <= cfg_random_min_visits) {
return;
}
}
if (visits > cfg_random_min_visits) {
accum += std::pow(visits / norm_factor, 1.0 / cfg_random_temp);
accum_vector.emplace_back(accum);
}
}
auto distribution = std::uniform_real_distribution<double>{0.0, accum};
auto pick = distribution(Random::get_Rng());
auto index = size_t{0};
for (size_t i = 0; i < accum_vector.size(); i++) {
if (pick < accum_vector[i]) {
index = i;
break;
}
}
// Take the early out
if (index == 0) {
return;
}
assert(m_children.size() > index);
// Now swap the child at index with the first child
std::iter_swap(begin(m_children), begin(m_children) + index);
}
UCTNode* UCTNode::get_nopass_child(FastState& state) const {
for (const auto& child : m_children) {
/* If we prevent the engine from passing, we must bail out when
we only have unreasonable moves to pick, like filling eyes.
Note that this knowledge isn't required by the engine,
we require it because we're overruling its moves. */
if (child->m_move != FastBoard::PASS
&& !state.board.is_eye(state.get_to_move(), child->m_move)) {
return child.get();
}
}
return nullptr;
}
// Used to find new root in UCTSearch.
std::unique_ptr<UCTNode> UCTNode::find_child(const int move) {
for (auto& child : m_children) {
if (child.get_move() == move) {
// no guarantee that this is a non-inflated node
child.inflate();
return std::unique_ptr<UCTNode>(child.release());
}
}
// Can happen if we resigned or children are not expanded
return nullptr;
}
void UCTNode::inflate_all_children() {
for (const auto& node : get_children()) {
node.inflate();
}
}
void UCTNode::prepare_root_node(Network& network, const int color,
std::atomic<int>& nodes,
GameState& root_state) {
float root_eval;
const auto had_children = has_children();
if (expandable()) {
create_children(network, nodes, root_state, root_eval);
}
if (had_children) {
root_eval = get_net_eval(color);
} else {
update(root_eval);
root_eval = (color == FastBoard::BLACK ? root_eval : 1.0f - root_eval);
}
Utils::myprintf("NN eval=%f\n", root_eval);
// There are a lot of special cases where code assumes
// all children of the root are inflated, so do that.
inflate_all_children();
// Remove illegal moves, so the root move list is correct.
// This also removes a lot of special cases.
kill_superkos(root_state);
if (cfg_noise) {
// Adjust the Dirichlet noise's alpha constant to the board size
auto alpha = 0.03f * 361.0f / NUM_INTERSECTIONS;
dirichlet_noise(0.25f, alpha);
}
}