Datagen #46
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#include <random>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include "datagen.h"
|
||||
#include "movegen.h"
|
||||
#include "evaluate.h"
|
||||
@@ -42,6 +43,79 @@ static bool IsGameOver(Board &board) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Serialize MarlinFormat
|
||||
std::ostream &operator<<(std::ostream &os, const MarlinFormat &format) {
|
||||
os.write(reinterpret_cast<const char *>(&format), sizeof(MarlinFormat));
|
||||
return os;
|
||||
}
|
||||
|
||||
// Deserialize MarlinFormat
|
||||
std::istream &operator>>(std::istream &is, MarlinFormat &format) {
|
||||
is.read(reinterpret_cast<char *>(&format), sizeof(MarlinFormat));
|
||||
return is;
|
||||
}
|
||||
|
||||
// Serialize ScoredMove
|
||||
std::ostream &operator<<(std::ostream &os, const ScoredMove &move) {
|
||||
os.write(reinterpret_cast<const char *>(&move), sizeof(ScoredMove));
|
||||
return os;
|
||||
}
|
||||
|
||||
// Deserialize ScoredMove
|
||||
std::istream &operator>>(std::istream &is, ScoredMove &move) {
|
||||
is.read(reinterpret_cast<char *>(&move), sizeof(ScoredMove));
|
||||
return is;
|
||||
}
|
||||
|
||||
// Serialize Game
|
||||
std::ostream &operator<<(std::ostream &os, const Game &game) {
|
||||
os << game.format; // Write MarlinFormat
|
||||
|
||||
uint32_t moveCount = game.moves.size();
|
||||
os.write(reinterpret_cast<const char *>(&moveCount), sizeof(moveCount)); // Write move count
|
||||
|
||||
for (const auto &move : game.moves) {
|
||||
os << move; // Write each move
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
// Deserialize Game
|
||||
std::istream &operator>>(std::istream &is, Game &game) {
|
||||
is >> game.format; // Read MarlinFormat
|
||||
|
||||
uint32_t moveCount;
|
||||
is.read(reinterpret_cast<char *>(&moveCount), sizeof(moveCount)); // Read move count
|
||||
|
||||
game.moves.resize(moveCount);
|
||||
for (auto &move : game.moves) {
|
||||
is >> move; // Read each move
|
||||
}
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
void WriteToFile(const Game &game, const std::string &filename) {
|
||||
std::ofstream file(filename, std::ios::binary);
|
||||
if (!file) throw std::runtime_error("Failed to open file for writing");
|
||||
|
||||
// Write MarlinFormat (full format)
|
||||
file.write(reinterpret_cast<const char*>(&game.format), sizeof(MarlinFormat));
|
||||
|
||||
// Write each ScoredMove
|
||||
for (const auto &move : game.moves) {
|
||||
file.write(reinterpret_cast<const char*>(&move), sizeof(ScoredMove));
|
||||
}
|
||||
|
||||
// Write 4 zero bytes at the end
|
||||
uint32_t zero = 0;
|
||||
file.write(reinterpret_cast<const char*>(&zero), sizeof(zero));
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Run(int games) {
|
||||
for (int i = 0; i < games; i++) {
|
||||
Game game;
|
||||
@@ -76,6 +150,7 @@ void Run(int games) {
|
||||
}
|
||||
|
||||
game.format.packFrom(board, staticEval, wdl);
|
||||
WriteToFile(game, "data.binpack");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,8 @@ struct ScoredMove {
|
||||
uint16_t move;
|
||||
int16_t score;
|
||||
|
||||
ScoredMove() : move(0), score(0) {}
|
||||
|
||||
ScoredMove(Move m, int s) {
|
||||
move = m;
|
||||
score = s;
|
||||
|
||||
+6
-3
@@ -148,7 +148,7 @@ Move parseMove(Board &board, std::string_view str) {
|
||||
}
|
||||
|
||||
std::array<uint8_t, 16> CompressPieces(Board &board) {
|
||||
const uint8_t UNMOVED_ROOK = 0x08; // We only have 4 bits, so we use bit 3 to mark unmoved rooks
|
||||
const uint8_t UNMOVED_ROOK = 0x08; // We use bit 3 to mark unmoved rooks
|
||||
std::array<uint8_t, 16> compressed{}; // Zero-initialize
|
||||
|
||||
for (int square = 0; square < 64; square++) {
|
||||
@@ -164,9 +164,12 @@ std::array<uint8_t, 16> CompressPieces(Board &board) {
|
||||
pieceCode = pieceType + 6; // Black: 7-12
|
||||
}
|
||||
|
||||
// Check for unmoved rook
|
||||
// Check for unmoved rooks (based solely on castling rights)
|
||||
if (pieceType == Rook) {
|
||||
if ((board.castlingRights & (whiteKingRight | whiteQueenRight | blackKingRight | blackQueenRight)) != 0) {
|
||||
if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) {
|
||||
pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set)
|
||||
}
|
||||
if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) {
|
||||
pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user