diff --git a/source/search.cpp b/source/search.cpp index b3542b8..4f4397f 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -11,6 +11,7 @@ namespace SEARCH { U64 nodes = 0; +U64 nodesToGo = 0; bool benchStarted = false; static inline int timeToSearch = 0; @@ -139,14 +140,22 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int return reduction; } +template static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { - if (!benchStarted && nodes % 1024 == 0) { - if (sw.GetElapsedMS() >= timeToSearch) { + if constexpr (mode != nodesMode) { + if (!benchStarted && nodes % 1024 == 0) { + if (sw.GetElapsedMS() >= timeToSearch) { + StopSearch(); + return 0; + } + } + } else { + if (nodes >= nodesToGo) { StopSearch(); return 0; } } - + int bestScore = HCE::Evaluate(board); TTEntry *entry = TT.GetRawEntry(board.hashKey); @@ -173,7 +182,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { Board copy = board; int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; + int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; positionIndex--; if (score >= beta) { @@ -195,20 +204,25 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { - if constexpr (mode != bench) { + if constexpr (mode != bench || mode != nodesMode) { if (nodes % 1024 == 0) { if constexpr (mode == normal) { if (sw.GetElapsedMS() >= timeToSearch) { StopSearch(); return 0; } - } else { + } else if constexpr (mode == datagen) { if (nodes >= DATAGEN::HARD_NODES) { StopSearch(); return 0; } } } + } else if constexpr (mode == nodesMode) { + if (nodes >= nodesToGo) { + StopSearch(); + return 0; + } } pvLine.SetLength(ply); @@ -223,7 +237,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } - if (depth <= 0) return Quiescence(board, alpha, beta, ply); + if (depth <= 0) return Quiescence(board, alpha, beta, ply); const int staticEval = HCE::Evaluate(board); @@ -385,9 +399,7 @@ static SearchResults ID(Board &board, SearchParams params) { int elapsed = 0; - if constexpr (mode == normal) { - sw.Restart(); - } + sw.Restart(); for (int depth = 1; depth <= toDepth; depth++) { timeToSearch = std::max((fullTime / 20) + (inc / 2), 4); @@ -421,7 +433,7 @@ static SearchResults ID(Board &board, SearchParams params) { safeResults = currentResults; } - if constexpr (mode == normal) { + if constexpr (mode == normal || mode == nodesMode) { std::cout << "info "; std::cout << "depth " << depth; std::cout << " time " << elapsed; @@ -432,9 +444,11 @@ static SearchResults ID(Board &board, SearchParams params) { pvLine.Print(0); std::cout << std::endl; - if (sw.GetElapsedMS() >= softTime) { - StopSearch(); - break; + if constexpr (mode == normal) { + if (sw.GetElapsedMS() >= softTime) { + StopSearch(); + break; + } } } else if constexpr (mode == datagen) { if (nodes >= DATAGEN::SOFT_NODES) { @@ -453,6 +467,10 @@ SearchResults SearchPosition(Board &board, SearchParams params) { searchStopped = false; if constexpr (mode != bench) { nodes = 0; + + if constexpr (mode == nodesMode) { + nodesToGo = params.nodes; + } } pvLine.Clear(); @@ -481,5 +499,6 @@ template SearchResults PVS(Board, int, int, int, int template SearchResults SearchPosition(Board &board, SearchParams params); template SearchResults SearchPosition(Board &board, SearchParams params); template SearchResults SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); } diff --git a/source/search.h b/source/search.h index dd0160b..fa8855c 100644 --- a/source/search.h +++ b/source/search.h @@ -10,7 +10,8 @@ namespace SEARCH { enum searchMode { normal, bench, - datagen + datagen, + nodesMode }; constexpr int MATE_SCORE = -99000; diff --git a/source/uci.cpp b/source/uci.cpp index 948fbd6..53d8121 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -76,14 +76,20 @@ static void ParseGo(Board &board, std::string &command) { params.winc = ReadParam("winc", command); params.binc = ReadParam("binc", command); params.movesToGo = ReadParam("movestogo", command); + params.nodes = ReadParam("nodes", command); if (command.find("movetime") != std::string::npos) { params.wtime = ReadParam("movetime", command); params.btime = ReadParam("movetime", command); } - - auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); - worker.detach(); + + if (params.nodes) { + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + worker.detach(); + } else { + auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params); + worker.detach(); + } } static void SetOption(std::string &command) { diff --git a/source/uci.h b/source/uci.h index 11a231e..1f2553d 100644 --- a/source/uci.h +++ b/source/uci.h @@ -9,6 +9,7 @@ public: int winc = 0; int binc = 0; int movesToGo = 0; + int nodes = 0; SearchParams(){} };