SEE+-+The+Swap+Algorithm

toc
 * Home * Board Representation * Bitboards * SEE - The Swap Algorithm**

The iterative SEE Swap-Algorithm in Bitboards creates a swap-list of best case material gains by traversing a square attacked/defended by set in least valuable piece order from pawn, knight, bishop, rook, queen until king, with alternating sides. The swap-list, an unary tree since there are no branches but just a series of captures, is negamaxed for a final static exchange evaluation.

=Traversal of To-Attacks= Assuming this arbitrary Board-Definition with color as least significant piece bit and "even" pieces are the white ones, following routine returns a single populated square set and passes the least valuable piece per C++ reference to the caller. If no more piece is found for the appropriate side, it returns an empty set. code format="cpp" U64 Board::getLeastValuablePiece(U64 attadef, int bySide, int &piece) {  for (piece = nWhitePawn + bySide; piece <= nWhiteKing + bySide; piece += 2) { U64 subset = attadef & pieceBB[piece]; if ( subset ) return subset & -subset; // single bit }  return 0; // empty set } code

=SEE a Capture= The first two members of the gain swap-list are likely determined by the capture move we like to prove. Thus, the first element of the swap-list is the value of the target piece, while the second is only written (or used) if the target square is further defended. In this case it is the value of the capturing piece minus the value of captured piece, this is the best-case material gain from the defending point of view. However during traversal, X-ray attacks are considered while capturing with a piece, which may have indirect attacks or defends behind, which unions the set of attackers and defenders.

=Pseudo C-Code= code format="cpp" int Board::see ( enumSquare toSq, enumPiece target, enumSquare frSq, enumPiece aPiece) {  int gain[32], d = 0; U64 mayXray = pawns | bishops | rooks | queen; U64 fromSet = 1ULL << frSq; U64 occ    = occupiedBB; U64 attadef = attacksTo( occ, toSq ); gain[d]    = value[target]; do { d++; // next depth and side gain[d] = value[aPiece] - gain[d-1]; // speculative store, if defended if (max (-gain[d-1], gain[d]) < 0) break; // pruning does not influence the result attadef ^= fromSet; // reset bit in set to traverse occ    ^= fromSet; // reset bit in temporary occupancy (for x-Rays) if ( fromSet & mayXray ) attadef |= considerXrays(occ, ..); fromSet = getLeastValuablePiece (attadef, d & 1, aPiece); } while (fromSet); while (--d) gain[d-1]= -max (-gain[d-1], gain[d]) return gain[0]; } code Considering X-ray attacks leaves some implementation details left, dependent on what sliding attack-getter is handy, ray attacks are sufficient, but requires some effort to determine the ray-direction. With Magic Bitboards one will likely use something similar as the sliding piece subset of Square Attacked By. The preliminary pruning of the swap-list fill and other improvements were proposed by Michael Hoffmann.

=Traces= Two positions with traces of the swap-list during traversal and negamaxing with some arbitrary piece values.

Position 1
To demonstrate how SEE works in obvious cases, is Rook takes Pawn a winning capture? > code gain[0] =  100 ; win for white if black pawn e5 is en-prise by rxp gain[1] =  400 ; win for black if white rook e5 is en-prise,  500-100, speculative store no further attacks for black at depth 1 -> SEE(rxe5) == 100 code

Position 2
This position covers a more complicated case with X-rays. Is Knight takes pawn a winning capture? > code gain[0] = 100 ; win for white if black pawn   e5 is en-prise by nxp gain[1] = 225 ; win for black if white knight e5 is en-prise by nxn,  325- 100 gain[2] = 100 ; win for white if black knight e5 is en-prise by rxn,  325- 225 -> x-rays includes queen e1 gain[3] =  400 ; win for black if white rook   e5 is en-prise by bxr,  500- 100 -> x-rays includes queen h8 gain[4] =  -75 ; win for white if black bishop e5 is en-prise by qxb,  325- 400 gain[5] = 1075 ; win for black if white queen e5 is en-prise by qxq, 1000- -75 gain[6] = -75 ; win for white if black queen  e5 is en-prise,        1000-1075 speculative store no further attacks for white at depth = 6

gain[4] = -max(--75, 1075) = -1075 gain[3] = -max(-400, -1075) =  400 gain[2] = -max(-100,  400) =  -400 gain[1] = -max(-225, -400) =   225 gain[0] = -max(-100,  225) =  -225 -> SEE(nxe5) == -225 code

=See also=
 * Ed's Lookup from Attack and Defend Maps
 * MVV-LVA
 * SOMA
 * Static Exchange Evaluation

=Forum Posts=

2000 ...

 * [|Static Exchange Eval] by Artem Pyatakov, CCC, August 02, 2001
 * [|Does swap of Crafty find bad promotions] by Uri Blass, CCC, January 11, 2004
 * [|Re: WBEC Ridderkerk new results] by Dann Corbit, Winboard Forum, February 15, 2004 » DanChess
 * [|SEE with magic bitboards] by Pradu Kannan, Winboard Forum, January 24, 2007 » Magic Bitboards
 * [|Re: Movei added to Crafty vs Rybka comaprison data] by Edsel Apostol, CCC, June 06, 2007

2010 ...
> [|Re: Implementing SEE] by Michael Hoffmann, CCC, August 13, 2011
 * [|Question about SEE algorithm on Chessprogramming Wiki] by Mathieu Pagé, CCC, January 05, 2010
 * [|Implementing SEE] by colin, CCC, Aug 12, 2011
 * [|SEE with alpha beta] by Onno Garms, CCC, August 14, 2011 » Onno
 * [|Problems with SEE] by Matthew R. Brades, CCC, June 03, 2017

=References= =What links here?= include page="SEE - The Swap Algorithm" component="backlinks" limit="40"
 * Up one Level**