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

Add non-pawn evaluation correction history #765

Merged
merged 1 commit into from
Jan 3, 2025
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
16 changes: 16 additions & 0 deletions src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ static Key GenMaterialKey(const Position *pos) {
return key;
}

// Generates a hash key from scratch
static Key GenNonPawnKey(const Position *pos, const Color color) {

Key key = 0;

for (Square sq = A1; sq <= H8; ++sq)
if (pieceOn(sq) != EMPTY && ColorOf(pieceOn(sq)) == color && PieceTypeOf(pieceOn(sq)) != PAWN)
key ^= PieceKeys[pieceOn(sq)][sq];

return key;
}

// Calculates the position key after a move. Fails
// for special moves.
Key KeyAfter(const Position *pos, const Move move) {
Expand Down Expand Up @@ -241,6 +253,8 @@ void ParseFen(const char *fen, Position *pos) {
pos->checkers = Checkers(pos);
pos->key = GenPosKey(pos);
pos->materialKey = GenMaterialKey(pos);
pos->nonPawnKey[WHITE] = GenNonPawnKey(pos, WHITE);
pos->nonPawnKey[BLACK] = GenNonPawnKey(pos, BLACK);
pos->phase = UpdatePhase(pos->phaseValue);

free(copy);
Expand Down Expand Up @@ -528,6 +542,8 @@ bool PositionOk(const Position *pos) {
assert(GenPosKey(pos) == pos->key);
assert(GenMaterialKey(pos) == pos->materialKey);
assert(GenPawnKey(pos) == pos->pawnKey);
assert(GenNonPawnKey(pos, WHITE) == pos->nonPawnKey[WHITE]);
assert(GenNonPawnKey(pos, BLACK) == pos->nonPawnKey[BLACK]);

assert(!KingAttacked(pos, !sideToMove));

Expand Down
1 change: 1 addition & 0 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct Position {
Key key;
Key materialKey;
Key pawnKey;
Key nonPawnKey[COLOR_NB];

uint64_t nodes;
int trend;
Expand Down
6 changes: 6 additions & 0 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define PawnCorrEntry() (&thread->pawnCorrHistory[thread->pos.stm][PawnCorrIndex(&thread->pos)])
#define MatCorrEntry() (&thread->matCorrHistory[thread->pos.stm][MatCorrIndex(&thread->pos)])
#define ContCorrEntry(offset) (&(*(ss-offset)->contCorr)[piece((ss-1)->move)][toSq((ss-1)->move)])
#define NonPawnCorrEntry(color) (&thread->nonPawnCorrHistory[color][thread->pos.stm][NonPawnCorrIndex(&thread->pos, color)])

#define QuietHistoryUpdate(move, bonus) (HistoryBonus(QuietEntry(move), bonus, 5280))
#define PawnHistoryUpdate(move, bonus) (HistoryBonus(PawnEntry(move), bonus, 9275))
Expand All @@ -41,11 +42,13 @@
#define PawnCorrHistoryUpdate(bonus) (HistoryBonus(PawnCorrEntry(), bonus, 1662))
#define MatCorrHistoryUpdate(bonus) (HistoryBonus(MatCorrEntry(), bonus, 1077))
#define ContCorrHistoryUpdate(offset, bonus) (HistoryBonus(ContCorrEntry(offset), bonus, 1220))
#define NonPawnCorrHistoryUpdate(bonus, color) (HistoryBonus(NonPawnCorrEntry(color), bonus, 1024))


INLINE int PawnStructure(const Position *pos) { return pos->pawnKey & (PAWN_HISTORY_SIZE - 1); }
INLINE int PawnCorrIndex(const Position *pos) { return pos->pawnKey & (CORRECTION_HISTORY_SIZE - 1); }
INLINE int MatCorrIndex(const Position *pos) { return pos->materialKey & (CORRECTION_HISTORY_SIZE - 1); }
INLINE int NonPawnCorrIndex(const Position *pos, Color c) { return pos->nonPawnKey[c] & (CORRECTION_HISTORY_SIZE - 1); }


INLINE void HistoryBonus(int16_t *entry, int bonus, int div) {
Expand Down Expand Up @@ -119,6 +122,8 @@ INLINE void UpdateCorrectionHistory(Thread *thread, Stack *ss, int bestScore, in
int bonus = CorrectionBonus(bestScore, eval, depth);
PawnCorrHistoryUpdate(bonus);
MatCorrHistoryUpdate(bonus);
NonPawnCorrHistoryUpdate(bonus, WHITE);
NonPawnCorrHistoryUpdate(bonus, BLACK);
ContCorrHistoryUpdate(2, bonus);
ContCorrHistoryUpdate(3, bonus);
ContCorrHistoryUpdate(4, bonus);
Expand Down Expand Up @@ -146,6 +151,7 @@ INLINE int GetHistory(const Thread *thread, Stack *ss, Move move) {
INLINE int GetCorrectionHistory(const Thread *thread, const Stack *ss) {
int c = 6554 * *PawnCorrEntry()
+ 4520 * *MatCorrEntry()
+ 7000 * (*NonPawnCorrEntry(WHITE) + *NonPawnCorrEntry(BLACK))
+ 3121 * *ContCorrEntry(2)
+ 2979 * *ContCorrEntry(3)
+ 2849 * *ContCorrEntry(4)
Expand Down
6 changes: 6 additions & 0 deletions src/makemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ static void ClearPiece(Position *pos, const Square sq, const bool hash) {

if (PieceTypeOf(piece) == PAWN)
pos->pawnKey ^= PieceKeys[piece][sq];
else
pos->nonPawnKey[color] ^= PieceKeys[piece][sq];

// Set square to empty
pieceOn(sq) = EMPTY;
Expand Down Expand Up @@ -82,6 +84,8 @@ static void AddPiece(Position *pos, const Square sq, const Piece piece, const bo

if (PieceTypeOf(piece) == PAWN)
pos->pawnKey ^= PieceKeys[piece][sq];
else
pos->nonPawnKey[color] ^= PieceKeys[piece][sq];

// Update square
pieceOn(sq) = piece;
Expand Down Expand Up @@ -119,6 +123,8 @@ static void MovePiece(Position *pos, const Square from, const Square to, const b

if (PieceTypeOf(piece) == PAWN)
pos->pawnKey ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];
else
pos->nonPawnKey[color] ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];

// Set old square to empty, new to piece
pieceOn(from) = EMPTY;
Expand Down
1 change: 1 addition & 0 deletions src/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ typedef struct Thread {
ContinuationHistory continuation[2][2];
CorrectionHistory pawnCorrHistory;
CorrectionHistory matCorrHistory;
CorrectionHistory nonPawnCorrHistory[COLOR_NB];
ContiuationCorrectionHistory contCorrHistory;

int index;
Expand Down
Loading