From 84f2fd0ea8d4f878987e08dab8cb897943d5de2e Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 11 Jun 2025 10:45:33 +0200 Subject: [PATCH] added 1-ply conthist without malus | bench 3298447 --- source/search.cpp | 35 ++++++++++++++++++++++++++++++----- source/search.h | 26 ++++++++++++++++++++++++++ source/uci.cpp | 1 + 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index efc267f..e194314 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -25,7 +25,7 @@ void InitLMRTable() { 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) { - return 50000; + return 100000; } if (move.IsCapture()) { @@ -36,15 +36,23 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } - return 30000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; + return 60000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; } else { if (ctx->killerMoves[0][ply] == move) { - return 20000; + return 40000; } else if (ctx->killerMoves[1][ply] == move) { - return 18000; + return 36000; } else { // Max 16384 - return ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; + int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; + int conthistScore = 0; + + if (ply > 0) { + conthistScore = ctx->conthist[board.sideToMove][ctx->ss[ply-1].pieceType] + [ctx->ss[ply-1].moveTo][ctx->ss[ply].pieceType][ctx->ss[ply].moveTo]; + } + + return historyScore + conthistScore; } } @@ -258,6 +266,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; + ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom()); + ctx->ss[ply].moveTo = board.moveList[i].MoveTo(); + if (!SEE(board, board.moveList[i], 0)) continue; @@ -413,6 +424,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC bool isLegal = copy.MakeMove(currMove); if (!isLegal) continue; + + ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom()); + ctx->ss[ply].moveTo = currMove.MoveTo(); + if (copy.positionIndex >= ctx->positionHistory.size()) { ctx->positionHistory.resize(copy.positionIndex + 100); } @@ -475,6 +490,16 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->history.Update(board.sideToMove, currMove, bonus); + if (ply > 0) { + int prevType = ctx->ss[ply-1].pieceType; + int prevTo = ctx->ss[ply-1].moveTo; + int pieceType = ctx->ss[ply].pieceType; + int to = ctx->ss[ply].moveTo; + + ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, bonus); + } + + // Malus for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus); diff --git a/source/search.h b/source/search.h index 9041625..b2f9366 100644 --- a/source/search.h +++ b/source/search.h @@ -34,6 +34,27 @@ constexpr std::array SEEPieceValues = { void InitLMRTable(); +class ContHistory { +private: + // indexed by [stm][prev move piece][prev move to][piece][to] + MultiArray contHistMoves; +public: + void Update(bool stm, int prevType, int prevTo, int type, int to, int bonus) { + int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY); + + contHistMoves[stm][prevType][prevTo][type][to] += + clampedBonus - contHistMoves[stm][prevType][prevTo][type][to] * std::abs(clampedBonus) / MAX_HISTORY; + } + + void Clear() { + std::fill(&contHistMoves[0][0][0][0][0], &contHistMoves[0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0); + } + + auto& operator[](int index) { + return contHistMoves[index]; + } +}; + class History { private: MultiArray historyMoves; @@ -88,6 +109,9 @@ public: class Stack { public: int32_t eval = ScoreNone; + + int pieceType = -1; + int moveTo = -1; }; class SearchContext { @@ -103,6 +127,7 @@ public: PVLine pvLine; History history; + ContHistory conthist; std::array, 2> killerMoves{}; @@ -117,6 +142,7 @@ public: SearchContext(){ pvLine.Clear(); history.Clear(); + conthist.Clear(); TT.Clear(); sw.Restart(); killerMoves = {}; diff --git a/source/uci.cpp b/source/uci.cpp index 0b1486e..7b487f9 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -139,6 +139,7 @@ void UCILoop(Board &board) { ctx->killerMoves = {}; ctx->history.Clear(); + ctx->conthist.Clear(); ctx->positionHistory = {}; continue;