added threats hist | bench 5938098

This commit is contained in:
rektdie committed 2025-09-17 00:42:04 +02:00
1 parent 729d24c7c8
commit 1dd0e33d2b
5 files changed
+43 -1

No files matched your search

+4
View File
@@ -553,3 +553,7 @@ Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
return attacks; return attacks;
} }
bool Board::IsSquareThreatened(bool side, int square) {
return colorThreats[!side].IsSet(square);
}
+2
View File
@@ -70,4 +70,6 @@ public:
bool InPossibleZug(); bool InPossibleZug();
Bitboard AttacksTo(int square, Bitboard occupancy); Bitboard AttacksTo(int square, Bitboard occupancy);
bool IsSquareThreatened(bool side, int square);
}; };
+14 -1
View File
@@ -73,6 +73,12 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
int conthistScore = 0; int conthistScore = 0;
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
int threatsScore =
ctx->threatsHist[board.sideToMove][move.MoveFrom()][move.MoveTo()][sourceThreatened][targetThreatened];
if (ply > 0) { if (ply > 0) {
conthistScore = ctx->conthist.GetOnePly(board, move, ctx, ply); conthistScore = ctx->conthist.GetOnePly(board, move, ctx, ply);
/* /*
@@ -82,7 +88,7 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
*/ */
} }
return 20000 + historyScore + conthistScore; return 20000 + historyScore + conthistScore + threatsScore;
} }
} }
@@ -616,6 +622,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
int bonus = historyBonusMultiplier * depth - historyBonusSub; int bonus = historyBonusMultiplier * depth - historyBonusSub;
ctx->history.Update(board.sideToMove, currMove, bonus); ctx->history.Update(board.sideToMove, currMove, bonus);
ctx->threatsHist.Update(board.sideToMove, currMove,
board.IsSquareThreatened(board.sideToMove, currMove.MoveFrom()),
board.IsSquareThreatened(board.sideToMove, currMove.MoveTo()), bonus);
if (ply > 0) { if (ply > 0) {
int prevType = ctx->ss[ply-1].pieceType; int prevType = ctx->ss[ply-1].pieceType;
@@ -656,6 +665,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
// Malus // Malus
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus); ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus);
ctx->threatsHist.Update(board.sideToMove, currMove,
board.IsSquareThreatened(board.sideToMove, currMove.MoveFrom()),
board.IsSquareThreatened(board.sideToMove, currMove.MoveTo()), bonus);
} }
} }
+22
View File
@@ -102,6 +102,26 @@ public:
} }
}; };
class ThreatsHistory {
private:
// indexed by [stm][from][to][threatSource][threatTarget]
MultiArray<int, 2, 64, 64, 2, 2> historyMoves;
public:
void Update(bool stm, Move move, bool source, bool target, int bonus) {
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
historyMoves[stm][move.MoveFrom()][move.MoveTo()][source][target] +=
clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()][source][target] * std::abs(clampedBonus) / MAX_HISTORY;
}
void Clear() {
std::fill(&historyMoves[0][0][0][0][0], &historyMoves[0][0][0][0][0] + sizeof(historyMoves) / sizeof(int), 0);
}
auto& operator[](int index) {
return historyMoves[index];
}
};
class History { class History {
private: private:
MultiArray<int, 2, 64, 64> historyMoves; MultiArray<int, 2, 64, 64> historyMoves;
@@ -179,6 +199,7 @@ public:
PVLine pvLine; PVLine pvLine;
History history; History history;
ThreatsHistory threatsHist;
ContHistory conthist; ContHistory conthist;
CorrHist corrhist; CorrHist corrhist;
@@ -195,6 +216,7 @@ public:
SearchContext(){ SearchContext(){
pvLine.Clear(); pvLine.Clear();
history.Clear(); history.Clear();
threatsHist.Clear();
conthist.Clear(); conthist.Clear();
corrhist.Clear(); corrhist.Clear();
TT.Clear(); TT.Clear();
+1
View File
@@ -249,6 +249,7 @@ void UCILoop(Board &board) {
ctx->killerMoves = {}; ctx->killerMoves = {};
ctx->history.Clear(); ctx->history.Clear();
ctx->threatsHist.Clear();
ctx->conthist.Clear(); ctx->conthist.Clear();
ctx->corrhist.Clear(); ctx->corrhist.Clear();
ctx->positionHistory = {}; ctx->positionHistory = {};