Corrhist #96

Merged
rektdie merged 4 commits from corrhist into main 2025-09-03 14:22:30 +02:00
5 changed files with 65 additions and 3 deletions

No files matched your search

+9
View File
@@ -31,6 +31,7 @@ void Board::Reset() {
currentMoveIndex = 0;
hashKey = 0ULL;
pawnKey = 0ULL;
}
void Board::SetByFen(std::string_view fen) {
@@ -271,6 +272,10 @@ void Board::SetPiece(int piece, int square, bool color) {
occupied.SetBit(square);
hashKey ^= UTILS::zKeys[color][piece][square];
if (piece == Pawn) {
pawnKey ^= UTILS::zKeys[color][Pawn][square];
}
}
void Board::RemovePiece(int piece, int square, bool color) {
@@ -279,6 +284,10 @@ void Board::RemovePiece(int piece, int square, bool color) {
occupied.PopBit(square);
hashKey ^= UTILS::zKeys[color][piece][square];
if (piece == Pawn) {
pawnKey ^= UTILS::zKeys[color][Pawn][square];
}
}
// Updates castling rights
+1
View File
@@ -40,6 +40,7 @@ public:
int fullMoves = 1;
U64 hashKey = 0ULL;
U64 pawnKey = 0ULL;
void Reset();
void SetByFen(std::string_view fen);
+22 -3
View File
@@ -4,6 +4,7 @@
#include <cmath>
#include "datagen.h"
#include "benchmark.h"
#include "types.h"
namespace SEARCH {
@@ -42,6 +43,14 @@ int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int
return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to];
}
static int AdjustEval(Board &board, SearchContext* ctx, int eval) {
int pawnHist = ctx->corrhist.GetPawnHist(board);
int mateFound = MATE_SCORE - MAX_PLY;
return std::clamp(eval + pawnHist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1);
}
static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
TTEntry current = ctx->TT.GetRawEntry(board.hashKey);
if (current.hashKey == board.hashKey && current.bestMove == move) {
@@ -270,7 +279,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
if (ply > ctx->seldepth)
ctx->seldepth = ply;
int bestScore = NNUE::net.Evaluate(board);
int bestScore = AdjustEval(board, ctx, NNUE::net.Evaluate(board));
ctx->ss[ply].eval = bestScore;
if (!ctx->excluded) {
@@ -382,7 +391,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
const int staticEval = NNUE::net.Evaluate(board);
const int staticEval = AdjustEval(board, ctx, NNUE::net.Evaluate(board));
ctx->ss[ply].eval = staticEval;
const bool improving = [&]
@@ -648,8 +657,18 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
}
if (ctx->searchStopped) return 0;
if (!ctx->excluded)
if (!ctx->excluded) {
if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove))
&& !(nodeType == AllNode && results.score >= staticEval)) {
int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT);
ctx->corrhist.UpdatePawnHist(board, depth, corrHistBonus);
}
ctx->TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove);
}
return results;
}
+32
View File
@@ -23,6 +23,12 @@ constexpr int MAX_DEPTH = 128;
constexpr int MAX_PLY = 512;
constexpr int MAX_HISTORY = 16384;
constexpr int CORRHIST_WEIGHT_SCALE = 256;
constexpr int CORRHIST_GRAIN = 256;
constexpr int CORRHIST_LIMIT = 1024;
constexpr int CORRHIST_SIZE = 16384;
constexpr int CORRHIST_MAX = 16384;
constexpr int32_t ScoreNone = -255000;
constexpr int inf = 100000;
@@ -35,6 +41,30 @@ constexpr std::array<int, 6> SEEPieceValues = {
void InitLMRTable();
class SearchContext;
class CorrHist {
private:
// indexed by [stm][key]
MultiArray<int, 2, CORRHIST_SIZE> pawnHist;
public:
void UpdatePawnHist(Board& board, int depth, int diff) {
int* entry = &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE];
const int scaledDiff = diff * CORRHIST_GRAIN;
const int newWeight = std::min(depth * depth + 2 * depth + 1, 128);
*entry = (*entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE;
*entry = std::clamp(*entry, -CORRHIST_MAX, CORRHIST_MAX);
}
int GetPawnHist(Board& board) {
return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE];
}
void ClearPawnHist() {
std::fill(&pawnHist[0][0], &pawnHist[0][0] + sizeof(pawnHist) / sizeof(int), 0);
}
};
class ContHistory {
private:
// indexed by [other color][prev move piece][stm][prev move to][piece][to]
@@ -135,6 +165,7 @@ public:
PVLine pvLine;
History history;
ContHistory conthist;
CorrHist corrhist;
std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
@@ -150,6 +181,7 @@ public:
pvLine.Clear();
history.Clear();
conthist.Clear();
corrhist.ClearPawnHist();
TT.Clear();
sw.Restart();
killerMoves = {};
+1
View File
@@ -193,6 +193,7 @@ void UCILoop(Board &board) {
ctx->killerMoves = {};
ctx->history.Clear();
ctx->conthist.Clear();
ctx->corrhist.ClearPawnHist();
ctx->positionHistory = {};
ctx->ss = {};