changed search functions to templates
This commit is contained in:
1 parent
3fe79ab497
commit
15a8e1c134
5 files changed
+86
-32
No files matched your search
@@ -63,7 +63,7 @@ constexpr std::array<std::string_view, 50> fenPositions = {
|
|||||||
|
|
||||||
void RunBenchmark() {
|
void RunBenchmark() {
|
||||||
Board board;
|
Board board;
|
||||||
int depth = 8;
|
int depth = BENCH_DEPTH;
|
||||||
int ply = 0;
|
int ply = 0;
|
||||||
benchStarted = true;
|
benchStarted = true;
|
||||||
searchStopped = false;
|
searchStopped = false;
|
||||||
@@ -80,7 +80,7 @@ void RunBenchmark() {
|
|||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
for (int i = 0; i < fenPositions.size(); i++) {
|
for (int i = 0; i < fenPositions.size(); i++) {
|
||||||
board.SetByFen(fenPositions[i]);
|
board.SetByFen(fenPositions[i]);
|
||||||
PVS<true>(board, depth, -inf, inf, ply);
|
SearchPosition<bench>(board, SearchParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps" << std::endl;
|
std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps" << std::endl;
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
constexpr int BENCH_DEPTH = 8;
|
||||||
|
|
||||||
void RunBenchmark();
|
void RunBenchmark();
|
||||||
+73
-28
@@ -5,6 +5,8 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
|
#include "datagen.h"
|
||||||
|
#include "benchmark.h"
|
||||||
|
|
||||||
namespace SEARCH {
|
namespace SEARCH {
|
||||||
|
|
||||||
@@ -187,12 +189,21 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool isPV>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
||||||
if (!benchStarted && nodes % 1024 == 0) {
|
if constexpr (mode != bench) {
|
||||||
if (sw.GetElapsedMS() >= timeToSearch) {
|
if (nodes % 1024 == 0) {
|
||||||
StopSearch();
|
if constexpr (mode == normal) {
|
||||||
return 0;
|
if (sw.GetElapsedMS() >= timeToSearch) {
|
||||||
|
StopSearch();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (nodes >= DATAGEN::HARD_NODES) {
|
||||||
|
StopSearch();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,7 +239,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
// Always legal so we dont check it
|
// Always legal so we dont check it
|
||||||
|
|
||||||
doingNullMove = true;
|
doingNullMove = true;
|
||||||
int score = -PVS<false>(copy, depth - 3, -beta, -beta + 1, ply + 1).score;
|
int score = -PVS<false, mode>(copy, depth - 3, -beta, -beta + 1, ply + 1).score;
|
||||||
doingNullMove = false;
|
doingNullMove = false;
|
||||||
|
|
||||||
if (searchStopped) return 0;
|
if (searchStopped) return 0;
|
||||||
@@ -275,23 +286,23 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
// First move (suspected PV node)
|
// First move (suspected PV node)
|
||||||
if (!moveSeen) {
|
if (!moveSeen) {
|
||||||
// Full search
|
// Full search
|
||||||
score = -PVS<isPV>(copy, newDepth, -beta, -alpha, ply + 1).score;
|
score = -PVS<isPV, mode>(copy, newDepth, -beta, -alpha, ply + 1).score;
|
||||||
} else if (reductions) {
|
} else if (reductions) {
|
||||||
// Null-window search with reductions
|
// Null-window search with reductions
|
||||||
score = -PVS<false>(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1).score;
|
score = -PVS<false, mode>(copy, newDepth - reductions, -alpha-1, -alpha, ply + 1).score;
|
||||||
|
|
||||||
if (score > alpha) {
|
if (score > alpha) {
|
||||||
// Null-window search now without the reduction
|
// Null-window search now without the reduction
|
||||||
score = -PVS<false>(copy, newDepth, -alpha-1, -alpha, ply + 1).score;
|
score = -PVS<false, mode>(copy, newDepth, -alpha-1, -alpha, ply + 1).score;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Null-window search
|
// Null-window search
|
||||||
score = -PVS<false>(copy, newDepth, -alpha-1, -alpha, ply + 1).score;
|
score = -PVS<false, mode>(copy, newDepth, -alpha-1, -alpha, ply + 1).score;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we need to do full window re-search
|
// Check if we need to do full window re-search
|
||||||
if (moveSeen && score > alpha && score < beta) {
|
if (moveSeen && score > alpha && score < beta) {
|
||||||
score = -PVS<isPV>(copy, newDepth, -beta, -alpha, ply + 1).score;
|
score = -PVS<isPV, mode>(copy, newDepth, -beta, -alpha, ply + 1).score;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchStopped) return 0;
|
if (searchStopped) return 0;
|
||||||
@@ -347,6 +358,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterative deepening
|
// Iterative deepening
|
||||||
|
template <searchMode mode>
|
||||||
static SearchResults ID(Board &board, SearchParams params) {
|
static SearchResults ID(Board &board, SearchParams params) {
|
||||||
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
int fullTime = board.sideToMove ? params.btime : params.wtime;
|
||||||
int inc = board.sideToMove ? params.binc : params.winc;
|
int inc = board.sideToMove ? params.binc : params.winc;
|
||||||
@@ -354,6 +366,12 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
SearchResults safeResults;
|
SearchResults safeResults;
|
||||||
safeResults.score = -inf;
|
safeResults.score = -inf;
|
||||||
|
|
||||||
|
int toDepth = MAX_DEPTH;
|
||||||
|
|
||||||
|
if constexpr (mode == bench) {
|
||||||
|
toDepth = BENCH_DEPTH;
|
||||||
|
}
|
||||||
|
|
||||||
int alpha = -inf;
|
int alpha = -inf;
|
||||||
int beta = inf;
|
int beta = inf;
|
||||||
|
|
||||||
@@ -362,13 +380,16 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
int ply = 0;
|
int ply = 0;
|
||||||
|
|
||||||
int elapsed = 0;
|
int elapsed = 0;
|
||||||
sw.Restart();
|
|
||||||
|
|
||||||
for (int depth = 1; depth <= MAX_DEPTH; depth++) {
|
if constexpr (mode == normal) {
|
||||||
|
sw.Restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int depth = 1; depth <= toDepth; depth++) {
|
||||||
timeToSearch = std::max((fullTime / 20) + (inc / 2), 4);
|
timeToSearch = std::max((fullTime / 20) + (inc / 2), 4);
|
||||||
int softTime = timeToSearch * 0.65;
|
int softTime = timeToSearch * 0.65;
|
||||||
|
|
||||||
SearchResults currentResults = PVS<true>(board, depth, alpha, beta, ply);
|
SearchResults currentResults = PVS<true, mode>(board, depth, alpha, beta, ply);
|
||||||
elapsed = sw.GetElapsedMS();
|
elapsed = sw.GetElapsedMS();
|
||||||
|
|
||||||
// If we fell outside the window, try again with full width
|
// If we fell outside the window, try again with full width
|
||||||
@@ -396,19 +417,26 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
safeResults = currentResults;
|
safeResults = currentResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "info ";
|
if constexpr (mode == normal) {
|
||||||
std::cout << "depth " << depth;
|
std::cout << "info ";
|
||||||
std::cout << " time " << elapsed;
|
std::cout << "depth " << depth;
|
||||||
std::cout << " score cp " << safeResults.score;
|
std::cout << " time " << elapsed;
|
||||||
std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec());
|
std::cout << " score cp " << safeResults.score;
|
||||||
std::cout << " hashfull " << TT.GetUsedPercentage();
|
std::cout << " nodes " << nodes << " nps " << int(nodes/sw.GetElapsedSec());
|
||||||
std::cout << " pv ";
|
std::cout << " hashfull " << TT.GetUsedPercentage();
|
||||||
pvLine.Print(0);
|
std::cout << " pv ";
|
||||||
std::cout << std::endl;
|
pvLine.Print(0);
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
if (sw.GetElapsedMS() >= softTime) {
|
if (sw.GetElapsedMS() >= softTime) {
|
||||||
StopSearch();
|
StopSearch();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
} else if constexpr (mode == datagen) {
|
||||||
|
if (nodes >= DATAGEN::SOFT_NODES) {
|
||||||
|
StopSearch();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -416,12 +444,17 @@ static SearchResults ID(Board &board, SearchParams params) {
|
|||||||
return safeResults;
|
return safeResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <searchMode mode>
|
||||||
void SearchPosition(Board &board, SearchParams params) {
|
void SearchPosition(Board &board, SearchParams params) {
|
||||||
searchStopped = false;
|
searchStopped = false;
|
||||||
nodes = 0;
|
if constexpr (mode != bench) {
|
||||||
|
nodes = 0;
|
||||||
|
}
|
||||||
pvLine.Clear();
|
pvLine.Clear();
|
||||||
|
|
||||||
SearchResults results = ID(board, params);
|
SearchResults results = ID<mode>(board, params);
|
||||||
|
|
||||||
|
if constexpr (mode != normal) return;
|
||||||
|
|
||||||
std::cout << "bestmove ";
|
std::cout << "bestmove ";
|
||||||
results.bestMove.PrintMove();
|
results.bestMove.PrintMove();
|
||||||
@@ -431,4 +464,16 @@ void SearchPosition(Board &board, SearchParams params) {
|
|||||||
void StopSearch() {
|
void StopSearch() {
|
||||||
searchStopped = true;
|
searchStopped = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template SearchResults PVS<true, searchMode::bench>(Board, int, int, int, int);
|
||||||
|
template SearchResults PVS<false, searchMode::bench>(Board, int, int, int, int);
|
||||||
|
template SearchResults PVS<true, searchMode::normal>(Board, int, int, int, int);
|
||||||
|
template SearchResults PVS<false, searchMode::normal>(Board, int, int, int, int);
|
||||||
|
template SearchResults PVS<true, searchMode::datagen>(Board, int, int, int, int);
|
||||||
|
template SearchResults PVS<false, searchMode::datagen>(Board, int, int, int, int);
|
||||||
|
|
||||||
|
template void SearchPosition<normal>(Board &board, SearchParams params);
|
||||||
|
template void SearchPosition<bench>(Board &board, SearchParams params);
|
||||||
|
template void SearchPosition<datagen>(Board &board, SearchParams params);
|
||||||
|
|
||||||
}
|
}
|
||||||
+8
-1
@@ -7,6 +7,12 @@
|
|||||||
|
|
||||||
namespace SEARCH {
|
namespace SEARCH {
|
||||||
|
|
||||||
|
enum searchMode {
|
||||||
|
normal,
|
||||||
|
bench,
|
||||||
|
datagen
|
||||||
|
};
|
||||||
|
|
||||||
constexpr int MATE_SCORE = -99000;
|
constexpr int MATE_SCORE = -99000;
|
||||||
constexpr int MAX_DEPTH = 64;
|
constexpr int MAX_DEPTH = 64;
|
||||||
constexpr int MAX_HISTORY = 16384;
|
constexpr int MAX_HISTORY = 16384;
|
||||||
@@ -101,9 +107,10 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <bool isPV>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply);
|
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply);
|
||||||
|
|
||||||
|
template <searchMode mode>
|
||||||
void SearchPosition(Board &board, SearchParams params);
|
void SearchPosition(Board &board, SearchParams params);
|
||||||
void StopSearch();
|
void StopSearch();
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -82,7 +82,7 @@ static void ParseGo(Board &board, std::string &command) {
|
|||||||
params.btime = ReadParam("movetime", command);
|
params.btime = ReadParam("movetime", command);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto worker = std::thread(SEARCH::SearchPosition, std::ref(board), params);
|
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::normal>, std::ref(board), params);
|
||||||
worker.detach();
|
worker.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user