From fde3e281b0bd494867ff78eca67d9599c44b15f7 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 14 Oct 2024 15:07:58 +0200 Subject: [PATCH] added Iterative Deepening with very basic time management --- source/search.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++--- source/search.h | 3 ++- source/uci.cpp | 38 +++++++++++++++++++++++------------ source/uci.h | 15 ++++++++++++-- 4 files changed, 87 insertions(+), 19 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 19e68e5..9593770 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -2,10 +2,14 @@ #include "movegen.h" #include "evaluate.h" #include +#include #include "tt.h" static inline int nodes = 0; static inline int ply = 0; +static inline auto timeStart = std::chrono::high_resolution_clock::now(); +static inline int timeToSearch = 0; +static inline bool searchStopped = false; static int ScoreMove(Board &board, Move &move) { const int attackerType = board.GetPieceType(move.MoveFrom()); @@ -58,6 +62,13 @@ void ListScores(Board &board) { } static SearchResults Quiescence(Board board, int alpha, int beta) { + auto currTime = std::chrono::high_resolution_clock::now(); + int elapsed = std::chrono::duration_cast(currTime - timeStart).count(); + if (elapsed >= timeToSearch) { + searchStopped = true; + return 0; + } + nodes++; int staticScore = Evaluate(board); @@ -98,6 +109,13 @@ static SearchResults Quiescence(Board board, int alpha, int beta) { } static SearchResults PVS(Board board, int depth, int alpha, int beta) { + auto currTime = std::chrono::high_resolution_clock::now(); + int elapsed = std::chrono::duration_cast(currTime - timeStart).count(); + if (elapsed >= timeToSearch) { + searchStopped = true; + return 0; + } + nodes++; if (depth == 0) return Quiescence(board, alpha, beta).score; @@ -157,9 +175,35 @@ static SearchResults PVS(Board board, int depth, int alpha, int beta) { return results; } -void SearchPosition(Board &board, int depth) { - SearchResults results = PVS(board, depth, -50000, 50000); - std::cout << "info depth " << depth << " nodes " << nodes << '\n'; +// Iterative deepening +SearchResults ID(Board &board, SearchParams ¶ms) { + timeStart = std::chrono::high_resolution_clock::now(); + + int fullTime = board.sideToMove ? params.btime : params.wtime; + int inc = board.sideToMove ? params.binc : params.winc; + + SearchResults safeResults; + for (int depth = 1; depth <= 8; depth++) { + int timeRemaining = fullTime / 20 + inc / 2; + timeToSearch = timeRemaining; + + SearchResults currentResults = PVS(board, depth, -50000, 50000); + if (currentResults.score >= safeResults.score) { + safeResults = currentResults; + } + + std::cout << "info nodes " << nodes << '\n'; + std::cout << timeRemaining << std::endl; + + if (searchStopped) break; + } + + return safeResults; +} + +void SearchPosition(Board &board, SearchParams ¶ms) { + searchStopped = false; + SearchResults results = ID(board, params); std::cout << "bestmove " << squareCoords[results.bestMove.MoveFrom()] << squareCoords[results.bestMove.MoveTo()] << '\n'; nodes = 0; diff --git a/source/search.h b/source/search.h index c0dc4ad..386d584 100644 --- a/source/search.h +++ b/source/search.h @@ -1,5 +1,6 @@ #pragma once #include "board.h" +#include "uci.h" class SearchResults { public: @@ -18,7 +19,7 @@ public: } }; -void SearchPosition(Board &board, int depth); +void SearchPosition(Board &board, SearchParams ¶ms); constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605, diff --git a/source/uci.cpp b/source/uci.cpp index 6f9fbe5..481ea84 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -136,22 +136,34 @@ void ParsePosition(Board &board, const char* command) { } } -void ParseGo(Board &board, const char* command) { - // init depth - int depth = -1; +static int ReadParam(std::string param, std::string &command) { + size_t pos = -1; + pos = command.find(param); - // handle fixed depth search - if (std::string(command).find("depth") != std::string::npos) { - // skipping to the depth number - command += 9; + if (pos != std::string::npos) { + std::string result = ""; + pos += param.length() + 1; - depth = atoi(command); - } else { // other command parameters - depth = 6; + while(pos <= command.length() && std::isdigit(command[pos])){ + result += command[pos]; + pos++; + } + return stoi(result); } - // search position - SearchPosition(board, depth); + return 0; +} + +void ParseGo(Board &board, std::string &command) { + SearchParams params; + + params.wtime = ReadParam("wtime", command); + params.btime = ReadParam("btime", command); + params.winc = ReadParam("winc", command); + params.binc = ReadParam("binc", command); + params.movesToGo = ReadParam("movestogo", command); + + SearchPosition(board, params); } void UCILoop(Board &board) { @@ -200,7 +212,7 @@ void UCILoop(Board &board) { // parse UCI "go" command if (input.find("go") != std::string::npos) { - ParseGo(board, input.c_str()); + ParseGo(board, input); continue; } diff --git a/source/uci.h b/source/uci.h index 8fe18d5..a355491 100644 --- a/source/uci.h +++ b/source/uci.h @@ -2,6 +2,17 @@ #include "move.h" #include "board.h" +class SearchParams { +public: + int wtime = 0; + int btime = 0; + int winc = 0; + int binc = 0; + int movesToGo = 0; + + SearchParams(){} +}; + // Returns Move object from a string Move ParseMove(Board &board, const char* moveString); @@ -10,7 +21,7 @@ void ParsePosition(Board &board, const char* command); // Parse UCI "go" command // GUI sends the engine this command if it should start searching for the best move -void ParseGo(Board &board, const char* command); +void ParseGo(Board &board, std::string &command); // main UCI loop -void UCILoop(Board &board); \ No newline at end of file +void UCILoop(Board &board);