From 05e902c683816e8c964f0e5a334a96225229c2ef Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 22:39:28 +0100 Subject: [PATCH] feat: cpuct scaled by the variance of Q Monty raises exploration where a node's results spread widely and lowers it where they agree: cpuct *= 1 + weight * (frac - 1) with frac = stddev(Q) / scale, pulled towards 1 by (1 - frac) / (1 + warmup * N) while the node has few visits. Rigby now does the same with Monty's weight 0.8462 and warmup 0.4988; Monty's scale 0.2710 is for Q in [0, 1], so it is doubled to 0.5420 for Rigby's [-1, 1]. The variance needs the running mean of the squared results, a new float per node: Node grows from 20 to 24 bytes, so the same Hash holds a sixth fewer nodes (README updated). Checked: bench deterministic; RIGBY_VERIFY_TREE build driven over UCI with pauses (tree reuse up to 208k nodes, collections at Hash 1 MB, wtime/winc, movetime, mate-in-one positions found, go infinite + stop) without an abort. Bench: 764580 Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/mcts.cpp | 9 ++++++++- src/mcts.h | 11 ++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8285a2e..b1b1ef9 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ terminal). | Option | Default | Meaning | | --- | --- | --- | -| Hash | 64 | Tree memory in MB. A node takes 20 bytes. | +| Hash | 64 | Tree memory in MB. A node takes 24 bytes. | | Threads | 1 | Only 1 for now. | | MoveOverhead | 20 | Milliseconds kept back per move. | | CPuct | 160 | Exploration constant x100. | diff --git a/src/mcts.cpp b/src/mcts.cpp index fd51f11..070157e 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -373,7 +373,13 @@ u32 Searcher::select_child(u32 idx) const { const float n = float(std::max(node.visits, 1)); // The root, where the move is chosen, explores more. const float base_cpuct = idx == Tree::root() ? params.root_cpuct : params.cpuct; - const float cpuct = base_cpuct * (1.0f + std::log((n + params.cpuct_visit_scale) / params.cpuct_visit_scale)); + float cpuct = base_cpuct * (1.0f + std::log((n + params.cpuct_visit_scale) / params.cpuct_visit_scale)); + // A node whose results spread widely explores more, a settled one less. + if (node.visits > 1) { + float frac = std::sqrt(node.variance()) / params.cpuct_var_scale; + frac += (1.0f - frac) / (1.0f + params.cpuct_var_warmup * float(node.visits)); + cpuct *= 1.0f + params.cpuct_var_weight * (frac - 1.0f); + } const float explore = cpuct * std::sqrt(n); u32 best = node.first_child; @@ -443,6 +449,7 @@ void Searcher::playout() { Node& node = tree_[path_[i]]; node.visits++; node.q += (value - node.q) / float(node.visits); + node.sq_q += (value * value - node.sq_q) / float(node.visits); if (i) butterfly_.update(root_pos_.stm ^ int((i - 1) & 1), Move::from_raw(node.move), bonus); } diff --git a/src/mcts.h b/src/mcts.h index 229b026..18d0ee8 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -19,18 +19,20 @@ enum NodeState : u8 { TERMINAL_DRAW, }; -// 20 bytes. Children of a node sit next to each other in the arena, so a node +// 24 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 { u32 first_child = 0; u32 visits = 0; float q = 0.0f; // running mean, from the view of the side that played `move` + float sq_q = 0.0f; // running mean of the squared results, for the variance of Q u16 move = 0; u16 policy = 0; // prior scaled to 0..65535 u8 num_children = 0; u8 state = UNEXPANDED; float prior() const { return float(policy) * (1.0f / 65535.0f); } + float variance() const { return std::max(sq_q - q * q, 0.0f); } }; class Tree { @@ -102,6 +104,13 @@ struct SearchParams { // cpuct is multiplied by 1 + ln((N + scale) / scale) for a node with N visits // (Monty: 64 x 128). float cpuct_visit_scale = 8192.0f; + // cpuct grows with the standard deviation of a node's Q (Monty's + // cpuct_var_scale, _weight, _warmup): x(1 + weight * (frac - 1)) with + // frac = stddev / scale, pulled towards 1 while the node has few visits. + // Monty's scale 0.2710 is for Q in [0, 1]; doubled for Rigby's [-1, 1]. + float cpuct_var_scale = 0.5420f; + float cpuct_var_weight = 0.8462f; + float cpuct_var_warmup = 0.4988f; float fpu_reduction = 0.30f; float policy_temperature = 1.00f; i64 move_overhead = 20;