From bf7adcb06b96d73f5b6d89e8dad498abed9b23bf Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 5 Aug 2025 19:16:37 +0200 Subject: [PATCH 1/4] added pawn corrhist | bench 4741642 --- source/board.cpp | 73 ++++++++++++++++++++++++++--------------------- source/board.h | 3 +- source/search.cpp | 23 +++++++++++++-- source/search.h | 42 +++++++++++++++++++++++---- source/uci.cpp | 1 + 5 files changed, 102 insertions(+), 40 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 06fda23..51dcd8f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -9,7 +9,7 @@ void Board::Reset() { castlingRights = 0; enPassantTarget = noEPTarget; - + halfMoves = 0; fullMoves = 1; @@ -31,6 +31,7 @@ void Board::Reset() { currentMoveIndex = 0; hashKey = 0ULL; + pawnKey = 0ULL; } void Board::SetByFen(std::string_view fen) { @@ -88,66 +89,66 @@ void Board::SetByFen(std::string_view fen) { std::string Board::GetFen() { std::ostringstream fen; - + for (int rank = 7; rank >= 0; --rank) { int emptyCount = 0; - + for (int file = 0; file < 8; ++file) { int square = rank * 8 + file; const uint64_t squareBit = 1ULL << square; - + if (!(occupied & squareBit)) { ++emptyCount; continue; } - + if (emptyCount > 0) { fen << emptyCount; emptyCount = 0; } - + static constexpr std::array pieceChars = {'p', 'n', 'b', 'r', 'q', 'k'}; char pieceChar = '?'; - + for (size_t pieceType = 0; pieceType < pieceChars.size(); ++pieceType) { if (pieces[pieceType] & squareBit) { pieceChar = pieceChars[pieceType]; break; } } - + fen << (colors[White] & squareBit ? static_cast(std::toupper(pieceChar)) : pieceChar); } - + if (emptyCount > 0) { fen << emptyCount; } - + if (rank > 0) { fen << '/'; } } - + fen << ' ' << (sideToMove ? 'b' : 'w'); - + fen << ' '; bool hasCastlingRights = false; - + const std::array, 4> castlingOptions = { {{whiteKingRight, 'K'}, {whiteQueenRight, 'Q'}, {blackKingRight, 'k'}, {blackQueenRight, 'q'}} }; - + for (const auto& [right, symbol] : castlingOptions) { if (castlingRights & right) { fen << symbol; hasCastlingRights = true; } } - + if (!hasCastlingRights) { fen << '-'; } - + fen << ' '; if (enPassantTarget == noEPTarget) { fen << '-'; @@ -156,11 +157,11 @@ std::string Board::GetFen() { const int rank = enPassantTarget / 8; fen << static_cast('a' + file) << (rank + 1); } - + fen << ' ' << halfMoves; - + fen << ' ' << fullMoves; - + return fen.str(); } @@ -271,6 +272,10 @@ void Board::SetPiece(int piece, int square, bool color) { occupied.SetBit(square); hashKey ^= UTILS::zKeys[color][piece][square]; + + if (piece == Pawn) { + pawnKey ^= UTILS::zKeys[color][Pawn][square]; + } } void Board::RemovePiece(int piece, int square, bool color) { @@ -279,6 +284,10 @@ void Board::RemovePiece(int piece, int square, bool color) { occupied.PopBit(square); hashKey ^= UTILS::zKeys[color][piece][square]; + + if (piece == Pawn) { + pawnKey ^= UTILS::zKeys[color][Pawn][square]; + } } // Updates castling rights @@ -322,23 +331,23 @@ bool Board::MakeMove(Move move) { // Null Move if (!move) { int newEpTarget = noEPTarget; - + sideToMove = !sideToMove; hashKey ^= UTILS::zSide; - + if (enPassantTarget != noEPTarget) { hashKey ^= UTILS::zEnPassant[enPassantTarget % 8]; } - + if (newEpTarget != noEPTarget) { hashKey ^= UTILS::zEnPassant[newEpTarget % 8]; } - + enPassantTarget = newEpTarget; - + return true; } - + int newEpTarget = noEPTarget; int attackerPiece = GetPieceType(move.MoveFrom()); @@ -397,7 +406,7 @@ bool Board::MakeMove(Move move) { case kingCastle: { int rookSquare = attackerColor ? h8 : h1; - + // Removing rook from old position RemovePiece(Rook, rookSquare, attackerColor); @@ -448,7 +457,7 @@ bool Board::MakeMove(Move move) { sideToMove = !sideToMove; if (InCheck()) { *this = save; - return false; + return false; } sideToMove = !sideToMove; @@ -458,7 +467,7 @@ bool Board::MakeMove(Move move) { } else { halfMoves++; } - + positionIndex++; return true; @@ -513,16 +522,16 @@ void Board::ResetAccPair() { Bitboard Board::AttacksTo(int square, Bitboard occupancy) { Bitboard attacks; - + // Pawn attacks - + // Opponent attacks attacks |= (MOVEGEN::pawnAttacks[sideToMove][square] & colors[!sideToMove] & pieces[Pawn]); - + // Own piece attacks attacks |= (MOVEGEN::pawnAttacks[!sideToMove][square] & colors[sideToMove] & pieces[Pawn]); - // Knight + // Knight attacks |= (MOVEGEN::knightAttacks[square] & pieces[Knight]); // King diff --git a/source/board.h b/source/board.h index 8a3a26a..aa37ff7 100644 --- a/source/board.h +++ b/source/board.h @@ -40,6 +40,7 @@ public: int fullMoves = 1; U64 hashKey = 0ULL; + U64 pawnKey = 0ULL; void Reset(); void SetByFen(std::string_view fen); @@ -56,7 +57,7 @@ public: int GetPieceType(int square); int GetPieceColor(int square); - + bool InCheck(); void SetPiece(int piece, int square, bool color); diff --git a/source/search.cpp b/source/search.cpp index 5969de9..53741e7 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -4,6 +4,7 @@ #include #include "datagen.h" #include "benchmark.h" +#include "types.h" namespace SEARCH { @@ -42,6 +43,14 @@ int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to]; } +static int AdjustEval(Board &board, SearchContext* ctx, int eval) { + int pawnHist = ctx->corrhist.GetPawnHist(board); + + int mateFound = MATE_SCORE - MAX_PLY; + + return std::clamp(eval + pawnHist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1); +} + static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { TTEntry current = ctx->TT.GetRawEntry(board.hashKey); if (current.hashKey == board.hashKey && current.bestMove == move) { @@ -270,7 +279,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear if (ply > ctx->seldepth) ctx->seldepth = ply; - int bestScore = NNUE::net.Evaluate(board); + int bestScore = AdjustEval(board, ctx, NNUE::net.Evaluate(board)); ctx->ss[ply].eval = bestScore; if (!ctx->excluded) { @@ -382,7 +391,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (depth <= 0) return Quiescence(board, alpha, beta, ply, ctx); - const int staticEval = NNUE::net.Evaluate(board); + const int staticEval = AdjustEval(board, ctx, NNUE::net.Evaluate(board)); ctx->ss[ply].eval = staticEval; const bool improving = [&] @@ -649,6 +658,16 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (ctx->searchStopped) return 0; if (!ctx->excluded) + + if (!board.InCheck() && (results.bestMove.IsQuiet()) + && !(nodeType == CutNode && results.score <= staticEval) + && !(nodeType == AllNode && results.score >= staticEval)) { + + int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT); + + ctx->corrhist.UpdatePawnHist(board, depth, corrHistBonus); + } + ctx->TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); return results; } diff --git a/source/search.h b/source/search.h index 3918ee6..229dd6b 100644 --- a/source/search.h +++ b/source/search.h @@ -23,6 +23,12 @@ constexpr int MAX_DEPTH = 128; constexpr int MAX_PLY = 512; constexpr int MAX_HISTORY = 16384; +constexpr int CORRHIST_WEIGHT_SCALE = 256; +constexpr int CORRHIST_GRAIN = 256; +constexpr int CORRHIST_LIMIT = 1024; +constexpr int CORRHIST_SIZE = 16384; +constexpr int CORRHIST_MAX = 16384; + constexpr int32_t ScoreNone = -255000; constexpr int inf = 100000; @@ -35,6 +41,30 @@ constexpr std::array SEEPieceValues = { void InitLMRTable(); class SearchContext; +class CorrHist { +private: + // indexed by [stm][key] + MultiArray pawnHist; +public: + void UpdatePawnHist(Board& board, int depth, int diff) { + int* entry = &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]; + + const int scaledDiff = diff * CORRHIST_GRAIN; + const int newWeight = 4 * std::min(depth + 1, 16); + + *entry = (*entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE; + *entry = std::clamp(*entry, -CORRHIST_MAX, CORRHIST_MAX); + } + + int GetPawnHist(Board& board) { + return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]; + } + + void ClearPawnHist() { + std::fill(&pawnHist[0][0], &pawnHist[0][0] + sizeof(pawnHist) / sizeof(int), 0); + } +}; + class ContHistory { private: // indexed by [other color][prev move piece][stm][prev move to][piece][to] @@ -50,7 +80,7 @@ public: void Clear() { std::fill(&contHistMoves[0][0][0][0][0][0], &contHistMoves[0][0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0); } - + int16_t GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply); int16_t GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply); @@ -65,7 +95,7 @@ private: public: void Update(bool stm, Move move, int bonus) { int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY); - historyMoves[stm][move.MoveFrom()][move.MoveTo()] += + historyMoves[stm][move.MoveFrom()][move.MoveTo()] += clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / MAX_HISTORY; } @@ -96,12 +126,12 @@ public: length[ply] = length[ply + 1]; } - + void Print(int n) { for (int i = 0; i < length[n]; i++) { table[n][i].PrintMove(); std::cout << ' '; - } + } } void Clear() { @@ -128,13 +158,14 @@ public: int seldepth = 0; Move excluded = Move(); - + bool doingNullMove = false; bool searchStopped = false; PVLine pvLine; History history; ContHistory conthist; + CorrHist corrhist; std::array, 2> killerMoves{}; @@ -150,6 +181,7 @@ public: pvLine.Clear(); history.Clear(); conthist.Clear(); + corrhist.ClearPawnHist(); TT.Clear(); sw.Restart(); killerMoves = {}; diff --git a/source/uci.cpp b/source/uci.cpp index de40107..9b28d53 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -193,6 +193,7 @@ void UCILoop(Board &board) { ctx->killerMoves = {}; ctx->history.Clear(); ctx->conthist.Clear(); + ctx->corrhist.ClearPawnHist(); ctx->positionHistory = {}; ctx->ss = {}; -- 2.47.3 From 2e1f84bda55811aaf6bb01ee9f290403e37fd101 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 5 Aug 2025 22:55:15 +0200 Subject: [PATCH 2/4] bugfix: missing curly brackets | bench 4559188 --- source/search.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 53741e7..03bb856 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -657,11 +657,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } if (ctx->searchStopped) return 0; - if (!ctx->excluded) + if (!ctx->excluded) { if (!board.InCheck() && (results.bestMove.IsQuiet()) - && !(nodeType == CutNode && results.score <= staticEval) - && !(nodeType == AllNode && results.score >= staticEval)) { + && !(nodeType == CutNode && results.score <= staticEval) + && !(nodeType == AllNode && results.score >= staticEval)) { int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT); @@ -669,6 +669,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } ctx->TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); + } return results; } -- 2.47.3 From 6ccd416840d1c4dbb779e4e9f549f7bd74edaad5 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 5 Aug 2025 23:44:31 +0200 Subject: [PATCH 3/4] removed unnecessary condition | bench 4559188 --- source/search.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 03bb856..d154728 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -660,7 +660,6 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (!ctx->excluded) { if (!board.InCheck() && (results.bestMove.IsQuiet()) - && !(nodeType == CutNode && results.score <= staticEval) && !(nodeType == AllNode && results.score >= staticEval)) { int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT); -- 2.47.3 From 40339b2d4591389bbf6fd3e7c7bbf2d2304a8cad Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 3 Sep 2025 01:21:16 +0200 Subject: [PATCH 4/4] changed the pawnhist update function | bench 8216434 --- source/search.cpp | 2 +- source/search.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index d154728..358388a 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -659,7 +659,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (ctx->searchStopped) return 0; if (!ctx->excluded) { - if (!board.InCheck() && (results.bestMove.IsQuiet()) + if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove)) && !(nodeType == AllNode && results.score >= staticEval)) { int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT); diff --git a/source/search.h b/source/search.h index 229dd6b..44b671a 100644 --- a/source/search.h +++ b/source/search.h @@ -50,7 +50,7 @@ public: int* entry = &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]; const int scaledDiff = diff * CORRHIST_GRAIN; - const int newWeight = 4 * std::min(depth + 1, 16); + const int newWeight = std::min(depth * depth + 2 * depth + 1, 128); *entry = (*entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE; *entry = std::clamp(*entry, -CORRHIST_MAX, CORRHIST_MAX); -- 2.47.3