Stockfish does the same moves everytime for a given postition

880 Views Asked by At

Using stockfish for a chess program. It gives the same moves every time for any position. It even opens with the same moves.

I'm using the python-chess library for communicating with stockfish I'm not sure if that's where the issue lies or if it's something else.

engine = chess.engine.SimpleEngine.popen_uci(
        r"engine/stockfish_14.1_win_x64_popcnt/stockfish_14.1_win_x64_popcnt.exe")
3

There are 3 best solutions below

0
On

About the OP's comment

" chess engines that players play against like on chess.com play various moves, especially in the opening. I realize that this may not be an inbuilt feature of the engine itself, is there a good way to implement this? "

You could pass multipv option, like

variations = engine.analyse(board, chess.engine.Limit(time=0.1), multipv=5)

maybe eliminate blunders (set e.g. thresh = 100 and decrese to make engine play stronger)

variations = [info for info in variations if
        variations[0]['score'].relative.score() - info['score'].relative.score() < thresh]

and pick a move at random

move = random.choice(variations)['pv'][0]

This way you wouldn't have to mess with opening books and you can throttle the strength of the engine a bit :)

Ok, I'm going to test it now, to see what value of thresh I can beat ...

2
On

Stockfish plays the move it thinks is best in any given position. That is why it will always give you the same move if you feed it the same position and give it the same amount to think.

In engine vs engine tournaments they often play according to given opening positions. The first X moves are therefore given and then the engines will play on from there by themselves. This way the games will be different most of the times, otherwise the games would look the same no matter how many times they played.

0
On

Chess engines like Stockfish try to play the best move for a given position. The move found by an engine depends mainly on time constraints and number of threads searching - if those parameters are the same, you can expect the returned best move to be the same - especially when the engine is searching on a single thread.