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..4012606 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, !cutnode).score; ctx.doingNullMove = false; if (ctx.searchStopped) return 0; @@ -332,30 +335,34 @@ 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; + 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).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, !cutnode).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, !cutnode).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 +447,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 +529,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);