forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcts_node_test.cc
More file actions
492 lines (420 loc) · 15.5 KB
/
Copy pathmcts_node_test.cc
File metadata and controls
492 lines (420 loc) · 15.5 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
// 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 <array>
#include <set>
#include "absl/memory/memory.h"
#include "cc/position.h"
#include "cc/random.h"
#include "cc/test_utils.h"
#include "cc/zobrist.h"
#include "gtest/gtest.h"
namespace minigo {
namespace {
static constexpr char kAlmostDoneBoard[] = R"(
.XO.XO.OO
X.XXOOOO.
XXXXXOOOO
XXXXXOOOO
.XXXXOOO.
XXXXXOOOO
.XXXXOOO.
XXXXXOOOO
XXXXOOOOO)";
// Test puct and child action score calculation
TEST(MctsNodeTest, UpperConfidenceBound) {
float epsilon = 1e-7;
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.02;
}
MctsNode::EdgeStats root_stats;
MctsNode root(&root_stats, TestablePosition("", Color::kBlack));
auto* leaf = root.SelectLeaf();
EXPECT_EQ(&root, leaf);
leaf->IncorporateResults(probs, 0.5, &root);
// 0.02 are normalized to 1/82
EXPECT_NEAR(1.0 / 82, root.child_P(0), epsilon);
EXPECT_NEAR(1.0 / 82, root.child_P(1), epsilon);
double puct_policy = kPuct * 1.0 / 82;
ASSERT_EQ(1, root.N());
EXPECT_NEAR(puct_policy * std::sqrt(1) / (1 + 0), root.child_U(0), epsilon);
leaf = root.SelectLeaf();
leaf->IncorporateResults(probs, 0.5, &root);
EXPECT_NE(&root, leaf);
EXPECT_EQ(&root, leaf->parent);
EXPECT_EQ(Coord(0), leaf->move);
// With the first child expanded.
ASSERT_EQ(2, root.N());
EXPECT_NEAR(puct_policy * std::sqrt(1) / (1 + 1), root.child_U(0), epsilon);
EXPECT_NEAR(puct_policy * std::sqrt(1) / (1 + 0), root.child_U(1), epsilon);
auto* leaf2 = root.SelectLeaf();
EXPECT_NE(&root, leaf2);
EXPECT_EQ(&root, leaf2->parent);
EXPECT_EQ(Coord(1), leaf2->move);
leaf2->IncorporateResults(probs, 0.5, &root);
// With the 2nd child expanded.
ASSERT_EQ(3, root.N());
EXPECT_NEAR(puct_policy * std::sqrt(2) / (1 + 1), root.child_U(0), epsilon);
EXPECT_NEAR(puct_policy * std::sqrt(2) / (1 + 1), root.child_U(1), epsilon);
EXPECT_NEAR(puct_policy * std::sqrt(2) / (1 + 0), root.child_U(2), epsilon);
}
// Verifies that no matter who is to play, when we know nothing else, the priors
// should be respected, and the same move should be picked.
TEST(MctsNodeTest, ActionFlipping) {
Random rnd(1);
std::array<float, kNumMoves> probs;
std::uniform_real_distribution<float> dist(0.02, 0.021);
for (float& prob : probs) {
prob = rnd();
}
MctsNode::EdgeStats black_stats, white_stats;
MctsNode black_root(&black_stats, TestablePosition("", Color::kBlack));
MctsNode white_root(&white_stats, TestablePosition("", Color::kWhite));
black_root.SelectLeaf()->IncorporateResults(probs, 0, &black_root);
white_root.SelectLeaf()->IncorporateResults(probs, 0, &white_root);
auto* black_leaf = black_root.SelectLeaf();
auto* white_leaf = white_root.SelectLeaf();
EXPECT_EQ(black_leaf->move, white_leaf->move);
EXPECT_EQ(black_root.CalculateChildActionScore(),
white_root.CalculateChildActionScore());
}
// Verfies that SelectLeaf chooses the child with the highest action score.
TEST(MctsNodeTest, SelectLeaf) {
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.02;
}
Coord c = Coord::FromKgs("D9");
probs[c] = 0.4;
MctsNode::EdgeStats root_stats;
auto board = TestablePosition(kAlmostDoneBoard, Color::kWhite);
MctsNode root(&root_stats, board);
root.SelectLeaf()->IncorporateResults(probs, 0, &root);
EXPECT_EQ(Color::kWhite, root.position.to_play());
auto* leaf = root.SelectLeaf();
EXPECT_EQ(root.children[c].get(), leaf);
}
// Verifies IncorporateResults and BackupValue.
TEST(MctsNodeTest, BackupIncorporateResults) {
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.02;
}
MctsNode::EdgeStats root_stats;
auto board = TestablePosition(kAlmostDoneBoard, Color::kWhite);
MctsNode root(&root_stats, board);
root.SelectLeaf()->IncorporateResults(probs, 0, &root);
auto* leaf = root.SelectLeaf();
leaf->IncorporateResults(probs, -1, &root); // white wins!
// Root was visited twice: first at the root, then at this child.
EXPECT_EQ(2, root.N());
// Root has 0 as a prior and two visits with value 0, -1.
EXPECT_FLOAT_EQ(-1.0 / 3, root.Q()); // average of 0, 0, -1
// Leaf should have one visit
EXPECT_EQ(1, root.child_N(leaf->move));
EXPECT_EQ(1, leaf->N());
// And that leaf's value had its parent's Q (0) as a prior, so the Q
// should now be the average of 0, -1
EXPECT_FLOAT_EQ(-0.5, root.child_Q(leaf->move));
EXPECT_FLOAT_EQ(-0.5, leaf->Q());
// We're assuming that SelectLeaf() returns a leaf like:
// root
// |
// leaf
// |
// leaf2
// which happens in this test because root is W to play and leaf was a W win.
EXPECT_EQ(Color::kWhite, root.position.to_play());
auto* leaf2 = root.SelectLeaf();
ASSERT_EQ(leaf, leaf2->parent);
leaf2->IncorporateResults(probs, -0.2, &root); // another white semi-win
EXPECT_EQ(3, root.N());
// average of 0, 0, -1, -0.2
EXPECT_FLOAT_EQ(-0.3, root.Q());
EXPECT_EQ(2, leaf->N());
EXPECT_EQ(1, leaf2->N());
// average of 0, -1, -0.2
EXPECT_FLOAT_EQ(root.child_Q(leaf->move), leaf->Q());
EXPECT_FLOAT_EQ(-0.4, leaf->Q());
// average of -1, -0.2
EXPECT_FLOAT_EQ(-0.6, leaf->child_Q(leaf2->move));
EXPECT_FLOAT_EQ(-0.6, leaf2->Q());
}
TEST(MctsNodeTest, DoNotExplorePastFinish) {
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.02;
}
MctsNode::EdgeStats root_stats;
auto board = TestablePosition(kAlmostDoneBoard, Color::kWhite);
MctsNode root(&root_stats, board);
root.SelectLeaf()->IncorporateResults(probs, 0, &root);
auto* first_pass = root.MaybeAddChild(Coord::kPass);
first_pass->IncorporateResults(probs, 0, &root);
auto* second_pass = first_pass->MaybeAddChild(Coord::kPass);
EXPECT_DEATH(second_pass->IncorporateResults(probs, 0, &root), "game_over");
float value = second_pass->position.CalculateScore(0) > 0 ? 1 : -1;
second_pass->IncorporateEndGameResult(value, &root);
auto* node_to_explore = second_pass->SelectLeaf();
// should just stop exploring at the end position.
EXPECT_EQ(second_pass, node_to_explore);
}
TEST(MctsNodeTest, AddChild) {
MctsNode::EdgeStats root_stats;
TestablePosition board("");
MctsNode root(&root_stats, board);
Coord c = Coord::FromKgs("B9");
auto* child = root.MaybeAddChild(c);
EXPECT_EQ(1, root.children.count(c));
EXPECT_EQ(&root, child->parent);
EXPECT_EQ(child->move, c);
}
TEST(MctsNodeTest, AddChildIdempotency) {
MctsNode::EdgeStats root_stats;
TestablePosition board("");
MctsNode root(&root_stats, board);
Coord c = Coord::FromKgs("B9");
auto* child = root.MaybeAddChild(c);
EXPECT_EQ(1, root.children.count(c));
EXPECT_EQ(1, root.children.size());
auto* child2 = root.MaybeAddChild(c);
EXPECT_EQ(child, child2);
EXPECT_EQ(1, root.children.count(c));
EXPECT_EQ(1, root.children.size());
}
TEST(MctsNodeTest, NeverSelectIllegalMoves) {
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.02;
}
// let's say the NN were to accidentally put a high weight on an illegal move
probs[1] = 0.99;
MctsNode::EdgeStats root_stats;
auto board = TestablePosition(kAlmostDoneBoard, Color::kWhite);
MctsNode root(&root_stats, board);
root.SelectLeaf()->IncorporateResults(probs, 0, &root);
// and let's say the root were visited a lot of times, which pumps up the
// action score for unvisited moves...
root.stats->N = 100000;
for (int i = 0; i < kNumMoves; ++i) {
if (root.position.ClassifyMove(i) != Position::MoveType::kIllegal) {
root.edges[i].N = 10000;
}
}
// this should not throw an error...
auto* leaf = root.SelectLeaf();
// the returned leaf should not be the illegal move
EXPECT_NE(1, leaf->move);
// and even after injecting noise, we should still not select an illegal move
Random rnd(1);
for (int i = 0; i < 10; ++i) {
std::array<float, kNumMoves> noise;
rnd.Uniform(0, 1, &noise);
root.InjectNoise(noise);
leaf = root.SelectLeaf();
EXPECT_NE(1, leaf->move);
}
}
TEST(MctsNodeTest, DontTraverseUnexpandedChild) {
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.001;
}
// Make one move really likely so that tree search goes down that path twice
// even with a virtual loss.
probs[17] = 0.99;
MctsNode::EdgeStats root_stats;
auto board = TestablePosition(kAlmostDoneBoard, Color::kWhite);
MctsNode root(&root_stats, board);
root_stats.N = 5;
root.SelectLeaf()->IncorporateResults(probs, 0, &root);
auto* leaf1 = root.SelectLeaf();
EXPECT_EQ(17, leaf1->move);
leaf1->AddVirtualLoss(&root);
auto* leaf2 = root.SelectLeaf();
EXPECT_EQ(leaf1, leaf2); // assert we didn't go below the first leaf.
}
// Verifies that action score is used as a tie-breaker to choose between moves
// with the same visit count when selecting the best one.
// This test uses raw indices here instead of KGS coords to make it clear that
// without using action score as a tie-breaker, the move with the lower index
// would be selected by GetMostVisitedMove.
TEST(MctsNodeTest, GetMostVisitedPath) {
// Give two moves a higher probability.
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.001;
}
probs[15] = 0.5;
probs[16] = 0.6;
MctsNode::EdgeStats root_stats;
auto board = TestablePosition("", Color::kBlack);
MctsNode root(&root_stats, board);
root.SelectLeaf()->IncorporateResults(probs, 0, &root);
// We should select the highest probabilty first.
auto* leaf1 = root.SelectLeaf();
EXPECT_EQ(Coord(16), leaf1->move);
leaf1->AddVirtualLoss(&root);
// Then the second highest probability.
auto* leaf2 = root.SelectLeaf();
EXPECT_EQ(Coord(15), leaf2->move);
leaf1->RevertVirtualLoss(&root);
// Both Coord(15) and Coord(16) have visit counts of 1.
// Coord(16) should be selected because of it's higher action score.
EXPECT_EQ(Coord(16), root.GetMostVisitedMove());
}
// Verifies that even when one move is hugely more likely than all the others,
// SelectLeaf will eventually start exploring other moves given enough
// iterations.
TEST(MctsNodeTest, TestSelectLeaf) {
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.001;
}
probs[17] = 0.99;
MctsNode::EdgeStats root_stats;
auto board = TestablePosition(kAlmostDoneBoard, Color::kWhite);
MctsNode root(&root_stats, board);
root.SelectLeaf()->IncorporateResults(probs, 0, &root);
std::set<MctsNode*> leaves;
auto* leaf = root.SelectLeaf();
EXPECT_EQ(17, leaf->move);
leaf->AddVirtualLoss(&root);
leaves.insert(leaf);
for (int i = 0; i < 1000; ++i) {
leaf = root.SelectLeaf();
leaf->AddVirtualLoss(&root);
leaves.insert(leaf);
}
// We should have selected at least 2 leaves.
EXPECT_LE(2, leaves.size());
}
TEST(MctsNodeTest, NormalizeTest) {
// Generate probability with sum of policy less than 1
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.001;
}
// Five times larger to test normalization
probs[17] = 0.005;
probs[18] = 0;
MctsNode::EdgeStats root_stats;
auto board = TestablePosition("");
MctsNode root(&root_stats, board);
root.IncorporateResults(probs, 0, &root);
// Adjust for the one value that is five times larger and one missing value.
float normalized = 1.0 / (kNumMoves - 1 + 4);
for (int i = 0; i < kNumMoves; ++i) {
if (i == 17) {
EXPECT_FLOAT_EQ(5 * normalized, root.child_P(i));
} else if (i == 18) {
EXPECT_FLOAT_EQ(0, root.child_P(i));
} else {
EXPECT_FLOAT_EQ(normalized, root.child_P(i));
}
}
}
TEST(MctsNodeTest, InjectNoiseOnlyLegalMoves) {
// Give moves a uniform policy value.
std::array<float, kNumMoves> probs;
for (float& prob : probs) {
prob = 0.02;
}
MctsNode::EdgeStats root_stats;
auto board = TestablePosition(kAlmostDoneBoard, Color::kWhite);
MctsNode root(&root_stats, board);
root.IncorporateResults(probs, 0, &root);
// kAlmostDoneBoard has 6 legal moves including pass.
float uniform_policy = 1.0 / 6;
for (int i = 0; i < kNumMoves; ++i) {
if (root.legal_moves[i]) {
EXPECT_FLOAT_EQ(uniform_policy, root.edges[i].P);
} else {
EXPECT_FLOAT_EQ(0, root.edges[i].P);
}
}
// and even after injecting noise, we should still not select an illegal move
Random rnd(1);
std::array<float, kNumMoves> noise;
rnd.Uniform(0, 1, &noise);
root.InjectNoise(noise);
for (int i = 0; i < kNumMoves; ++i) {
if (root.legal_moves[i]) {
EXPECT_LT(0.75 * uniform_policy, root.edges[i].P);
EXPECT_GT(0.75 * uniform_policy + 0.25, root.edges[i].P);
} else {
EXPECT_FLOAT_EQ(0, root.edges[i].P);
}
}
}
TEST(MctsNodeTest, TestSuperko) {
// clang-format off
std::vector<std::string> non_ko_moves = {
// Some moves at the top edge of the board that don't interfere with the kos
// at the bottom of the board.
"A9", "B9", "C9", "D9", "E9", "F9", "G9", "H9", "J9",
"A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8", "J8",
};
std::vector<std::string> ko_moves = {
// Create two kos threats on the bottom edge of the board:
// .........
// .XO...OX.
// X.XO.O.OX
"A1", "F1", "B2", "G2", "C1", "H1", "J1", "D1", "H2", "C2",
// Capture one ko.
"G1", "B1", "pass", "H1",
};
// clang-format on
// Superko detection inserts caches into the tree at regularly spaced depths.
// For nodes that don't have a superko dectection cache, a linear search up
// the tree, comparing the stone hashes at each node is performed until a
// superko cache is hit.
// In order to verify that there isn't a bug related to the linear-scan &
// cache-lookup pair of checks, we run the superko test multiple times, with a
// different number of moves played at the start each time.
for (size_t iteration = 0; iteration < non_ko_moves.size(); ++iteration) {
std::vector<std::unique_ptr<MctsNode>> nodes;
MctsNode::EdgeStats root_stats;
BoardVisitor bv;
GroupVisitor gv;
nodes.push_back(absl::make_unique<MctsNode>(
&root_stats, Position(&bv, &gv, Color::kBlack)));
for (size_t move_idx = 0; move_idx < iteration; ++move_idx) {
Coord c = Coord::FromKgs(non_ko_moves[move_idx]);
ASSERT_TRUE(nodes.back()->legal_moves[c]);
nodes.push_back(absl::make_unique<MctsNode>(nodes.back().get(), c));
}
for (const auto& move : ko_moves) {
Coord c = Coord::FromKgs(move);
ASSERT_TRUE(nodes.back()->legal_moves[c]);
nodes.push_back(absl::make_unique<MctsNode>(nodes.back().get(), c));
}
// Without superko checking, it should look like capturing the second ko at
// C1 is valid.
auto c1 = Coord::FromKgs("C1");
EXPECT_EQ(Position::MoveType::kCapture,
nodes.back()->position.ClassifyMove(c1));
// When checking superko however, playing at C1 is not legal because it
// repeats a position.
EXPECT_FALSE(nodes.back()->legal_moves[c1]);
}
}
} // namespace
} // namespace minigo
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
::minigo::zobrist::Init(614944751);
return RUN_ALL_TESTS();
}