added Iterative Deepening with very basic time management

This commit is contained in:
rektdie committed 2024-10-14 15:07:58 +02:00
1 parent abe07eb844
commit fde3e281b0
4 files changed
+87 -19

No files matched your search

+47 -3
View File
@@ -2,10 +2,14 @@
#include "movegen.h"
#include "evaluate.h"
#include <algorithm>
#include <chrono>
#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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(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 &params) {
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 &params) {
searchStopped = false;
SearchResults results = ID(board, params);
std::cout << "bestmove " << squareCoords[results.bestMove.MoveFrom()]
<< squareCoords[results.bestMove.MoveTo()] << '\n';
nodes = 0;
+2 -1
View File
@@ -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 &params);
constexpr int moveScoreTable[6][6] = {
105, 205, 305, 405, 505, 605,
+25 -13
View File
@@ -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;
}
+13 -2
View File
@@ -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);
void UCILoop(Board &board);