Skip to content

Commit

Permalink
Introduce separate butterfly history table for sorting root moves
Browse files Browse the repository at this point in the history
Idea of this patch comes from the fact that current history heuristics are mostly populated by low depth entries since our stat bonus reaches maximum value at depth 5-6 and number of low depth nodes is much bigger than number of high depth nodes.
But it doesn't make a whole lost of sense to use this low-depth centered histories to sort moves at root.
Current patch introduces special history table that is used exclusively at root, it remembers which quiet moves were good and which quiet moves were not good there and uses this information for move ordering.
  • Loading branch information
PikaCat-OuO committed Sep 17, 2024
1 parent 272952f commit 83af752
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
11 changes: 9 additions & 2 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,20 @@ MovePicker::MovePicker(const Position& p,
Move ttm,
Depth d,
const ButterflyHistory* mh,
const ButterflyHistory* rh,
const CapturePieceToHistory* cph,
const PieceToHistory** ch,
const PawnHistory* ph) :
const PawnHistory* ph,
bool rn) :
pos(p),
mainHistory(mh),
rootHistory(rh),
captureHistory(cph),
continuationHistory(ch),
pawnHistory(ph),
ttMove(ttm),
depth(d) {
depth(d),
rootNode(rn) {

if (pos.checkers())
stage = EVASION_TT + !(ttm && pos.pseudo_legal(ttm));
Expand Down Expand Up @@ -180,6 +184,9 @@ void MovePicker::score() {
m.value -= (pt == ROOK ? bool(to & threatenedByMinor) * 50000
: (pt == KNIGHT || pt == CANNON) ? bool(to & threatenedByDefender) * 25000
: bool(to & threatenedByPawn) * 15000);

if (rootNode)
m.value += 4 * (*rootHistory)[pos.side_to_move()][m.from_to()];
}

else // Type == EVASIONS
Expand Down
6 changes: 5 additions & 1 deletion src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ class MovePicker {
Move,
Depth,
const ButterflyHistory*,
const ButterflyHistory*,
const CapturePieceToHistory*,
const PieceToHistory**,
const PawnHistory*);
const PawnHistory*,
bool);
MovePicker(const Position&, Move, int, const CapturePieceToHistory*);
Move next_move(bool skipQuiets = false);

Expand All @@ -187,6 +189,7 @@ class MovePicker {

const Position& pos;
const ButterflyHistory* mainHistory;
const ButterflyHistory* rootHistory;
const CapturePieceToHistory* captureHistory;
const PieceToHistory** continuationHistory;
const PawnHistory* pawnHistory;
Expand All @@ -195,6 +198,7 @@ class MovePicker {
int stage;
int threshold;
Depth depth;
bool rootNode;
ExtMove moves[MAX_MOVES];
};

Expand Down
59 changes: 38 additions & 21 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,21 @@ Value value_to_tt(Value v, int ply);
Value value_from_tt(Value v, int ply, int r60c);
void update_pv(Move* pv, Move move, const Move* childPv);
void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus);
void update_quiet_histories(
const Position& pos, Stack* ss, Search::Worker& workerThread, Move move, int bonus);
void update_all_stats(const Position& pos,
Stack* ss,
Search::Worker& workerThread,
Move bestMove,
Square prevSq,
ValueList<Move, 32>& quietsSearched,
ValueList<Move, 32>& capturesSearched,
Depth depth);
void update_quiet_histories(const Position& pos,
Stack* ss,
Search::Worker& workerThread,
Move move,
int bonus,
bool rootNode);
void update_all_stats(const Position& pos,
Stack* ss,
Search::Worker& workerThread,
Move bestMove,
Square prevSq,
ValueList<Move, 32>& quietsSearched,
ValueList<Move, 32>& capturesSearched,
Depth depth,
bool rootNode);

} // namespace

Expand Down Expand Up @@ -245,6 +250,8 @@ void Search::Worker::iterative_deepening() {

int searchAgainCounter = 0;

rootHistory.fill(0);

// Iterative deepening loop until requested to stop or the target depth is reached
while (++rootDepth < MAX_PLY && !threads.stop
&& !(limits.depth && mainThread && rootDepth > limits.depth))
Expand Down Expand Up @@ -447,6 +454,7 @@ void Search::Worker::iterative_deepening() {
// Reset histories, usually before a new game
void Search::Worker::clear() {
mainHistory.fill(0);
rootHistory.fill(0);
captureHistory.fill(-733);
pawnHistory.fill(-1316);
pawnCorrectionHistory.fill(0);
Expand Down Expand Up @@ -588,7 +596,7 @@ Value Search::Worker::search(
{
// Bonus for a quiet ttMove that fails high (~2 Elo)
if (!ttCapture)
update_quiet_histories(pos, ss, *this, ttData.move, stat_bonus(depth));
update_quiet_histories(pos, ss, *this, ttData.move, stat_bonus(depth), rootNode);

// Extra penalty for early quiet moves of
// the previous ply (~1 Elo on STC, ~2 Elo on LTC)
Expand Down Expand Up @@ -823,8 +831,8 @@ Value Search::Worker::search(
(ss - 6)->continuationHistory};


MovePicker mp(pos, ttData.move, depth, &thisThread->mainHistory, &thisThread->captureHistory,
contHist, &thisThread->pawnHistory);
MovePicker mp(pos, ttData.move, depth, &thisThread->mainHistory, &thisThread->rootHistory,
&thisThread->captureHistory, contHist, &thisThread->pawnHistory, rootNode);

value = bestValue;

Expand Down Expand Up @@ -1250,7 +1258,8 @@ Value Search::Worker::search(
// If there is a move that produces search value greater than alpha,
// we update the stats of searched moves.
else if (bestMove)
update_all_stats(pos, ss, *this, bestMove, prevSq, quietsSearched, capturesSearched, depth);
update_all_stats(pos, ss, *this, bestMove, prevSq, quietsSearched, capturesSearched, depth,
rootNode);

// Bonus for prior countermove that caused the fail low
else if (!priorCapture && prevSq != SQ_NONE)
Expand Down Expand Up @@ -1455,8 +1464,9 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta)
// Initialize a MovePicker object for the current position, and prepare to search
// the moves. We presently use two stages of move generator in quiescence search:
// captures, or evasions only when in check.
MovePicker mp(pos, ttData.move, DEPTH_QS, &thisThread->mainHistory, &thisThread->captureHistory,
contHist, &thisThread->pawnHistory);
MovePicker mp(pos, ttData.move, DEPTH_QS, &thisThread->mainHistory, &thisThread->rootHistory,
&thisThread->captureHistory, contHist, &thisThread->pawnHistory,
nodeType == Root);

// Step 5. Loop through all pseudo-legal moves until no moves remain or a beta
// cutoff occurs.
Expand Down Expand Up @@ -1654,7 +1664,8 @@ void update_all_stats(const Position& pos,
Square prevSq,
ValueList<Move, 32>& quietsSearched,
ValueList<Move, 32>& capturesSearched,
Depth depth) {
Depth depth,
bool rootNode) {

CapturePieceToHistory& captureHistory = workerThread.captureHistory;
Piece moved_piece = pos.moved_piece(bestMove);
Expand All @@ -1665,11 +1676,11 @@ void update_all_stats(const Position& pos,

if (!pos.capture(bestMove))
{
update_quiet_histories(pos, ss, workerThread, bestMove, quietMoveBonus);
update_quiet_histories(pos, ss, workerThread, bestMove, quietMoveBonus, rootNode);

// Decrease stats for all non-best quiet moves
for (Move move : quietsSearched)
update_quiet_histories(pos, ss, workerThread, move, -quietMoveMalus);
update_quiet_histories(pos, ss, workerThread, move, -quietMoveMalus, rootNode);
}
else
{
Expand Down Expand Up @@ -1711,11 +1722,17 @@ void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus) {

// Updates move sorting heuristics

void update_quiet_histories(
const Position& pos, Stack* ss, Search::Worker& workerThread, Move move, int bonus) {
void update_quiet_histories(const Position& pos,
Stack* ss,
Search::Worker& workerThread,
Move move,
int bonus,
bool rootNode) {

Color us = pos.side_to_move();
workerThread.mainHistory[us][move.from_to()] << bonus;
if (rootNode)
workerThread.rootHistory[us][move.from_to()] << bonus;

update_continuation_histories(ss, pos.moved_piece(move), move.to_sq(), bonus);

Expand Down
1 change: 1 addition & 0 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class Worker {

// Public because they need to be updatable by the stats
ButterflyHistory mainHistory;
ButterflyHistory rootHistory;
CapturePieceToHistory captureHistory;
ContinuationHistory continuationHistory[2][2];
PawnHistory pawnHistory;
Expand Down

0 comments on commit 83af752

Please sign in to comment.