Escape bonus for any piece the opponent can win #13

Merged
claude merged 1 commits from policy-escape-see into main 2026-11-15 15:41:41 +01:00
+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).