Changes

Jump to: navigation, search

King Safety

12,403 bytes added, 13:44, 16 May 2018
Created page with "'''Home * Evaluation * King Safety''' FILE:SamuelBak_King.jpg|border|right|thumb|link=http://chgs.elevator.umn.edu/asset/viewAsset/57f3b6787d58ae5f74bf8ba..."
'''[[Main Page|Home]] * [[Evaluation]] * King Safety'''

[[FILE:SamuelBak_King.jpg|border|right|thumb|link=http://chgs.elevator.umn.edu/asset/viewAsset/57f3b6787d58ae5f74bf8ba9#57f3b6d77d58ae5574bf8bb0|[[Arts#Bak|Samuel Bak]] - King <ref>[http://chgs.elevator.umn.edu/asset/viewAsset/57f3b6787d58ae5f74bf8ba9#57f3b6d77d58ae5574bf8bb0 Chess in the Art of Samuel Bak], [http://www.chgs.umn.edu/ Center for Holocaust & Genocide Studies], [https://en.wikipedia.org/wiki/University_of_Minnesota University of Minnesota]</ref> ]]

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.
<span id="PawnShield"></span>
=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|open file]] next to the king.
<span id="PawnStorm"></span>
=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.
<span id="KingTropism"></span>
=King Tropism=
King [https://en.wikipedia.org/wiki/Tropism tropism] is a simplified form of king safety evaluation. It takes into account the [[Distance|distance]] between the King and the attacking pieces, possibly weighted against [[Point Value|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]].
<span id="Scaling"></span>
=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.
<span id="Attacking"></span>
=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#UserManual|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'''.

{| class="wikitable"
|-
! Nr of<br/>Attackers
! Attack<br/>weight
|-
| style="text-align:center;" | 1
| style="text-align:right;" | 0
|-
| style="text-align:center;" | 2
| style="text-align:right;" | 50
|-
| style="text-align:center;" | 3
| style="text-align:right;" | 75
|-
| style="text-align:center;" | 4
| style="text-align:right;" | 88
|-
| style="text-align:center;" | 5
| style="text-align:right;" | 94
|-
| style="text-align:center;" | 6
| style="text-align:right;" | 97
|-
| style="text-align:center;" | 7
| style="text-align:right;" | 99
|}
<span id="SquareControl"></span>
==Square Control==
The most elaborate king safety evaluation schemes gather information about [[Square Control|control of the squares]] near the enemy king. A good explanation of such an algorithm might be found on [[Ed Schroder|Ed Schröder's]] ''Programmer Corner'' <ref>[http://www.top-5000.nl/authors/rebel/chess840.htm Programmer Corner] by [[Ed Schroder|Ed Schröder]], King Safety in REBEL</ref> <ref>How Rebel Plays Chess is also available as [http://members.home.nl/matador/Inside%20Rebel.pdf pdf]</ref>. If a program, unlike [[Rebel]], does not keep [[Incremental Updates|incrementally updated]] [[Attack and Defend Maps|attack tables]], this knowledge is likely to be uncovered while calculating [[Mobility|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:
<pre>
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
};
</pre>

This is the table generated using a formula from [[Stockfish]], rescaled to [[Centipawns|centipawns]] :
<pre>
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
};
</pre>

Using such tables, it is advisable not to evaluate king attack if only two pieces are attacking.
<span id="Patterns"></span>
=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#ChainDirection|Pawn Chain Direction]]
* [[Evaluation Function Draft]]
* [[Evaluation Patterns]]
* [[King Pattern]] in [[Bitboards]]
* [[Mate at a Glance]]

=Forums Posts=
==1998 ...==
* [https://www.stmintz.com/ccc/index.php?id=17373 King Safety] by [[Stuart Cracraft]], [[CCC]], April 22, 1998
* [https://www.stmintz.com/ccc/index.php?id=19415 King safety evaluation] by [[Roberto Waldteufel]], [[CCC]], May 28, 1998
* [http://groups.google.com/group/rec.games.chess.computer/browse_frm/thread/462b49226d1f1dfe# Asymetric king safety] by [[Tom King]], [[Computer Chess Forums|rgcc]], March 13, 1999
==2000 ...==
* [https://www.stmintz.com/ccc/index.php?id=192418 What is Piece´s tropism?] by carlos, [[CCC]], October 08, 2001
* [https://www.stmintz.com/ccc/index.php?id=235856 Bishop tropism?] by [[Pham Hong Nguyen]], [[CCC]], June 17, 2002
==2005 ...==
* [http://www.talkchess.com/forum/viewtopic.php?t=17546 king safety] by [[Charles Roberson]], [[CCC]], November 02, 2007
==2010 ...==
* [http://www.talkchess.com/forum/viewtopic.php?t=31571 Distance to King] by [[Adam Berent]], [[CCC]], January 08, 2010
* [http://www.talkchess.com/forum/viewtopic.php?t=39102 value of king tropism in eval function] by [[Tom King]], [[CCC]], May 17, 2011
* [http://www.talkchess.com/forum/viewtopic.php?t=42065 Approaches to king safety?] by [[Mike Robinson]], [[CCC]], January 19, 2012
* [http://www.talkchess.com/forum/viewtopic.php?t=44849 For Ed Schroeder: Rebel's Pawn Shield and Pawn Storm Eval] by Marcel Fournier, [[CCC]], August 21, 2012
* [http://www.talkchess.com/forum/viewtopic.php?t=52774 King shelter x-ray attacks] by [[Lyudmil Tsvetkov]], [[CCC]], June 27, 2014 » [[X-ray Attacks (Bitboards)]]
* [http://www.talkchess.com/forum/viewtopic.php?t=52779 Heavy piece shelter] by [[Lyudmil Tsvetkov]], [[CCC]], June 27, 2014
* [http://www.talkchess.com/forum/viewtopic.php?t=53508 King safety refinements] by [[Lyudmil Tsvetkov]], [[CCC]], August 31, 2014
==2015 ...==
* [http://www.talkchess.com/forum/viewtopic.php?t=57403 about king attack] by [[Daniel Anulliero]], [[CCC]], August 27, 2015
* [http://www.talkchess.com/forum/viewtopic.php?t=59583 king shelter - when and how?] by [[Alexandru Mosoi]], [[CCC]], March 21, 2016
* [http://www.talkchess.com/forum/viewtopic.php?t=59647 king safety: hard positions for zurichess] by [[Alexandru Mosoi]], [[CCC]], March 27, 2016 » [[Zurichess]]
* [http://www.talkchess.com/forum/viewtopic.php?t=61014 Castling Evaluation] by [[Dennis Sceviour]], [[CCC]], August 02, 2016 » [[Castling]]
* [http://www.talkchess.com/forum/viewtopic.php?t=65338 enemy edge pawn as part of own king shelter] by Bram Mourik, [[CCC]], September 30, 2017
* [http://www.talkchess.com/forum/viewtopic.php?t=66599 Pawn Storm - Theory] by [[Dennis Sceviour]], [[CCC]], February 14, 2018 » [[King Safety#PawnStorm|Pawn Storm]]

=External Links=
* [http://www.top-5000.nl/authors/rebel/chess840.htm Programmer Corner] by [[Ed Schroder|Ed Schröder]], King Safety in REBEL <ref>[http://www.talkchess.com/forum/viewtopic.php?t=44849 For Ed Schroeder: Rebel's Pawn Shield and Pawn Storm Eval] by Marcel Fournier, [[CCC]], August 21, 2012</ref>
* [http://www.mark-weeks.com/aboutcom/aa03e17.htm Middle game - King safety] from [http://www.mark-weeks.com/aboutcom/aa02j19.htm Chess Tutorial : Improve Your Middle Game] by [[Mark Weeks]]
* [https://en.wikibooks.org/wiki/Chess_Strategy/The_positions_of_the_kings Chess Strategy/The positions of the kings - Wikibooks]
* [[Videos#Stones|The Rolling Stones]] - [https://en.wikipedia.org/wiki/Gimme_Shelter Gimme Shelter] (1998 promotion video), [https://en.wikipedia.org/wiki/YouTube YouTube] Video
: [https://en.wikipedia.org/wiki/Bridges_to_Babylon_Tour_%2797%E2%80%9398 Bridges to Babylon Tour] feat. [https://en.wikipedia.org/wiki/Lisa_Fischer Lisa Fischer] and [[Videos#DarrylJones|Darryl Jones]], et al., December 12 1997, [https://en.wikipedia.org/wiki/The_Dome_at_America%27s_Center Trans World Dome], [https://en.wikipedia.org/wiki/St._Louis St. Louis] [https://en.wikipedia.org/wiki/Missouri Missouri]
: {{#evu:https://www.youtube.com/watch?v=8kl6q_9qZOs|alignment=left|valignment=top}}

=References=
<references />

'''[[Evaluation|Up one Level]]'''
[[Category:Samuel Bak]]

Navigation menu