Skip to content

Commit 83e60c1

Browse files
committed
Implement board's tensor representation extraction
1 parent a49a937 commit 83e60c1

File tree

7 files changed

+72
-3
lines changed

7 files changed

+72
-3
lines changed

board.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ namespace Napoleon
128128
EmptySquares = ~OccupiedSquares;
129129
}
130130

131+
std::string Board::ToCsv() const
132+
{
133+
std::string csv;
134+
135+
for (auto c = PieceColor::White; c < PieceColor::None; c++) {
136+
for (Type p = PieceType::Pawn; p < PieceType::None; p++) {
137+
csv += Utils::BitBoard::ToString(Pieces(c, p)) += ",";
138+
}
139+
}
140+
141+
csv += std::to_string(sideToMove) + ",";
142+
csv += std::to_string(enPassantSquare) + ",";
143+
csv += std::to_string(castlingStatus);
144+
return csv;
145+
}
146+
131147
void Board::Display() const
132148
{
133149
Piece piece;

board.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace Napoleon
3333

3434
void LoadGame(std::string = Constants::StartPosition);
3535

36+
std::string ToCsv() const;
3637
void Display() const;
3738
void AddPiece(Piece, Square);
3839
BitBoard PlayerPieces() const;

search.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace Napoleon
3838
int Age = 0;
3939

4040
std::atomic<unsigned long> node_count(0);
41+
std::ofstream* Search::positions_dataset;
42+
bool Search::record_positions = false;
4143

4244
//int Search::param[Parameters::MAX] = { 25 , 1 , 150 , 3, 250, 50, 6 , 3 , 2 , 500 , 250 , 5 , 3 , 5 , 4 , 2 , 1 , 2 , 9}; // original
4345
int Search::param[Parameters::MAX] = { 91 , 1 , 18 , 9 , 118 , 36 , 2 , 3 , 2 , 490 , 72 , 4 , 3 , 1 , 4 , 2 , 1 , 2 , 8 , }; // tuned
@@ -209,13 +211,17 @@ namespace Napoleon
209211
{
210212
Move move = Constants::NullMove;
211213
Move toMake = Constants::NullMove;
214+
int move_score = Constants::Unknown;
212215
int score;
213216
int temp;
214217

215218
Age = (Age + 1) % 64;
216219
score = searchRoot(searchInfo.MaxDepth(), -Constants::Infinity, Constants::Infinity, move, board);
217220
searchInfo.IncrementDepth();
218-
if (score != Constants::Unknown) toMake = move;
221+
if (score != Constants::Unknown) {
222+
toMake = move;
223+
move_score = score;
224+
}
219225

220226
while ((searchInfo.MaxDepth() < 100 && !searchInfo.TimeOver()) || pondering)
221227
{
@@ -248,14 +254,24 @@ namespace Napoleon
248254

249255
score = temp;
250256

251-
if (score != Constants::Unknown)
257+
if (score != Constants::Unknown) {
252258
toMake = move;
259+
move_score = score;
260+
}
253261

254262
searchInfo.IncrementDepth();
255263
}
256264

257265
StopThinking();
258266

267+
if (record_positions)
268+
{
269+
(*positions_dataset) << board.GetFen() << ","
270+
<< board.ToCsv() << ","
271+
<< searchInfo.MaxDepth()-1 << ","
272+
<< move_score << std::endl;
273+
}
274+
259275
return toMake;
260276
}
261277

search.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <vector>
1111
#include <thread>
1212
#include <atomic>
13+
#include <fstream>
1314

1415
namespace Napoleon
1516
{
@@ -43,6 +44,8 @@ namespace Napoleon
4344
extern int cores;
4445
extern std::atomic<bool> quit;
4546
extern const int default_cores;
47+
extern std::ofstream* positions_dataset;
48+
extern bool record_positions;
4649

4750
enum Parameters : int {
4851
RAZOR1=0, RAZOR2, RAZOR3,

uci.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ namespace Napoleon
7373
while (stream >> val)
7474
PieceSquareTable[PieceType::Pawn][Opening][i++] = val;
7575
}
76-
76+
else if (token == "Record")
77+
{
78+
Search::positions_dataset = new ofstream();
79+
Search::positions_dataset->open("positions_dataset.csv", ios::out | ios::app);
80+
Search::record_positions = true;
81+
}
7782
else
7883
{
7984
for (auto i=0; i<Search::Parameters::MAX; i++)
@@ -92,6 +97,9 @@ namespace Napoleon
9297
SendCommand<Command::Generic>("Bye Bye");
9398
Search::KillThreads();
9499
exit = true;
100+
if (Search::record_positions) {
101+
Search::positions_dataset->close();
102+
}
95103
}
96104
else if (cmd == "isready")
97105
{
@@ -170,6 +178,10 @@ namespace Napoleon
170178
{
171179
Search::PonderHit = true;
172180
}
181+
else if (cmd == "csv")
182+
{
183+
std::cout << board.ToCsv() << std::endl;
184+
}
173185
/*
174186
else if (cmd == "tune")
175187
{

utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ namespace Napoleon
6767
}
6868
std::cout << "\n A B C D E F G H\n";
6969
}
70+
71+
std::string ToString(Napoleon::BitBoard bitBoard)
72+
{
73+
std::string flattened;
74+
for (int r = 7; r >= 0; r--)
75+
{
76+
for (int c = 0; c <= 7; c++)
77+
{
78+
if (IsBitSet(bitBoard, Square::GetSquareIndex(c, r)))
79+
{
80+
flattened += "1";
81+
}
82+
else
83+
{
84+
flattened += "0";
85+
}
86+
}
87+
}
88+
return flattened;
89+
}
7090
}
7191

7292
namespace Piece

utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace Napoleon
3030
bool IsBitSet(Napoleon::BitBoard, int);
3131
void Display(Napoleon::BitBoard);
3232
void SetBit(Napoleon::BitBoard&, unsigned int);
33+
std::string ToString(Napoleon::BitBoard);
3334

3435
int PopCount(Napoleon::BitBoard);
3536
int BitScanForward(Napoleon::BitBoard);

0 commit comments

Comments
 (0)