r/ComputerChess • u/andrewl_ • Oct 07 '22
Why does stockfish answer differently on first vs. second request to evaluate?
I have a situation where stockfish, freshly initiated, will give a different answer than when it's evaluating a second position.
The position is simple: 8/4k3/8/4K3/1P6/8/8/8 b - - 3 2 and I expected black to give his best defense: Kd7 and the engine performs as expected:
fen = '8/4k3/8/4K3/1P6/8/8/8 b - - 3 2'
engine = chess.engine.SimpleEngine.popen_uci("/usr/local/bin/stockfish")
board.set_fen(fen)
move = engine.play(board, chess.engine.Limit(time=1.0)).move
print(f'found move: {move}')However, when I asked it to evaluate a previous position, and THEN this position, it returns Kf8 instead (an obvious loss):
engine = chess.engine.SimpleEngine.popen_uci("/usr/local/bin/stockfish")
board.set_fen('8/5k2/8/8/1P4K1/8/8/8 w - - 0 1')
engine.play(board, chess.engine.Limit(time=1.0))
board.set_fen(fen)
move = engine.play(board, chess.engine.Limit(time=1.0)).move
print(f'found move: {move}')I'm using python-chess and, suspecting it could be at fault, replicated the results with a raw session:
uci
...
position fen 8/5k2/8/8/1P4K1/8/8/8 w - - 0 1
go movetime 1000
...
position fen 8/4k3/8/4K3/1P6/8/8/8 b - - 3 2
go movetime 1000
...
bestmove e7f8 ponder e5d6(where UCI e7f8 is SAN kf8) Any help's appreciated. This is an issue because in my training tool I need stockfish to give its best defense while white attempts to win (maintaining opposition until in front of the pawn, then outflanking).
2
6
u/causa-sui Oct 07 '22
I'm not able to replicate this with Stockfish 15:
Note that Stockfish is multi-threaded and, therefore, its outcomes are somewhat chaotic (some say "non-deterministic"). The initial state of Stockfish's cache, the CPU cache, memory timings, other activities the machine performs while the search is in progress, and a million things you don't control can affect these outcomes. For instance, you may notice that the number of nodes searched varies from one run to another. There is nothing you or anyone else can do about that without drastically damaging the efficiency of the search and therefore the overall strength of the engine.