King Safety

Home * Evaluation * King Safety



Good evaluation of the king safety is one of the most challenging tasks in writing an evaluation function, but also the most rewarding. The subjects are placed here in order of approximate (implementation) difficulty. If this page grows, it might be worthwhile to create a sub-page for each term. =Pawn Shield= When the king has castled, it is important to preserve pawns next to it, in order to protect it against the assault. Generally speaking, it is best to keep the pawns unmoved or possibly moved up one square. The lack of a shielding pawn deserves a penalty, even more so if there is an open file next to the king. =Pawn Storm= If the enemy pawns are near to the king, there might be a threat of opening a file, even if the pawn shield is intact. Penalties for storming enemy pawns must be lower than penalties for (semi)open files, otherwise the pawn storm might backfire, resulting in a blockage. =King Tropism= King tropism is a simplified form of king safety evaluation. It takes into account the distance between the King and the attacking pieces, possibly weighted against piece value. For example, one may double the distance value for a queen, and halve it for bishops and rooks. This kind of evaluation acts in a probabilistic way - it is by no means certain that being close to the king helps in attacking it. For example, if white castles short, black rook on h8 gets a higher tropism value regardless whether it stands on an open file. Nevertheless, using this kind of crude evaluation term increases a probability of building up an attack.

This kind of algorithm is used by Crafty. Another, perhaps more basic example is the CPW-Engine as implemented in CPW-Engine_eval, and the one demonstrated in the Evaluation Function Draft. =Virtual Mobility= Another heuristic is to temporary replace the king by a queen, to use her virtual mobility as a measure of opponent's sliding piece attacking chances and therefore an unsafe king. One may even temporary modify the occupancy to improve this virtual mobility, e.g by removing mobile opponent pieces. =Scaling= Usually king safety value is scaled one way or the other. Even TSCP uses the pawn shield and pawn storm score, scaled by the opponent's material. This way, whenever the engine finds itself with a broken pawn shield, it tends to exchange pieces in order to alleviate the danger. Fruit uses a more elaborate scheme, counting the bonuses for attacking the squares near to the enemy king, and then multiplying their sum by the constant derived from the number of attackers. For an approximation of such approach, see CPW_King evaluation function. =Attacking King Zone= sample specification King zone is usually defined as squares to which enemy King can move plus two or three additional squares facing enemy position. Basic King safety function, similar to the one described in Toga log user manual, can work as follows: we have two variables, attackingPiecesCount and valueOfAttacks, zeroed at startup. If a piece attacks enemy king zone, we increase attackingPiecesCount by one, and count how many squares within enemy King zone are attacked. We multpily the number of attacked squares by a constant: 20 for a knight, 20 for a bishop, 40 for a rook and 80 for a queen. The result of multiplication is added to valueOfAttacks. After finding all attacks, we look at attackingPiecesCount, use it as an index to the table given below, and our king attack score is valueOfAttacks * attackWeight[attackingPiecesCount] / 100.

Square Control
The most elaborate king safety evaluation schemes gather information about control of the squares near the enemy king. A good explanation of such an algorithm might be found on Ed Schröder's Programmer Corner. If a program, unlike Rebel, does not keep incrementally updated attack tables, this knowledge is likely to be uncovered while calculating mobility.

Attack Units
Rebel (as we know from its description) and Stockfish (as we know from its code) use attack counter as an index to a table holding king attack scores. Stockfish counts each minor piece attack on a king zone (defined as squares that enemy king can reach + three more forward squares facing enemy position) as 2 attack units, rook attack on king zone as 3 attack units and a queen attack as 5 attack units. The virtue of such approach is twofold. (1) other factors beside these attacks can be counted. For example, Stockfish adds 6 attack units for a safe queen contact check and a couple attack units for a safe rook contact check. (2) the values held in the table may reflect attitude that "the whole is greater than the sum of parts". Typical curve is S-shaped: it raises slowly at first, then it goes up faster, becoming almost flat at the end.

This is the table from Glaurung 1.2: static const int SafetyTable[100] = { 0,  0,   0,   1,   1,   2,   3,   4,   5,   6,   8,  10,  13,  16,  20,  25,  30,  36,  42,  48,  55,  62,  70,  80,  90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650, 650 };

This is the table generated using a formula from Stockfish, rescaled to centipawns : static const int SafetyTable[100] = { 0, 0,   1,   2,   3,   5,   7,   9,  12,  15,  18,  22,  26,  30,  35,  39,  44,  50,  56,  62,  68,  75,  82,  85,  89,  97, 105, 113, 122, 131, 140, 150, 169, 180, 191, 202, 213, 225, 237, 248, 260, 272, 283, 295, 307, 319, 330, 342, 354, 366, 377, 389, 401, 412, 424, 436, 448, 459, 471, 483, 494, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500 };

Using such tables, it is advisable not to evaluate king attack if only two pieces are attacking. =Patterns= There are positions that tend to be notoriously difficult for the chess programs. One of them is a sacrifice of a minor piece on g5/g4, when it is simultaneously attacked and protected by the "h" pawns. Another one occurs after a standard Bxh7 sacrifice: White knight stands unchallenged on g5, white queen on h5, black King on g8 (positions with Kg6 are best left to the search). Hard-coding such patterns raises program's tactical awareness.

=See also=
 * Castling Rights
 * Pawn Chain Direction
 * Evaluation Function Draft
 * Evaluation Patterns
 * King Safety in King Pattern with Bitboards
 * Mate at a Glance

=Forums Posts=

1998 ...

 * King Safety by Stuart Cracraft, CCC, April 22, 1998
 * King safety evaluation by Roberto Waldteufel, CCC, May 28, 1998
 * Asymetric king safety by Tom King, rgcc, March 13, 1999

2000 ...

 * What is Piece´s tropism? by carlos, CCC, October 08, 2001
 * Bishop tropism? by Pham Hong Nguyen, CCC, June 17, 2002

2005 ...

 * king safety by Charles Roberson, CCC, November 02, 2007

2010 ...

 * Distance to King by Adam Berent, CCC, January 08, 2010
 * About king safety by Fermin Serrano, CCC, April 15, 2011 » Rodin
 * value of king tropism in eval function by Tom King, CCC, May 17, 2011
 * Approaches to king safety? by Mike Robinson, CCC, January 19, 2012
 * For Ed Schroeder: Rebel's Pawn Shield and Pawn Storm Eval by Marcel Fournier, CCC, August 21, 2012
 * Pinned pieces in king safety by Stefan Geschwentner, FishCooking, March 28, 2014 » Pin, Stockfish
 * King shelter x-ray attacks by Lyudmil Tsvetkov, CCC, June 27, 2014 » X-ray Attacks (Bitboards)
 * Heavy piece shelter by Lyudmil Tsvetkov, CCC, June 27, 2014
 * King safety refinements by Lyudmil Tsvetkov, CCC, August 31, 2014

2015 ...

 * about king attack by Daniel Anulliero, CCC, August 27, 2015
 * king shelter - when and how? by Alexandru Mosoi, CCC, March 21, 2016
 * king safety: hard positions for zurichess by Alexandru Mosoi, CCC, March 27, 2016 » Zurichess
 * Castling Evaluation by Dennis Sceviour, CCC, August 02, 2016 » Castling
 * enemy edge pawn as part of own king shelter by Bram Mourik, CCC, September 30, 2017
 * Pawn Storm - Theory by Dennis Sceviour, CCC, February 14, 2018 » Pawn Storm

2020 ...

 * Pruning / reduction depending on king safety by Vivien Clauzon, CCC, February 02, 2020 » Pruning, Reductions
 * Positional evaluation of your engine on this? by Tom King, CCC, May 09, 2020
 * King safety evaluation by Niels Abildskov, CCC, March 29, 2021

=External Links=
 * Middle game - King safety from Chess Tutorial : Improve Your Middle Game by Mark Weeks
 * Chess Strategy/The positions of the kings - Wikibooks
 * The Rolling Stones - Gimme Shelter (1998 promotion video), YouTube Video
 * Bridges to Babylon Tour feat. Lisa Fischer and Darryl Jones, et al., December 12 1997, Trans World Dome, St. Louis Missouri

=References=

Up one Level