fairy-stockfish / Fairy-Stockfish

chess variant engine supporting Xiangqi, Shogi, Janggi, Makruk, S-Chess, Crazyhouse, Bughouse, and many more
https://fairy-stockfish.github.io/
GNU General Public License v3.0
617 stars 193 forks source link

Wrong ply? #148

Closed antoyo closed 4 years ago

antoyo commented 4 years ago

Hi. I made an analysis:

xboard
variant bughouse
setboard r1bq2nQ~/ppp1kpR1/3ppnp1/8/4P1n1/4B1P1/P1P3P1/1N1QKB1R[NQnrq] w - - 3 36
analyze
1 251 0 392 1 78400 0    Q@a3 g4e3
2 853 0 645 2 92142 0    g7f7 e7f7
3 853 0 782 4 111714 0   g7f7 e7f7 Q@g7 f7e8
4 989 0 939 6 117375 0   g7f7 e7f7 Q@g7 f7e8
5 1386 1 2898 8 193200 0     g7f7 e7e8 h8g7 Q@a5 Q@d2 g4e3 d2a5 e3d1 e1d1
6 1414 1 3164 7 210933 0     g7f7 e7e8 h8g7 Q@b4 Q@d2 b4e4
7 1563 1 3452 8 215750 0     g7f7 e7e8 h8g7
8 1785 1 4321 8 240055 0     g7f7 e7e8 h8g7 Q@b4 Q@d2
9 100003 2 5145 8 257250 0   g7f7 e7f7 N@g5 f7e7 Q@f7

And I was wondering if the ply (first column) is wrong. How come the engine only found the mate in 3 at ply 9? Shouldn't it have find it a ply 6? Thanks.

Edit: Also, not sure if it's related, but the engine seem to have found a checkmate in 5 before a checkmate in 3. Is this normal? What is the best way to find the shortest checkmate?

ianfab commented 4 years ago

Your assumptions would be correct if the engine would just do a textbook alpha-beta search. However, since modern chess engines like Stockfish apply a lot of search extensions and reductions, quiescence search, as well as aggressive forward pruning, the situation is not that simple, see e.g. https://www.chessprogramming.org/Selectivity. Therefore, even though the base depth might be 6 (ply), the engine might search specific branches of the search tree to very different depths, e.g., one branch to 3 ply and another one to 8 ply, so this is expected.

This also explains why sometimes the engine finds a longer checkmate sequence before a shorter one. For mate finding one can try to limit the reduction and pruning techniques. One simple way is by setting multipv to a larger value. There also have been SF forks specifically for mate finding in the past, but usually in practice the most efficient way is to do an interactive analysis and play candidate moves on the board, and if a sequence leads to a forced mate the engine will find and remember that when going back to the initial position.