Merge pull request 'Escape bonus for any piece the opponent can win' (#13) from policy-escape-see into main

SPRT https://bench.rektdie.com/test/5294/
Elo   | 103.71 +- 29.38 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=64MB
LLR   | 3.20 (-2.94, 2.94) [0.00, 10.00]
Games | N: 338 W: 174 L: 76 D: 88
Penta | [5, 15, 60, 55, 34]

Bench: 554375
This commit was merged in pull request #13.
This commit is contained in:
claude committed 2026-11-15 15:41:40 +01:00
commit 6f4c3b2656
1 file changed
+28 -1
+28 -1
View File
@@ -1,6 +1,7 @@
#include "eval.h"
#include <algorithm>
#include <iterator>
#include <cmath>
#include <cstdlib>
@@ -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).