1 Commits
Author SHA1 Message Date
rektdieandclaude e102b4434e feat: mate proving
Rigby knew checkmate only at the leaf where it happens; above it the
mate was just a Q average among other winning lines. Now proven
results are backed up the playout path, as in Monty and the
cpu-mcts-tutorial: a node with a lost child (checkmated, or proven
lost) is PROVEN_WIN, a node whose children are all PROVEN_WIN is
PROVEN_LOSS, with the plies to mate kept in the node's spare padding
byte (Node stays 20 bytes). Draws are not proven.

A playout stops at a proven node with its result (+-1), selection
counts a proven child as its result, and the final move prefers the
shortest proven mate and, when every move loses, the longest
resistance. A proven root stops the search (not under go infinite)
and is searched afresh by the next search; tree reuse walks through
proven nodes and collections keep their proofs. UCI reports
"score mate N" for a proven best move.

Checked: bench deterministic; RIGBY_VERIFY_TREE build driven over UCI
with pauses (tree reuse up to 82k nodes, collections at Hash 1 MB,
mate-in-one positions, go infinite + stop) without an abort; a mate in
two (Nf6+ gxf6 Bxf7#) reports "score mate 2" with that PV; a
checkmated root reports mate 0.

Bench: 619769

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:38:11 +01:00
2 changed files with 83 additions and 7 deletions

No files matched your search

+70 -7
View File
@@ -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<u32>& 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<int>(shortest_win, child.proof);
if (child.state == PROVEN_WIN) longest_loss = std::max<int>(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,6 +531,12 @@ void Searcher::print_info() const {
<< seldepth_;
const float q = best && tree_[best].visits ? tree_[best].q : -root.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<u64>& 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;
+13
View File
@@ -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<u32>& 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