From ebdf295eecc3a3663d3d93180945c8e27286c158 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 31 Mar 2025 05:02:44 +0200 Subject: [PATCH] added some selfplay logic --- source/datagen.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- source/datagen.h | 2 +- source/main.cpp | 7 +++++++ source/search.cpp | 16 +++++++++++----- source/search.h | 4 +++- 5 files changed, 67 insertions(+), 8 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index 8f46e0e..ca32451 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -1,9 +1,14 @@ #include +#include #include "datagen.h" #include "movegen.h" +#include "evaluate.h" +#include "search.h" +#include "movegen.h" namespace DATAGEN { +// TODO: Causes segment fault static void PlayRandMoves(Board &board) { std::random_device dev; std::mt19937 rng(dev()); @@ -17,7 +22,46 @@ static void PlayRandMoves(Board &board) { } } -void Run() { +static bool IsGameOver(Board &board) { + Board copy = board; + // Checkmate / stalemate + int moveCounter = 0; + for (int i = 0; board.currentMoveIndex; i++) { + bool isLegal = copy.MakeMove(board.moveList[i]); + if (!isLegal) continue; + positionIndex--; + moveCounter++; + } + if (moveCounter == 0) return true; + + // Draws + return SEARCH::IsDraw(board); +} + +void Run(int games) { + for (int i = 0; i < games; i++) { + Board board; + MarlinFormat position; + uint8_t wdl = 1; + positionIndex = 0; + std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); + SEARCH::history.Clear(); + + //PlayRandMoves(board); + + SEARCH::SearchResults safeResults; + + // TODO: Game loop + + if (std::abs(safeResults.score) >= SEARCH::inf - SEARCH::MAX_DEPTH) { + wdl = board.sideToMove ? 2 : 0; + } + + position.packFrom(board, HCE::Evaluate(board), wdl); + board.PrintBoard(); + std::cout << "Eval: " << position.eval << std::endl; + std::cout << "WDL: " << int(position.wdl) << std::endl; + } } } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 0e3568a..03a5b18 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -50,5 +50,5 @@ struct Game { } }; -void Run(); +void Run(int games); } \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 000c9d5..818c263 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -4,6 +4,7 @@ #include "utils.h" #include "benchmark.h" #include "search.h" +#include "datagen.h" int main(int argc, char* argv[]) { MOVEGEN::initLeaperAttacks(); @@ -16,6 +17,12 @@ int main(int argc, char* argv[]) { if (argc > 1) { if (std::string(argv[1]) == "bench") { RunBenchmark(); + } else if (std::string(argv[1]) == "datagen") { + int count = 1; + if (argc > 2) { + count = std::stoi(argv[2]); + } + DATAGEN::Run(count); } } else { UCILoop(board); diff --git a/source/search.cpp b/source/search.cpp index c208b2a..d65ffdb 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -124,6 +124,10 @@ static bool IsInsuffMat(Board &board) { && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); } +bool IsDraw(Board &board) { + return IsFifty || IsInsuffMat || IsThreefold; +} + static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -445,7 +449,7 @@ static SearchResults ID(Board &board, SearchParams params) { } template -void SearchPosition(Board &board, SearchParams params) { +SearchResults SearchPosition(Board &board, SearchParams params) { searchStopped = false; if constexpr (mode != bench) { nodes = 0; @@ -454,11 +458,13 @@ void SearchPosition(Board &board, SearchParams params) { SearchResults results = ID(board, params); - if constexpr (mode != normal) return; + if constexpr (mode != normal) return results; std::cout << "bestmove "; results.bestMove.PrintMove(); std::cout << std::endl; + + return results; } void StopSearch() { @@ -472,8 +478,8 @@ template SearchResults PVS(Board, int, int, int, int) template SearchResults PVS(Board, int, int, int, int); template SearchResults PVS(Board, int, int, int, int); -template void SearchPosition(Board &board, SearchParams params); -template void SearchPosition(Board &board, SearchParams params); -template void SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); +template SearchResults SearchPosition(Board &board, SearchParams params); } diff --git a/source/search.h b/source/search.h index a4fea4a..dd0160b 100644 --- a/source/search.h +++ b/source/search.h @@ -111,9 +111,11 @@ template SearchResults PVS(Board board, int depth, int alpha, int beta, int ply); template -void SearchPosition(Board &board, SearchParams params); +SearchResults SearchPosition(Board &board, SearchParams params); void StopSearch(); +bool IsDraw(Board &board); + constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605, 104, 204, 304, 404, 504, 604,