added command 'bench'

This commit is contained in:
rektdie committed 2024-10-15 01:26:44 +02:00
1 parent 81c52f05e3
commit c1e048c0c6
6 files changed
+119 -18

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
#include "benchmark.h"
#include "search.h"
#include <array>
#include <chrono>
const std::array<std::string, 50> fenPositions = {
"rnbq1k1r/ppp1bppp/4pn2/8/2B5/2NP1N2/PPP2PPP/R1BQR1K1 b - - 2 8",
"rnbq1k1r/pp2bppp/4pn2/2p5/2B2B2/2NP1N2/PPP2PPP/R2QR1K1 b - - 1 9",
"r1bq1k1r/pp2bppp/2n1pn2/2p5/2B1NB2/3P1N2/PPP2PPP/R2QR1K1 b - - 3 10",
"r1bq1k1r/pp2bppp/2n1p3/2p5/2B1PB2/5N2/PPP2PPP/R2QR1K1 b - - 0 11",
"r1b2k1r/pp2bppp/2n1p3/2p5/2B1PB2/5N2/PPP2PPP/3RR1K1 b - - 0 12",
"r1b1k2r/pp2bppp/2n1p3/2p5/2B1PB2/2P2N2/PP3PPP/3RR1K1 b - - 0 13",
"r1b1k2r/1p2bppp/p1n1p3/2p5/4PB2/2P2N2/PP2BPPP/3RR1K1 b - - 1 14",
"r1b1k2r/4bppp/p1n1p3/1pp5/P3PB2/2P2N2/1P2BPPP/3RR1K1 b - - 0 15",
"r1b1k2r/4bppp/p1n1p3/1P6/2p1PB2/2P2N2/1P2BPPP/3RR1K1 b - - 0 16",
"r1b1k2r/4bppp/2n1p3/1p6/2p1PB2/1PP2N2/4BPPP/3RR1K1 b - - 0 17",
"r3k2r/3bbppp/2n1p3/1p6/2P1PB2/2P2N2/4BPPP/3RR1K1 b - - 0 18",
"r3k2r/3bbppp/2n1p3/8/1pP1P3/2P2N2/3BBPPP/3RR1K1 b - - 1 19",
"1r2k2r/3bbppp/2n1p3/8/1pPNP3/2P5/3BBPPP/3RR1K1 b - - 3 20",
"1r2k2r/3bbppp/2n1p3/8/2PNP3/2B5/4BPPP/3RR1K1 b - - 0 21",
"1r2k2r/3bb1pp/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P3/r2B2P1/3K3P/8 b - - 14 42",
"5R2/2k3pp/2b5/2P1p3/4P3/3BK1P1/r6P/8 b - - 16 43",
"5R2/2k3pp/2b5/2P1p3/4P3/r2B2P1/4K2P/8 b - - 18 44",
"5R2/2k3pp/2b5/2P1p3/4P3/3B1KP1/r6P/8 b - - 20 45",
"8/2k2Rpp/2b5/2P1p3/4P3/r2B1KP1/7P/8 b - - 22 46",
"3k4/5Rpp/2b5/2P1p3/4P3/r2B2P1/4K2P/8 b - - 24 47",
"3k4/5Rpp/2b5/2P1p3/4P3/3B1KP1/r6P/8 b - - 26 48",
"3k4/5Rpp/2b5/2P1p3/4P3/r2B2P1/4K2P/8 b - - 28 49",
"3k4/5Rpp/2b5/2P1p3/4P3/3BK1P1/r6P/8 b - - 30 50",
"3k4/5Rpp/2b5/2P1p3/4P3/r2B2P1/3K3P/8 b - - 32 51",
"3k4/5Rpp/2b5/2P1p3/4P3/2KB2P1/r6P/8 b - - 34 52",
"3k4/5Rpp/2b5/2P1p3/4P3/r2B2P1/2K4P/8 b - - 36 53",
"3k4/5Rpp/2b5/2P1p3/4P3/1K1B2P1/r6P/8 b - - 38 54",
"3k4/6Rp/2b5/2P1p3/4P3/1K1B2P1/7r/8 b - - 0 55",
"3k4/8/2b3Rp/2P1p3/4P3/1K1B2P1/7r/8 b - - 1 56",
"8/2k3R1/2b4p/2P1p3/4P3/1K1B2P1/7r/8 b - - 3 57",
};
void RunBenchmark() {
Board board;
int depth = 5;
benchStarted = true;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < fenPositions.size(); i++) {
board.SetByFen(fenPositions[i].c_str());
PVS(board, depth, -50000, 50000);
}
auto finish = std::chrono::steady_clock::now();
double elapsed_seconds = std::chrono::duration_cast<
std::chrono::duration<double>>(finish - start).count();
std::cout << nodes << " nodes " << nodes/elapsed_seconds << " nps\n";
nodes = 0;
benchStarted = false;
}
+3
View File
@@ -0,0 +1,3 @@
#include <string>
void RunBenchmark();
+7 -2
View File
@@ -2,15 +2,20 @@
#include "movegen.h"
#include "uci.h"
#include "tt.h"
#include "benchmark.h"
int main() {
int main(int argc, char* argv[]) {
initLeaperAttacks();
initSliderAttacks();
InitZobrist();
Board board;
UCILoop(board);
if (std::string(argv[1]) == "bench") {
RunBenchmark();
} else {
UCILoop(board);
}
return 0;
}
+21 -16
View File
@@ -5,12 +5,15 @@
#include <chrono>
#include "tt.h"
static inline int nodes = 0;
int nodes = 0;
bool benchStarted = false;
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());
const int targetType = board.GetPieceType(move.MoveTo());
@@ -64,11 +67,13 @@ void ListScores(Board &board) {
static SearchResults Quiescence(Board board, int alpha, int beta) {
if (searchStopped) return 0;
auto currTime = std::chrono::high_resolution_clock::now();
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
if (elapsed >= timeToSearch) {
StopSearch();
return 0;
if (!benchStarted) {
auto currTime = std::chrono::high_resolution_clock::now();
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
if (elapsed >= timeToSearch) {
StopSearch();
return 0;
}
}
nodes++;
@@ -110,14 +115,16 @@ static SearchResults Quiescence(Board board, int alpha, int beta) {
return results;
}
static SearchResults PVS(Board board, int depth, int alpha, int beta) {
SearchResults PVS(Board board, int depth, int alpha, int beta) {
if (searchStopped) return 0;
auto currTime = std::chrono::high_resolution_clock::now();
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
if (elapsed >= timeToSearch) {
StopSearch();
return 0;
if (!benchStarted) {
auto currTime = std::chrono::high_resolution_clock::now();
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - timeStart).count();
if (elapsed >= timeToSearch) {
StopSearch();
return 0;
}
}
nodes++;
@@ -180,7 +187,7 @@ static SearchResults PVS(Board board, int depth, int alpha, int beta) {
}
// Iterative deepening
SearchResults ID(Board &board, SearchParams &params) {
static SearchResults ID(Board &board, SearchParams &params) {
timeStart = std::chrono::high_resolution_clock::now();
int fullTime = board.sideToMove ? params.btime : params.wtime;
@@ -204,7 +211,7 @@ SearchResults ID(Board &board, SearchParams &params) {
void SearchPosition(Board &board, SearchParams &params) {
searchStopped = false;
nodes = 0;
SearchResults results = ID(board, params);
auto currTime = std::chrono::high_resolution_clock::now();
@@ -214,10 +221,8 @@ void SearchPosition(Board &board, SearchParams &params) {
std::cout << "info " << nodes << " nodes " << nodes/elapsed << " nps\n";
std::cout << "bestmove " << squareCoords[results.bestMove.MoveFrom()]
<< squareCoords[results.bestMove.MoveTo()] << '\n';
nodes = 0;
}
void StopSearch() {
searchStopped = true;
std::cout << "Search stopped\n";
}
+4
View File
@@ -2,6 +2,9 @@
#include "board.h"
#include "uci.h"
extern int nodes;
extern bool benchStarted;
class SearchResults {
public:
int score = 0;
@@ -19,6 +22,7 @@ public:
}
};
SearchResults PVS(Board board, int depth, int alpha, int beta);
void SearchPosition(Board &board, SearchParams &params);
void StopSearch();
+7
View File
@@ -6,6 +6,7 @@
#include "types.h"
#include "movegen.h"
#include "search.h"
#include "benchmark.h"
Move ParseMove(Board &board, const char* moveString) {
GenerateMoves(board, board.sideToMove);
@@ -241,5 +242,11 @@ void UCILoop(Board &board) {
std::cout << "uciok\n";
continue;
}
// Handling non-UCI "bench" command
if (input.find("bench") != std::string::npos) {
RunBenchmark();
continue;
}
}
}