Compare commits
1
Commits
786783d7d1
...
0f1657eb00
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f1657eb00 |
No files matched your search
+2
-1
@@ -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]);
|
||||
|
||||
+3
-2
@@ -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.
|
||||
|
||||
+13
-1
@@ -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_++;
|
||||
|
||||
+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