From 246af156ef36d3f4ee25a1c13f1303b9ba456bde Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 15 Nov 2026 15:36:46 +0100 Subject: [PATCH] feat: escape bonus for any piece the opponent can win The policy rewarded moving a piece away (0.003 x its value) only when an enemy pawn attacked it. A knight attacked by an undefended-against bishop got nothing. Now the bonus goes to every move of a knight, bishop, rook or queen standing where the opponent wins it in an exchange: their least valuable attacker starts it and SEE (from their side) gains at least 1; a king may only take an undefended piece. A pawn attack always passes this test, so the old case is covered. The result is computed once per square in each policy call. The counterpart of the quiet-move SEE penalty on the target square. Example: with a white knight on e5 attacked by Bd6 and undefended, its four retreats rank first (5.6-6.1%); in the Ruy Lopez the defended Nc6 gets no bonus. Bench is deterministic. Bench: 554375 Co-Authored-By: Claude Opus 5.5 --- src/eval.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/eval.cpp b/src/eval.cpp index 31f0594..8d5d301 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -1,6 +1,7 @@ #include "eval.h" #include +#include #include #include @@ -217,6 +218,31 @@ void policy(const Position& pos, const MoveList& moves, float temperature, const check_squares[QUEEN] = check_squares[BISHOP] | check_squares[ROOK]; check_squares[KING] = 0; + // Whether the opponent wins the piece on a square in an exchange started + // with their least valuable attacker. Worked out once per square. + Position their_turn = pos; + their_turn.stm = them; + int hanging_cache[64]; + std::fill(std::begin(hanging_cache), std::end(hanging_cache), -1); + const auto hanging = [&](int sq) { + if (hanging_cache[sq] < 0) { + const u64 attackers = pos.attackers_to(sq, occ) & pos.colors[them]; + int lva = -1; + for (int t = PAWN; t <= KING && lva < 0; t++) + if (attackers & pos.pieces[t]) lva = lsb(attackers & pos.pieces[t]); + bool wins = false; + if (lva >= 0) { + // A king can only take a piece nothing defends. + if (piece_type(pos.piece_on(lva)) == KING) + wins = !(pos.attackers_to(sq, occ) & pos.colors[us]); + else + wins = see_ge(their_turn, Move(lva, sq, Move::CAPTURE), 1); + } + hanging_cache[sq] = wins; + } + return hanging_cache[sq] == 1; + }; + const float inv_temp = 1.0f / std::max(temperature, 0.01f); float max_logit = -1e30f; @@ -238,7 +264,8 @@ void policy(const Position& pos, const MoveList& moves, float temperature, const if (type != PAWN && type != KING) { if (m.is_capture() && (pawn_threats & bb(to))) s -= 0.004f * SEE_VALUE[type]; - if (pawn_threats & bb(from)) s += 0.003f * SEE_VALUE[type]; + // Moving a piece the opponent could win (to a pawn or any other attacker). + if (hanging(from)) s += 0.003f * SEE_VALUE[type]; } // A quiet move that puts the piece where the exchange loses it (to a // pawn, or to any attacker it is not defended well enough against).