From c6eb3f464b4adfbcfe8e8cdefb491839a040a17c Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 15 Apr 2025 14:27:19 +0200 Subject: [PATCH] datagen could get in an inf loop if random moves resulted in game over --- source/datagen.cpp | 45 ++++++++++++++++++++++++--------------------- source/search.cpp | 19 ++++++++++++------- source/uci.cpp | 7 +++++++ 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index df17e6c..218d0f5 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -42,24 +42,6 @@ static void ShowCursorLinux() { namespace DATAGEN { -static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) { - std::random_device dev; - std::mt19937 rng(dev()); - - for (int count = 0; count < RAND_MOVES; count++) { - MOVEGEN::GenerateMoves(board); - std::uniform_int_distribution dist(0, board.currentMoveIndex - 1); - - bool isLegal = board.MakeMove(board.moveList[dist(rng)]); - - - if (!isLegal) count--; - ctx.positionHistory[board.positionIndex] = board.hashKey; - } - - MOVEGEN::GenerateMoves(board); -} - static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { if (SEARCH::IsDraw(board, ctx)) return true; @@ -70,10 +52,30 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - moveSeen++; } + moveSeen++; + } - if (moveSeen == 0) return true; - return false; + return moveSeen == 0; +} + +static void PlayRandMoves(Board &board, SEARCH::SearchContext& ctx) { + std::random_device dev; + std::mt19937_64 rng(dev()); + + for (int count = 0; count < RAND_MOVES; count++) { + MOVEGEN::GenerateMoves(board); + std::uniform_int_distribution dist(0, board.currentMoveIndex - 1); + + bool isLegal = board.MakeMove(board.moveList[dist(rng)]); + + if (!isLegal) { + count--; + continue; + } + ctx.positionHistory[board.positionIndex] = board.hashKey; + MOVEGEN::GenerateMoves(board); + if (IsGameOver(board, ctx)) break; + } } static void WriteToFile(std::vector &gamesBuffer, std::ofstream &file) { @@ -104,6 +106,7 @@ static void PlayGames(int id, std::atomic& positions, std::atomic& st Board board; SEARCH::SearchContext ctx; PlayRandMoves(board, ctx); + if (IsGameOver(board, ctx)) continue; SearchResults safeResults; diff --git a/source/search.cpp b/source/search.cpp index 6e3e9a0..c810e61 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -138,11 +138,16 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear } } } - } else { + } else if constexpr (mode == nodesMode) { if (ctx.nodes > ctx.nodesToGo) { ctx.searchStopped = true; return 0; } + } else if constexpr (mode == datagen) { + if (ctx.nodes > DATAGEN::HARD_NODES) { + ctx.searchStopped = true; + return 0; + } } int bestScore = HCE::Evaluate(board); @@ -194,18 +199,13 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear template SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext& ctx) { - if constexpr (mode != bench || mode != nodesMode) { + if constexpr (mode != bench && mode != nodesMode) { if (ctx.nodes % 1024 == 0) { if constexpr (mode == normal) { if (ctx.sw.GetElapsedMS() >= ctx.timeToSearch) { ctx.searchStopped = true; return 0; } - } else if constexpr (mode == datagen) { - if (ctx.nodes >= DATAGEN::HARD_NODES) { - ctx.searchStopped = true; - return 0; - } } } } else if constexpr (mode == nodesMode) { @@ -213,6 +213,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx.searchStopped = true; return 0; } + } else if constexpr (mode == datagen) { + if (ctx.nodes > DATAGEN::HARD_NODES) { + ctx.searchStopped = true; + return 0; + } } ctx.pvLine.SetLength(ply); diff --git a/source/uci.cpp b/source/uci.cpp index 357105c..3f2c4e5 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -10,6 +10,7 @@ #include "benchmark.h" #include "perft.h" #include "utils.h" +#include "datagen.h" static void ParsePosition(Board &board, std::string_view command, SEARCH::SearchContext& ctx) { if (command.find("startpos") != std::string::npos) { @@ -197,6 +198,12 @@ void UCILoop(Board &board) { if (input.find("nnue") != std::string::npos) { board.PrintNNUE(); + continue; + } + + if (input.find("datagen") != std::string::npos) { + DATAGEN::Run(500000*2, 22); + continue; } } }