lichess.org
Donate

I have a problem with Python's code designed to solve trivial endgames

I have a simple bug in my code:The code in python is here: pastebin.com/xJzREewU
I have discovered a bug in my code: it says 973, but Lichess analysis says 1000-23=977, please see here: pastebin.com/hZ6AaBZe

Here 23 means that 23 plies are to be played by optimally playing B&W players, the mated one seeks for delaying the checkmate while the mating one for approaching it as soon as possible.
I suggest change this line of your code:
if best_case > 0:
AR[fen]['result'] = update_best_case(best_case)
AR[fen]['best_child'] = best_child
changed = True
To : AR[fen]['result'] = update_best_case(best_case)
AR[fen]['best_child'] = best_child
changed = True
Remove the if best_case > 0 cause in a checkmate position the value is -1000 and as -1000 is less than 0 the if condition is not satisfied. This might be the cause of the bug.
It gives yet less accurate result:

K = 1
_t = 0
_t = 1
Finally we found a mate! -inf
AR for initial fen is = {'result': -inf, 'last_move': None, 'children': None, 'best_child': None}
. . . . . . . .
. . . k . . . .
. . . . . . . .
. . K . . R . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
<
Path is: 8/3k4/8/2K2R2/8/8/8/8 w - -