From e719da0a44cf624f9728bef6222e0033da26592f Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 22:35:18 +0100 Subject: [PATCH] feat: exploration scaled by the Gini impurity of the policy With the policy network some nodes have one overwhelming move and others a spread of reasonable ones. Monty multiplies the exploration term by a factor of the node's Gini impurity (1 - sum of the squared priors): min(0.5129 - 1.4737 * ln(gini + 0.001), 2.2546), so a sharp policy (gini 0.1) explores 2.25x, gini 0.5 1.54x and a flat one (0.9) 0.67x. Rigby now does the same with Monty's constants (gini is independent of the Q range). It is computed in select_child from the children's priors in the loop that already sums the visited policy, so no node memory is needed and relabelled priors are picked up automatically. Checked: bench deterministic; RIGBY_VERIFY_TREE build driven over UCI with pauses (tree reuse up to 121k nodes, collections at Hash 1 MB, wtime/winc, movetime, mate-in-one positions found, go infinite + stop) without an abort. Bench: 656763 Co-Authored-By: Claude Opus 5.5 --- src/mcts.cpp | 11 +++++++++-- src/mcts.h | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/mcts.cpp b/src/mcts.cpp index fd51f11..f285d87 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -362,10 +362,17 @@ u32 Searcher::select_child(u32 idx) const { const float parent_q = node.visits ? -node.q : 0.0f; float visited_policy = 0.0f; + float sum_sq = 0.0f; for (u32 i = 0; i < node.num_children; i++) { const Node& child = tree_[node.first_child + i]; - if (child.visits) visited_policy += child.prior(); + const float p = child.prior(); + if (child.visits) visited_policy += p; + sum_sq += p * p; } + // A sharp policy gets more exploration, a flat one less (Monty). + const float gini = std::clamp(1.0f - sum_sq, 0.0f, 1.0f); + const float gini_factor = + std::min(params.gini_base - params.gini_ln_multiplier * std::log(gini + 0.001f), params.gini_max); const float fpu = parent_q - params.fpu_reduction * std::sqrt(visited_policy); // Exploration grows with the visits (Monty's scaling): a node searched a @@ -374,7 +381,7 @@ u32 Searcher::select_child(u32 idx) const { // 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)); - const float explore = cpuct * std::sqrt(n); + const float explore = cpuct * std::sqrt(n) * gini_factor; u32 best = node.first_child; float best_score = -1e30f; diff --git a/src/mcts.h b/src/mcts.h index 229b026..b7ecd95 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -103,6 +103,12 @@ struct SearchParams { // (Monty: 64 x 128). float cpuct_visit_scale = 8192.0f; float fpu_reduction = 0.30f; + // Exploration is multiplied by min(base - mult * ln(gini + 0.001), max), + // gini = 1 - sum of the squared priors (Monty's gini_base, gini_ln_multiplier, + // gini_min): a sharp policy (gini 0.1) explores 2.25x, a flat one (0.9) 0.67x. + float gini_base = 0.5129f; + float gini_ln_multiplier = 1.4737f; + float gini_max = 2.2546f; float policy_temperature = 1.00f; i64 move_overhead = 20; };