added Iterative Deepening with very basic time management
This commit is contained in:
1 parent
abe07eb844
commit
fde3e281b0
4 files changed
+86
-18
No files matched your search
+47
-3
@@ -2,10 +2,14 @@
|
|||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "evaluate.h"
|
#include "evaluate.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
static inline int nodes = 0;
|
static inline int nodes = 0;
|
||||||
static inline int ply = 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) {
|
static int ScoreMove(Board &board, Move &move) {
|
||||||
const int attackerType = board.GetPieceType(move.MoveFrom());
|
const int attackerType = board.GetPieceType(move.MoveFrom());
|
||||||
@@ -58,6 +62,13 @@ void ListScores(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static SearchResults Quiescence(Board board, int alpha, int beta) {
|
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++;
|
nodes++;
|
||||||
|
|
||||||
int staticScore = Evaluate(board);
|
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) {
|
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++;
|
nodes++;
|
||||||
if (depth == 0) return Quiescence(board, alpha, beta).score;
|
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;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SearchPosition(Board &board, int depth) {
|
// Iterative deepening
|
||||||
SearchResults results = PVS(board, depth, -50000, 50000);
|
SearchResults ID(Board &board, SearchParams ¶ms) {
|
||||||
std::cout << "info depth " << depth << " nodes " << nodes << '\n';
|
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()]
|
std::cout << "bestmove " << squareCoords[results.bestMove.MoveFrom()]
|
||||||
<< squareCoords[results.bestMove.MoveTo()] << '\n';
|
<< squareCoords[results.bestMove.MoveTo()] << '\n';
|
||||||
nodes = 0;
|
nodes = 0;
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
#include "uci.h"
|
||||||
|
|
||||||
class SearchResults {
|
class SearchResults {
|
||||||
public:
|
public:
|
||||||
@@ -18,7 +19,7 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void SearchPosition(Board &board, int depth);
|
void SearchPosition(Board &board, SearchParams ¶ms);
|
||||||
|
|
||||||
constexpr int moveScoreTable[6][6] = {
|
constexpr int moveScoreTable[6][6] = {
|
||||||
105, 205, 305, 405, 505, 605,
|
105, 205, 305, 405, 505, 605,
|
||||||
|
|||||||
+25
-13
@@ -136,22 +136,34 @@ void ParsePosition(Board &board, const char* command) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParseGo(Board &board, const char* command) {
|
static int ReadParam(std::string param, std::string &command) {
|
||||||
// init depth
|
size_t pos = -1;
|
||||||
int depth = -1;
|
pos = command.find(param);
|
||||||
|
|
||||||
// handle fixed depth search
|
if (pos != std::string::npos) {
|
||||||
if (std::string(command).find("depth") != std::string::npos) {
|
std::string result = "";
|
||||||
// skipping to the depth number
|
pos += param.length() + 1;
|
||||||
command += 9;
|
|
||||||
|
|
||||||
depth = atoi(command);
|
while(pos <= command.length() && std::isdigit(command[pos])){
|
||||||
} else { // other command parameters
|
result += command[pos];
|
||||||
depth = 6;
|
pos++;
|
||||||
|
}
|
||||||
|
return stoi(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// search position
|
return 0;
|
||||||
SearchPosition(board, depth);
|
}
|
||||||
|
|
||||||
|
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) {
|
void UCILoop(Board &board) {
|
||||||
@@ -200,7 +212,7 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "go" command
|
// parse UCI "go" command
|
||||||
if (input.find("go") != std::string::npos) {
|
if (input.find("go") != std::string::npos) {
|
||||||
ParseGo(board, input.c_str());
|
ParseGo(board, input);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-1
@@ -2,6 +2,17 @@
|
|||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include "board.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
|
// Returns Move object from a string
|
||||||
Move ParseMove(Board &board, const char* moveString);
|
Move ParseMove(Board &board, const char* moveString);
|
||||||
|
|
||||||
@@ -10,7 +21,7 @@ void ParsePosition(Board &board, const char* command);
|
|||||||
|
|
||||||
// Parse UCI "go" command
|
// Parse UCI "go" command
|
||||||
// GUI sends the engine this command if it should start searching for the best move
|
// 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
|
// main UCI loop
|
||||||
void UCILoop(Board &board);
|
void UCILoop(Board &board);
|
||||||
Reference in new issue
Block a user