BimmerBass / Loki

C++17 chess engine
GNU General Public License v3.0
9 stars 2 forks source link

Illegal moves at low time controls #2

Closed BimmerBass closed 3 years ago

BimmerBass commented 3 years ago

Loki sometimes suggests illegal moves that are part of the PV, but I recently noticed that it also gives illegal moves in the "bestmove" uci command. The particular position is: 8/6R1/5k2/8/8/6PK/5P2/2r5 w - - 1 50 Here Loki played white and suggested f6g7 (SAN: Kxg7) when it had little time left. Weirdly enough, I haven't been able to reproduce it after that, but I suspect it is because Loki made a mistake and only displayed black's response to its suggested best move for white.

I say this because it passes all perft tests, so there isn't a problem with the move generation, but rather it is the search function where the problem is.

BimmerBass commented 3 years ago

Alright, I have gotten it down to one game out of 150. The fen is: 8/2B5/2p2k2/3p3p/b2P1K1P/5N2/5r2/4R3 b - - 3 43. Here Loki suggests the move f4f3. PGN: [Event "test"] [Site ""] [Date "2021.02.23"] [Round "58"] [White "Loki 1.0.2"] [Black "Loki 1.1.0"] [Result "1-0"] [WhiteElo "?"] [BlackElo "?"] [Variant "Standard"] [TimeControl "-"] [ECO "E36"] [Opening "Nimzo-Indian Defense: Classical Variation, Noa Variation, Botvinnik Variation"] [Termination "Illegal move"] [Annotator ""]

  1. d4 Nf6 2. c4 e6 3. Nc3 Bb4 4. Qc2 d5 5. a3 Bxc3+ 6. Qxc3 Nc6 { E36 Nimzo-Indian Defense: Classical Variation, Noa Variation, Botvinnik Variation } 7. Nf3 Ne4 8. Qc2 O-O 9. e3 g5 10. Bd3 f5 11. O-O Bd7 12. Ne1 Rc8 13. cxd5 exd5 14. Bxe4 fxe4 15. Qb3 Ne7 16. Qxb7 Rb8 17. Qxa7 Rb6 18. Qa5 Bb5 19. Qd2 Qd7 20. a4 Bxa4 21. f3 Rbf6 22. Rf2 Bb5 23. Qd1 Rf5 24. Bd2 Nc6 25. Rc1 exf3 26. gxf3 Nd8 27. f4 Ne6 28. Bb4 Rb8 29. Qc2 Nxd4 30. exd4 Ba4 31. Qd2 Rxf4 32. Ba5 Rxf2 33. Qxg5+ Qg7 34. Qxg7+ Kxg7 35. Kxf2 Rxb2+ 36. Ke3 c6 37. Nf3 Bb5 38. Re1 Rc2 39. Kf4 Rf2 40. Bd2 Kf6 41. h4 h5 42. Ba5 Ba4 43. Bc7 { White wins. (Black plays an illegal move: f4f3) } 1-0

It still isn't reproducible by just entering the fen and searching, so i assume that it is a bug with the transposition table. Loki still passes perft tests.

BimmerBass commented 3 years ago

The problem seems to have been fixed by adding new PRNG for the hashkeys, and re-setting the SearchPv when probing the transposition table.

BimmerBass commented 3 years ago

Alright, after having disabled all search optimizations (not move ordering heuristics) including use of transposition table and hash move ordering, Loki now only makes legal moves. I then tested a match between a version using transposition table and one that does not: The version not using tt seems to not make illegal moves, which suggests that it is the transposition table that is the cause of the bug.

BimmerBass commented 3 years ago

The problem seemed to be caused by the UCI functions parse_position and parse_perft taking the input string as std::string instead of char. Because I passed a char this might've shortened the input string. The problem now seems to be fixed (with transposition table), but I will first push the change when I'm done testing strength increases for pruning and reductions.