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:
1 parent
786783d7d1
commit
0f1657eb00
4 files changed
+49
-5
No files matched your search
+31
-1
@@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
#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<std::array<std::array<i16, 64>, 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<u64> history_;
|
||||
size_t root_history_size_ = 0;
|
||||
|
||||
Reference in new issue
Block a user