Datagen #46
No files matched your search
@@ -8,3 +8,4 @@ Eleanor
|
|||||||
# Other
|
# Other
|
||||||
.vscode
|
.vscode
|
||||||
Scripts/game.pgn
|
Scripts/game.pgn
|
||||||
|
data.binpack
|
||||||
+92
-16
@@ -1,11 +1,48 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <thread>
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "evaluate.h"
|
#include "evaluate.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
#include "stopwatch.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Function to hide the cursor on Windows
|
||||||
|
#ifdef _WIN32
|
||||||
|
void HideCursor() {
|
||||||
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
CONSOLE_CURSOR_INFO cursorInfo;
|
||||||
|
GetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||||
|
cursorInfo.bVisible = FALSE; // Set cursor visibility to false
|
||||||
|
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to show the cursor on Windows
|
||||||
|
void ShowCursor() {
|
||||||
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
CONSOLE_CURSOR_INFO cursorInfo;
|
||||||
|
GetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||||
|
cursorInfo.bVisible = TRUE; // Set cursor visibility to true
|
||||||
|
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Function to hide the cursor on Linux/macOS
|
||||||
|
void HideCursorLinux() {
|
||||||
|
std::cout << "\033[?25l"; // ANSI escape sequence to hide the cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to show the cursor on Linux/macOS
|
||||||
|
void ShowCursorLinux() {
|
||||||
|
std::cout << "\033[?25h"; // ANSI escape sequence to show the cursor
|
||||||
|
}
|
||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
@@ -43,26 +80,21 @@ static bool IsGameOver(Board &board) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Serialize MarlinFormat
|
|
||||||
std::ostream &operator<<(std::ostream &os, const MarlinFormat &format) {
|
std::ostream &operator<<(std::ostream &os, const MarlinFormat &format) {
|
||||||
os.write(reinterpret_cast<const char *>(&format), sizeof(MarlinFormat));
|
os.write(reinterpret_cast<const char *>(&format), sizeof(MarlinFormat));
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deserialize MarlinFormat
|
|
||||||
std::istream &operator>>(std::istream &is, MarlinFormat &format) {
|
std::istream &operator>>(std::istream &is, MarlinFormat &format) {
|
||||||
is.read(reinterpret_cast<char *>(&format), sizeof(MarlinFormat));
|
is.read(reinterpret_cast<char *>(&format), sizeof(MarlinFormat));
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize ScoredMove
|
|
||||||
std::ostream &operator<<(std::ostream &os, const ScoredMove &move) {
|
std::ostream &operator<<(std::ostream &os, const ScoredMove &move) {
|
||||||
os.write(reinterpret_cast<const char *>(&move), sizeof(ScoredMove));
|
os.write(reinterpret_cast<const char *>(&move), sizeof(ScoredMove));
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deserialize ScoredMove
|
|
||||||
std::istream &operator>>(std::istream &is, ScoredMove &move) {
|
std::istream &operator>>(std::istream &is, ScoredMove &move) {
|
||||||
is.read(reinterpret_cast<char *>(&move), sizeof(ScoredMove));
|
is.read(reinterpret_cast<char *>(&move), sizeof(ScoredMove));
|
||||||
return is;
|
return is;
|
||||||
@@ -70,13 +102,13 @@ std::istream &operator>>(std::istream &is, ScoredMove &move) {
|
|||||||
|
|
||||||
// Serialize Game
|
// Serialize Game
|
||||||
std::ostream &operator<<(std::ostream &os, const Game &game) {
|
std::ostream &operator<<(std::ostream &os, const Game &game) {
|
||||||
os << game.format; // Write MarlinFormat
|
os << game.format;
|
||||||
|
|
||||||
uint32_t moveCount = game.moves.size();
|
uint32_t moveCount = game.moves.size();
|
||||||
os.write(reinterpret_cast<const char *>(&moveCount), sizeof(moveCount)); // Write move count
|
os.write(reinterpret_cast<const char *>(&moveCount), sizeof(moveCount));
|
||||||
|
|
||||||
for (const auto &move : game.moves) {
|
for (const auto &move : game.moves) {
|
||||||
os << move; // Write each move
|
os << move;
|
||||||
}
|
}
|
||||||
|
|
||||||
return os;
|
return os;
|
||||||
@@ -84,40 +116,70 @@ std::ostream &operator<<(std::ostream &os, const Game &game) {
|
|||||||
|
|
||||||
// Deserialize Game
|
// Deserialize Game
|
||||||
std::istream &operator>>(std::istream &is, Game &game) {
|
std::istream &operator>>(std::istream &is, Game &game) {
|
||||||
is >> game.format; // Read MarlinFormat
|
is >> game.format;
|
||||||
|
|
||||||
uint32_t moveCount;
|
uint32_t moveCount;
|
||||||
is.read(reinterpret_cast<char *>(&moveCount), sizeof(moveCount)); // Read move count
|
is.read(reinterpret_cast<char *>(&moveCount), sizeof(moveCount));
|
||||||
|
|
||||||
game.moves.resize(moveCount);
|
game.moves.resize(moveCount);
|
||||||
for (auto &move : game.moves) {
|
for (auto &move : game.moves) {
|
||||||
is >> move; // Read each move
|
is >> move;
|
||||||
}
|
}
|
||||||
|
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteToFile(const Game &game, const std::string &filename) {
|
void WriteToFile(const Game &game, const std::string &filename) {
|
||||||
std::ofstream file(filename, std::ios::binary);
|
std::ofstream file(filename, std::ios::binary | std::ios::app);
|
||||||
if (!file) throw std::runtime_error("Failed to open file for writing");
|
if (!file) throw std::runtime_error("Failed to open file for writing");
|
||||||
|
|
||||||
// Write MarlinFormat (full format)
|
// Write MarlinFormat (full format)
|
||||||
file.write(reinterpret_cast<const char*>(&game.format), sizeof(MarlinFormat));
|
file.write(reinterpret_cast<const char*>(&game.format), sizeof(MarlinFormat));
|
||||||
|
|
||||||
// Write each ScoredMove
|
|
||||||
for (const auto &move : game.moves) {
|
for (const auto &move : game.moves) {
|
||||||
file.write(reinterpret_cast<const char*>(&move), sizeof(ScoredMove));
|
file.write(reinterpret_cast<const char*>(&move), sizeof(ScoredMove));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write 4 zero bytes at the end
|
|
||||||
uint32_t zero = 0;
|
uint32_t zero = 0;
|
||||||
file.write(reinterpret_cast<const char*>(&zero), sizeof(zero));
|
file.write(reinterpret_cast<const char*>(&zero), sizeof(zero));
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Run(int games) {
|
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch) {
|
||||||
for (int i = 0; i < games; i++) {
|
double elapsed = stopwatch.GetElapsedSec();
|
||||||
|
double positionsPerSec = elapsed > 0 ? positions / elapsed : 0;
|
||||||
|
|
||||||
|
double positionsInThousands = positions / 1000.0;
|
||||||
|
double targetInThousands = targetPositions / 1000.0;
|
||||||
|
|
||||||
|
std::cout << "\033[H";
|
||||||
|
std::cout << "Datagen: " << std::fixed << std::setprecision(2) << std::setw(6)
|
||||||
|
<< positionsInThousands << "K positions processed | "
|
||||||
|
<< std::setw(6) << targetInThousands << "K target" << std::endl;
|
||||||
|
|
||||||
|
std::cout << "Elapsed Time: " << std::fixed << std::setprecision(2) << elapsed << " sec" << std::endl;
|
||||||
|
std::cout << "Positions/sec: " << std::fixed << std::setprecision(2) << positionsPerSec << std::endl;
|
||||||
|
std::cout.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Run(int targetPositions, int threads) {
|
||||||
|
// Clear the console and hide the cursor
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("cls");
|
||||||
|
HideCursor(); // Hide the cursor on Windows
|
||||||
|
#else
|
||||||
|
std::cout << "\033[2J\033[H"; // Clear console and move cursor to top-left on Linux/macOS
|
||||||
|
HideCursorLinux(); // Hide the cursor on Linux/macOS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Stopwatch stopwatch;
|
||||||
|
stopwatch.Restart();
|
||||||
|
|
||||||
|
int positions = 0;
|
||||||
|
int lastPrintedPosition = 0;
|
||||||
|
|
||||||
|
while (positions < targetPositions * 1000000) {
|
||||||
Game game;
|
Game game;
|
||||||
Board board;
|
Board board;
|
||||||
|
|
||||||
@@ -143,6 +205,8 @@ void Run(int games) {
|
|||||||
game.moves.push_back(ScoredMove(results.bestMove, results.score));
|
game.moves.push_back(ScoredMove(results.bestMove, results.score));
|
||||||
board.MakeMove(results.bestMove);
|
board.MakeMove(results.bestMove);
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
|
|
||||||
|
positions++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) {
|
if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) {
|
||||||
@@ -151,6 +215,18 @@ void Run(int games) {
|
|||||||
|
|
||||||
game.format.packFrom(board, staticEval, wdl);
|
game.format.packFrom(board, staticEval, wdl);
|
||||||
WriteToFile(game, "data.binpack");
|
WriteToFile(game, "data.binpack");
|
||||||
|
|
||||||
|
if (positions / 100 > lastPrintedPosition) {
|
||||||
|
lastPrintedPosition = positions / 1000;
|
||||||
|
PrintProgress(positions, targetPositions * 1000000, stopwatch);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show the cursor again after the process is done
|
||||||
|
#ifdef _WIN32
|
||||||
|
ShowCursor(); // Show the cursor on Windows
|
||||||
|
#else
|
||||||
|
ShowCursorLinux(); // Show the cursor on Linux/macOS
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -54,5 +54,5 @@ struct Game {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void Run(int games);
|
void Run(int targetPositions, int threads);
|
||||||
}
|
}
|
||||||
+8
-3
@@ -18,11 +18,16 @@ int main(int argc, char* argv[]) {
|
|||||||
if (std::string(argv[1]) == "bench") {
|
if (std::string(argv[1]) == "bench") {
|
||||||
RunBenchmark();
|
RunBenchmark();
|
||||||
} else if (std::string(argv[1]) == "datagen") {
|
} else if (std::string(argv[1]) == "datagen") {
|
||||||
int count = 1;
|
int positions = 1;
|
||||||
|
int threads = 1;
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
count = std::stoi(argv[2]);
|
positions = std::stoi(argv[2]);
|
||||||
|
|
||||||
|
if (argc > 3) {
|
||||||
|
threads = std::stoi(argv[3]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DATAGEN::Run(count);
|
DATAGEN::Run(positions, threads);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
UCILoop(board);
|
UCILoop(board);
|
||||||
|
|||||||
+5
-6
@@ -148,8 +148,8 @@ Move parseMove(Board &board, std::string_view str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::array<uint8_t, 16> CompressPieces(Board &board) {
|
std::array<uint8_t, 16> CompressPieces(Board &board) {
|
||||||
const uint8_t UNMOVED_ROOK = 0x08; // We use bit 3 to mark unmoved rooks
|
const uint8_t UNMOVED_ROOK = 0x08;
|
||||||
std::array<uint8_t, 16> compressed{}; // Zero-initialize
|
std::array<uint8_t, 16> compressed{};
|
||||||
|
|
||||||
for (int square = 0; square < 64; square++) {
|
for (int square = 0; square < 64; square++) {
|
||||||
int pieceCode = 0;
|
int pieceCode = 0;
|
||||||
@@ -164,13 +164,12 @@ std::array<uint8_t, 16> CompressPieces(Board &board) {
|
|||||||
pieceCode = pieceType + 6; // Black: 7-12
|
pieceCode = pieceType + 6; // Black: 7-12
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for unmoved rooks (based solely on castling rights)
|
|
||||||
if (pieceType == Rook) {
|
if (pieceType == Rook) {
|
||||||
if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) {
|
if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) {
|
||||||
pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set)
|
pieceCode |= UNMOVED_ROOK;
|
||||||
}
|
}
|
||||||
if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) {
|
if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) {
|
||||||
pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set)
|
pieceCode |= UNMOVED_ROOK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -180,7 +179,7 @@ std::array<uint8_t, 16> CompressPieces(Board &board) {
|
|||||||
int byteIndex = square / 4;
|
int byteIndex = square / 4;
|
||||||
int shift = (square % 4) * 4;
|
int shift = (square % 4) * 4;
|
||||||
|
|
||||||
compressed[byteIndex] |= (pieceCode & 0x0F) << shift; // Pack 4-bit pieceCode
|
compressed[byteIndex] |= (pieceCode & 0x0F) << shift;
|
||||||
}
|
}
|
||||||
|
|
||||||
return compressed;
|
return compressed;
|
||||||
|
|||||||
Reference in new issue
Block a user