From 84f2fd0ea8d4f878987e08dab8cb897943d5de2e Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 11 Jun 2025 10:45:33 +0200 Subject: [PATCH 01/13] 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; -- 2.47.3 From c58d29ac61f9544b64a0821a1a55db139981d973 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 11 Jun 2025 11:08:44 +0200 Subject: [PATCH 02/13] added conthist malus | bench 3731826 --- source/search.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index e194314..a3f91ce 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -48,8 +48,12 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { 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]; + 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; + + conthistScore = ctx->conthist[board.sideToMove][prevType][prevTo][pieceType][to]; } return historyScore + conthistScore; @@ -497,6 +501,14 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC 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++) { + pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom()); + to = seenQuiets[moveIndex].MoveTo(); + + ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, -bonus); + } } -- 2.47.3 From d7c4383783c341de847c9cf8845de0e7ee4ff6fd Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 17 Jul 2025 11:18:36 +0200 Subject: [PATCH 03/13] changed conthist max value | bench 3012327 --- source/search.cpp | 8 ++++---- source/search.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index a3f91ce..ca0e733 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 100000; + return 200000; } if (move.IsCapture()) { @@ -36,12 +36,12 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } - return 60000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; + return 120000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; } else { if (ctx->killerMoves[0][ply] == move) { - return 40000; + return 110000; } else if (ctx->killerMoves[1][ply] == move) { - return 36000; + return 100000; } else { // Max 16384 int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; diff --git a/source/search.h b/source/search.h index b2f9366..eb10a3e 100644 --- a/source/search.h +++ b/source/search.h @@ -43,7 +43,7 @@ public: 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; + clampedBonus - contHistMoves[stm][prevType][prevTo][type][to] * std::abs(clampedBonus) / MAX_HISTORY * 4; } void Clear() { -- 2.47.3 From db565c6100f687b274362f9db05115a773f1645b Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 17 Jul 2025 11:26:33 +0200 Subject: [PATCH 04/13] changed conthist max value to half of normal hist | bench 2646638 --- source/search.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.h b/source/search.h index eb10a3e..5c8afed 100644 --- a/source/search.h +++ b/source/search.h @@ -43,7 +43,7 @@ public: 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 * 4; + clampedBonus - contHistMoves[stm][prevType][prevTo][type][to] * std::abs(clampedBonus) / MAX_HISTORY / 2; } void Clear() { -- 2.47.3 From 2181d446bf593da647146c66c09d1438558f0f0f Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 18 Jul 2025 14:50:39 +0200 Subject: [PATCH 05/13] changed move ordering values | bench 2589729 --- source/search.cpp | 8 ++++---- source/search.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index ca0e733..9a2dc86 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 200000; + return 100000; } if (move.IsCapture()) { @@ -36,12 +36,12 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } - return 120000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; + return 50000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; } else { if (ctx->killerMoves[0][ply] == move) { - return 110000; + return 21000; } else if (ctx->killerMoves[1][ply] == move) { - return 100000; + return 20000; } else { // Max 16384 int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; diff --git a/source/search.h b/source/search.h index 5c8afed..b2f9366 100644 --- a/source/search.h +++ b/source/search.h @@ -43,7 +43,7 @@ public: 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 / 2; + clampedBonus - contHistMoves[stm][prevType][prevTo][type][to] * std::abs(clampedBonus) / MAX_HISTORY; } void Clear() { -- 2.47.3 From 4fe645ff1bf99072e4baa9754fc880f6643698fb Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 18 Jul 2025 14:56:20 +0200 Subject: [PATCH 06/13] changed move ordering values | bench 2533622 --- source/search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 9a2dc86..42f3d5a 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -36,7 +36,7 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } - return 50000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; + return 50000 + (20000 * (SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; } else { if (ctx->killerMoves[0][ply] == move) { return 21000; -- 2.47.3 From 2ab4868c75796ac8271f5250c0805b54cc60a7fb Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 20 Jul 2025 03:16:18 +0200 Subject: [PATCH 07/13] resetting ss | bench 2533622 --- source/search.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/search.h b/source/search.h index b2f9366..3d17c03 100644 --- a/source/search.h +++ b/source/search.h @@ -146,6 +146,7 @@ public: TT.Clear(); sw.Restart(); killerMoves = {}; + ss = {}; positionHistory.reserve(1000); } }; -- 2.47.3 From dab5bad0898551dac1478ed3c7e7dbd760cb7c4c Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 20 Jul 2025 03:23:27 +0200 Subject: [PATCH 08/13] forgot to reset on ucinewgame | bench 2533622 --- source/uci.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/uci.cpp b/source/uci.cpp index 7b487f9..9d9e982 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -141,6 +141,7 @@ void UCILoop(Board &board) { ctx->history.Clear(); ctx->conthist.Clear(); ctx->positionHistory = {}; + ctx->ss = {}; continue; } -- 2.47.3 From 33bafd461af50a942bde8bf949cb5d196111eae3 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 20 Jul 2025 03:33:56 +0200 Subject: [PATCH 09/13] changed move order values | bench 2734093 --- source/search.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 42f3d5a..7018ee9 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -36,14 +36,13 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } - return 50000 + (20000 * (SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; + return 50000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; } else { if (ctx->killerMoves[0][ply] == move) { - return 21000; + return 41000; } else if (ctx->killerMoves[1][ply] == move) { - return 20000; + return 40000; } else { - // Max 16384 int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; int conthistScore = 0; @@ -56,7 +55,7 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { conthistScore = ctx->conthist[board.sideToMove][prevType][prevTo][pieceType][to]; } - return historyScore + conthistScore; + return 20000 + historyScore + conthistScore; } } -- 2.47.3 From 20c636a3b46a6ad6eda8be23823ac50c1c9f419f Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 20 Jul 2025 03:42:49 +0200 Subject: [PATCH 10/13] added Eren's lmp formula too | bench 2097722 --- source/search.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 7018ee9..ee6e6ce 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -404,9 +404,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // If we are near a leaf node we prune moves // that are late in the list if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) { - int lmpBase = 7; + int lmpBase = 4; - int lmpThreshold = lmpBase + 4 * depth; + int lmpMultiplier = 3; + int lmpThreshold = (lmpBase + lmpMultiplier * (depth - 1) * (depth - 1)); if (moveSeen >= lmpThreshold) { continue; -- 2.47.3 From 43c59f29f912bddeb170f37585889101afb6edd7 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 20 Jul 2025 03:46:41 +0200 Subject: [PATCH 11/13] diff lmp formula | bench 2941128 --- source/search.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index ee6e6ce..01a5d8e 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -406,8 +406,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) { int lmpBase = 4; + int lmpMultiplier = 3; - int lmpThreshold = (lmpBase + lmpMultiplier * (depth - 1) * (depth - 1)); + int lmpThreshold = (lmpBase + lmpMultiplier * depth * depth); if (moveSeen >= lmpThreshold) { continue; -- 2.47.3 From 1832d460c5d85a908864a8a37d16c64a97f41c23 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 20 Jul 2025 03:52:23 +0200 Subject: [PATCH 12/13] reverted lmp formula | bench 2734093 --- source/search.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 01a5d8e..0b29a51 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -404,11 +404,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // If we are near a leaf node we prune moves // that are late in the list if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) { - int lmpBase = 4; + int lmpBase = 7; - int lmpMultiplier = 3; - int lmpThreshold = (lmpBase + lmpMultiplier * depth * depth); + int lmpThreshold = lmpBase + 4 * depth; if (moveSeen >= lmpThreshold) { continue; -- 2.47.3 From f666d1ea05c842a78d041c77935d160b91a0f559 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 20 Jul 2025 12:14:35 +0200 Subject: [PATCH 13/13] bugfix: ScoreMove didnt score the passed move, but the last in the ss | bench 2985672 --- source/search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 0b29a51..8316fc9 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -49,8 +49,8 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { 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; + int pieceType = board.GetPieceType(move.MoveFrom()); + int to = move.MoveTo(); conthistScore = ctx->conthist[board.sideToMove][prevType][prevTo][pieceType][to]; } -- 2.47.3