Skip to content

Commit

Permalink
Refactor root history into low ply history
Browse files Browse the repository at this point in the history
This patch changes root history to low ply history - butterfly history for plies < 4.
Doubles weight of this history for root, latter plies have lesser effect.
  • Loading branch information
Vizvezdenec authored and PikaCat-OuO committed Oct 4, 2024
1 parent e176dcd commit 3bf3b93
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
12 changes: 6 additions & 6 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ MovePicker::MovePicker(const Position& p,
Move ttm,
Depth d,
const ButterflyHistory* mh,
const ButterflyHistory* rh,
const LowPlyHistory* lph,
const CapturePieceToHistory* cph,
const PieceToHistory** ch,
const PawnHistory* ph,
bool rn) :
int pl) :
pos(p),
mainHistory(mh),
rootHistory(rh),
lowPlyHistory(lph),
captureHistory(cph),
continuationHistory(ch),
pawnHistory(ph),
ttMove(ttm),
depth(d),
rootNode(rn) {
ply(pl) {

if (pos.checkers())
stage = EVASION_TT + !(ttm && pos.pseudo_legal(ttm));
Expand Down Expand Up @@ -185,8 +185,8 @@ void MovePicker::score() {
: (pt == KNIGHT || pt == CANNON) && bool(to & threatenedByDefender) ? 25000
: 0);

if (rootNode)
m.value += 4 * (*rootHistory)[pos.side_to_move()][m.from_to()];
if (ply < 4)
m.value += 8 * (*lowPlyHistory)[ply][m.from_to()] / (1 + 2 * ply);
}

else // Type == EVASIONS
Expand Down
12 changes: 8 additions & 4 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ enum StatsType {
// see https://www.chessprogramming.org/Butterfly_Boards (~11 elo)
using ButterflyHistory = Stats<int16_t, 7183, COLOR_NB, 1 << 14>;

// LowPlyHistory is adressed by play and move's from and to squares, used
// to improve move ordering near the root
using LowPlyHistory = Stats<int16_t, 7183, 4, int(SQUARE_NB) * int(SQUARE_NB)>;

// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
using CapturePieceToHistory = Stats<int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;

Expand Down Expand Up @@ -195,11 +199,11 @@ class MovePicker {
Move,
Depth,
const ButterflyHistory*,
const ButterflyHistory*,
const LowPlyHistory*,
const CapturePieceToHistory*,
const PieceToHistory**,
const PawnHistory*,
bool);
int);
MovePicker(const Position&, Move, int, const CapturePieceToHistory*);
Move next_move(bool skipQuiets = false);

Expand All @@ -213,7 +217,7 @@ class MovePicker {

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

Expand Down
62 changes: 25 additions & 37 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,16 @@ 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,
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);
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);

} // namespace

Expand Down Expand Up @@ -251,7 +246,7 @@ void Search::Worker::iterative_deepening() {

int searchAgainCounter = 0;

rootHistory.fill(0);
lowPlyHistory.fill(0);

// Iterative deepening loop until requested to stop or the target depth is reached
while (++rootDepth < MAX_PLY && !threads.stop
Expand Down Expand Up @@ -455,7 +450,7 @@ void Search::Worker::iterative_deepening() {
// Reset histories, usually before a new game
void Search::Worker::clear() {
mainHistory.fill(0);
rootHistory.fill(0);
lowPlyHistory.fill(0);
captureHistory.fill(-771);
pawnHistory.fill(-1353);
pawnCorrectionHistory.fill(0);
Expand Down Expand Up @@ -601,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), rootNode);
update_quiet_histories(pos, ss, *this, ttData.move, stat_bonus(depth));

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


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

value = bestValue;

Expand Down Expand Up @@ -1263,8 +1258,7 @@ 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,
rootNode);
update_all_stats(pos, ss, *this, bestMove, prevSq, quietsSearched, capturesSearched, depth);

// Bonus for prior countermove that caused the fail low
else if (!priorCapture && prevSq != SQ_NONE)
Expand Down Expand Up @@ -1476,9 +1470,8 @@ 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->rootHistory,
&thisThread->captureHistory, contHist, &thisThread->pawnHistory,
nodeType == Root);
MovePicker mp(pos, ttData.move, DEPTH_QS, &thisThread->mainHistory, &thisThread->lowPlyHistory,
&thisThread->captureHistory, contHist, &thisThread->pawnHistory, ss->ply);

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

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

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

// Decrease stats for all non-best quiet moves
for (Move move : quietsSearched)
update_quiet_histories(pos, ss, workerThread, move, -quietMoveMalus, rootNode);
update_quiet_histories(pos, ss, workerThread, move, -quietMoveMalus);
}
else
{
Expand Down Expand Up @@ -1726,17 +1718,13 @@ 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,
bool rootNode) {
void update_quiet_histories(
const Position& pos, Stack* ss, Search::Worker& workerThread, Move move, int bonus) {

Color us = pos.side_to_move();
workerThread.mainHistory[us][move.from_to()] << bonus;
if (rootNode)
workerThread.rootHistory[us][move.from_to()] << bonus;
if (ss->ply < 4)
workerThread.lowPlyHistory[ss->ply][move.from_to()] << bonus;

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

Expand Down
5 changes: 3 additions & 2 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ class Worker {
void ensure_network_replicated();

// Public because they need to be updatable by the stats
ButterflyHistory mainHistory;
ButterflyHistory rootHistory;
ButterflyHistory mainHistory;
LowPlyHistory lowPlyHistory;

CapturePieceToHistory captureHistory;
ContinuationHistory continuationHistory[2][2];
PawnHistory pawnHistory;
Expand Down

0 comments on commit 3bf3b93

Please sign in to comment.