-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce trained PSQT depending on king position
This patch introduces PSQ tables dependent on the attacking/defending king position. Similarly to regular PSQ tables, these can be efficiently updated for most moves, and only need to be recomputed from scratch after a king move. The king's position is encoded in a mirrored fashion. The current table has been trained from a dataset of roughly 8M games at depth 6 (generated using the branch data_gen). The default table is embedded in the binary using incbin. STC: LLR: 2.94/2.94<0.00, 6.00> Elo diff: 70.84 [52.71, 89.35] (95%) Games: 728 W: 271 L: 124 D: 333 Draw ratio: 45.7% Pntl: [10, 57, 117, 135, 44] LTC: LLR: 2.95/2.94<0.00, 6.00> Elo diff: 87.82 [69.86, 106.26] (95%) Games: 608 W: 227 L: 76 D: 305 Draw ratio: 50.2% Pntl: [2, 35, 113, 117, 36] Closes #12 Bench: 2169197
- Loading branch information
1 parent
567797f
commit 1aa3bb3
Showing
12 changed files
with
696 additions
and
98 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "Types.hpp" | ||
#include "PieceSquareTables.hpp" | ||
#include <iostream> | ||
#include <fstream> | ||
#include <string> | ||
|
||
namespace PSQT | ||
{ | ||
const FeatureType* psqt_data; | ||
|
||
|
||
INCBIN(FeatureType, EmbeddedPSQT, PSQT_Default_File); | ||
|
||
|
||
void init() | ||
{ | ||
psqt_data = nullptr; | ||
load(PSQT_Default_File); | ||
} | ||
|
||
|
||
void load(std::string file) | ||
{ | ||
if (psqt_data) | ||
clean(); | ||
|
||
if (file == "" || file == PSQT_Default_File) | ||
{ | ||
psqt_data = gEmbeddedPSQTData; | ||
} | ||
else | ||
{ | ||
std::ifstream input(file, std::ios_base::binary); | ||
if (!input.is_open()) | ||
{ | ||
std::cerr << "Failed to open PSQ input file!" << std::endl; | ||
std::abort(); | ||
} | ||
|
||
psqt_data = new FeatureType[NUM_FEATURES]; | ||
|
||
if(!input.read(const_cast<char*>(reinterpret_cast<const char*>(psqt_data)), NUM_FEATURES * sizeof(FeatureType))) | ||
{ | ||
std::cerr << "Failed to read PSQ data!" << std::endl; | ||
std::abort(); | ||
} | ||
} | ||
} | ||
|
||
|
||
void clean() | ||
{ | ||
if (psqt_data != gEmbeddedPSQTData) | ||
delete[] psqt_data; | ||
psqt_data = nullptr; | ||
} | ||
} | ||
|
||
|
||
Score piece_square(PieceType piece, Square square, Turn turn, Square king_sq, Turn king_turn) | ||
{ | ||
// No PSQ data for the king | ||
if (piece == KING) | ||
return 0; | ||
|
||
// Vertical mirror for black | ||
if (turn == BLACK) | ||
{ | ||
king_turn = ~king_turn; | ||
square = vertical_mirror(square); | ||
king_sq = vertical_mirror(king_sq); | ||
} | ||
|
||
// Horiziontal mirror if king on the files E to H | ||
if (file(king_sq) >= 4) | ||
{ | ||
square = horizontal_mirror(square); | ||
king_sq = horizontal_mirror(king_sq); | ||
} | ||
|
||
// Compute corrected king index (since we are mirrored there are only 4 files) | ||
int king_index = 4 * rank(king_sq) + file(king_sq); | ||
|
||
// Compute PSQ table index | ||
int index = square | ||
+ piece * NUM_SQUARES | ||
+ king_index * (NUM_SQUARES * (NUM_PIECE_TYPES - 1)) | ||
+ king_turn * (NUM_SQUARES * (NUM_PIECE_TYPES - 1) * NUM_SQUARES / 2); | ||
|
||
return PSQT::psqt_data[index]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.