Place to find correct perft result from a fen position

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Place to find correct perft result from a fen position

Post by eligolf »

Hi,

I am wondering if there is a good place to put in a fen and search depth and get the correct number of searched nodes for that case? I have Googled but all I found was some C code which I am not very familiar with.
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Place to find correct perft result from a fen position

Post by hgm »

Usually people generate such counts as they need them, with the aid of an engine or dedicated perft program such as qperft. Then you can generate the counts for any position you would like, by just specifying the FEN. (Popular positions are the FIDE start positions, or 'Kiwi Pete'; the latter has many quick promotions and castlings.)

E.g.:

Code: Select all

$ ./perft
Usage is: perft <depth> [H<hash size>] [-<split depth>] [<FEN string>]
          <hash size> = 20 gives you 2^20 = 1M entries (16MB)

Makro@Makro-PC /home/perft
$ ./perft 5 "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -"
 - - - - - - - - - - - -
 - - - - - - - - - - - -
 - - r . . . k . . r - -
 - - p . p p q p b . - -
 - - b n . . p n p . - -
 - - . . . P N . . . - -
 - - . p . . P . . . - -
 - - . . N . . Q . p - -
 - - P P P B B P P P - -
 - - R . . . K . . R - -
 - - - - - - - - - - - -
 - - - - - - - - - - - -

Quick Perft by H.G. Muller
Perft mode: No hashing, bulk counting in horizon nodes

perft( 1)=           48 ( 0.000 sec)
perft( 2)=         2039 ( 0.000 sec)
perft( 3)=        97862 ( 0.000 sec)
perft( 4)=      4085603 ( 0.050 sec)
perft( 5)=    193690690 ( 1.030 sec)
eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Re: Place to find correct perft result from a fen position

Post by eligolf »

Thank you H.G.

I am wondering because I get the correct results from all cases on https://www.chessprogramming.org/Perft_Results. But when I try to run some other cases that I found from random people online, e.g. the first answer here http://talkchess.com/forum3/viewtopic.php?t=59046, I get lots of bad results. So I am wondering if these might be wrong, or if my code is wrong? It is especially the promotion and checkmate/stalemate cases that I get some strange results from.

I also wonder what the rules are for stalemate. Is it stalemate immediately when material is wrong with promotion, or do black have to move out of check or something? I mean, I get the wrong result for even an easy position as below. I try it human vs human with takebacks and all looks fine, until I try with perft. [d]K1k5/8/P7/8/8/8/8/8 w - - 0 1
Which claims to be 2217 nodes at depth 6, but I get 2015 (for both black and white to move first). Is my code wrong, or are these random FENs I found online wrong? :)

Here is my perft function if I messed something up...

Code: Select all

    def perft(self, gamestate, depth):

        self.counter += 1

        if depth == 0:
            return

        children = gamestate.get_valid_moves()

        if gamestate.is_check_mate or gamestate.is_stale_mate:
            return

        for child in children:

            gamestate.make_move(child[0], child[1], child[2])
            self.perft(gamestate, depth - 1)
            gamestate.unmake_move()

        return
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Place to find correct perft result from a fen position

Post by elcabesa »

here you can find 6838 random position with perft values from depth 1 to 6 in a csv like format

https://github.com/elcabesa/vajolet/blo ... /perft.txt
eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Re: Place to find correct perft result from a fen position

Post by eligolf »

Thank you elca! Are these confirmed to be correct?
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Place to find correct perft result from a fen position

Post by elcabesa »

eligolf wrote: Fri Nov 20, 2020 3:44 pm
Which claims to be 2217 nodes at depth 6, but I get 2015 (for both black and white to move first). Is my code wrong, or are these random FENs I found online wrong? :)
in this case the correct value is 2217.

to debug your movegen and perft I suggest to implement "divide", you can find perft & divide command of vajolet here.

https://github.com/elcabesa/vajolet/blo ... /perft.cpp

using divide and comparing divide result with the one given from a reference program you can understand where the problem is.

here an example of perft and divide results

Code: Select all

position fen K1k5/8/P7/8/8/8/8/8 w - - 0 1
perft 6
Perft 6 leaf nodes: 2217
1ms 2217 kN/s

divide 6
1) Ka7: 1380
2) a7: 837
divide Res= 2217

The divide command make possible understand where the error is.
let's suppose that your divide 6 returns 1379 moves for Ka7 instead of 1380
now you have to issue the uci command

Code: Select all

position fen K1k5/8/P7/8/8/8/8/8 w - - 0 1 moves a8a7
divide 5
and continue searching until you found the error
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Place to find correct perft result from a fen position

Post by elcabesa »

eligolf wrote: Fri Nov 20, 2020 4:02 pm Thank you elca! Are these confirmed to be correct?
I hope so, Vajolet and Stockfish indipendently give the same result on those positions, so I suppose they are confirmed. But I haven't tried counting all the moves using a chessboard :)
eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Re: Place to find correct perft result from a fen position

Post by eligolf »

Not questioning you, just found that some other I found online must be wrong :) Like this one:

[d]r1k1r2q/p1ppp1pp/8/8/8/8/P1PPP1PP/R1K1R2Q w KQkq - 0 1

It says it should be 23 at depth 1 but I can clearly count that it should be 21 for both black and white... Makes me wonder about the others I found :)
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Place to find correct perft result from a fen position

Post by hgm »

There are no known errors in qperft. But when you use hashing (which shouldn't be necessary for comparatively low depths), there is always the (very small) risk of a key collision spoiling the count.

This is a divided perft for 3-move sequences of the given position:

Code: Select all

$ ./perft 6 -4 "K1k5/8/P7/8/8/8/8/8 w - - 0 1"
 - - - - - - - - - - - -
 - - - - - - - - - - - -
 - - K . k . . . . . - -
 - - . . . . . . . . - -
 - - P . . . . . . . - -
 - - . . . . . . . . - -
 - - . . . . . . . . - -
 - - . . . . . . . . - -
 - - . . . . . . . . - -
 - - . . . . . . . . - -
 - - - - - - - - - - - -
 - - - - - - - - - - - -

Quick Perft by H.G. Muller
Perft mode: No hashing, bulk counting in horizon nodes

perft( 1)=            2 ( 0.000 sec)
perft( 2)=            6 ( 0.000 sec)
2. a6a7 moves =          4 ( 0.000 sec)
2. a8a7 moves =          9 ( 0.000 sec)
perft( 3)=           13 ( 0.000 sec)
3. a6a7 c8d8 moves =          6 ( 0.000 sec)
3. a6a7 c8c7 moves =          0 ( 0.000 sec)
3. a6a7 c8d7 moves =         11 ( 0.000 sec)
2. a6a7 moves =         17 ( 0.000 sec)
3. a8a7 c8d8 moves =         15 ( 0.000 sec)
3. a8a7 c8c7 moves =          6 ( 0.000 sec)
3. a8a7 c8d7 moves =         25 ( 0.000 sec)
2. a8a7 moves =         46 ( 0.000 sec)
perft( 4)=           63 ( 0.000 sec)
4. a6a7 c8d8 a8b8 moves =         22 ( 0.000 sec)
4. a6a7 c8d8 a8b7 moves =         30 ( 0.000 sec)
3. a6a7 c8d8 moves =         52 ( 0.000 sec)
3. a6a7 c8c7 moves =          0 ( 0.000 sec)
4. a6a7 c8d7 a8b8 moves =         43 ( 0.000 sec)
4. a6a7 c8d7 a8b7 moves =         51 ( 0.000 sec)
3. a6a7 c8d7 moves =         94 ( 0.000 sec)
2. a6a7 moves =        146 ( 0.000 sec)
4. a8a7 c8d8 a7b7 moves =         21 ( 0.000 sec)
4. a8a7 c8d8 a7b8 moves =         16 ( 0.000 sec)
4. a8a7 c8d8 a7a8 moves =         16 ( 0.000 sec)
4. a8a7 c8d8 a7b6 moves =         28 ( 0.000 sec)
3. a8a7 c8d8 moves =         81 ( 0.000 sec)
4. a8a7 c8c7 a7a8 moves =         19 ( 0.000 sec)
3. a8a7 c8c7 moves =         19 ( 0.000 sec)
4. a8a7 c8d7 a7b7 moves =         36 ( 0.000 sec)
4. a8a7 c8d7 a7b8 moves =         31 ( 0.000 sec)
4. a8a7 c8d7 a7a8 moves =         27 ( 0.000 sec)
4. a8a7 c8d7 a7b6 moves =         42 ( 0.000 sec)
3. a8a7 c8d7 moves =        136 ( 0.000 sec)
2. a8a7 moves =        236 ( 0.000 sec)
perft( 5)=          382 ( 0.000 sec)
4. a6a7 c8d8 a8b8 moves =        130 ( 0.000 sec)
4. a6a7 c8d8 a8b7 moves =        165 ( 0.000 sec)
3. a6a7 c8d8 moves =        295 ( 0.000 sec)
3. a6a7 c8c7 moves =          0 ( 0.000 sec)
4. a6a7 c8d7 a8b8 moves =        251 ( 0.000 sec)
4. a6a7 c8d7 a8b7 moves =        291 ( 0.000 sec)
3. a6a7 c8d7 moves =        542 ( 0.000 sec)
2. a6a7 moves =        837 ( 0.000 sec)
4. a8a7 c8d8 a7b7 moves =        125 ( 0.000 sec)
4. a8a7 c8d8 a7b8 moves =         96 ( 0.000 sec)
4. a8a7 c8d8 a7a8 moves =         96 ( 0.000 sec)
4. a8a7 c8d8 a7b6 moves =        154 ( 0.000 sec)
3. a8a7 c8d8 moves =        471 ( 0.000 sec)
4. a8a7 c8c7 a7a8 moves =        108 ( 0.000 sec)
3. a8a7 c8c7 moves =        108 ( 0.000 sec)
4. a8a7 c8d7 a7b7 moves =        213 ( 0.000 sec)
4. a8a7 c8d7 a7b8 moves =        189 ( 0.000 sec)
4. a8a7 c8d7 a7a8 moves =        165 ( 0.000 sec)
4. a8a7 c8d7 a7b6 moves =        234 ( 0.000 sec)
3. a8a7 c8d7 moves =        801 ( 0.000 sec)
2. a8a7 moves =       1380 ( 0.000 sec)
perft( 6)=         2217 ( 0.000 sec)
I guess the other position is a Chess960 position. So you failed to count the two castlings.

P.S. I did not understand your code. It seems like you also count internal nodes of the tree, not just leaves.
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Place to find correct perft result from a fen position

Post by elcabesa »

eligolf wrote: Fri Nov 20, 2020 4:22 pm Not questioning you, just found that some other I found online must be wrong :) Like this one:

[d]r1k1r2q/p1ppp1pp/8/8/8/8/P1PPP1PP/R1K1R2Q w KQkq - 0 1

It says it should be 23 at depth 1 but I can clearly count that it should be 21 for both black and white... Makes me wonder about the others I found :)
this looks like a fischer 960 position, so the 2 moves you can't find seems to be O-O and O-O-O