Test position that requires KKP evaluation

Discussion of chess software programming and technical issues.

Moderator: Ras

zd3nik
Posts: 193
Joined: Wed Mar 11, 2015 3:34 am
Location: United States

Test position that requires KKP evaluation

Post by zd3nik »

[d] 8/3k1r1p/4RP1K/7P/8/8/8/8 w - - 2 58 am Re7+

Was watching a game that resulted in this position and my engine played Re7+? Obviously if it had specific knowledge about KKP endgames (or endgame tablebase support) it would have seen this leads to a draw.

I'm curious if anyone has an engine that sees the folly of Re7 very quickly *without* endgame tablebase or special KKP handling. And if so what technique do you use? I have logic to see runaway passers in K vs P endgames, but no logic to recognize blocked passers in K vs P endgames.

My engine takes about 40 seconds and 19 plies to see a correc solution. :(

Code: Select all

info depth 19 seldepth 35 nodes 49992180 time 22710 nps 2201328 score cp 180 pv e6e7 f7e7 f6e7 d7e7 h6h7 e7f6 h7h6 f6f7 h6g5 f7g7 g5f4 g7h6 f4g4 h6h7 g4h3 h7g8 h3h4 g8h7 h4g3 h7g7
info depth 19 seldepth 35 nodes 57647437 time 26325 nps 2189836 currmovenumber 2 currmove e6a6 score cp 181 lowerbound
info depth 19 seldepth 35 nodes 64267463 time 29570 nps 2173400 currmovenumber 2 currmove e6a6 score cp 197 lowerbound
info depth 19 seldepth 35 nodes 72557269 time 33399 nps 2172438 currmovenumber 2 currmove e6a6 score cp 229 lowerbound
info depth 19 seldepth 35 nodes 86953526 time 39888 nps 2179941 score cp 254 pv e6a6 d7e8 a6a8 e8d7 h6g5 d7e6 a8e8 e6d7 e8e5 d7d6 g5f5 f7f8 e5e7 f8h8 h5h6 d6c5 e7g7 c5d6 f6f7 d6e7 g7g8
User avatar
hgm
Posts: 28331
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Test position that requires KKP evaluation

Post by hgm »

When I switch on KingSlayer/Simple's (rather rudimentary) end-game knowledge (the 'drawish material' and 'specific endings' options, KPK being one of the latter), it sees the trouble at 12 ply/0.18 sec (2.4GHz i3):

Code: Select all

dep	score	nodes	time	(not shown:  tbhits	knps	seldep)
 15	+1.96 	1.82M  	0:01.40	e6a6 d7c8 h6g5 f7d7 g5f5 c8b7 a6e6 d7d1 e6e7 b7c6 e7h7 d1f1 f5e5 f1e1 e5f4 e1f1 
 14	+1.95 	663171	0:00.59	e6a6 d7c8 h6g5 f7d7 g5f5 c8b7 a6e6 d7d2 e6e7 b7c6 e7h7 d2f2 f5e5 f2e2 e5d4 e2d2 d4e4 c6d6 
 13	+1.92 	406317	0:00.40	e6a6 d7c8 h6g5 f7c7 a6a8 c8b7 a8f8 c7c5 g5h6 c5f5 f6f7 b7a7 h6h7 
 12	+1.66 	218262	0:00.26	e6a6 d7c8 h6g5 f7c7 h5h6 c7d7 g5f5 d7c7 f5f4 c7d7 a6a5 d7d4 f4e3 
 12	+0.23 	125159	0:00.18	e6e7 f7e7 f6e7 d7e7 h6h7 e7f6 h7h6 f6f7 
 11	+2.30 	109775	0:00.17	e6e7 f7e7 f6e7 d7e7 h6h7 e7f6 h5h6 f6f7 h7h8 
 11	+1.66 	102819	0:00.16	e6a6 d7c8 h6g5 f7c7 h5h6 c7d7 g5f5 c8b7 f5e6 d7c7 a6d6 
 10	+1.61 	56507  	0:00.11	e6a6 d7c8 h6g5 f7c7 h5h6 c8b7 a6e6 c7c5 g5f4 c5c4 e6e4 c4c2 
  9	+1.55 	21591  	0:00.06	e6a6 d7c8 h6g5 c8b7 a6e6 b7c8 h5h6 c8d7 g5f5 
  8	+1.55 	9122    	0:00.04	e6a6 d7c8 h6g5 f7d7 h5h6 c8b7 a6e6 b7c7 
  7	+1.55 	5752    	0:00.00	e6a6 d7c8 h6g5 f7d7 h5h6 
  6	+1.35 	3164    	0:00.00	e6a6 d7c8 h6g5 c8b7 a6d6 b7c7 
  5	+1.45 	1685    	0:00.00	e6a6 d7c8 h6g5 c8b7 a6d6 
  4	+1.31 	1013    	0:00.00	e6a6 d7c8 
  4	+1.21 	366      	0:00.00	e6b6 d7c7 b6a6 c7d7 
  3	+1.27 	181      	0:00.00	e6b6 d7c7 b6e6 
  2	+1.27 	69        	0:00.00	e6b6 d7c7 
  1	+1.23 	22        	0:00.00	e6b6 
This case is handled by the following code in the KPK recognizer ('KPKdraw()'):

Code: Select all

  if(edge) { // rook-pawns: detect some cases that don't apply to other Pawns
    int dPath = (hisKing^pawn) & 7;                                       // distance of bare King to Pawn path
    if(dPath < 2 && dist[hisKing - promo] < d - dPath) return 1;          // bare King in or next to path of Pawn
    if(((king^pawn) & 7) == 0 && dist[hisKing - (king^2)] < 2 - myTurn)   // own King is trapped in path of Pawn
      return 1;
  }
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: Test position that requires KKP evaluation

Post by cdani »

I removed kpk bitbase of Andscacs and I tried:

Code: Select all

info depth 1 currmove e6e1 currmovenumber 1
info depth 1 seldepth 5 score cp 207 nodes 63 nps 31500 time 2 pv e6e7 f7e7
info depth 2 seldepth 2 score cp 207 nodes 66 nps 11000 time 6 pv e6e7
info depth 3 seldepth 7 score cp 274 nodes 214 nps 21400 time 10 pv e6e7 f7e7 f6
e7 d7e7 h6h7 e7f6 h5h6
info depth 4 seldepth 8 score cp 232 nodes 285 nps 19000 time 15 pv e6e7 f7e7 f6
e7 d7e7 h6h7 e7f6 h5h6 f6g5
info depth 5 seldepth 9 score cp 273 nodes 564 nps 26857 time 21 pv e6e7 f7e7 f6
e7 d7e7 h6h7 e7f6 h5h6 f6g5 h7g7
info depth 5 currmove e6b6 currmovenumber 3
info depth 6 seldepth 11 score cp 189 nodes 2039 nps 67966 time 30 pv e6b6 d7d8
h6g5 d8c7 b6a6 c7d7
As it does not have any knowledge of kpk apart of the bitbase, it's just a raw search result.
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: Test position that requires KKP evaluation

Post by cdani »

cdani wrote:I removed kpk bitbase of Andscacs and I tried:

Code: Select all

info depth 1 currmove e6e1 currmovenumber 1
info depth 1 seldepth 5 score cp 207 nodes 63 nps 31500 time 2 pv e6e7 f7e7
info depth 2 seldepth 2 score cp 207 nodes 66 nps 11000 time 6 pv e6e7
info depth 3 seldepth 7 score cp 274 nodes 214 nps 21400 time 10 pv e6e7 f7e7 f6
e7 d7e7 h6h7 e7f6 h5h6
info depth 4 seldepth 8 score cp 232 nodes 285 nps 19000 time 15 pv e6e7 f7e7 f6
e7 d7e7 h6h7 e7f6 h5h6 f6g5
info depth 5 seldepth 9 score cp 273 nodes 564 nps 26857 time 21 pv e6e7 f7e7 f6
e7 d7e7 h6h7 e7f6 h5h6 f6g5 h7g7
info depth 5 currmove e6b6 currmovenumber 3
info depth 6 seldepth 11 score cp 189 nodes 2039 nps 67966 time 30 pv e6b6 d7d8
h6g5 d8c7 b6a6 c7d7
As it does not have any knowledge of kpk apart of the bitbase, it's just a raw search result.
I can add that here the check extension, capture extension, and king near passed pawn bonus helps to arrive to the critical position rather quickly.
User avatar
hgm
Posts: 28331
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Test position that requires KKP evaluation

Post by hgm »

But does it really see at this depth that Re7 is a draw, or is it just accidental that it thinks another move is better than an also very positive score? Can you do multi-PV, so that we can see the score of Re7 also at larger depth?
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: Test position that requires KKP evaluation

Post by cdani »

hgm wrote:But does it really see at this depth that Re7 is a draw, or is it just accidental that it thinks another move is better than an also very positive score? Can you do multi-PV, so that we can see the score of Re7 also at larger depth?
You have reason. If I put the position after Re7+, it requires some time until it realizes that is draw, at depth 17:
Andscacs 0.86071 by Daniel Jose
pf 8/3kRr1p/5P1K/7P/8/8/8/8 b - - 0 58
20
info depth 1 currmove d7c6 currmovenumber 1
info depth 1 seldepth 4 score cp -207 nodes 25 nps 12500 time 2 pv f7e7 f6e7
info depth 2 seldepth 6 score cp -274 nodes 69 nps 11500 time 6 pv f7e7 f6e7 d7e
7 h6h7 e7f6 h5h6
info depth 3 seldepth 7 score cp -232 nodes 116 nps 11600 time 10 pv f7e7 f6e7 d
7e7 h6h7 e7f6 h5h6 f6g5
info depth 4 seldepth 8 score cp -273 nodes 203 nps 14500 time 14 pv f7e7 f6e7 d
7e7 h6h7 e7f6 h5h6 f6g5 h7g7
info depth 5 currmove f7e7 currmovenumber 1
info depth 5 seldepth 10 score cp -267 nodes 604 nps 30200 time 20 pv f7e7 f6e7
d7e7 h6g7 e7e6 g7h7 e6f5 h5h6 f5g4
info depth 6 seldepth 11 score cp -226 nodes 917 nps 36680 time 25 pv f7e7 f6e7
d7e7 h6g7 h7h6 g7h6 e7f6 h6h7 f6g5 h5h6
info depth 7 currmove f7e7 currmovenumber 1
info depth 7 seldepth 12 score cp -252 nodes 1900 nps 59375 time 32 pv f7e7 f6e7
d7e7 h6g7 h7h6 g7h6 e7f6 h6h7 f6g5 h5h6 g5h5
info depth 8 seldepth 15 score cp -243 nodes 2217 nps 59918 time 37 pv f7e7 f6e7
d7e7 h6g7 h7h6 g7h6 e7f7 h6h7 f7f6 h5h6 f6f7 h7h8
info depth 9 seldepth 15 score cp -259 nodes 2583 nps 61500 time 42 pv f7e7 f6e7
d7e7 h6g7 h7h6 g7h6 e7f7 h6h7 f7f6 h5h6 f6f7 h7h8 f7g6
info depth 10 seldepth 15 score cp -300 nodes 3590 nps 76382 time 47 pv f7e7 f6e
7 d7e7 h6g7 h7h6 g7h6 e7f7 h6h7 f7f6 h5h6 f6f7 h7h8 f7g6 h6h7 g6h6
info depth 11 currmove f7e7 currmovenumber 1
info depth 11 seldepth 18 score cp -248 nodes 5713 nps 103872 time 55 pv f7e7 f6
e7 d7e7 h6g7 h7h6 g7h7 e7f6 h7h6 f6f7 h6g5 f7g7 h5h6 g7g8 g5g6 g8h8 g6f6
info depth 12 seldepth 18 score cp -248 nodes 6270 nps 102786 time 61 pv f7e7 f6
e7 d7e7 h6g7 h7h6 g7h7 e7f6 h7h6 f6f7 h6g5 f7g7 h5h6 g7h7 g5h5 h7g8 h5g6 g8h8
info depth 13 seldepth 19 score cp -211 nodes 9745 nps 143308 time 68 pv f7e7 f6
e7 d7e7 h6g7 h7h6 g7h6 e7f7 h6g5 f7g7 h5h6 g7h7 g5h5 h7g8 h5g4 g8h8 g4f4 h8g8 f4
g3
info depth 14 currmove f7e7 currmovenumber 1
info depth 14 seldepth 19 score cp -141 nodes 14114 nps 178658 time 79 pv f7e7 f
6e7 d7e7 h6g7 h7h6 g7h6 e7f7 h6g5 f7g7 g5h4 g7h6 h4g4 h6h7 g4h3 h7g7 h3g3 g7h6 g
3h4
info depth 15 seldepth 19 score cp -160 nodes 20178 nps 231931 time 87 pv f7e7 f
6e7 d7e7 h6g7 e7e6 g7h7 e6f6 h7h6 f6f7 h6g5 f7g7 g5h4 g7h6 h4g4 h6h7 g4h3 h7g7 h
3g3 g7h6
info depth 16 currmove f7e7 currmovenumber 1
info depth 16 seldepth 22 score cp -202 nodes 29161 nps 291610 time 100 pv f7e7
f6e7 d7e7 h6g7 e7e6 g7h7 e6f6 h7h6 f6f7 h6g5 f7g7 h5h6 g7h8 g5h4 h8h7 h4h5 h7g8
h5g6 g8h8 g6g5 h8h7
info depth 17 currmove f7e7 currmovenumber 1
info depth 17 seldepth 23 score cp -1 nodes 46688 nps 385851 time 121 pv f7e7 f6
e7 d7e7 h6g7 e7e6 g7h7 e6f6 h7h6 f6f7 h6g5 f7g7 h5h6 g7h8 g5h4 h8h7 h4h5 h7g8 h5
g4 g8h7 g4h5
Stan Arts
Posts: 179
Joined: Fri Feb 14, 2014 10:53 pm
Location: the Netherlands

Re: Test position that requires KKP evaluation

Post by Stan Arts »

With it turned off Nemeton struggles as well but in this position it's about edge pawns and edge pawns are even easier to handle than general kpk opposition in the middle of the board.

If you don't have king inside the square of the pawn code you can probably return a draw if the king can reach the g8, h8 and h7 squares.

But I recently added general kpk opposition as well. That always seemed a daunting task to me having to be clever, take in account side to move etc. but nope. Even something so simple as the defending king reaching the two rows infront of the pawn and the attacking king on the same row or behind the pawn handles it well in practise not even looking at side to move or distances to the pawn horizontally. The problem with taking horizontal distances into account infact was that search has a knack of breaking that on the last ply. Stepping it's own king out or forcing the other king out of the draw rule on the last ply. Sneaky sneaky search.
User avatar
hgm
Posts: 28331
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Test position that requires KKP evaluation

Post by hgm »

What seemed to be important is that when you are in a position that is recognized as a draw, the defending side should always be able to stay in such a recognized positions. (Of course he will always be able to stay in a position that is a draw, otherwise the current position would not have been a draw in the first place, but it might not be recognized.) Otherwise the search starts stalling to shif the recognized draw position one ply before the horizon, imagining a zugzwang that would revive the win.

The code I finally arrived at was

Code: Select all

int
KPKdraw (int side)
{ // identify some draw positions that will not quickly resolve.
  int king = location[side+KING], pawn = location[side+PAWN], hisKing = location[KING+xside];
  int myTurn = (side == stm), forward = 16-4*side; // from strong-side POV
  int promo = (pawn & 7) + 7*16*!side;             // promotion square
  int lastRank = !((hisKing^promo) & 0x70);        // bare King is on last rank
  int next = (king == pawn+1 || king == pawn-1);   // strong-side King standing beside Pawn
  int edge = (pawn & 7) == 0 || (pawn & 7) == 7;   // Pawn is on edge file
  int d = dist[pawn-promo], stopSqr;

  if(board[pawn] != side+PAWN) return 0;       // sanity check, as location[] is not 100% reliable for Pawns
  if(dist[hisKing-king] < 2) return 0;         // reject illegal position. (KPKdraw is called before legality test...)
  if(myTurn && aligned[hisKing-pawn] & code[side+PAWN]) return 0; // Pawn captures King

  // rule of squares
  d -= (d ==6);                                      // account for initial double-push
  if(d - myTurn < dist[hisKing-promo] - 1) return 0; // outside 'square' is always won

  if(edge) { // rook-pawns: detect some cases that don't apply to other Pawns
    int dPath = (hisKing^pawn) & 7;                                       // distance of bare King to Pawn path
    if(dPath < 2 && dist[hisKing - promo] < d - dPath) return 1;          // bare King in or next to path of Pawn
    if(((king^pawn) & 7) == 0 && dist[hisKing - (king^2)] < 2 - myTurn)   // own King is trapped in path of Pawn
      return 1;
  }

  if(hisKing == pawn + forward)   return (!lastRank | myTurn);            // I must stalemate him or abandon Pawn
  if(pawn == (promo^16)) return 0;                                        // play out other 7th-rank Pawns

  stopSqr = pawn + 2*forward;
  if(stopSqr == promo) { // last-rank defense; quite tricky!
    return (dist[hisKing - promo] < 2 && (!next || dist[king + 2*forward - hisKing] + myTurn == 1));
  } else return (dist[stopSqr - king] - myTurn > dist[stopSqr - hisKing]);
  if(hisKing == stopSqr) return (edge | !lastRank | !myTurn | !next);     // 2 squares in front of Pawn

  if((pawn&0x70) == (king&0x70)) { // attacking K is on same rank as P
    if(dist[stopSqr - hisKing] <= 1 && !myTurn && !lastRank) return 1;    // he can step in path
    if(hisKing == king + 2*forward) return (!lastRank || next && myTurn); // oppose K next to P (cut off on last rank!)
    if(dist[king + 4*forward - hisKing] + myTurn == 1) return 1;                   // he has far opposition (or can take it)
  }
  if((pawn&0x70) == (king&0x70) - forward) { // attacking K is on rank before P
    return (!lastRank & dist[king + 2*forward - hisKing] + myTurn == 1);           // he has opposition, or can take it
  }

  return 0;
}
This is a bit more complex than I had hoped for, so I wonder if further simplification would be possible.
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: Test position that requires KKP evaluation

Post by cdani »

If anyone is interested, some time ago I put here code that solves 265222 of 331352 possible kpk positions:

http://talkchess.com/forum/viewtopic.ph ... 8&start=10
User avatar
hgm
Posts: 28331
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Test position that requires KKP evaluation

Post by hgm »

In practice it is not necessary to solve all positions. You only need to recognize some tedious draws that can be quickly reached. E.g. a lot of positions are draws because the Pawn will be lost. But the search will find those quickly. (OTOH, they also are not difficult to recognize, with the aid of a distance table.) What the search needs help with is 'stand-off' positions, where it is a draw, but you can drag it out almost indefinitely by aimlessly moving around in the draw sector. E.g. in KBPK, where the leading side can keep the material advantage hundreds of moves without repeating by touring the board with its King, and then stepping the Bishop to another square on the diagonal of the Pawn to make another King toer, etc. Those draws the search would never find. Recognizing positions with the defending King on or next to the promotion corner square as draws solves that. That many other positions are draw because you cannot stop the defending King from getting to that corner doesn't contibute much strength, as the search will quickly find that.

In KPK the key positions are those where the defending King is in or next to the path of the Pawn.