Under promotion and perft

Discussion of chess software programming and technical issues.

Moderator: Ras

lauriet
Posts: 199
Joined: Sun Nov 03, 2013 9:32 am

Under promotion and perft

Post by lauriet »

Hi All,

I am working on my move gen and perft.
At the moment I don't implement under promotion.(and I may not as it is so rare)
Does anyone know how deep I can run perft from the opening position without an error due to not having under promotion ?

Thanks
Laurie
elcabesa
Posts: 858
Joined: Sun May 23, 2010 1:32 pm

Re: Under promotion and perft

Post by elcabesa »

If I didn't blundered you need at least perft 9 to have any pawn promotion
This could be a line
A4 h6 a5 h5 a6 h4 axb7 h3 bxa8=n

9 ply
MahmoudUthman
Posts: 237
Joined: Sat Jan 17, 2015 11:54 pm

Re: Under promotion and perft

Post by MahmoudUthman »

lauriet wrote:Hi All,

I am working on my move gen and perft.
At the moment I don't implement under promotion.(and I may not as it is so rare)
Does anyone know how deep I can run perft from the opening position without an error due to not having under promotion ?

Thanks
Laurie
if speed isn't a concern whenever you encounter a promotion increment the counter by 4 instead of one "this can be branchless" , then you can test any position at any given depth.
AlvaroBegue
Posts: 932
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Under promotion and perft

Post by AlvaroBegue »

MahmoudUthman wrote:
lauriet wrote:Hi All,

I am working on my move gen and perft.
At the moment I don't implement under promotion.(and I may not as it is so rare)
Does anyone know how deep I can run perft from the opening position without an error due to not having under promotion ?

Thanks
Laurie
if speed isn't a concern whenever you encounter a promotion increment the counter by 4 instead of one "this can be branchless" , then you can test any position at any given depth.
Of course this trick will only get you to depth 9: At depth 10 the determination of what black moves are legal will depend on what squares the under-promoted pieces are attacking, which you'll have no way of knowing.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Under promotion and perft

Post by Sven »

lauriet wrote:At the moment I don't implement under promotion.(and I may not as it is so rare)
I suggest that you simply give up that restriction quickly. Implementing underpromotion is easy, but if you postpone it for a long time then it will become more difficult as your code grows and you will build one assumption upon the other ... So better do it now :-)

Typically engines generate all four possible ways of promotion during full-width search but only queen promotion during quiescence search.
lauriet
Posts: 199
Joined: Sun Nov 03, 2013 9:32 am

Re: Under promotion and perft

Post by lauriet »

How often do you think you need to under promote in real life games ???

Laurie.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Under promotion and perft

Post by Sven »

lauriet wrote:How often do you think you need to under promote in real life games ???
That is not the point. A chess engine needs to evaluate (search) millions or even billions of positions during a game, where most of them are *not* "real-life positions". And your engine will certainly miss the right move in some endgames, for instance this one:
[d]
And your engine will lose games by "false illegal move claim" if the opponent makes an underpromotion which is qualified by your engine as an illegal move.

And furthermore you will not be able to use those perft test positions that require to understand underpromotion.

And it is *so* simple to implement underpromotion, and has almost zero additional cost. So why discuss a lot about it?
MahmoudUthman
Posts: 237
Joined: Sat Jan 17, 2015 11:54 pm

Re: Under promotion and perft

Post by MahmoudUthman »

AlvaroBegue wrote:
MahmoudUthman wrote:
lauriet wrote:Hi All,

I am working on my move gen and perft.
At the moment I don't implement under promotion.(and I may not as it is so rare)
Does anyone know how deep I can run perft from the opening position without an error due to not having under promotion ?

Thanks
Laurie
if speed isn't a concern whenever you encounter a promotion increment the counter by 4 instead of one "this can be branchless" , then you can test any position at any given depth.
Of course this trick will only get you to depth 9: At depth 10 the determination of what black moves are legal will depend on what squares the under-promoted pieces are attacking, which you'll have no way of knowing.
yes, sorry about that :) .
User avatar
hgm
Posts: 28461
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Under promotion and perft

Post by hgm »

lauriet wrote:How often do you think you need to under promote in real life games ???
Promotion to Knight happens often enough to be noticible as e defect of the engine, even if it won't lose much Elo. Rook and Bishop are probably only needed if you want to solve studies.
User avatar
Ajedrecista
Posts: 2192
Joined: Wed Jul 13, 2011 9:04 pm
Location: Madrid, Spain.

Re: Underpromotions and perft.

Post by Ajedrecista »

Hello Laurie:
lauriet wrote:How often do you think you need to under promote in real life games ???

Laurie.
I started such a thread more than three years ago:

Some statistics about promotions and underpromotions.

The analysis was on engine-engine matches, that can underpromote if the just promoted pawn will be captured in the next move.

So I decided to download some human games (OM Power 2500+) from here:

www.openingmaster.com

https://app.sugarsync.com/wf/D0407059_093_708878422

Because humans do not tend to do such things, at least not so much.

I have used joined tool with the following results:

Code: Select all

C:\[...]\OM Power 201504 PGN\joined>joined -h
Illegal option: h !
Usage: joined [<*.PGN, *.FEN or *.EPD file>] [-<options>]
Legal options are:
   -p        : Print messages to both stderr and stdout
   -q        : Quit after scanning input file
   -r        : Initial value for random algorithm
   -s        : Print messages only to stdout
   -v<number>: Set verbose level to <number> (default 4)
      The numbers may be added. The numbers are:
       1: Print PGN
       2: Calculate number of legal moves in all positions
       4: Print progress (default)
       8: Print all warnings
      16: Check PGN
      32: Report result = '*'
      64: Get statistics on all types of moves
***** Error exit *****

C:\[...]\OM Power 201504 PGN\joined>joined OM_Power_201504.pgn -v70
Scanning file OM_Power_201504.pgn containing 45236605 bytes !
*** Warning: Illegal PGN syntax: ´╗┐[Event in line 1 !
--- Skipped to next header from line 1 to 1 !
*** Warning: end = -- in line 71385 !
--- Skipped to next header from line 71385 to 71387 !
*** Warning: end = -- in line 127714 !
--- Skipped to next header from line 127714 to 127716 !
*** Warning: end = -- in line 149687 !
--- Skipped to next header from line 149687 to 149689 !
*** Warning: end = -- in line 150369 !
--- Skipped to next header from line 150369 to 150371 !
*** Warning: end = -- in line 625479 !
--- Skipped to next header from line 625479 to 625481 !
*** Warning: end = -- in line 1076092 !
--- Skipped to next header from line 1076092 to 1076094 !
Read 49933 games with 4192509 moves, 1323100 lines, 45236605 bytes !
Got max. 430 moves in game 29478 before line 788847 !
Got 130120384 legal moves in 4242442 positons !
Got max. 74 legal moves after move 48 in game 14361 line 383603 !

Total move statistics:
Pawn moves = 1082281, Knight moves = 732132, Bishop moves = 667089
Rook moves = 818396, Queen moves = 483765, King moves = 497000
Check moves = 216006, Mate moves = 431, Stalemate moves = 111
Hit moves = 843599, En passant moves = 2880, Pawn two moves = 292233
Short castlings = 79777, Long castlings = 8377
Promotion to queen = 2840, Promotion to rook = 40
Promotion to bishop = 28, Promotion to knight = 60

Total game statistics:
White won = 14685, Drawn = 26204, Black won = 9044
Unknown result = 0, Illegal result = 0, Conflicting results = 0
White mated = 194, White stalemated = 48
Black mated = 237, Black stalemated = 63

Number of games with different lengths:
 15:    57,  16:    28,  17:    72,  18:    54,  19:   138,  20:    69
 21:   155,  22:    74,  23:   184,  24:    99,  25:   190,  26:   119
 27:   220,  28:   119,  29:   245,  30:   149,  31:   265,  32:   137
 33:   299,  34:   182,  35:   302,  36:   205,  37:   327,  38:   239
 39:   377,  40:   267,  41:   401,  42:   251,  43:   433,  44:   264
 45:   455,  46:   297,  47:   465,  48:   311,  49:   512,  50:   376
 51:   502,  52:   328,  53:   547,  54:   367,  55:   528,  56:   379
 57:   544,  58:   412,  59:   610,  60:   588,  61:   733,  62:   490
 63:   698,  64:   470,  65:   631,  66:   431,  67:   747,  68:   496
 69:   642,  70:   472,  71:   681,  72:   508,  73:   666,  74:   504
 75:   707,  76:   442,  77:   687,  78:   498,  79:   730,  80:   798
 81:  1112,  82:   639,  83:   736,  84:   518,  85:   698,  86:   509
 87:   679,  88:   434,  89:   625,  90:   452,  91:   561,  92:   443
 93:   496,  94:   395,  95:   523,  96:   401,  97:   509,  98:   395
 99:   464, 100:   356, 101:   470, 102:   341, 103:   416, 104:   355
105:   408, 106:   342, 107:   434, 108:   296, 109:   394, 110:   286
111:   348, 112:   303, 113:   350, 114:   275, 115:   349, 116:   271
117:   315, 118:   246, 119:   310, 120:   299, 121:   298, 122:   236
123:   275, 124:   178, 125:   228, 126:   187, 127:   210, 128:   174
129:   190, 130:   182, 131:   228, 132:   162, 133:   151, 134:   154
135:   164, 136:   136, 137:   146, 138:   116, 139:   155, 140:   114
141:   125, 142:   103, 143:   137, 144:    76, 145:   109, 146:    92
147:   123, 148:    96, 149:   106, 150:    79, 151:    87, 152:    59
153:    85, 154:    62, 155:    85, 156:    58, 157:    67, 158:    49
159:    76, 160:    63, 161:    58, 162:    51, 163:    47, 164:    52
165:    43, 166:    45, 167:    49, 168:    40, 169:    53, 170:    34
171:    41, 172:    30, 173:    52, 174:    39, 175:    43, 176:    27
177:    31, 178:    25, 179:    34, 180:    30, 181:    34, 182:    22
183:    24, 184:    19, 185:    32, 186:    18, 187:    17, 188:    22
189:    13, 190:    12, 191:    24, 192:    16, 193:    12, 194:    13
195:    12, 196:     8, 197:    13, 198:    10, 199:    17, 200:    17
201:    11, 202:    17, 203:    17, 204:    10, 205:    10, 206:     8
207:    10, 208:     9, 209:    10, 210:     8, 211:     5, 212:     5
213:    11, 214:     9, 215:    13, 216:     9, 217:     7, 218:     2
219:     7, 220:     9, 221:     8, 222:     2, 223:     3, 224:    10
225:     9, 226:     4, 227:    10, 228:     7, 229:     5, 230:     4
231:     4, 232:     2, 233:     6, 234:     1, 235:     3, 236:     4
237:     5, 238:     2, 239:     3, 240:     2, 241:     2, 242:     6
243:     2, 244:     5, 245:     5, 246:     2, 247:     4, 248:     2
249:     3, 250:     3, 251:     3, 252:     2, 253:     7, 254:     3
255:     4, 256:     4, 257:     1, 258:     2, 261:     1, 262:     1
263:     2, 265:     2, 266:     1, 267:     5, 269:     1, 270:     1
271:     1, 272:     3, 273:     2, 274:     1, 275:     4, 276:     1
277:     4, 278:     2, 279:     1, 281:     2, 285:     1, 287:     1
290:     2, 291:     1, 292:     2, 293:     2, 298:     1, 299:     1
303:     1, 305:     2, 307:     1, 311:     1, 317:     1, 318:     1
320:     1, 321:     1, 325:     1, 327:     1, 329:     1, 330:     1
331:     1, 338:     1, 348:     1, 349:     1, 400:     1, 407:     1
430:     1
White 18 (9):
-v70 flag means that I used options 2, 4 and 64 (2 + 4 + 64 = 70). FWIW here:

Code: Select all

49933 games.

Promotion to queen = 2840, Promotion to rook = 40
Promotion to bishop = 28, Promotion to knight = 60

Code: Select all

Total number of promotions = Pr = 2840 + 40 + 28 + 60 = 2968

Q/Pr ~ 95.69 %
N/Pr ~  2.02 %
R/Pr ~  1.35 %
B/Pr ~  0.94 %
It would be curious to see how many of these knight underpromotions are in check, captured, etc.

I have used my not perfect own tool, which is described in the thread whose link I posted above, with the following results after 171 seconds:

Code: Select all

Search_promotions_and_underpromotions_by_crowning_squares, © 2013.

------------------------------------------------------------
Tool that provides some info about promotions in a PGN file.
------------------------------------------------------------

Write down the full name of the PGN file (up to 64 characters), including .pgn:

OM_Power_201504.pgn

Write down the clock rate of the CPU (in GHz), only for timing the elapsed time of the calculations:

3

Searching for games and results...

Games:       49933      Estimated remaining time:   185.6 seconds.
  1-0:       14685      Estimated remaining time:   178.8 seconds.
  0-1:        9044      Estimated remaining time:   175.0 seconds.
  1/2-1/2:   26204      Estimated remaining time:   169.2 seconds.
  Unknown:       0
______________________________

White score:  55.65%
______________________________

White advantage:    39.42 Elo.
______________________________

Searching promotions and underpromotions...

 1/64  a1=Q     231     Estimated remaining time:   167.0 seconds.
 2/64  a1=R       0     Estimated remaining time:   165.3 seconds.
 3/64  a1=B       4     Estimated remaining time:   163.0 seconds.
 4/64  a1=N       3     Estimated remaining time:   160.6 seconds.
 5/64  b1=Q     201     Estimated remaining time:   158.0 seconds.
 6/64  b1=R       6     Estimated remaining time:   155.4 seconds.
 7/64  b1=B       3     Estimated remaining time:   153.5 seconds.
 8/64  b1=N       2     Estimated remaining time:   151.0 seconds.
 9/64  c1=Q     198     Estimated remaining time:   148.5 seconds.
10/64  c1=R       2     Estimated remaining time:   146.3 seconds.
11/64  c1=B       0     Estimated remaining time:   144.0 seconds.
12/64  c1=N       3     Estimated remaining time:   141.8 seconds.
13/64  d1=Q     138     Estimated remaining time:   140.2 seconds.
14/64  d1=R       4     Estimated remaining time:   138.2 seconds.
15/64  d1=B       3     Estimated remaining time:   135.8 seconds.
16/64  d1=N       2     Estimated remaining time:   133.7 seconds.
17/64  e1=Q     116     Estimated remaining time:   132.1 seconds.
18/64  e1=R       3     Estimated remaining time:   130.0 seconds.
19/64  e1=B       3     Estimated remaining time:   127.8 seconds.
20/64  e1=N       4     Estimated remaining time:   126.4 seconds.
21/64  f1=Q     119     Estimated remaining time:   124.1 seconds.
22/64  f1=R       3     Estimated remaining time:   121.8 seconds.
23/64  f1=B       0     Estimated remaining time:   119.5 seconds.
24/64  f1=N       3     Estimated remaining time:   117.4 seconds.
25/64  g1=Q     114     Estimated remaining time:   115.1 seconds.
26/64  g1=R       4     Estimated remaining time:   113.0 seconds.
27/64  g1=B       3     Estimated remaining time:   111.0 seconds.
28/64  g1=N       4     Estimated remaining time:   108.8 seconds.
29/64  h1=Q     148     Estimated remaining time:   106.7 seconds.
30/64  h1=R       0     Estimated remaining time:   104.5 seconds.
31/64  h1=B       1     Estimated remaining time:   102.3 seconds.
32/64  h1=N       2     Estimated remaining time:   100.1 seconds.
33/64  a8=Q     246     Estimated remaining time:    97.8 seconds.
34/64  a8=R       3     Estimated remaining time:    95.5 seconds.
35/64  a8=B       0     Estimated remaining time:    93.4 seconds.
36/64  a8=N       3     Estimated remaining time:    91.2 seconds.
37/64  b8=Q     248     Estimated remaining time:    88.9 seconds.
38/64  b8=R       3     Estimated remaining time:    86.9 seconds.
39/64  b8=B       0     Estimated remaining time:    84.6 seconds.
40/64  b8=N       2     Estimated remaining time:    82.3 seconds.
41/64  c8=Q     216     Estimated remaining time:    80.1 seconds.
42/64  c8=R       3     Estimated remaining time:    78.0 seconds.
43/64  c8=B       2     Estimated remaining time:    76.0 seconds.
44/64  c8=N       4     Estimated remaining time:    74.0 seconds.
45/64  d8=Q     246     Estimated remaining time:    71.9 seconds.
46/64  d8=R       2     Estimated remaining time:    69.8 seconds.
47/64  d8=B       4     Estimated remaining time:    67.7 seconds.
48/64  d8=N       6     Estimated remaining time:    65.6 seconds.
49/64  e8=Q     149     Estimated remaining time:    63.7 seconds.
50/64  e8=R       4     Estimated remaining time:    61.6 seconds.
51/64  e8=B       1     Estimated remaining time:    59.5 seconds.
52/64  e8=N       8     Estimated remaining time:    57.4 seconds.
53/64  f8=Q     174     Estimated remaining time:    55.3 seconds.
54/64  f8=R       1     Estimated remaining time:    53.3 seconds.
55/64  f8=B       0     Estimated remaining time:    51.2 seconds.
56/64  f8=N      10     Estimated remaining time:    49.1 seconds.
57/64  g8=Q     144     Estimated remaining time:    47.0 seconds.
58/64  g8=R       2     Estimated remaining time:    44.9 seconds.
59/64  g8=B       2     Estimated remaining time:    42.8 seconds.
60/64  g8=N       3     Estimated remaining time:    40.7 seconds.
61/64  h8=Q     151     Estimated remaining time:    38.6 seconds.
62/64  h8=R       0     Estimated remaining time:    36.5 seconds.
63/64  h8=B       2     Estimated remaining time:    34.5 seconds.
64/64  h8=N       1     Estimated remaining time:    32.5 seconds.

Promotions/underpromotions to:
  Q:   2839
  R:     40
  B:     28
  N:     60

Searching promotions/underpromotions that give check...

1/8   *1=Q+     306     Estimated remaining time:    30.4 seconds.
2/8   *1=R+       9     Estimated remaining time:    28.4 seconds.
3/8   *1=B+       6     Estimated remaining time:    26.4 seconds.
4/8   *1=N+      19     Estimated remaining time:    24.4 seconds.
5/8   *8=Q+     401     Estimated remaining time:    22.4 seconds.
6/8   *8=R+       8     Estimated remaining time:    20.3 seconds.
7/8   *8=B+       6     Estimated remaining time:    18.3 seconds.
8/8   *8=N+      31     Estimated remaining time:    16.3 seconds.

Searching promotions/underpromotions that give checkmate...

1/8   *1=Q#       1     Estimated remaining time:    14.2 seconds.
2/8   *1=R#       0     Estimated remaining time:    12.2 seconds.
3/8   *1=B#       0     Estimated remaining time:    10.2 seconds.
4/8   *1=N#       0     Estimated remaining time:     8.2 seconds.
5/8   *8=Q#       5     Estimated remaining time:     6.1 seconds.
6/8   *8=R#       0     Estimated remaining time:     4.1 seconds.
7/8   *8=B#       0     Estimated remaining time:     2.0 seconds.
8/8   *8=N#       0

Number of lines of this PGN file:    1323100

Approximated elapsed time:   171.0 seconds.

The obtained results will be saved into Promotions_stats_OM_Power_201504.pgn.txt file.

The results were successfully saved into Promotions_stats_OM_Power_201504.pgn.txt file.

Thanks for using this tool. Press enter to exit.

Code: Select all

OM_Power_201504.pgn
 
a1=Q:    231
a1=R:      0
a1=B:      4
a1=N:      3
 
b1=Q:    201
b1=R:      6
b1=B:      3
b1=N:      2
 
c1=Q:    198
c1=R:      2
c1=B:      0
c1=N:      3
 
d1=Q:    138
d1=R:      4
d1=B:      3
d1=N:      2
 
e1=Q:    116
e1=R:      3
e1=B:      3
e1=N:      4
 
f1=Q:    119
f1=R:      3
f1=B:      0
f1=N:      3
 
g1=Q:    114
g1=R:      4
g1=B:      3
g1=N:      4
 
h1=Q:    148
h1=R:      0
h1=B:      1
h1=N:      2
 
a8=Q:    246
a8=R:      3
a8=B:      0
a8=N:      3
 
b8=Q:    248
b8=R:      3
b8=B:      0
b8=N:      2
 
c8=Q:    216
c8=R:      3
c8=B:      2
c8=N:      4
 
d8=Q:    246
d8=R:      2
d8=B:      4
d8=N:      6
 
e8=Q:    149
e8=R:      4
e8=B:      1
e8=N:      8
 
f8=Q:    174
f8=R:      1
f8=B:      0
f8=N:     10
 
g8=Q:    144
g8=R:      2
g8=B:      2
g8=N:      3
 
h8=Q:    151
h8=R:      0
h8=B:      2
h8=N:      1
 
=======================
 
a1=*:     238 ( 17.94%)
b1=*:     212 ( 15.98%)
c1=*:     203 ( 15.30%)
d1=*:     147 ( 11.08%)
e1=*:     126 (  9.50%)
f1=*:     125 (  9.42%)
g1=*:     125 (  9.42%)
h1=*:     151 ( 11.38%)
 SUM:    1327
 
-----------------------
 
a8=*:     252 ( 15.37%)
b8=*:     253 ( 15.43%)
c8=*:     225 ( 13.72%)
d8=*:     258 ( 15.73%)
e1=*:     162 (  9.88%)
f8=*:     185 ( 11.28%)
g8=*:     151 (  9.21%)
h8=*:     154 (  9.39%)
 SUM:    1640
 
-----------------------
 
a*=*:     490 ( 16.51%)
b*=*:     465 ( 15.67%)
c*=*:     428 ( 14.43%)
d*=*:     405 ( 13.65%)
e*=*:     288 (  9.71%)
f*=*:     310 ( 10.45%)
g*=*:     276 (  9.30%)
h*=*:     305 ( 10.28%)
 SUM:    2967
 
=======================
 
*1=*:    1327 ( 44.73%)
*8=*:    1640 ( 55.27%)
 SUM:    2967
 
=======================
 
*1=Q:    1265 ( 95.33%)
*1=R:      22 (  1.66%)
*1=B:      17 (  1.28%)
*1=N:      23 (  1.73%)
 SUM:    1327
 
-----------------------
 
*8=Q:    1574 ( 95.98%)
*8=R:      18 (  1.10%)
*8=B:      11 (  0.67%)
*8=N:      37 (  2.26%)
 SUM:    1640
 
-----------------------
 
 *=Q:    2839 ( 95.69%)
 *=R:      40 (  1.35%)
 *=B:      28 (  0.94%)
 *=N:      60 (  2.02%)
 SUM:    2967
 
=======================
 
(   2967 promotions and underpromotions in   49933 games) ~ 0.0594 (promotions and underpromotions)/game.
One queen promotion is missing. Anyway, I got 786 promotions and underpromotions giving check and only six promotions giving checkmate:

Code: Select all

Promotions/underpromotions that give check:

*1=Q+     306
*1=R+       9
*1=B+       6
*1=N+      19
*8=Q+     401
*8=R+       8
*8=B+       6
*8=N+      31

------------------------

Promotions/underpromotions that give checkmate:

*1=Q#       1
*1=R#       0
*1=B#       0
*1=N#       0
*8=Q#       5
*8=R#       0
*8=B#       0
*8=N#       0
50 out of 60 knight underpromotions are giving check, so far.

You might not lose anything if you implement underpromotions in your engine. Good luck!

I apologise for this long, long post.

Regards from Spain.

Ajedrecista.