Skip to content

Commit

Permalink
non-pawn corrhist (#476)
Browse files Browse the repository at this point in the history
Elo   | 6.98 +- 3.56 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=16MB
LLR   | 2.91 (-2.25, 2.89) [0.00, 3.00]
Games | N: 9812 W: 2462 L: 2265 D: 5085
Penta | [21, 1120, 2446, 1279, 40]

Elo   | 12.28 +- 4.75 (95%)
SPRT  | 40.0+0.40s Threads=1 Hash=64MB
LLR   | 2.89 (-2.25, 2.89) [0.00, 3.00]
Games | N: 4980 W: 1213 L: 1037 D: 2730
Penta | [1, 512, 1292, 680, 5]

Bench: 6558838
  • Loading branch information
Ciekce authored Oct 17, 2024
1 parent 01aab3e commit 1f26efc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,29 @@ int GetCapthistScore(const Position* pos, const SearchData* sd, const Move move)
return sd->captHist[PieceTo(move)][capturedPiece];
}

void updateSingleCorrHistScore(int &entry, const int scaledDiff, const int newWeight) {
entry = (entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE;
entry = std::clamp(entry, -CORRHIST_MAX, CORRHIST_MAX);
}

void updateCorrHistScore(const Position *pos, SearchData *sd, const int depth, const int diff) {
int &entry = sd->corrHist[pos->side][pos->pawnKey % CORRHIST_SIZE];
const int scaledDiff = diff * CORRHIST_GRAIN;
const int newWeight = std::min(depth * depth + 2 * depth + 1, 128);
assert(newWeight <= CORRHIST_WEIGHT_SCALE);

entry = (entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE;
entry = std::clamp(entry, -CORRHIST_MAX, CORRHIST_MAX);
updateSingleCorrHistScore(sd->pawnCorrHist[pos->side][pos->pawnKey % CORRHIST_SIZE], scaledDiff, newWeight);
updateSingleCorrHistScore(sd->whiteNonPawnCorrHist[pos->side][pos->whiteNonPawnKey % CORRHIST_SIZE], scaledDiff, newWeight);
updateSingleCorrHistScore(sd->blackNonPawnCorrHist[pos->side][pos->blackNonPawnKey % CORRHIST_SIZE], scaledDiff, newWeight);
}

int adjustEvalWithCorrHist(const Position *pos, const SearchData *sd, const int rawEval) {
const int &entry = sd->corrHist[pos->side][pos->pawnKey % CORRHIST_SIZE];
return std::clamp(rawEval + entry / CORRHIST_GRAIN, -MATE_FOUND + 1, MATE_FOUND - 1);
int adjustedEval = rawEval;

adjustedEval += sd->pawnCorrHist[pos->side][pos->pawnKey % CORRHIST_SIZE] / CORRHIST_GRAIN;
adjustedEval += sd->whiteNonPawnCorrHist[pos->side][pos->whiteNonPawnKey % CORRHIST_SIZE] / CORRHIST_GRAIN;
adjustedEval += sd->blackNonPawnCorrHist[pos->side][pos->blackNonPawnKey % CORRHIST_SIZE] / CORRHIST_GRAIN;

return std::clamp(adjustedEval, -MATE_FOUND + 1, MATE_FOUND - 1);
}

int GetHistoryScore(const Position* pos, const SearchData* sd, const Move move, const SearchStack* ss, const bool rootNode) {
Expand All @@ -144,5 +154,7 @@ void CleanHistories(SearchData* sd) {
std::memset(sd->rootHistory, 0, sizeof(sd->rootHistory));
std::memset(sd->contHist, 0, sizeof(sd->contHist));
std::memset(sd->captHist, 0, sizeof(sd->captHist));
std::memset(sd->corrHist, 0, sizeof(sd->corrHist));
std::memset(sd->pawnCorrHist, 0, sizeof(sd->pawnCorrHist));
std::memset(sd->whiteNonPawnCorrHist, 0, sizeof(sd->whiteNonPawnCorrHist));
std::memset(sd->blackNonPawnCorrHist, 0, sizeof(sd->blackNonPawnCorrHist));
}
10 changes: 10 additions & 0 deletions src/makemove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ template void ClearPiece<true>(const int piece, const int to, Position* pos);
// Remove a piece from a square, the UPDATE params determines whether we want to update the NNUE weights or not
template <bool UPDATE = true>
void ClearPiece(const int piece, const int from, Position* pos) {
assert(piece != EMPTY);
// Do this first because if we happened to have moved the king we first need to get1lsb the king bitboard before removing it
if constexpr(UPDATE){
std::array<bool, 2> flip({get_file[KingSQ(pos, WHITE)] > 3, get_file[KingSQ(pos, BLACK)] > 3});
Expand All @@ -27,6 +28,10 @@ void ClearPiece(const int piece, const int from, Position* pos) {
HashKey(pos->posKey, PieceKeys[piece][from]);
if(GetPieceType(piece) == PAWN)
HashKey(pos->pawnKey, PieceKeys[piece][from]);
else if(Color[piece] == WHITE)
HashKey(pos->whiteNonPawnKey, PieceKeys[piece][from]);
else
HashKey(pos->blackNonPawnKey, PieceKeys[piece][from]);
}

template void AddPiece<false>(const int piece, const int to, Position* pos);
Expand All @@ -35,13 +40,18 @@ template void AddPiece<true>(const int piece, const int to, Position* pos);
// Add a piece to a square, the UPDATE params determines whether we want to update the NNUE weights or not
template <bool UPDATE = true>
void AddPiece(const int piece, const int to, Position* pos) {
assert(piece != EMPTY);
const int color = Color[piece];
set_bit(pos->bitboards[piece], to);
set_bit(pos->occupancies[color], to);
pos->pieces[to] = piece;
HashKey(pos->posKey, PieceKeys[piece][to]);
if(GetPieceType(piece) == PAWN)
HashKey(pos->pawnKey, PieceKeys[piece][to]);
else if(Color[piece] == WHITE)
HashKey(pos->whiteNonPawnKey, PieceKeys[piece][to]);
else
HashKey(pos->blackNonPawnKey, PieceKeys[piece][to]);
// Do this last because if we happened to have moved the king we first need to re-add to the piece bitboards least we get1lsb an empty bitboard
if constexpr(UPDATE){
std::array<bool, 2> flip({get_file[KingSQ(pos, WHITE)] > 3, get_file[KingSQ(pos, BLACK)] > 3});
Expand Down
15 changes: 15 additions & 0 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ ZobristKey GeneratePawnKey(const Position* pos) {
return pawnKey;
}

// Generates zobrist key (for non-pawns) from scratch
ZobristKey GenerateNonPawnKey(const Position* pos, int side) {
Bitboard nonPawnKey = 0;
for (int sq = 0; sq < 64; ++sq) {
// get piece on that square
const int piece = pos->PieceOn(sq);
if (piece != EMPTY && piece != WP && piece != BP && Color[piece] == side) {
nonPawnKey ^= PieceKeys[piece][sq];
}
}
return nonPawnKey;
}

// parse FEN string
void ParseFen(const std::string& command, Position* pos) {

Expand Down Expand Up @@ -208,6 +221,8 @@ void ParseFen(const std::string& command, Position* pos) {

pos->posKey = GeneratePosKey(pos);
pos->pawnKey = GeneratePawnKey(pos);
pos->whiteNonPawnKey = GenerateNonPawnKey(pos, WHITE);
pos->blackNonPawnKey = GenerateNonPawnKey(pos, BLACK);

// Update pinmasks and checkers
UpdatePinsAndCheckers(pos, pos->side);
Expand Down
2 changes: 2 additions & 0 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct Position {
// unique hashkey that encodes a board position
ZobristKey posKey = 0ULL;
ZobristKey pawnKey = 0ULL;
ZobristKey whiteNonPawnKey = 0ULL;
ZobristKey blackNonPawnKey = 0ULL;
// stores the state of the board rollback purposes
int historyStackHead = 0;
BoardState history[MAXPLY];
Expand Down
4 changes: 3 additions & 1 deletion src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ struct SearchData {
int captHist[12 * 64][6] = {};
int counterMoves[64 * 64] = {};
int contHist[12 * 64][12 * 64] = {};
int corrHist[2][CORRHIST_SIZE] = {};
int pawnCorrHist[2][CORRHIST_SIZE] = {};
int whiteNonPawnCorrHist[2][CORRHIST_SIZE] = {};
int blackNonPawnCorrHist[2][CORRHIST_SIZE] = {};
};

struct PvTable {
Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// include the tune stuff here to give it global visibility
#include "tune.h"

#define NAME "Alexandria-7.0.22"
#define NAME "Alexandria-7.0.23"

inline int reductions[2][64][64];
inline int lmp_margin[64][2];
Expand Down

0 comments on commit 1f26efc

Please sign in to comment.