From 24ac6c74a88c14e0e3314bb9b42b083689562b10 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 00:47:16 +0200 Subject: [PATCH 1/8] added cutnodes | bench 3935733 --- .gitignore | 2 +- source/search.cpp | 37 ++++++++++++++++++++----------------- source/search.h | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 74eaf9f..30b2469 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ Eleanor # Other .vscode Scripts/game.pgn -data.binpack \ No newline at end of file +*binpack \ No newline at end of file diff --git a/source/search.cpp b/source/search.cpp index d78b0c5..5a21883 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -114,15 +114,18 @@ bool IsDraw(Board &board, SearchContext& ctx) { return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); } -static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { +static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode) { int reduction = 0; // Late Move Reduction if (depth >= 3 && moveSeen >= 3 && !move.IsCapture()) { reduction = lmrTable[depth][moveSeen]; + + if (cutnode) + reduction += 2; } - return reduction; + return std::clamp(reduction, -1, depth - 1); } template @@ -203,7 +206,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear } template -SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx) { +SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode) { if constexpr (mode != bench && mode != nodesMode) { if (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { @@ -274,7 +277,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Always legal so we dont check it ctx.doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, true).score; ctx.doingNullMove = false; if (ctx.searchStopped) return 0; @@ -332,30 +335,30 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; - int reductions = GetReductions(board, currMove, depth, moveSeen, ply); + int reductions = GetReductions(board, currMove, depth, moveSeen, ply, cutnode); int newDepth = depth + copy.InCheck() - 1; // First move (suspected PV node) if (!moveSeen) { // Full search - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; } else if (reductions) { // Null-window search with reductions - score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx, true).score; if (score > alpha) { // Null-window search now without the reduction - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; } } else { // Null-window search - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; } // Check if we need to do full window re-search if (moveSeen && score > alpha && score < beta) { - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; } moveSeen++; @@ -440,7 +443,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { ctx.timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); int softTime = ctx.timeToSearch * 0.65; - SearchResults currentResults = PVS(board, depth, alpha, beta, ply, ctx); + SearchResults currentResults = PVS(board, depth, alpha, beta, ply, ctx, false); elapsed = ctx.sw.GetElapsedMS(); // If we fell outside the window, try again with full width @@ -522,12 +525,12 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& c return results; } -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); diff --git a/source/search.h b/source/search.h index d56ca14..3fa71ec 100644 --- a/source/search.h +++ b/source/search.h @@ -125,7 +125,7 @@ public: }; template -SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx); +SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); From b0165199981b441b838e1ae617044034e814aed6 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 01:37:31 +0200 Subject: [PATCH 2/8] node after a nullmove is not a cutnode | bench 3915796 --- source/search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 5a21883..79f6fc2 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -277,7 +277,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Always legal so we dont check it ctx.doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, true).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, false).score; ctx.doingNullMove = false; if (ctx.searchStopped) return 0; From 26ac46d8ce156067e444777c4a17f8c8fb8e23c7 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 14:05:24 +0200 Subject: [PATCH 3/8] fixed cutnode passing | bench 5299823 --- source/search.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 79f6fc2..84fa15e 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -277,7 +277,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Always legal so we dont check it ctx.doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, false).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, !cutnode).score; ctx.doingNullMove = false; if (ctx.searchStopped) return 0; @@ -349,11 +349,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (score > alpha) { // Null-window search now without the reduction - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, !cutnode).score; } } else { // Null-window search - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, !cutnode).score; } // Check if we need to do full window re-search From 40d67d101710631fd236d8b246ee10707d990c36 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 14:49:22 +0200 Subject: [PATCH 4/8] fix in LMR | bench 5251927 --- source/search.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 84fa15e..4012606 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -342,7 +342,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // First move (suspected PV node) if (!moveSeen) { // Full search - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; + if constexpr (isPV) { + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; + } else { + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, !cutnode).score; + } } else if (reductions) { // Null-window search with reductions score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx, true).score; From a39abe23aac5624992b391939232f1410c77cf63 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 00:47:16 +0200 Subject: [PATCH 5/8] added cutnodes | bench 3935733 --- .gitignore | 2 +- source/search.cpp | 37 ++++++++++++++++++++----------------- source/search.h | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 74eaf9f..30b2469 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ Eleanor # Other .vscode Scripts/game.pgn -data.binpack \ No newline at end of file +*binpack \ No newline at end of file diff --git a/source/search.cpp b/source/search.cpp index d78b0c5..5a21883 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -114,15 +114,18 @@ bool IsDraw(Board &board, SearchContext& ctx) { return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); } -static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { +static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode) { int reduction = 0; // Late Move Reduction if (depth >= 3 && moveSeen >= 3 && !move.IsCapture()) { reduction = lmrTable[depth][moveSeen]; + + if (cutnode) + reduction += 2; } - return reduction; + return std::clamp(reduction, -1, depth - 1); } template @@ -203,7 +206,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear } template -SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx) { +SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode) { if constexpr (mode != bench && mode != nodesMode) { if (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { @@ -274,7 +277,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Always legal so we dont check it ctx.doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, true).score; ctx.doingNullMove = false; if (ctx.searchStopped) return 0; @@ -332,30 +335,30 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; - int reductions = GetReductions(board, currMove, depth, moveSeen, ply); + int reductions = GetReductions(board, currMove, depth, moveSeen, ply, cutnode); int newDepth = depth + copy.InCheck() - 1; // First move (suspected PV node) if (!moveSeen) { // Full search - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; } else if (reductions) { // Null-window search with reductions - score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx, true).score; if (score > alpha) { // Null-window search now without the reduction - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; } } else { // Null-window search - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; } // Check if we need to do full window re-search if (moveSeen && score > alpha && score < beta) { - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx).score; + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; } moveSeen++; @@ -440,7 +443,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext& ctx) { ctx.timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); int softTime = ctx.timeToSearch * 0.65; - SearchResults currentResults = PVS(board, depth, alpha, beta, ply, ctx); + SearchResults currentResults = PVS(board, depth, alpha, beta, ply, ctx, false); elapsed = ctx.sw.GetElapsedMS(); // If we fell outside the window, try again with full width @@ -522,12 +525,12 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& c return results; } -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); -template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); +template SearchResults PVS(Board&, int, int, int, int, SearchContext& ctx, bool cutnode); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); diff --git a/source/search.h b/source/search.h index d56ca14..3fa71ec 100644 --- a/source/search.h +++ b/source/search.h @@ -125,7 +125,7 @@ public: }; template -SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx); +SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx, bool cutnode); template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); From 69d8bc906404702aae833bf407976e4b669b33f5 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 01:37:31 +0200 Subject: [PATCH 6/8] node after a nullmove is not a cutnode | bench 3915796 --- source/search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 5a21883..79f6fc2 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -277,7 +277,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Always legal so we dont check it ctx.doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, true).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, false).score; ctx.doingNullMove = false; if (ctx.searchStopped) return 0; From 18c0eca87defa3b1da0be6de0f095ba01d933815 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 14:05:24 +0200 Subject: [PATCH 7/8] fixed cutnode passing | bench 5299823 --- source/search.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 79f6fc2..84fa15e 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -277,7 +277,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Always legal so we dont check it ctx.doingNullMove = true; - int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, false).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1, ctx, !cutnode).score; ctx.doingNullMove = false; if (ctx.searchStopped) return 0; @@ -349,11 +349,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (score > alpha) { // Null-window search now without the reduction - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, !cutnode).score; } } else { // Null-window search - score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, true).score; + score = -PVS(copy, newDepth, -alpha-1, -alpha, ply + 1, ctx, !cutnode).score; } // Check if we need to do full window re-search From 45a26d8952c505d8c30f9add146911de82d2c73a Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 23 May 2025 14:49:22 +0200 Subject: [PATCH 8/8] fix in LMR | bench 5251927 --- source/search.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 84fa15e..4012606 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -342,7 +342,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // First move (suspected PV node) if (!moveSeen) { // Full search - score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; + if constexpr (isPV) { + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, false).score; + } else { + score = -PVS(copy, newDepth, -beta, -alpha, ply + 1, ctx, !cutnode).score; + } } else if (reductions) { // Null-window search with reductions score = -PVS(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1, ctx, true).score;