added some selfplay logic

This commit is contained in:
rektdie committed 2025-03-31 05:02:44 +02:00
1 parent d703ea6010
commit ebdf295eec
5 files changed
+67 -8

No files matched your search

+45 -1
View File
@@ -1,9 +1,14 @@
#include <random>
#include <cstring>
#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;
}
}
}
+1 -1
View File
@@ -50,5 +50,5 @@ struct Game {
}
};
void Run();
void Run(int games);
}
+7
View File
@@ -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);
+11 -5
View File
@@ -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 <searchMode mode>
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<mode>(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<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);
template SearchResults SearchPosition<normal>(Board &board, SearchParams params);
template SearchResults SearchPosition<bench>(Board &board, SearchParams params);
template SearchResults SearchPosition<datagen>(Board &board, SearchParams params);
}
+3 -1
View File
@@ -111,9 +111,11 @@ template <bool isPV, searchMode mode>
SearchResults PVS(Board board, int depth, int alpha, int beta, int ply);
template <searchMode mode>
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,