Home * Evaluation * King Safety
SBak_King.jpg

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.
Samuel Bak - King [1]


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.

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.

Nr of
Attackers

Attack
weight
1

0
2

50
3

75
4

88
5

94
6

97
7

99

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 [2] [3]. 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


Forums Posts

1998 ...

2000 ...

2005 ...

2010 ...

2015 ...


External Links


References

  1. ^ Chess in the Art of Samuel Bak, Center for Holocaust & Genocide Studies, University of Minnesota
  2. ^ Programmer Corner by Ed Schröder, King Safety in REBEL
  3. ^ How Rebel Plays Chess is also available as pdf
  4. ^ For Ed Schroeder: Rebel's Pawn Shield and Pawn Storm Eval by Marcel Fournier, CCC, August 21, 2012

What links here?


Up one Level