miguel-ambrona / D3-Chess

Chess Unwinnability Analyzer is an implementation of a decision procedure for checking whether there exists a sequence of legal moves that allows a certain player to checkmate their opponent in a given chess position.
https://chasolver.org
GNU General Public License v3.0
51 stars 8 forks source link

Fifty and seventy-five moves rule #15

Open dlbbld opened 2 years ago

dlbbld commented 2 years ago

My understanding of the spirit of the FIDE rules for the timeout is as follows. When a player flags and there is no possible continuation of the game where the opponent could have won (common for insufficient material, rare but possible otherwise), the game is a draw. Otherwise, the flagging player loses. So that is, the half-move clock of the game on timeout is relevant.

Example 1: On White timeout, Black can win but must push the pawn to prevent the draw by the seventy-five move rule: 8/p4r2/4k3/8/K7/P7/8/8 w - - 148 100 (Lichess only shows half-move clock of 100)

CHA declares the position as winnable for Black (correct), but the given mating line starting with Kb3, Kd5 runs into the draw by seventy-five moves. There is a mating line preventing the draw by the seventy-five move rule; CHA does not choose it. In this case, CHA could choose the mating line preventing the draw.

Example 2: On White timeout, Black cannot win, for it cannot prevent the draw for the seventy-five moves rule, the half-move clock being at 148: 8/5r2/p3k3/P7/8/1K6/8/8 w - - 148 100 (Lichess only shows half-move clock of 100)

CHA declares the game as winnable for Black, for it does not check the seventy-five move rule.

Now Lichess auto draws for the fifty-move rule, which is not a FIDE rule. The FIDE rules have an auto draw for seventy-five moves, and the draw before is on a player claim only.

I suggest discussing the option to auto draw on n moves, so in practice, to support 50 for Lichess and 75 for games played according to the FIDE rules.

However, doing so has quite some implications for the simplicity of the algorithm. The algorithm can now ignore the half-move clock in the FEN to evaluate 8/p4r2/4k3/8/K7/P7/8/8 w - - 148 100 and 8/p4r2/4k3/8/K7/P7/8/8 w - - 0 100 the same. Otherwise not. Implementation-wise I think it's maybe too much of a complication, mainly for increased difficulty on node caching, but then I recommend stating this for discussion

miguel-ambrona commented 2 years ago

My interpretation of the FIDE rules is that the 75-move rule or 5-fold repetition do not apply when adjudicating timeouts.

I remember we have discussed this in the past and agreed on that the rules are ambiguous with respect to this point.

Implementing the 75-moves rule in CHA should be simple (just add a conditional on the half-move counter at the beginning of the find-mate routine). We could deploy a mode of CHA (with an optional flag) to do this.

However, if we apply the 75-moves rule, we should also consider the 5-fold repetition, which is harder to encode as input. Do you have any suggestions for this? (Note that the actual implementation of the 5-fold repetition is also easy if we use the hash table that find-mate leverages.)

dlbbld commented 2 years ago

I know we previously discussed, but I prefer my interpretation because it better aligns with the spirit of chess, as I understand it.

I consider finding different mating lines as easy for the seventy-five move rule. What is not easy is the required node caching change. The algorithm must include the half-move clock in the caching. Stockfish key() does not support it, but a fast position hash like key() is essential for performance.

In that respect, I consider dropping the support for the seventy-five move rule is feasible (analogously, one does not consider the fifty-move auto draw in Lichess for mating lines).

For the fivefold, I have no solution. Initially, one must not only load the FEN but all the game moves, which is a significant overhead. But, on the other hand, I am unsure if the helpmate lines can produce repetitions? I think rather not. The occurrence of the fivefold case is even much less likely than for the seventy-five moves.

Putting it all together, I would instead state in the PDF that the mating lines ignore the seventy-five move rule and fivefold repetition rule if one goes with my FIDE interpretation on timeout. So it will not correct the result in extremely rare cases, which I think is acceptable.

dlbbld commented 2 years ago

Wrapping up the seventy-five move and the fivefold rule would require changing the node caching logic. The effort and performance sacrifice involved is too high compared to the gain of catching a few extremely rare cases.

miguel-ambrona commented 2 years ago

Two observations:

dlbbld commented 2 years ago

Thanks for the feedback. I agree that it seems feasible.

There will be fewer hits in the transposition table for the half-move clock. But maybe that is not a problem.

As the only possible input format for the fivefold repetition, I see the initial position and the moves in UCI move notation. Unfortunately, there is no PGN reader, and one must use Stockfish to generate position hashes for performance reasons, so that requires the UCI move notation.

The previous approach results in two possibilities for deciding on a position. One inputs the FEN of the final position, which can check the seventy-five move rule but not the fivefold. Or one provides the initial FEN, and the game moves, which can decide everything. Some very rare cases, which in practice are unlikely ever to happen, can produce different results. I think one can live with that. What is your opinion on this point?

Since C++ is not my primary skill, I will keep this open for now but keep it in my backlog.

andrew-buchanan commented 1 year ago

Thanks for this discussion. From a problem perspective, there is also the Codex, which provides some minor interpretation how the rules apply to compositions. Key points under the current Codex are: (1) 3Rep is always claimed (2) DP & 50M only apply if specified or if the composition is "retro" (where this term was deliberately left vague by GMs Kjell Widlert & Michel Caillaud) (3) if 50M applies, then it is always claimed. (4) 5Rep & 75Rep do not apply. An open question is what visibility DP has of the conventions. This has been up in the air for a long time, but miguel-ambrona's incisive idea is that it depends whether the problem is retro. In this case, DP sees looming 3Rep & 50M draws. Otherwise (e.g. HDP stipulation) DP does not see them. This seems to work very well for the existing estate of compositions: allowing cool retro effects, while stopping longer HDPs from being boringly cooked by 3Rep. Other areas still to be explored in compositions may be DP interaction with: castling/ep conventions, move counts as part of the stipulation & touch move