forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcts_player_test.cc
More file actions
377 lines (319 loc) · 10.9 KB
/
Copy pathmcts_player_test.cc
File metadata and controls
377 lines (319 loc) · 10.9 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
// 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_player.h"
#include <memory>
#include "absl/memory/memory.h"
#include "cc/algorithm.h"
#include "cc/constants.h"
#include "cc/dual_net.h"
#include "cc/test_utils.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)";
class FakeNet : public DualNet {
public:
FakeNet(absl::Span<const float> priors, float value) : value_(value) {
if (!priors.empty()) {
assert(priors.size() == kNumMoves);
for (int i = 0; i < kNumMoves; ++i) {
priors_[i] = priors[i];
}
} else {
for (auto& prior : priors_) {
prior = 1.0 / kNumMoves;
}
}
}
void RunMany(absl::Span<const BoardFeatures* const> features,
absl::Span<Output> outputs, Random* rnd) override {
for (auto& output : outputs) {
output.policy = priors_;
output.value = value_;
}
}
private:
std::array<float, kNumMoves> priors_;
float value_;
};
class TestablePlayer : public MctsPlayer {
public:
explicit TestablePlayer(const Options& options)
: MctsPlayer(absl::make_unique<FakeNet>(absl::Span<const float>(), 0),
options) {}
TestablePlayer(absl::Span<const float> fake_priors, float fake_value,
const Options& options)
: MctsPlayer(absl::make_unique<FakeNet>(fake_priors, fake_value),
options) {}
using MctsPlayer::PickMove;
using MctsPlayer::PlayMove;
using MctsPlayer::rnd;
using MctsPlayer::Run;
using MctsPlayer::TreeSearch;
std::array<float, kNumMoves> Noise() {
std::array<float, kNumMoves> noise;
rnd()->Dirichlet(kDirichletAlpha, &noise);
return noise;
}
};
std::unique_ptr<TestablePlayer> CreateBasicPlayer(MctsPlayer::Options options) {
// Always use a deterministic random seed.
options.random_seed = 17;
auto player = absl::make_unique<TestablePlayer>(options);
auto* first_node = player->root()->SelectLeaf();
auto output = player->Run(&player->root()->features);
first_node->IncorporateResults(output.policy, output.value, player->root());
return player;
}
std::unique_ptr<TestablePlayer> CreateAlmostDonePlayer(
MctsPlayer::Options options, int n) {
// Always use a deterministic random seed.
options.random_seed = 17;
std::array<float, kNumMoves> probs;
for (auto& p : probs) {
p = 0.001;
}
probs[Coord(0, 2)] = 0.2;
probs[Coord(0, 3)] = 0.2;
probs[Coord(0, 4)] = 0.2;
probs[Coord::kPass] = 0.2;
auto player = absl::make_unique<TestablePlayer>(probs, 0, options);
auto board = TestablePosition(kAlmostDoneBoard, 2.5, Color::kBlack, n);
player->InitializeGame(board);
return player;
}
TEST(MctsPlayerTest, InjectNoise) {
MctsPlayer::Options options;
auto player = CreateBasicPlayer(options);
auto* root = player->root();
// FakeNet should return normalized priors.
float sum_P = 0;
for (int i = 0; i < kNumMoves; ++i) {
sum_P += root->child_P(i);
}
EXPECT_NEAR(1, sum_P, 0.000001);
for (int i = 0; i < kNumMoves; ++i) {
EXPECT_EQ(root->child_U(0), root->child_U(i));
}
root->InjectNoise(player->Noise());
// Priors should still be normalized after injecting noise.
sum_P = 0;
for (int i = 0; i < kNumMoves; ++i) {
sum_P += root->child_P(i);
}
EXPECT_NEAR(1, sum_P, 0.000001);
// With Dirichelet noise, majority of density should be in one node.
int i = ArgMax(root->edges, MctsNode::CmpP);
float max_P = root->child_P(i);
EXPECT_GT(max_P, 3.0 / kNumMoves);
}
// Verify that with soft pick disabled, the player will always choose the best
// move.
TEST(MctsPlayerTest, PickMoveArgMax) {
MctsPlayer::Options options;
options.soft_pick = false;
auto player = CreateBasicPlayer(options);
auto* root = player->root();
root->edges[Coord(2, 0)].N = 10;
root->edges[Coord(1, 0)].N = 5;
root->edges[Coord(3, 0)].N = 1;
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(Coord(2, 0), player->PickMove());
}
}
// Verify that with soft pick enabled, the player will choose moves early in the
// game proportionally to their visit count.
TEST(MctsPlayerTest, PickMoveSoft) {
MctsPlayer::Options options;
options.soft_pick = true;
auto player = CreateBasicPlayer(options);
auto* root = player->root();
root->edges[Coord(2, 0)].N = 10;
root->edges[Coord(1, 0)].N = 5;
root->edges[Coord(3, 0)].N = 1;
int count_1_0 = 0;
int count_2_0 = 0;
int count_3_0 = 0;
for (int i = 0; i < 160; ++i) {
auto move = player->PickMove();
if (move == Coord(1, 0)) {
++count_1_0;
} else if (move == Coord(2, 0)) {
++count_2_0;
} else {
ASSERT_EQ(Coord(3, 0), move);
++count_3_0;
}
}
EXPECT_NEAR(100, count_2_0, 5);
EXPECT_NEAR(50, count_1_0, 5);
EXPECT_NEAR(10, count_3_0, 5);
}
TEST(MctsPlayerTest, DontPassIfLosing) {
auto player = CreateAlmostDonePlayer({}, 0);
auto* root = player->root();
EXPECT_EQ(-0.5, root->position.CalculateScore());
for (int i = 0; i < 20; ++i) {
player->TreeSearch(1);
}
// Search should converge on D9 as only winning move.
auto best_move = ArgMax(root->edges, MctsNode::CmpN);
ASSERT_EQ(best_move, Coord::FromKgs("D9"));
// D9 should have a positive value.
EXPECT_LT(0, root->child_Q(best_move));
EXPECT_LE(20, root->N());
// Passing should be ineffective.
EXPECT_GT(0, root->child_Q(Coord::kPass));
// No virtual losses should be pending.
EXPECT_EQ(0, CountPendingVirtualLosses(root));
}
TEST(MctsPlayerTest, ParallelTreeSearch) {
auto player = CreateAlmostDonePlayer({}, 0);
auto* root = player->root();
// Initialize the tree so that the root node has populated children.
player->TreeSearch(1);
// Virtual losses should enable multiple searches to happen simultaneously
// without throwing an error...
for (int i = 0; i < 5; ++i) {
player->TreeSearch(5);
}
// Search should converge on D9 as only winning move.
auto best_move = ArgMax(root->edges, MctsNode::CmpN);
EXPECT_EQ(Coord::FromString("D9"), best_move);
// D9 should have a positive value.
EXPECT_LT(0, root->child_Q(best_move));
EXPECT_LE(20, root->N());
// Passing should be ineffective.
EXPECT_GT(0, root->child_Q(Coord::kPass));
// No virtual losses should be pending.
EXPECT_EQ(0, CountPendingVirtualLosses(root));
}
TEST(MctsPlayerTest, RidiculouslyParallelTreeSearch) {
auto player = CreateAlmostDonePlayer({}, 0);
auto* root = player->root();
for (int i = 0; i < 10; ++i) {
// Test that an almost complete game will tree search with
// # parallelism > # legal moves.
player->TreeSearch(50);
}
// No virtual losses should be pending.
EXPECT_EQ(0, CountPendingVirtualLosses(root));
}
TEST(MctsPlayerTest, LongGameTreeSearch) {
auto player = CreateAlmostDonePlayer({}, kMaxSearchDepth - 2);
// Test that an almost complete game.
for (int i = 0; i < 10; ++i) {
player->TreeSearch(8);
}
EXPECT_EQ(0, CountPendingVirtualLosses(player->root()));
EXPECT_LT(0, player->root()->Q());
}
TEST(MctsPlayerTest, ColdStartParallelTreeSearch) {
MctsPlayer::Options options;
options.random_seed = 17;
auto player = absl::make_unique<TestablePlayer>(absl::Span<const float>(),
0.17, options);
auto* root = player->root();
// Test that parallel tree search doesn't trip on an empty tree.
EXPECT_EQ(0, root->N());
EXPECT_EQ(MctsNode::State::kCollapsed, root->state);
player->TreeSearch(4);
EXPECT_EQ(0, CountPendingVirtualLosses(root));
// Even though we attempted to run 4 parallel searchs, the root should have
// only been selected once (the subsequent calls to SelectLeaf should have
// returned null).
EXPECT_EQ(1, root->N());
// 0.085 = average(0, 0.17), since 0 is the prior on the root.
EXPECT_NEAR(0.085, root->Q(), 0.01);
}
TEST(MctsPlayerTest, TreeSearchFailsafe) {
// Test that the failsafe works correctly. It can trigger if the MCTS
// repeatedly visits a finished game state.
std::array<float, kNumMoves> probs;
for (auto& p : probs) {
p = 0.001;
}
probs[Coord::kPass] = 1; // Make the dummy net always want to pass.
MctsPlayer::Options options;
options.random_seed = 17;
auto player = absl::make_unique<TestablePlayer>(probs, 0, options);
auto board = TestablePosition("");
board.PlayMove("pass");
player->InitializeGame(board);
player->TreeSearch(1);
EXPECT_EQ(0, CountPendingVirtualLosses(player->root()));
}
// When presented with a situation where the last move was a pass, and we have
// to decide whether to pass, it should be the first thing we check, but not
// more than that.
TEST(MctsPlayerTest, OnlyCheckGameEndOnce) {
BoardVisitor bv;
GroupVisitor gv;
Position position(&bv, &gv, kDefaultKomi, Color::kBlack);
position.PlayMove({3, 3}); // B plays.
position.PlayMove({3, 4}); // W plays.
position.PlayMove({4, 3}); // B plays.
// W passes. If B passes too, B would lose by komi..
position.PlayMove(Coord::kPass);
auto player = absl::make_unique<TestablePlayer>(MctsPlayer::Options());
player->InitializeGame(position);
auto* root = player->root();
// Initialize the root
player->TreeSearch(1);
// Explore a child - should be a pass move.
player->TreeSearch(1);
EXPECT_EQ(1, root->child_N(Coord::kPass));
player->TreeSearch(1);
// Check that we didn't visit the pass node any more times.
EXPECT_EQ(1, root->child_N(Coord::kPass));
}
TEST(MctsPlayerTest, ExtractDataNormalEnd) {
auto player = absl::make_unique<TestablePlayer>(MctsPlayer::Options());
player->TreeSearch(1);
player->PlayMove(Coord::kPass);
player->TreeSearch(1);
player->PlayMove(Coord::kPass);
auto* root = player->root();
EXPECT_TRUE(root->position.is_game_over());
EXPECT_EQ(Color::kBlack, root->position.to_play());
ASSERT_EQ(2, player->history().size());
// White wins by komi
EXPECT_EQ(-1, player->result());
EXPECT_EQ("W+7.5", player->result_string());
}
TEST(MctsPlayerTest, ExtractDataResignEnd) {
auto player = absl::make_unique<TestablePlayer>(MctsPlayer::Options());
player->TreeSearch(1);
player->PlayMove({0, 0});
player->TreeSearch(1);
player->PlayMove(Coord::kPass);
player->TreeSearch(1);
player->PlayMove(Coord::kResign);
auto* root = player->root();
// Black is winning on the board.
EXPECT_LT(0, root->position.CalculateScore());
EXPECT_EQ(-1, player->result());
EXPECT_EQ("W+R", player->result_string());
}
} // namespace
} // namespace minigo