diff --git a/src/mcts.cpp b/src/mcts.cpp index fd51f11..3c10770 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -83,7 +83,8 @@ size_t Tree::compact(u32 new_root, u32 min_visits) { if (idx != new_root && node.visits < min_visits) { node.first_child = 0; node.num_children = 0; - node.state = UNEXPANDED; + // A proven node stays proven without its children. + if (node.state == EXPANDED) node.state = UNEXPANDED; continue; } blocks.emplace_back(node.first_child, node.num_children); @@ -187,6 +188,8 @@ bool Searcher::best_move_settled(i64 ms, i64 limit_ms) const { bool Searcher::should_stop(const SearchLimits& limits) const { if (stop_flag.load(std::memory_order_relaxed)) return true; + // A proven root has nothing left to search (unless told to go on). + if (!limits.infinite && is_proven(tree_[Tree::root()].state)) return true; if (limits.nodes && iterations_ >= limits.nodes) return true; if (limits.depth && iterations_) { // "depth" is the mean playout depth. Solved positions may never get @@ -229,7 +232,7 @@ size_t Searcher::reuse_tree(const Position& root) { Position pos = prev_root_; for (size_t i = prev_history_.size(); i < history_.size(); i++) { const Node& node = tree_[idx]; - if (node.state != EXPANDED) return 0; + if (!node.first_child) return 0; u32 next = 0; for (u32 c = 0; c < node.num_children && !next; c++) { @@ -246,7 +249,7 @@ size_t Searcher::reuse_tree(const Position& root) { // A node that was a draw by repetition or the 50-move rule inside the old // tree can be a real position to play from now; start such a root afresh. - if (pos.key != root.key || tree_[idx].state != EXPANDED) return 0; + if (pos.key != root.key || !tree_[idx].first_child) return 0; return tree_.reroot(idx); } @@ -381,7 +384,11 @@ u32 Searcher::select_child(u32 idx) const { for (u32 i = 0; i < node.num_children; i++) { const u32 c = node.first_child + i; const Node& child = tree_[c]; - const float q = child.visits ? child.q : fpu; + // A proven child counts as the result it is proven to be. + const float q = child.state == PROVEN_WIN ? -1.0f + : is_lost(child.state) ? 1.0f + : child.visits ? child.q + : fpu; const float score = q + explore * child.prior() / float(1 + child.visits); if (score > best_score) { best_score = score; @@ -391,6 +398,36 @@ u32 Searcher::select_child(u32 idx) const { return best; } +void Searcher::prove(const std::vector& path) { + // Mate proving (Monty, cpu-mcts-tutorial), from the end of the playout up: + // a node with a lost child is won, a node whose children are all won is + // lost. Stops at the first node whose state does not change. + for (size_t i = path.size() - 1; i > 0; i--) { + Node& parent = tree_[path[i - 1]]; + if (parent.state != EXPANDED) return; + const u8 child_state = tree_[path[i]].state; + if (!is_lost(child_state) && child_state != PROVEN_WIN) return; + + int shortest_win = 256, longest_loss = -1; + bool all_won = true; + for (u32 c = 0; c < parent.num_children; c++) { + const Node& child = tree_[parent.first_child + c]; + if (is_lost(child.state)) shortest_win = std::min(shortest_win, child.proof); + if (child.state == PROVEN_WIN) longest_loss = std::max(longest_loss, child.proof); + else all_won = false; + } + if (shortest_win < 256) { + parent.state = PROVEN_WIN; + parent.proof = u8(std::min(shortest_win + 1, 255)); + } else if (all_won) { + parent.state = PROVEN_LOSS; + parent.proof = u8(std::min(longest_loss + 1, 255)); + } else { + return; + } + } +} + void Searcher::playout() { Position pos = root_pos_; history_.resize(root_history_size_); @@ -403,10 +440,14 @@ void Searcher::playout() { for (;;) { const Node& node = tree_[idx]; - if (node.state == TERMINAL_LOSS) { + if (is_lost(node.state)) { value = -1.0f; break; } + if (node.state == PROVEN_WIN) { + value = 1.0f; + break; + } if (node.state == TERMINAL_DRAW) { value = 0.0f; break; @@ -446,6 +487,8 @@ void Searcher::playout() { if (i) butterfly_.update(root_pos_.stm ^ int((i - 1) & 1), Move::from_raw(node.move), bonus); } + prove(path_); + iterations_++; depth_sum_ += u64(depth); seldepth_ = std::max(seldepth_, depth); @@ -462,8 +505,18 @@ u32 Searcher::best_child(u32 idx) const { } const Node& a = tree_[c]; const Node& b = tree_[best]; - // Most visits wins; ties go to the better Q, then to the higher prior. - if (a.visits != b.visits ? a.visits > b.visits : a.visits ? a.q > b.q : a.policy > b.policy) best = c; + // A proven mate first (the shortest), a proven loss last (the longest); + // otherwise most visits wins, ties go to the better Q, then the higher prior. + const auto rank = [](const Node& n) { return is_lost(n.state) ? 2 : n.state == PROVEN_WIN ? 0 : 1; }; + if (rank(a) != rank(b)) { + if (rank(a) > rank(b)) best = c; + } else if (rank(a) == 2) { + if (a.proof < b.proof) best = c; + } else if (rank(a) == 0) { + if (a.proof > b.proof) best = c; + } else if (a.visits != b.visits ? a.visits > b.visits : a.visits ? a.q > b.q : a.policy > b.policy) { + best = c; + } } return best; } @@ -478,7 +531,13 @@ void Searcher::print_info() const { << seldepth_; const float q = best && tree_[best].visits ? tree_[best].q : -root.q; - out << " score cp " << eval::q_to_cp(q); + const u8 best_state = best ? tree_[best].state : u8(UNEXPANDED); + if (is_lost(best_state)) + out << " score mate " << (tree_[best].proof + 2) / 2; + else if (best_state == PROVEN_WIN) + out << " score mate -" << (tree_[best].proof + 1) / 2; + else + out << " score cp " << eval::q_to_cp(q); out << " nodes " << iterations_ << " nps " << (ms ? iterations_ * 1000 / u64(ms) : iterations_) << " time " << ms; out << " hashfull " << (tree_.used() * 1000 / tree_.capacity()); @@ -574,6 +633,10 @@ SearchResult Searcher::search(const Position& root, const std::vector& hist // The root needs its children before the first playout, so that even a // single playout leaves a move to play. + // A root proven in an earlier search is searched again (its children keep + // their proofs and prove it again at once): it has to produce a move. + if (Node& r = tree_[Tree::root()]; is_proven(r.state)) r.state = r.first_child ? EXPANDED : UNEXPANDED; + if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) { tree_.clear(); gc_pending_ = false; diff --git a/src/mcts.h b/src/mcts.h index 229b026..a976904 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -17,8 +17,17 @@ enum NodeState : u8 { EXPANDED, TERMINAL_LOSS, // side to move is checkmated TERMINAL_DRAW, + // Mate proving: the side to move here wins (some move reaches a lost + // node) or loses (every move reaches a won node) by force. Playouts stop + // at such a node, whether or not it still has children. + PROVEN_WIN, + PROVEN_LOSS, }; +// Lost for the side to move at the node: checkmated now or by force. +constexpr bool is_lost(u8 state) { return state == TERMINAL_LOSS || state == PROVEN_LOSS; } +constexpr bool is_proven(u8 state) { return state == PROVEN_WIN || state == PROVEN_LOSS; } + // 20 bytes. Children of a node sit next to each other in the arena, so a node // only needs the index of its first child. struct Node { @@ -29,6 +38,8 @@ struct Node { u16 policy = 0; // prior scaled to 0..65535 u8 num_children = 0; u8 state = UNEXPANDED; + // Plies to the mate of a lost or won node (TERMINAL_LOSS: 0), capped at 255. + u8 proof = 0; float prior() const { return float(policy) * (1.0f / 65535.0f); } }; @@ -177,6 +188,8 @@ private: static constexpr double GC_KEEP_FRACTION = 0.5; void playout(); + // Mate proving along a playout's path (see PROVEN_WIN / PROVEN_LOSS). + void prove(const std::vector& path); // 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