Corrhist #96
No files matched your search
+41
-32
@@ -9,7 +9,7 @@
|
||||
void Board::Reset() {
|
||||
castlingRights = 0;
|
||||
enPassantTarget = noEPTarget;
|
||||
|
||||
|
||||
halfMoves = 0;
|
||||
fullMoves = 1;
|
||||
|
||||
@@ -31,6 +31,7 @@ void Board::Reset() {
|
||||
currentMoveIndex = 0;
|
||||
|
||||
hashKey = 0ULL;
|
||||
pawnKey = 0ULL;
|
||||
}
|
||||
|
||||
void Board::SetByFen(std::string_view fen) {
|
||||
@@ -88,66 +89,66 @@ void Board::SetByFen(std::string_view fen) {
|
||||
|
||||
std::string Board::GetFen() {
|
||||
std::ostringstream fen;
|
||||
|
||||
|
||||
for (int rank = 7; rank >= 0; --rank) {
|
||||
int emptyCount = 0;
|
||||
|
||||
|
||||
for (int file = 0; file < 8; ++file) {
|
||||
int square = rank * 8 + file;
|
||||
const uint64_t squareBit = 1ULL << square;
|
||||
|
||||
|
||||
if (!(occupied & squareBit)) {
|
||||
++emptyCount;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (emptyCount > 0) {
|
||||
fen << emptyCount;
|
||||
emptyCount = 0;
|
||||
}
|
||||
|
||||
|
||||
static constexpr std::array<char, 6> pieceChars = {'p', 'n', 'b', 'r', 'q', 'k'};
|
||||
char pieceChar = '?';
|
||||
|
||||
|
||||
for (size_t pieceType = 0; pieceType < pieceChars.size(); ++pieceType) {
|
||||
if (pieces[pieceType] & squareBit) {
|
||||
pieceChar = pieceChars[pieceType];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fen << (colors[White] & squareBit ? static_cast<char>(std::toupper(pieceChar)) : pieceChar);
|
||||
}
|
||||
|
||||
|
||||
if (emptyCount > 0) {
|
||||
fen << emptyCount;
|
||||
}
|
||||
|
||||
|
||||
if (rank > 0) {
|
||||
fen << '/';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fen << ' ' << (sideToMove ? 'b' : 'w');
|
||||
|
||||
|
||||
fen << ' ';
|
||||
bool hasCastlingRights = false;
|
||||
|
||||
|
||||
const std::array<std::pair<uint8_t, char>, 4> castlingOptions = {
|
||||
{{whiteKingRight, 'K'}, {whiteQueenRight, 'Q'}, {blackKingRight, 'k'}, {blackQueenRight, 'q'}}
|
||||
};
|
||||
|
||||
|
||||
for (const auto& [right, symbol] : castlingOptions) {
|
||||
if (castlingRights & right) {
|
||||
fen << symbol;
|
||||
hasCastlingRights = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!hasCastlingRights) {
|
||||
fen << '-';
|
||||
}
|
||||
|
||||
|
||||
fen << ' ';
|
||||
if (enPassantTarget == noEPTarget) {
|
||||
fen << '-';
|
||||
@@ -156,11 +157,11 @@ std::string Board::GetFen() {
|
||||
const int rank = enPassantTarget / 8;
|
||||
fen << static_cast<char>('a' + file) << (rank + 1);
|
||||
}
|
||||
|
||||
|
||||
fen << ' ' << halfMoves;
|
||||
|
||||
|
||||
fen << ' ' << fullMoves;
|
||||
|
||||
|
||||
return fen.str();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -322,23 +331,23 @@ bool Board::MakeMove(Move move) {
|
||||
// Null Move
|
||||
if (!move) {
|
||||
int newEpTarget = noEPTarget;
|
||||
|
||||
|
||||
sideToMove = !sideToMove;
|
||||
hashKey ^= UTILS::zSide;
|
||||
|
||||
|
||||
if (enPassantTarget != noEPTarget) {
|
||||
hashKey ^= UTILS::zEnPassant[enPassantTarget % 8];
|
||||
}
|
||||
|
||||
|
||||
if (newEpTarget != noEPTarget) {
|
||||
hashKey ^= UTILS::zEnPassant[newEpTarget % 8];
|
||||
}
|
||||
|
||||
|
||||
enPassantTarget = newEpTarget;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int newEpTarget = noEPTarget;
|
||||
|
||||
int attackerPiece = GetPieceType(move.MoveFrom());
|
||||
@@ -397,7 +406,7 @@ bool Board::MakeMove(Move move) {
|
||||
case kingCastle:
|
||||
{
|
||||
int rookSquare = attackerColor ? h8 : h1;
|
||||
|
||||
|
||||
// Removing rook from old position
|
||||
RemovePiece(Rook, rookSquare, attackerColor);
|
||||
|
||||
@@ -448,7 +457,7 @@ bool Board::MakeMove(Move move) {
|
||||
sideToMove = !sideToMove;
|
||||
if (InCheck()) {
|
||||
*this = save;
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
sideToMove = !sideToMove;
|
||||
|
||||
@@ -458,7 +467,7 @@ bool Board::MakeMove(Move move) {
|
||||
} else {
|
||||
halfMoves++;
|
||||
}
|
||||
|
||||
|
||||
positionIndex++;
|
||||
|
||||
return true;
|
||||
@@ -513,16 +522,16 @@ void Board::ResetAccPair() {
|
||||
|
||||
Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
|
||||
Bitboard attacks;
|
||||
|
||||
|
||||
// Pawn attacks
|
||||
|
||||
|
||||
// Opponent attacks
|
||||
attacks |= (MOVEGEN::pawnAttacks[sideToMove][square] & colors[!sideToMove] & pieces[Pawn]);
|
||||
|
||||
|
||||
// Own piece attacks
|
||||
attacks |= (MOVEGEN::pawnAttacks[!sideToMove][square] & colors[sideToMove] & pieces[Pawn]);
|
||||
|
||||
// Knight
|
||||
// Knight
|
||||
attacks |= (MOVEGEN::knightAttacks[square] & pieces[Knight]);
|
||||
|
||||
// King
|
||||
|
||||
+2
-1
@@ -40,6 +40,7 @@ public:
|
||||
int fullMoves = 1;
|
||||
|
||||
U64 hashKey = 0ULL;
|
||||
U64 pawnKey = 0ULL;
|
||||
|
||||
void Reset();
|
||||
void SetByFen(std::string_view fen);
|
||||
@@ -56,7 +57,7 @@ public:
|
||||
|
||||
int GetPieceType(int square);
|
||||
int GetPieceColor(int square);
|
||||
|
||||
|
||||
bool InCheck();
|
||||
|
||||
void SetPiece(int piece, int square, bool color);
|
||||
|
||||
+22
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+37
-5
@@ -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]
|
||||
@@ -50,7 +80,7 @@ public:
|
||||
void Clear() {
|
||||
std::fill(&contHistMoves[0][0][0][0][0][0], &contHistMoves[0][0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0);
|
||||
}
|
||||
|
||||
|
||||
int16_t GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply);
|
||||
int16_t GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply);
|
||||
|
||||
@@ -65,7 +95,7 @@ private:
|
||||
public:
|
||||
void Update(bool stm, Move move, int bonus) {
|
||||
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
||||
historyMoves[stm][move.MoveFrom()][move.MoveTo()] +=
|
||||
historyMoves[stm][move.MoveFrom()][move.MoveTo()] +=
|
||||
clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / MAX_HISTORY;
|
||||
}
|
||||
|
||||
@@ -96,12 +126,12 @@ public:
|
||||
|
||||
length[ply] = length[ply + 1];
|
||||
}
|
||||
|
||||
|
||||
void Print(int n) {
|
||||
for (int i = 0; i < length[n]; i++) {
|
||||
table[n][i].PrintMove();
|
||||
std::cout << ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
@@ -128,13 +158,14 @@ public:
|
||||
int seldepth = 0;
|
||||
|
||||
Move excluded = Move();
|
||||
|
||||
|
||||
bool doingNullMove = false;
|
||||
bool searchStopped = false;
|
||||
|
||||
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 = {};
|
||||
|
||||
@@ -193,6 +193,7 @@ void UCILoop(Board &board) {
|
||||
ctx->killerMoves = {};
|
||||
ctx->history.Clear();
|
||||
ctx->conthist.Clear();
|
||||
ctx->corrhist.ClearPawnHist();
|
||||
ctx->positionHistory = {};
|
||||
ctx->ss = {};
|
||||
|
||||
|
||||
Reference in new issue
Block a user