1 Commits
Author SHA1 Message Date
rektdieandclaude ed1c2b77f7 feat: SEE pruning of losing captures in quiescence
Profiling showed that 80-85% of a middlegame playout is the quiescence
search behind the value network: 15-41 quiescence nodes per leaf, since
every capture was searched, including ones that just lose the piece.

The quiescence search now skips captures whose static exchange on the
target square loses material (SEE < 0) when not in check. The SEE is the
usual threshold swap (least valuable attacker first, x-rays opened as
pieces leave, a king recaptures only an undefended square, pins
ignored); castling, en passant and promotions count as even.

Effect at 60k playouts (instrumented build, qsearch nodes per leaf and
time per playout):

  startpos           3.7 -> 2.2     -20%
  QGD middlegame    14.7 -> 4.2     -54%
  Kiwipete          41.4 -> 25.5    -25%
  Sicilian middle   21.0 -> 3.4     -68%
  rook endgame       2.6 -> 2.3      -2%

Checked: the SEE matches an independent python-chess reference exactly
on 9957 captures from 3000 random positions (4884 with recaptures).
Bench is deterministic. Move generation is untouched.

Bench: 546966

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:43:40 +01:00
2 changed files with 64 additions and 0 deletions

No files matched your search

+60
View File
@@ -47,6 +47,64 @@ int victim_type(const Position& pos, Move m) { return m.is_en_passant() ? PAWN :
} // namespace
bool see_ge(const Position& pos, Move m, int threshold) {
// Castling, en passant and promotions are taken as an even trade.
if (m.is_castle() || m.is_en_passant() || m.is_promotion()) return threshold <= 0;
const int from = m.from();
const int to = m.to();
// `swap` is what the side to move is ahead of the threshold if the exchange
// stops now, seen from the side that just captured.
int swap = (pos.piece_on(to) == NO_PIECE ? 0 : SEE_VALUE[piece_type(pos.piece_on(to))]) - threshold;
if (swap < 0) return false;
swap = SEE_VALUE[piece_type(pos.piece_on(from))] - swap;
if (swap <= 0) return true;
u64 occ = pos.occupancy() ^ bb(from) ^ bb(to);
u64 attackers = pos.attackers_to(to, occ);
const u64 diagonal = pos.pieces[BISHOP] | pos.pieces[QUEEN];
const u64 straight = pos.pieces[ROOK] | pos.pieces[QUEEN];
int stm = pos.stm;
int result = 1;
for (;;) {
stm ^= 1;
attackers &= occ;
const u64 ours = attackers & pos.colors[stm];
if (!ours) break;
result ^= 1;
// Recapture with the least valuable piece; taking it off the board may
// open a line for a slider behind it.
u64 b;
if ((b = ours & pos.pieces[PAWN])) {
if ((swap = SEE_VALUE[PAWN] - swap) < result) break;
occ ^= bb(lsb(b));
attackers |= attacks::bishop(to, occ) & diagonal;
} else if ((b = ours & pos.pieces[KNIGHT])) {
if ((swap = SEE_VALUE[KNIGHT] - swap) < result) break;
occ ^= bb(lsb(b));
} else if ((b = ours & pos.pieces[BISHOP])) {
if ((swap = SEE_VALUE[BISHOP] - swap) < result) break;
occ ^= bb(lsb(b));
attackers |= attacks::bishop(to, occ) & diagonal;
} else if ((b = ours & pos.pieces[ROOK])) {
if ((swap = SEE_VALUE[ROOK] - swap) < result) break;
occ ^= bb(lsb(b));
attackers |= attacks::rook(to, occ) & straight;
} else if ((b = ours & pos.pieces[QUEEN])) {
if ((swap = SEE_VALUE[QUEEN] - swap) < result) break;
occ ^= bb(lsb(b));
attackers |= (attacks::bishop(to, occ) & diagonal) | (attacks::rook(to, occ) & straight);
} else {
// Only the king is left: it may recapture only if nothing defends the square.
return (attackers & ~pos.colors[stm]) ? result ^ 1 : result;
}
}
return result;
}
void init() {
for (int type = PAWN; type <= KING; type++) {
for (int sq = 0; sq < 64; sq++) {
@@ -86,6 +144,8 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) {
const int victim = victim_type(pos, m);
// Delta pruning: even winning the piece for free cannot reach alpha.
if (!in_check && !m.is_promotion() && stand_pat + SEE_VALUE[victim] + 200 <= alpha) continue;
// Captures that lose material in the exchange on the square.
if (!in_check && !see_ge(pos, m, 0)) continue;
score = SEE_VALUE[victim] * 16 - piece_type(pos.piece_on(m.from()));
}
if (m.is_promotion()) score += SEE_VALUE[m.promo_type()] * 16;
+4
View File
@@ -12,6 +12,10 @@ void init();
// Static evaluation (the value network) in centipawns, side to move's view.
int static_eval(const Position& pos);
// Static exchange evaluation: true when the exchange `m` starts on its target
// square gains at least `threshold` centipawns for the side to move (pins ignored).
bool see_ge(const Position& pos, Move m, int threshold);
// Capture-only search on top of the static evaluation, side to move's view.
int quiescence(const Position& pos, int alpha, int beta, int ply);