diff --git a/src/mcts.cpp b/src/mcts.cpp index ee0d025..36c5791 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -240,7 +240,10 @@ void Searcher::verify_tree() const { next.make_move(m); stack.push_back({node.first_child + c, next}); } - if (!ok || node.visits < child_visits + 1) { + // The root is expanded before its first visit, so it may have exactly + // as many visits as its children. + const u64 own_visit = item.idx == Tree::root() ? 0 : 1; + if (!ok || node.visits < child_visits + own_visit) { std::cerr << "TREE CORRUPT at node " << item.idx << " (" << item.pos.fen() << ") ok=" << ok << " visits=" << node.visits << " child_visits=" << child_visits << " children=" << int(node.num_children) << " legal=" << legal.size() @@ -252,7 +255,7 @@ void Searcher::verify_tree() const { } #endif -float Searcher::expand(u32 idx, const Position& pos, int depth) { +float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) { Node& node = tree_[idx]; MoveList moves; @@ -269,12 +272,21 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) { return 0.0f; } + return eval::value(pos); +} + +bool Searcher::expand(u32 idx, const Position& pos) { + Node& node = tree_[idx]; + + MoveList moves; + generate_legal(pos, moves); + const u32 first = tree_.alloc(u32(moves.size())); if (!first) { // Out of memory: this time the node stays a leaf; the tree is collected // right after this playout, so the next visit can expand it. gc_pending_ = true; - return eval::value(pos); + return false; } float priors[256]; @@ -290,8 +302,7 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) { node.first_child = first; node.num_children = u8(moves.size()); node.state = EXPANDED; - - return eval::value(pos); + return true; } u32 Searcher::select_child(u32 idx) const { @@ -346,8 +357,16 @@ void Searcher::playout() { break; } if (node.state == UNEXPANDED) { - value = expand(idx, pos, depth); - break; + // Most leaves are only ever visited once, so a first visit just + // evaluates; children (moves, priors, memory) come on the second. + if (node.visits == 0) { + value = evaluate_leaf(idx, pos, depth); + break; + } + if (!expand(idx, pos)) { + value = eval::value(pos); + break; + } } idx = select_child(idx); @@ -452,8 +471,15 @@ SearchResult Searcher::search(const Position& root, const std::vector& hist return result; } + // The root needs its children before the first playout, so that even a + // single playout leaves a move to play. + if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) { + tree_.clear(); + gc_pending_ = false; + expand(Tree::root(), root); + } + i64 last_info = 0; - // Always run at least one playout so the root gets expanded. do { playout(); if (gc_pending_) { diff --git a/src/mcts.h b/src/mcts.h index 4898b85..e5f98c4 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -128,7 +128,11 @@ private: static constexpr double GC_KEEP_FRACTION = 0.5; void playout(); - float expand(u32 idx, const Position& pos, int depth); + // First visit of a node: settles terminal states, otherwise evaluates. + float evaluate_leaf(u32 idx, const Position& pos, int depth); + // Second visit: creates the children with their priors. False when the + // arena is full (a collection is then scheduled). + bool expand(u32 idx, const Position& pos); u32 select_child(u32 idx) const; bool is_repetition(const Position& pos) const; // Finds the node for `root` in the previous tree and makes it the root.