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 194 forks source link

How exactly nodes are counted in janggicasual? (Quick question) #283

Closed maksimKorzh closed 3 years ago

maksimKorzh commented 3 years ago

@ianfab Hi Fabian

Sorry for disturbing you, but could you please quickly explain how exactly nodes are counted in janggicasual perft? I see following:

  1. Bikjang is allowed (so kings can face each other and it's ok)
  2. moves like e2e2 represent "pass" move However what is not clear is how these "passes" are counted. Say in position 9/4k4/9/9/9/9/9/4K4/9 w - - 0 1 (empty board, opposed kings on initial squares) for janggicasual perft we have: depth 1: 9 (clear) depth 2: 81 (9 * 9 clear) depth 3: e2d1: 36 e2e1: 36 e2f1: 36 e2d2: 36 e2f2: 36 e2d3: 36 e2e3: 36 e2f3: 36 e2e2: 72

In my movegen I get: 54 nodes for orthogonal moves root moves (still 36 nodes for diagonal moves) and 81 move for "pass" root move.

So I feel confused about pass nodes counting for that seems to be the only source of extra nodes. Can you please explain how exactly nodes are counted in janggi casual

Thanks in advance!

ianfab commented 3 years ago

I suspect you might have misunderstood how kings move in Janggi. They can only move diagonally on the diagonal lines of the palace, but not in general. So after moving orthogonally leaving the diagonal lines, a king can not move diagonally on its next move.

Usually it is easy to find the differences by simply traversing the subtrees and checking perft (e.g., perft 3, do move, perft 2, do move, perft 1). If you always select a move where perft results differ, you by force will arrive at a leaf node with a discrepancy in perft. This can sometimes be a bit tedious, but this way at least it is trivial to identify the exact moves that cause the discrepancy.

maksimKorzh commented 3 years ago

Hi Fabian

re: I suspect you might have misunderstood how kings move in Janggi. They can only move diagonally on the diagonal lines of the palace, but not in general. So after moving orthogonally leaving the diagonal lines, a king can not move diagonally on its next move.

re: Usually it is easy to find the differences by simply traversing the subtrees and checking perft (e.g., perft 3, do move, perft 2, do move, perft 1). If you always select a move where perft results differ, you by force will arrive at a leaf node with a discrepancy in perft. This can sometimes be a bit tedious, but this way at least it is trivial to identify the exact moves that cause the discrepancy.

Now all is clear and I can proceed! Thank you so much for assistance! Fairy Stockfish is amazing tool for perft, I highly appreciate your work!

Thank you agian and sorry for disturbing, I'm closing this issue.

maksimKorzh commented 3 years ago

@ianfab Hi Fabian

Sorry for bothering you again, but I found a really strange behavior in Fairy Stockfish's perft which I can't understand, so here's how to reproduce it (this is janggicasual variant):

position fen 9/4k4/9/9/9/9/9/9/4K4/9 w - - 0 1 moves e2e2
d

 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |10 *
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   | k |   |   |   |   |9
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |8
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |7
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |6
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |5
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |4
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |3
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   | K |   |   |   |   |2
 +---+---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   |   |1  
 +---+---+---+---+---+---+---+---+---+
   a   b   c   d   e   f   g   h   i

Fen: 9/4k4/9/9/9/9/9/9/4K4/9 b - - 1 1
Sfen: 9/4k4/9/9/9/9/9/9/4K4/9 w - 2
Key: 458050954C8B9F07
Checkers: 
go perft 2
e9d8: 9
e9e8: 9
e9f8: 9
e9d9: 9
e9f9: 9
e9d10: 9
e9e10: 9
e9f10: 9
e9e9: 0  // doesn't make any moves as response to "pass" move

Nodes searched: 72

position fen 9/4k4/9/9/9/9/9/9/4K4/9 b - - 1 1
go perft 2
e9d8: 9
e9e8: 9
e9f8: 9
e9d9: 9
e9f9: 9
e9d10: 9
e9e10: 9
e9f10: 9
e9e9: 9 // responses to "pass" move as if it was a casual move

Nodes searched: 81

The problem is following: I run perft depth 2 for this position in janggicasual mode: 9/4k4/9/9/9/9/9/9/4K4/9 b - - 1 1 Depending on how to achieve that position on board perft results are different. For instance if I set the position via "position fen <>" then it gives 81 nodes BUT if I set position to: 9/4k4/9/9/9/9/9/9/4K4/9 w - - 1 1 (same but white to move) and then make a "pass" move via "position fen <> moves e2e2" and run perft it gives 72 nodes.

This is very confusing. Can you please explain the reason behind it and what number of nodes actually should be?

ianfab commented 3 years ago

This is expected, because two subsequent passes are expected to end the game, similar as in Go. Therefore it matters whether the previous move was a passing move or is unknown (when giving the position without history).

maksimKorzh commented 3 years ago

Thank you. Now it's clear.