forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcts_node.cc
More file actions
438 lines (380 loc) · 12.8 KB
/
Copy pathmcts_node.cc
File metadata and controls
438 lines (380 loc) · 12.8 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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "cc/mcts_node.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <tuple>
#include <utility>
#include "absl/strings/str_format.h"
#include "cc/algorithm.h"
#include "cc/logging.h"
namespace minigo {
namespace {
void InitLegalMoves(MctsNode* node) {
auto& position = node->position;
auto position_hash = position.stone_hash();
auto to_play = position.to_play();
for (int c = 0; c < kN * kN; ++c) {
switch (position.ClassifyMove(c)) {
case Position::MoveType::kIllegal: {
// The move is trivially not legal.
node->legal_moves[c] = false;
break;
}
case Position::MoveType::kNoCapture: {
// The move will not capture any stones: we can calculate the new
// position's stone hash directly.
auto new_hash = position_hash ^ zobrist::MoveHash(c, to_play);
node->legal_moves[c] = !node->HasPositionBeenPlayedBefore(new_hash);
break;
}
case Position::MoveType::kCapture: {
// The move will capture some opponent stones: in order to calculate the
// stone hash, we actually have to play the move.
Position new_position(position);
// It's safe to call AddStoneToBoard instead of PlayMove because:
// - we know the move is not kPass.
// - the move is legal (modulo superko).
// - we only care about new_position's stone_hash and not the rest of
// the bookkeeping that PlayMove updates.
new_position.AddStoneToBoard(c, to_play);
auto new_hash = new_position.stone_hash();
node->legal_moves[c] = !node->HasPositionBeenPlayedBefore(new_hash);
break;
}
}
}
node->legal_moves[Coord::kPass] = true;
}
constexpr int kSuperKoCacheStride = 8;
} // namespace
MctsNode::MctsNode(EdgeStats* stats, const Position& position)
: parent(nullptr), stats(stats), move(Coord::kInvalid), position(position) {
InitLegalMoves(this);
}
MctsNode::MctsNode(MctsNode* parent, Coord move)
: parent(parent),
stats(&parent->edges[move]),
move(move),
position(parent->position) {
position.PlayMove(move);
// Insert a cache of ancestor Zobrist hashes at regular depths in the tree.
// See the comment for superko_cache in the mcts_node.h for more details.
if ((position.n() % kSuperKoCacheStride) == 0) {
superko_cache = absl::make_unique<SuperkoCache>();
superko_cache->reserve(position.n() + 1);
superko_cache->insert(position.stone_hash());
for (auto* node = parent; node != nullptr; node = node->parent) {
if (node->superko_cache != nullptr) {
superko_cache->insert(node->superko_cache->begin(),
node->superko_cache->end());
break;
}
superko_cache->insert(node->position.stone_hash());
}
}
InitLegalMoves(this);
}
Coord MctsNode::GetMostVisitedMove() const {
// Find the set of moves with the largest N.
inline_vector<Coord, kNumMoves> moves;
int best_N = -1;
for (int i = 0; i < kNumMoves; ++i) {
int cn = child_N(i);
if (cn >= best_N) {
if (cn > best_N) {
moves.clear();
best_N = cn;
}
moves.push_back(i);
}
}
MG_CHECK(!moves.empty());
// If there's only one move with the largest N, we're done.
if (moves.size() == 1) {
return moves[0];
}
// Otherwise, break score using the child action score.
float to_play = position.to_play() == Color::kBlack ? 1 : -1;
float U_scale = kPuct * std::sqrt(1.0f + N());
Coord c = moves[0];
float best_cas =
CalculateSingleMoveChildActionScore(to_play, U_scale, moves[0]);
for (int i = 0; i < moves.size(); ++i) {
float cas = CalculateSingleMoveChildActionScore(to_play, U_scale, moves[i]);
if (cas > best_cas) {
best_cas = cas;
c = moves[i];
}
}
return c;
}
std::string MctsNode::Describe() const {
auto child_action_score = CalculateChildActionScore();
using SortInfo = std::tuple<float, float, int>;
std::array<SortInfo, kNumMoves> sort_order;
for (int i = 0; i < kNumMoves; ++i) {
sort_order[i] = SortInfo(child_N(i), child_action_score[i], i);
}
std::sort(sort_order.begin(), sort_order.end(), std::greater<SortInfo>());
auto result = absl::StrFormat(
"%0.4f\n%s\n"
"move : action Q U P P-Dir N soft-N p-delta p-rel",
Q(), MostVisitedPathString());
float child_N_sum = 0;
for (const auto& e : edges) {
child_N_sum += e.N;
}
for (int rank = 0; rank < 15; ++rank) {
Coord i = std::get<2>(sort_order[rank]);
float soft_N = child_N(i) / child_N_sum;
float p_delta = soft_N - child_P(i);
float p_rel = p_delta / child_P(i);
absl::StrAppendFormat(
&result,
"\n%-5s: % 4.3f % 4.3f %0.3f %0.3f %0.3f %5d %0.4f % 6.5f % 3.2f",
i.ToKgs(), child_action_score[i], child_Q(i), child_U(i), child_P(i),
child_original_P(i), static_cast<int>(child_N(i)), soft_N, p_delta,
p_rel);
}
return result;
}
std::vector<Coord> MctsNode::MostVisitedPath() const {
std::vector<Coord> path;
const auto* node = this;
while (!node->children.empty()) {
Coord c = node->GetMostVisitedMove();
if (node->child_N(c) == 0) {
// In cases where nodes have been added to the tree manually (after the
// user has played a move, loading an SGF game), it's possible that no
// children have been visited. Break before adding a spurious node to the
// path.
break;
}
path.push_back(c);
auto it = node->children.find(c);
if (it == node->children.end()) {
// When we reach the move limit, last node will have children with visit
// counts but no children.
break;
}
node = it->second.get();
}
return path;
}
std::string MctsNode::MostVisitedPathString() const {
std::string result;
const auto* node = this;
for (Coord c : MostVisitedPath()) {
auto it = node->children.find(c);
MG_CHECK(it != node->children.end());
node = it->second.get();
absl::StrAppendFormat(&result, "%s (%d) ==> ", node->move.ToKgs(),
static_cast<int>(node->N()));
}
absl::StrAppendFormat(&result, "Q: %0.5f", node->Q());
return result;
}
void MctsNode::GetMoveHistory(
int num_moves, std::vector<const Position::Stones*>* history) const {
history->clear();
history->reserve(num_moves);
const auto* node = this;
for (int j = 0; j < num_moves; ++j) {
history->push_back(&node->position.stones());
node = node->parent;
if (node == nullptr) {
break;
}
}
}
void MctsNode::InjectNoise(const std::array<float, kNumMoves>& noise) {
// NOTE: our interpretation is to only add dirichlet noise to legal moves.
// Because dirichlet entries are independent we can simply zero and rescale.
float scalar = 0;
for (int i = 0; i < kNumMoves; ++i) {
if (legal_moves[i]) {
scalar += noise[i];
}
}
if (scalar > std::numeric_limits<float>::min()) {
scalar = 1.0 / scalar;
}
for (int i = 0; i < kNumMoves; ++i) {
float scaled_noise = scalar * (legal_moves[i] ? noise[i] : 0);
edges[i].P = 0.75f * edges[i].P + 0.25f * scaled_noise;
}
}
MctsNode* MctsNode::SelectLeaf() {
auto* node = this;
for (;;) {
// If a node has never been evaluated, we have no basis to select a child.
if (!node->HasFlag(Flag::kExpanded)) {
return node;
}
// HACK: if last move was a pass, always investigate double-pass first
// to avoid situations where we auto-lose by passing too early.
if (node->position.previous_move() == Coord::kPass &&
node->child_N(Coord::kPass) == 0) {
node = node->MaybeAddChild(Coord::kPass);
continue;
}
auto child_action_score = node->CalculateChildActionScore();
Coord best_move = ArgMax(child_action_score);
node = node->MaybeAddChild(best_move);
}
}
void MctsNode::IncorporateResults(absl::Span<const float> move_probabilities,
float value, MctsNode* up_to) {
MG_DCHECK(move_probabilities.size() == kNumMoves);
// A finished game should not be going through this code path, it should
// directly call BackupValue on the result of the game.
MG_DCHECK(!game_over());
// If the node has already been selected for the next inference batch, we
// shouldn't 'expand' it again.
if (HasFlag(Flag::kExpanded)) {
return;
}
float policy_scalar = 0;
for (int i = 0; i < kNumMoves; ++i) {
if (legal_moves[i]) {
policy_scalar += move_probabilities[i];
}
}
if (policy_scalar > std::numeric_limits<float>::min()) {
policy_scalar = 1 / policy_scalar;
}
SetFlag(Flag::kExpanded);
for (int i = 0; i < kNumMoves; ++i) {
// Zero out illegal moves, and re-normalize move_probabilities.
float move_prob =
legal_moves[i] ? policy_scalar * move_probabilities[i] : 0;
edges[i].original_P = edges[i].P = move_prob;
// Initialize child Q as current node's value, to prevent dynamics where
// if B is winning, then B will only ever explore 1 move, because the Q
// estimation will be so much larger than the 0 of the other moves.
//
// Conversely, if W is winning, then B will explore all 362 moves before
// continuing to explore the most favorable move. This is a waste of
// search.
//
// The value seeded here acts as a prior, and gets averaged into Q
// calculations.
edges[i].W += value;
}
BackupValue(value, up_to);
}
void MctsNode::IncorporateEndGameResult(float value, MctsNode* up_to) {
MG_DCHECK(game_over() || at_move_limit());
MG_DCHECK(!HasFlag(Flag::kExpanded));
BackupValue(value, up_to);
}
void MctsNode::BackupValue(float value, MctsNode* up_to) {
auto* node = this;
for (;;) {
node->stats->W += value;
++node->stats->N;
if (node == up_to) {
return;
}
node = node->parent;
}
}
void MctsNode::AddVirtualLoss(MctsNode* up_to) {
auto* node = this;
for (;;) {
++node->num_virtual_losses_applied;
node->stats->W += node->position.to_play() == Color::kBlack ? 1 : -1;
if (node == up_to) {
return;
}
node = node->parent;
}
}
void MctsNode::RevertVirtualLoss(MctsNode* up_to) {
auto* node = this;
for (;;) {
--node->num_virtual_losses_applied;
node->stats->W -= node->position.to_play() == Color::kBlack ? 1 : -1;
if (node == up_to) {
return;
}
node = node->parent;
}
}
void MctsNode::PruneChildren(Coord c) {
auto child = std::move(children[c]);
children.clear();
children[c] = std::move(child);
}
std::array<float, kNumMoves> MctsNode::CalculateChildActionScore() const {
float to_play = position.to_play() == Color::kBlack ? 1 : -1;
float U_scale = kPuct * std::sqrt(std::max<float>(1, N() - 1));
std::array<float, kNumMoves> result;
for (int i = 0; i < kNumMoves; ++i) {
result[i] = CalculateSingleMoveChildActionScore(to_play, U_scale, i);
}
return result;
}
MctsNode* MctsNode::MaybeAddChild(Coord c) {
auto it = children.find(c);
if (it == children.end()) {
auto child = absl::make_unique<MctsNode>(this, c);
MctsNode* result = child.get();
children[c] = std::move(child);
return result;
} else {
return it->second.get();
}
}
bool MctsNode::HasPositionBeenPlayedBefore(zobrist::Hash stone_hash) const {
for (const auto* node = this; node != nullptr; node = node->parent) {
if (node->superko_cache != nullptr) {
return node->superko_cache->contains(stone_hash);
} else {
if (node->position.stone_hash() == stone_hash) {
return true;
}
}
}
return false;
}
std::string MctsNode::CalculateTreeStats() const {
// TODO(sethtroisi): Make this return a struct instead of string
long num_nodes = 0;
long num_leaf_nodes = 0;
long depth_sum = 0;
long depth_max = 0;
std::function<void(const MctsNode&, int)> traverse =
[&](const MctsNode& node, int depth) {
num_nodes += 1;
num_leaf_nodes += node.N() <= 1;
depth_sum += depth;
if (depth > depth_max) {
depth_max = depth;
}
for (const auto& child : node.children) {
traverse(*child.second.get(), depth + 1);
}
};
traverse(*this, 0);
return absl::StrFormat(
"%d nodes, %d leaf, %.1f average children\n"
"%.1f average depth, %d max depth\n",
num_nodes, num_leaf_nodes,
1.0f * num_nodes / std::max(1L, num_nodes - num_leaf_nodes),
1.0f * depth_sum / num_nodes, depth_max);
}
} // namespace minigo