Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce trained PSQT depending on king position #12

Merged
merged 2 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added psqt-769d9df255ea.dat
Binary file not shown.
24 changes: 15 additions & 9 deletions src/Evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,21 @@ MixedScore piece_square_value(Board board, EvalData& eval)
{
eval.fields[WHITE].placement = MixedScore(0, 0);
eval.fields[BLACK].placement = MixedScore(0, 0);
Bitboard bb;

for (auto piece : { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING })
Square king_sq[] = { board.get_pieces<WHITE, KING>().bitscan_forward(),
board.get_pieces<BLACK, KING>().bitscan_forward() };

for (PieceType p : { PAWN, KNIGHT, BISHOP, ROOK, QUEEN })
{
for (auto turn : { WHITE, BLACK })
for (Turn t : { WHITE, BLACK })
{
bb = board.get_pieces(turn, piece);
while (bb)
eval.fields[turn].placement += piece_square(piece, bb.bitscan_forward_reset(), turn);
Bitboard b = board.get_pieces(t, p);
while (b)
{
Square s = b.bitscan_forward_reset();
eval.fields[t].placement += piece_square(p, s, t, king_sq[WHITE], WHITE)
+ piece_square(p, s, t, king_sq[BLACK], BLACK);
}
}
}

Expand Down Expand Up @@ -490,8 +496,8 @@ Score evaluation(const Board& board, EvalData& data, Thread& thread)
MaterialEntry* me = probe_material(board, thread.m_material_table);
mixed_result += me->imbalance();

// Material and PSQT: incrementally updated in the position
mixed_result += board.material() + board.psq();
// Material incrementally updated in the position
mixed_result += board.material();

// Pawn structure
mixed_result += pawns(board, data);
Expand All @@ -512,7 +518,7 @@ Score evaluation(const Board& board, EvalData& data, Thread& thread)
mixed_result += passed<WHITE>(board, data) - passed<BLACK>(board, data);

// Tapered eval
Score result = scale(board, data, mixed_result);
Score result = scale(board, data, mixed_result) + board.psq();

// We don't return exact draw scores -> add one unit to the moving side
if (result == SCORE_DRAW)
Expand Down
91 changes: 91 additions & 0 deletions src/PieceSquareTables.cpp
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];
}
99 changes: 22 additions & 77 deletions src/PieceSquareTables.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once
#include "incbin/incbin.h"
#include "Types.hpp"

#define PSQT_Default_File "psqt-769d9df255ea.dat"

using S = MixedScore;

constexpr S PawnValue( 125, 200);
Expand Down Expand Up @@ -37,88 +40,30 @@ constexpr Score piece_value_eg[] = { PawnValue.endgame(),
0, // Empty
0 }; // PIECE_NONE

// Tables stolen from Stockfish using the implicitly mirrored convention
constexpr S psq_table[][8][4] = {
{ // Knight
{ S(-175, -96), S(-92, -65), S(-74, -49), S(-73, -21) },
{ S( -77, -67), S(-41, -54), S(-27, -18), S(-15, 8) },
{ S( -61, -40), S(-17, -27), S( 6, -8), S( 12, 29) },
{ S( -35, -35), S( 8, -2), S( 40, 13), S( 49, 28) },
{ S( -34, -45), S( 13, -16), S( 44, 9), S( 51, 39) },
{ S( -9, -51), S( 22, -44), S( 58, -16), S( 53, 17) },
{ S( -67, -69), S(-27, -50), S( 4, -51), S( 37, 12) },
{ S(-201, -100), S(-83, -88), S(-56, -56), S(-26, -17) }
},
{ // Bishop
{ S(-37, -40), S( -4, -21), S( -6, -26), S(-16, -8) },
{ S(-11, -26), S( 6, -9), S( 13, -12), S( 3, 1) },
{ S( -5, -11), S( 15, -1), S( -4, -1), S( 12, 7) },
{ S( -4, -14), S( 8, -4), S( 18, 0), S( 27, 12) },
{ S( -8, -12), S( 20, -1), S( 15, -10), S( 22, 11) },
{ S(-11, -21), S( 4, 4), S( 1, 3), S( 8, 4) },
{ S(-12, -22), S(-10, -14), S( 4, -1), S( 0, 1) },
{ S(-34, -32), S( 1, -29), S(-10, -26), S(-16, -17) }
},
{ // Rook
{ S(-31, -9), S(-20, -13), S(-14, -10), S(-5, -9) },
{ S(-21, -12), S(-13, -9), S( -8, -1), S( 6, -2) },
{ S(-25, 6), S(-11, -8), S( -1, -2), S( 3, -6) },
{ S(-13, -6), S( -5, 1), S( -4, -9), S(-6, 7) },
{ S(-27, -5), S(-15, 8), S( -4, 7), S( 3, -6) },
{ S(-22, 6), S( -2, 1), S( 6, -7), S(12, 10) },
{ S( -2, 4), S( 12, 5), S( 16, 20), S(18, -5) },
{ S(-17, 18), S(-19, 0), S( -1, 19), S( 9, 13) }
},
{ // Queen
{ S( 3, -69), S(-5, -57), S(-5, -47), S( 4, -26) },
{ S(-3, -54), S( 5, -31), S( 8, -22), S(12, -4) },
{ S(-3, -39), S( 6, -18), S(13, -9), S( 7, 3) },
{ S( 4, -23), S( 5, -3), S( 9, 13), S( 8, 24) },
{ S( 0, -29), S(14, -6), S(12, 9), S( 5, 21) },
{ S(-4, -38), S(10, -18), S( 6, -11), S( 8, 1) },
{ S(-5, -50), S( 6, -27), S(10, -24), S( 8, -8) },
{ S(-2, -74), S(-2, -52), S( 1, -43), S(-2, -34) }
},
{ // King
{ S(271, 1), S(327, 45), S(271, 85), S(198, 76) },
{ S(278, 53), S(303, 100), S(234, 133), S(179, 135) },
{ S(195, 88), S(258, 130), S(169, 169), S(120, 175) },
{ S(164, 103), S(190, 156), S(138, 172), S( 98, 172) },
{ S(154, 96), S(179, 166), S(105, 199), S( 70, 199) },
{ S(123, 92), S(145, 172), S( 81, 184), S( 31, 191) },
{ S( 88, 47), S(120, 121), S( 65, 116), S( 33, 131) },
{ S( 59, 11), S( 89, 59), S( 45, 73), S( -1, 78) }
}
};


constexpr S psq_table_pawns[6][8] =
{ // Pawn (asymmetric distribution)
{ S( 2, -8), S( 4, -6), S( 11, 9), S( 18, 5), S(16, 16), S( 21, 6), S( 9, -6), S( -3, -18) },
{ S(-9, -9), S(-15, -7), S( 11, -10), S( 15, 5), S(31, 2), S( 23, 3), S( 6, -8), S(-20, -5) },
{ S(-3, 7), S(-20, 1), S( 8, -8), S( 19, -2), S(39, -14), S( 17, -13), S( 2, -11), S( -5, -6) },
{ S(11, 12), S( -4, 6), S(-11, 2), S( 2, -6), S(11, -5), S( 0, -4), S(-12, 14), S( 5, 9) },
{ S( 3, 27), S(-11, 18), S( -6, 19), S( 22, 29), S(-8, 30), S( -5, 9), S(-14, 8), S(-11, 14) },
{ S(-7, -1), S( 6, -14), S( -2, 13), S(-11, 22), S( 4, 24), S(-14, 17), S( 10, 7), S( -9, 7) }
};


constexpr S imbalance_terms[][NUM_PIECE_TYPES] =
{ // Pawn Knight Bishop Rook Queen
{ S( 0, 0) }, // Pawn
{ S(14, 10), S(-5, -6) }, // Knight
{ S( 6, 7), S( 1, 2), S( 0, 0) }, // Bishop
{ S( 0, 0), S( 7, 4), S( 9, 8), S(-12, -11) }, // Rook
{ S( 0, 0), S( 9, 9), S(10, 7), S(-11, -13), S(0, 0) } // Queen
{ S( 0, 0) }, // Pawn
{ S(14, 10), S(-5, -6) }, // Knight
{ S( 6, 7), S( 1, 2), S( 0, 0) }, // Bishop
{ S( 0, 0), S( 7, 4), S( 9, 8), S(-12, -11) }, // Rook
{ S( 0, 0), S( 9, 9), S(10, 7), S(-11, -13), S(0, 0) } // Queen
};


constexpr MixedScore piece_square(PieceType piece, Square square, Turn turn)
namespace PSQT
{
// Correct piece position for table lookup
int sq_rank = (turn == WHITE) ? rank(square) : (7 - rank(square));
if (piece == PAWN)
return psq_table_pawns[sq_rank - 1][file(square)];
else
return psq_table[piece - 1][sq_rank][horizontal_distance(square)] / 2;
using FeatureType = int16_t;
static constexpr std::size_t NUM_FEATURES = 20480;

extern const FeatureType* psqt_data;

void init();

void load(std::string file);

void clean();
}


Score piece_square(PieceType piece, Square square, Turn turn, Square king_sq, Turn king_turn);
47 changes: 41 additions & 6 deletions src/Position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ Board::Board()
Board::Board(std::string fen)
: m_hash(0),
m_material_hash(Zobrist::get_initial_material_hash()),
m_psq(0, 0),
m_phase(Phases::Total)
{
auto c = fen.cbegin();

// Default initialisation for board pieces
std::memset(m_board_pieces, PIECE_NONE, sizeof(m_board_pieces));
std::memset(m_piece_count, 0, sizeof(m_piece_count));
std::memset(m_psq, 0, sizeof(m_psq));
std::memset(m_king_sq, 0, sizeof(m_king_sq));

// Read position
Square square = SQUARE_A8;
Expand Down Expand Up @@ -120,6 +121,11 @@ Board::Board(std::string fen)
m_hash ^= Zobrist::get_ep_file(file(m_enpassant_square));

update_checkers();

m_king_sq[WHITE] = m_pieces[KING][WHITE].bitscan_forward();
m_king_sq[BLACK] = m_pieces[KING][BLACK].bitscan_forward();
regen_psqt(WHITE);
regen_psqt(BLACK);
}


Expand Down Expand Up @@ -213,6 +219,24 @@ void Board::update_checkers()
}


void Board::regen_psqt(Turn turn)
{
m_psq[turn] = 0;
for (PieceType p : { PAWN, KNIGHT, BISHOP, ROOK, QUEEN })
{
for (Turn t : { WHITE, BLACK })
{
Bitboard b = get_pieces(t, p);
while (b)
{
Square s = b.bitscan_forward_reset();
m_psq[turn] += piece_square(p, s, t, m_king_sq[turn], turn) * turn_to_color(t);
}
}
}
}


void Board::generate_moves(MoveList& list, MoveGenType type) const
{
if (m_turn == WHITE)
Expand Down Expand Up @@ -294,6 +318,13 @@ Board Board::make_move(Move move) const
result.move_piece(piece, m_turn, move.from(), move.to());
}

// After a king move, update PSQ tables
if (piece == KING)
{
result.m_king_sq[m_turn] = result.m_pieces[KING][m_turn].bitscan_forward();
result.regen_psqt(m_turn);
}

// Swap turns
result.m_turn = ~m_turn;
result.m_hash ^= Zobrist::get_black_move();
Expand Down Expand Up @@ -343,7 +374,7 @@ bool Board::is_valid() const
// Material and phase evaluation
uint8_t phase = Phases::Total;
MixedScore material(0, 0);
MixedScore psq(0, 0);
Score psq_sides[2] = { Score(0), Score(0) };
Hash material_hash = 0;
for (PieceType piece : { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING })
for (Turn turn : { WHITE, BLACK })
Expand All @@ -355,14 +386,18 @@ bool Board::is_valid() const
phase -= bb.count() * Phases::Pieces[piece];
material_hash ^= Zobrist::get_piece_turn_square(piece, turn, bb.count());
while (bb)
psq += piece_square(piece, bb.bitscan_forward_reset(), turn) * turn_to_color(turn);
{
Square s = bb.bitscan_forward_reset();
psq_sides[WHITE] += piece_square(piece, s, turn, m_king_sq[WHITE], WHITE) * turn_to_color(turn);
psq_sides[BLACK] += piece_square(piece, s, turn, m_king_sq[BLACK], BLACK) * turn_to_color(turn);
}
}
MixedScore total_material = m_material[WHITE] - m_material[BLACK];
if (phase != m_phase)
return false;
if (material.middlegame() != total_material.middlegame() || material.endgame() != total_material.endgame())
return false;
if (psq.middlegame() != m_psq.middlegame() || psq.endgame() != m_psq.endgame())
if (psq_sides[WHITE] != m_psq[WHITE] || psq_sides[BLACK] != m_psq[BLACK])
return false;
if (material_hash != m_material_hash)
return false;
Expand Down Expand Up @@ -527,9 +562,9 @@ MixedScore Board::material(Turn turn) const
}


MixedScore Board::psq() const
Score Board::psq() const
{
return m_psq;
return m_psq[WHITE] + m_psq[BLACK];
}


Expand Down
Loading