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 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 15:20:42 +01:00
1 parent 786783d7d1
commit 0f1657eb00
4 files changed
+49 -5

No files matched your search

+13 -1
View File
@@ -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_++;