Datagen #46

Merged
rektdie merged 27 commits from datagen into main 2025-04-03 03:54:14 +02:00
4 changed files with 52 additions and 43 deletions
Showing only changes of commit 649bee6490 - Show all commits

No files matched your search

+36 -15
View File
@@ -19,16 +19,34 @@ static void PlayRandMoves(Board &board) {
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
if (!isLegal) count--;
}
MOVEGEN::GenerateMoves<All>(board);
}
static bool IsGameOver(Board &board) {
// TODO
bool isDraw = SEARCH::IsDraw(board);
if (isDraw) return true;
int moveSeen = 0;
for (int i = 0; i < board.currentMoveIndex; i++) {
Board copy = board;
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
positionIndex--;
moveSeen++;
}
if (moveSeen == 0) return true;
return false;
}
void Run(int games) {
for (int i = 0; i < games; i++) {
Game game;
Board board;
MarlinFormat position;
uint8_t wdl = 1;
positionIndex = 0;
std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves));
@@ -36,25 +54,28 @@ void Run(int games) {
PlayRandMoves(board);
int staticEval = HCE::Evaluate(board);
SEARCH::SearchResults safeResults;
// while (!IsGameOver(board)) {
// SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams());
// if (!results.bestMove) break;
// safeResults = results;
MOVEGEN::GenerateMoves<All>(board);
// board.MakeMove(results.bestMove);
// }
if (std::abs(safeResults.score) >= SEARCH::inf - SEARCH::MAX_DEPTH) {
while (!IsGameOver(board)) {
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams());
if (!results.bestMove) break;
safeResults = results;
game.moves.push_back(ScoredMove(results.bestMove, results.score));
board.MakeMove(results.bestMove);
MOVEGEN::GenerateMoves<All>(board);
}
if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) {
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;
game.format.packFrom(board, staticEval, wdl);
}
}
}
+2
View File
@@ -44,6 +44,8 @@ struct Game {
MarlinFormat format;
std::vector<ScoredMove> moves;
Game(){};
Game(MarlinFormat f, std::vector<ScoredMove> m) {
format = f;
moves = m;
+3 -3
View File
@@ -323,6 +323,9 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
score = -PVS<isPV, mode>(copy, newDepth, -beta, -alpha, ply + 1).score;
}
moveSeen++;
positionIndex--;
if (searchStopped) return 0;
if (currMove.IsQuiet()) {
@@ -330,9 +333,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
seenQuietsCount++;
}
moveSeen++;
positionIndex--;
// Fail high (beta cutoff)
if (score >= beta) {
if (!currMove.IsCapture()) {
+11 -25
View File
@@ -148,11 +148,10 @@ Move parseMove(Board &board, std::string_view str) {
}
std::array<uint8_t, 16> CompressPieces(Board &board) {
const uint8_t UNMOVED_ROOK = 0x10; // Special value for unmoved rooks
const uint8_t UNMOVED_ROOK = 0x08; // We only have 4 bits, so we use bit 3 to mark unmoved rooks
std::array<uint8_t, 16> compressed{}; // Zero-initialize
std::array<uint8_t, 16> compressed;
for (int square = a1; square <= h8; square++) {
for (int square = 0; square < 64; square++) {
int pieceCode = 0;
for (int pieceType = Pawn; pieceType <= King; pieceType++) {
@@ -160,38 +159,25 @@ std::array<uint8_t, 16> CompressPieces(Board &board) {
if (pieceBitboard.IsSet(square)) {
if (board.colors[White] & board.pieces[pieceType]) {
pieceCode = pieceType + 1; // White pieces: 1-6
pieceCode = pieceType; // White: 1-6
} else {
pieceCode = pieceType + 7; // Black pieces: 7-12
pieceCode = pieceType + 6; // Black: 7-12
}
// If it's a rook and hasn't moved (check castling rights)
// Check for unmoved rook
if (pieceType == Rook) {
if (board.sideToMove == White) {
// If the white rooks are still eligible for castling, they haven't moved
if ((board.castlingRights & (whiteKingRight | whiteQueenRight)) != 0) {
pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook
}
} else if (board.sideToMove == Black) {
// If the black rooks are still eligible for castling, they haven't moved
if ((board.castlingRights & (blackKingRight | blackQueenRight)) != 0) {
pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook
}
if ((board.castlingRights & (whiteKingRight | whiteQueenRight | blackKingRight | blackQueenRight)) != 0) {
pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set)
}
}
break;
}
}
// Determine the byte index for storing the piece code (since we are packing 2 pieces per byte)
int byteIndex = square / 2;
int byteIndex = square / 4;
int shift = (square % 4) * 4;
// Pack the piece code into the byte array
if (square % 2 == 0) {
compressed[byteIndex] = (compressed[byteIndex] & 0xF0) | (pieceCode & 0x0F);
} else {
compressed[byteIndex] = (compressed[byteIndex] & 0x0F) | ((pieceCode & 0x0F) << 4);
}
compressed[byteIndex] |= (pieceCode & 0x0F) << shift; // Pack 4-bit pieceCode
}
return compressed;