py-stockfish / stockfish

Integrates the Stockfish chess engine with Python (Official fork)
https://py-stockfish.github.io/stockfish/
MIT License
29 stars 8 forks source link

Add an adjustable skill level (ELO for example) that works #27

Closed Zixer1 closed 1 year ago

Zixer1 commented 1 year ago

When using stockfish to implementent it to your chess game, if you currently want to have an adjustable skill level it doesnt really work, the best you can do is change the depth and reaction time but the "skill_level" doenst actually do much, what would be nice is a "set_elo" object that sets the bots elo to anything from 100 to 3900 and actually works

kieferro commented 1 year ago

The skill_level should actually already lead to the engine being better or worse. This means that it reaches a significantly lower depth at a lower skill level than at a high skill level. (If this doesn't work, then it's a bug and we will be happily fix it if you describe in more detail the case in which it occurs). There is also already the function to set an Elo value between 1320 and 3190. This should work the same way as the skill level. We are also happy to fix bugs with this if necessary.

johndoknjas commented 1 year ago

Hi @Zixer1, for skill_level and elo, expected behaviour should be sometimes getting the best move, and sometimes getting a lower ranked move. Stockfish decides this randomly, where the extent of the randomness is related to the strength you set. Is this similar to what you're getting?

Zixer1 commented 1 year ago

Hi, yes, that is what I'm getting, but what would be interesting is if we could have Stockfish play at a specific Elo constantly. So, for example, if I put a very low skill level, it would play very badly.

johndoknjas commented 1 year ago

@Zixer1 Unfortunately that's just Stockfish's behaviour, so it's probably beyond the scope of this python wrapper. I'm not aware of Stockfish having an option to consistently never play the top move. Although it does use some algorithm behind the scenes for the move selection, in order to approximate the elo you've set Stockfish to. I'm not sure how it works, but I'd assume that at lower elos, worse moves have a higher probability of being played. The relevant source code is here: https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp#L93-L96. Basically, even though in some positions Stockfish might play like a 3000+ player, in others it could play like a total beginner. On average, it should play like your desired elo in a given game.

If you'd prefer to never choose the best move, maybe what you could do is call the get_top_moves function of the python wrapper. Then, you can choose a suboptimal move from that selection however you wish. Although if you wanted something like say the 15th top move, get_top_moves(15) would have Stockfish use 15 principal variations, so the search would take longer.

Zixer1 commented 1 year ago

Oh ok , yeah i could use that function and i dont mind waiting a longer time, thank you for your time