From 0f1657eb0024bed33823c5e0e45fc295f1b5e4c5 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 15:20:42 +0100 Subject: [PATCH] feat: butterfly history added to the policy logits Rigby's policy is a hand-written heuristic, so it often ranks moves badly in ways the search itself finds out. Following Vine's butterfly history for MCTS (aronpetkovski.com/blog/butterfly-history-mcts: +10 Elo in Vine, about +6 in Monty, +34 in Jackal): - a [side][from][to] table of i16, cleared on ucinewgame and kept between the moves of a game like the tree; - every playout scores each move on its path with the playout's result for the side that played it, converted to centipawns (q_to_cp(clamp(Q, +-0.998)), i.e. the inverse sigmoid on the 400 scale), with gravity: entry += bonus - entry * |bonus| / 8192; - on expansion, entry / 16384 is added to each move's policy logit before the softmax. The priors are a snapshot; later updates do not change them. Constants are Vine's, no tuning. Checked: after 50k playouts the root moves' entries make sense (Qxf7# at +8192, queen-losing moves at -5000 to -7800). The -DRIGBY_VERIFY_TREE build passed 477 tree checks with 297 collections at 1 MB Hash. Bench is deterministic. Bench: 555674 Co-Authored-By: Claude Opus 5.5 --- src/eval.cpp | 3 ++- src/eval.h | 5 +++-- src/mcts.cpp | 14 +++++++++++++- src/mcts.h | 32 +++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/eval.cpp b/src/eval.cpp index e3ac59a..40ec99d 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -198,7 +198,7 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) { float value(const Position& pos) { return cp_to_q(quiescence(pos, -MATE, MATE, 0)); } -void policy(const Position& pos, const MoveList& moves, float temperature, float* out) { +void policy(const Position& pos, const MoveList& moves, float temperature, const float* bias, float* out) { const int us = pos.stm; const int them = us ^ 1; const u64 occ = pos.occupancy(); @@ -238,6 +238,7 @@ void policy(const Position& pos, const MoveList& moves, float temperature, float if (pawn_threats & bb(from)) s += 0.003f * SEE_VALUE[type]; } if (check_squares[m.is_promotion() ? m.promo_type() : type] & bb(to)) s += 0.5f; + if (bias) s += bias[i]; out[i] = s * inv_temp; max_logit = std::max(max_logit, out[i]); diff --git a/src/eval.h b/src/eval.h index 27972df..b01d464 100644 --- a/src/eval.h +++ b/src/eval.h @@ -22,8 +22,9 @@ int quiescence(const Position& pos, int alpha, int beta, int ply); // Expected score in [-1, 1] for the side to move. float value(const Position& pos); -// Writes softmax-normalised priors for `moves` into `out`. -void policy(const Position& pos, const MoveList& moves, float temperature, float* out); +// Writes softmax-normalised priors for `moves` into `out`. `bias`, when not +// null, is added to each move's logit before the softmax. +void policy(const Position& pos, const MoveList& moves, float temperature, const float* bias, float* out); // Conversion between centipawns and Q in [-1, 1]. One scale for both directions, // so reported scores match what the search actually optimises. diff --git a/src/mcts.cpp b/src/mcts.cpp index d0bb7d0..7bcac3c 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -290,8 +290,14 @@ bool Searcher::expand(u32 idx, const Position& pos) { return false; } + // The butterfly history of each move shifts its logit. The priors are a + // snapshot: later history updates do not change them. + float bias[256]; + for (int i = 0; i < moves.size(); i++) + bias[i] = float(butterfly_.get(pos.stm, moves[i])) / ButterflyHistory::LOGIT_SCALE; + float priors[256]; - eval::policy(pos, moves, params.policy_temperature, priors); + eval::policy(pos, moves, params.policy_temperature, bias, priors); for (int i = 0; i < moves.size(); i++) { Node& child = tree_[first + i]; @@ -379,11 +385,17 @@ void Searcher::playout() { // `value` is from the view of the side to move at the leaf. Each node stores // the view of the side that moved into it, so flip before every update. + // The same result in centipawns scores the move into each node in the + // butterfly history, for the side that played it (the side to move at the + // parent, depth i - 1). + int bonus = eval::q_to_cp(std::clamp(value, -0.998f, 0.998f)); for (size_t i = path_.size(); i-- > 0;) { value = -value; + bonus = -bonus; Node& node = tree_[path_[i]]; node.visits++; node.q += (value - node.q) / float(node.visits); + if (i) butterfly_.update(root_pos_.stm ^ int((i - 1) & 1), Move::from_raw(node.move), bonus); } iterations_++; diff --git a/src/mcts.h b/src/mcts.h index 1ba83a2..eabcac6 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -1,7 +1,10 @@ #pragma once +#include +#include #include #include +#include #include #include "position.h" @@ -60,6 +63,28 @@ private: u32 prune_threshold(size_t target) const; }; +// Butterfly history for MCTS (after Vine, aronpetkovski.com/blog/butterfly-history-mcts): +// every playout scores each move on its path with the playout's result, and +// the score is added to the move's policy logit when a node is expanded. +class ButterflyHistory { +public: + // Value of a move's entry at which it shifts the policy logit by 1. + static constexpr float LOGIT_SCALE = 16384.0f; + + void clear() { table_ = {}; } + int get(int stm, Move m) const { return table_[stm][m.from()][m.to()]; } + // `bonus` is the playout's result for the side that played `m`, in + // centipawns. Entries pull towards it and saturate near +-8192. + void update(int stm, Move m, int bonus) { + i16& entry = table_[stm][m.from()][m.to()]; + const int v = entry + bonus - entry * std::abs(bonus) / 8192; + entry = i16(std::clamp(v, -32000, 32000)); + } + +private: + std::array, 64>, 2> table_{}; +}; + struct SearchLimits { u64 nodes = 0; i64 movetime = 0; @@ -99,7 +124,10 @@ public: } // Forget the previous tree, e.g. on ucinewgame. - void new_game() { tree_valid_ = false; } + void new_game() { + tree_valid_ = false; + butterfly_.clear(); + } // Synchronous search. `history` holds the keys of every position that led to // `root`, including the key of `root` itself (for repetition detection). @@ -110,6 +138,8 @@ private: using Clock = std::chrono::steady_clock; Tree tree_; + // Kept between moves of a game, like the tree. + ButterflyHistory butterfly_; Position root_pos_; std::vector history_; size_t root_history_size_ = 0; -- 2.47.3