Saturday, January 7, 2012

The Lucena position in a King Rook and Pawn vs King and Rook endgame

After spotting the draws in the Philidor position I now wanted to enable to spot the Wins of the Lucena position. The Lucena position has the attacker king on the 8th rank in front of its pawn, which is not a rook pawn. The defender king is cut off from the pawn by own file or more by the attacker rook. The defender rook prevents the attacker king to leave the promotion square at one side.

The winning strategy from the Lucena position involves building a bridge with the attacker rook that shields the king from checks of the defender so the pawn can advance.

To teach the engine the rules of that positions is not so difficult. The tricky thing is the score that is awarded when this position is recognized.  When the score is to high the engine will play towards that position but then will try to stay in it. In order to win the attacker king must of course leave the promotion square which is then not the Lucena position anymore.

I decided to award a bonus worth the material of a pawn when the Lucena position or a position close to it is recognized. This seems to do the trick.

I tested the KRPKR module in a winning position with iCE 0.2 and iCE 0.3.


This is a typical position that you might encounter in a KRPKR endgame. White moves and this position is won for White. The only winning move is Rd4!

 White moves and Mates in 29 moves. 1. Rd4 !

I searched this position allowing both engines 60 seconds for the move. Both engines found the winning move almost instantly however when analyzing the output iCE 0.3 understands the position as winning where iCE 0.2 has no clue and just awards a better score for the more advanced pawn.

iCE 0.2 (reaching 18 ply in 60 sec)
info depth 2 seldepth 3 time 0 nodes 150 pv b4d4 c6h6  nps 150000 score cp 44 
info depth 18 seldepth 34 time 16094 nodes 25335132 pv b4d4 c6e6 ...  nps 1574197 score cp 94

iCE 0.3 (reaching 19 ply in 60 sec)
info depth 2 seldepth 3 time 0 nodes 160 pv b4d4 c6c1  nps 159999 score cp 47
info depth 17 seldepth 32 time 8391 nodes 12690308 pv b4d4 c6h6  nps 1512371 score cp 97
info depth 18 seldepth 32 time 9921 nodes 13093163 pv b4d4 c6h6  nps 1319742 score cp 205

info depth 19 seldepth 35 time 16219 nodes 21335820 pv b4d4 c6h6  nps 1315483 score cp 205

At ply 18 iCE 0.3 spotted the possibility to reach the Lucena position and awarded the bonus of 100 cp therefore the sudden rise in its score. When looking at the moves iCE thinks that will be played it reaches this position

White moves and Mates in 22. e7 !

The line of play iCE calculated is not optimal but forcing towards a closer win. This position is the position from the 18 ply search that was already awarded with the bonus. The Lucena position is not yet reached but the engine understands that Black is not able to prevent it anymore.

After 1. e7 Re1 2. Kf7 Rf1 3.Ke8  we are finally there

The Lucena position ! Black moves and loses in 19. (Ra1)

After 3. ... Ra1 iCE has no problem calculating the remaining moves

info depth 8 time 93 nodes 134444 pv d4d2 a1b1 d2d8 b1f1 d8c8 c6b5 nps 1445634 score cp 215
info depth 9 time 422 nodes 574486 pv d4c4 c6d6 c4f4 a1a7 f4f6 d6e5  nps 1361341 score cp 413
info depth 10 time 313 nodes 421256 pv d4c4 c6d6 c4c2 a1b1 c2f2 b1h1 e8d8 h1d1 e7e8q nps 1345865 score cp 990

In less than 1 sec it sees the queening of its pawn and raising the score.

So it seems I was able to teach the engine a fundamental chess lesson. Giving it access to a table base would probably be easier but also less fun and so I did learn my chess lesson too as I had no clue about the Lucena position either till I started programming it.