From 1dd0e33d2bae9992f77aa7222f859e65a9b9a85d Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 17 Sep 2025 00:42:04 +0200 Subject: [PATCH 1/2] added threats hist | bench 5938098 --- source/board.cpp | 4 ++++ source/board.h | 2 ++ source/search.cpp | 15 ++++++++++++++- source/search.h | 22 ++++++++++++++++++++++ source/uci.cpp | 1 + 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/source/board.cpp b/source/board.cpp index d52fa8d..2622935 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -553,3 +553,7 @@ Bitboard Board::AttacksTo(int square, Bitboard occupancy) { return attacks; } + +bool Board::IsSquareThreatened(bool side, int square) { + return colorThreats[!side].IsSet(square); +} diff --git a/source/board.h b/source/board.h index e93b491..e254044 100644 --- a/source/board.h +++ b/source/board.h @@ -70,4 +70,6 @@ public: bool InPossibleZug(); Bitboard AttacksTo(int square, Bitboard occupancy); + + bool IsSquareThreatened(bool side, int square); }; diff --git a/source/search.cpp b/source/search.cpp index c31d8de..8f8ffc0 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -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 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) { 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; 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) { 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 for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { 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); } } diff --git a/source/search.h b/source/search.h index 58ae35b..4df65c4 100644 --- a/source/search.h +++ b/source/search.h @@ -102,6 +102,26 @@ public: } }; +class ThreatsHistory { +private: + // indexed by [stm][from][to][threatSource][threatTarget] + MultiArray 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 { private: MultiArray historyMoves; @@ -179,6 +199,7 @@ public: PVLine pvLine; History history; + ThreatsHistory threatsHist; ContHistory conthist; CorrHist corrhist; @@ -195,6 +216,7 @@ public: SearchContext(){ pvLine.Clear(); history.Clear(); + threatsHist.Clear(); conthist.Clear(); corrhist.Clear(); TT.Clear(); diff --git a/source/uci.cpp b/source/uci.cpp index b624a30..6036bd5 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -249,6 +249,7 @@ void UCILoop(Board &board) { ctx->killerMoves = {}; ctx->history.Clear(); + ctx->threatsHist.Clear(); ctx->conthist.Clear(); ctx->corrhist.Clear(); ctx->positionHistory = {}; -- 2.47.3 From 10a0f502ae87422efb7e8ad20dee64ddea6bb513 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 17 Sep 2025 01:06:37 +0200 Subject: [PATCH 2/2] fixed threats hist? | bench 6448969 --- source/search.cpp | 33 ++++++++++++++++----------------- source/search.h | 25 ++----------------------- source/uci.cpp | 1 - 3 files changed, 18 insertions(+), 41 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 8f8ffc0..4a61bb9 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -70,14 +70,11 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { } else if (ctx->killerMoves[1][ply] == move) { return 40000; } else { - int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; - int conthistScore = 0; - bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom()); - bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom()); + bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo()); - int threatsScore = - ctx->threatsHist[board.sideToMove][move.MoveFrom()][move.MoveTo()][sourceThreatened][targetThreatened]; + int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()][sourceThreatened][targetThreatened]; + int conthistScore = 0; if (ply > 0) { conthistScore = ctx->conthist.GetOnePly(board, move, ctx, ply); @@ -88,7 +85,7 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { */ } - return 20000 + historyScore + conthistScore + threatsScore; + return 20000 + historyScore + conthistScore; } } @@ -175,8 +172,11 @@ static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int if (!improving) reduction += lmrImproving; + bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom()); + bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo()); + // History LMR - int historyReduction = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; + int historyReduction = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()][sourceThreatened][targetThreatened]; if (ply > 0) { historyReduction += ctx->conthist.GetOnePly(board, move, ctx, ply); @@ -485,10 +485,13 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } } + bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, currMove.MoveFrom()); + bool targetThreatened = board.IsSquareThreatened(board.sideToMove, currMove.MoveTo()); + // Futility pruning // If our static eval is far below alpha, there is only a small chance // that a quiet move will help us so we skip them - int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()]; + int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()][sourceThreatened][targetThreatened]; if (ply > 0) { historyScore += ctx->conthist.GetOnePly(board, currMove, ctx, ply); @@ -621,10 +624,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC int bonus = historyBonusMultiplier * depth - historyBonusSub; - 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); + ctx->history.Update(board.sideToMove, currMove, sourceThreatened, targetThreatened, bonus); if (ply > 0) { int prevType = ctx->ss[ply-1].pieceType; @@ -664,11 +664,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC // Malus for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { - ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus); + sourceThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveFrom()); + targetThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveTo()); - ctx->threatsHist.Update(board.sideToMove, currMove, - board.IsSquareThreatened(board.sideToMove, currMove.MoveFrom()), - board.IsSquareThreatened(board.sideToMove, currMove.MoveTo()), bonus); + ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], sourceThreatened, targetThreatened, -bonus); } } diff --git a/source/search.h b/source/search.h index 4df65c4..0116cdc 100644 --- a/source/search.h +++ b/source/search.h @@ -102,9 +102,9 @@ public: } }; -class ThreatsHistory { +class History { private: - // indexed by [stm][from][to][threatSource][threatTarget] + // indexed by [stm][from][to][threatenedSource][threatenedTarget] MultiArray historyMoves; public: void Update(bool stm, Move move, bool source, bool target, int bonus) { @@ -122,25 +122,6 @@ public: } }; -class History { -private: - MultiArray historyMoves; -public: - void Update(bool stm, Move move, int bonus) { - int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY); - historyMoves[stm][move.MoveFrom()][move.MoveTo()] += - clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / MAX_HISTORY; - } - - void Clear() { - std::fill(&historyMoves[0][0][0], &historyMoves[0][0][0] + sizeof(historyMoves) / sizeof(int), 0); - } - - auto& operator[](int index) { - return historyMoves[index]; - } -}; - class PVLine { private: int length[MAX_PLY] = {}; @@ -199,7 +180,6 @@ public: PVLine pvLine; History history; - ThreatsHistory threatsHist; ContHistory conthist; CorrHist corrhist; @@ -216,7 +196,6 @@ public: SearchContext(){ pvLine.Clear(); history.Clear(); - threatsHist.Clear(); conthist.Clear(); corrhist.Clear(); TT.Clear(); diff --git a/source/uci.cpp b/source/uci.cpp index 6036bd5..b624a30 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -249,7 +249,6 @@ void UCILoop(Board &board) { ctx->killerMoves = {}; ctx->history.Clear(); - ctx->threatsHist.Clear(); ctx->conthist.Clear(); ctx->corrhist.Clear(); ctx->positionHistory = {}; -- 2.47.3