Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get top moves at each depth #121

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

thedemons
Copy link

@thedemons thedemons commented Nov 17, 2022

Added the generate_top_moves method which returns a generator that yields top moves in the position at every depth.

This helps evaluate the position at a high depth and update the interface simultaneously. The evaluation could be stopped by calling Generator.close().

Breaking changes:

  • The list returned by the generator is now a list of Stockfish.TopMove class instead of a list of dict. This is necessary for sorting the list of top moves without writing a custom sort algorithm.
  • The centipawn and mate values are now relative to the turn, they are the same values as returned from stockfish.

Notice:

  • Top moves returned while evaluating are sorted by mate first, then depth, then centipawn.
  • The depth, while evaluating, could go backward, e.g. the next depth could be lower than the previous depth.
  • Stopping the evaluation can take some time, it sends the stop command to stockfish and waits for the bestmove to be returned to continue execution.

Example usage:

Simple usage:

for top_moves in stockfish.generate_top_moves():
    best_move = top_moves[0]
    print(f"Best move at depth {best_move.depth}: {best_move.move}")

Stop the evaluation early:

gen = stockfish.generate_top_moves()
try:
    while True:
        top_moves = next(gen)
        best_move = top_moves[0]
        print(f"Best move at depth {best_move.depth}: {best_move.move}")

        if best_move.depth == 5:
            gen.close() # stop the evaluation
            break

except StopIteration:
    print("Finished evaluation")

With this simple method added, you can do some pretty neat stuff like this:
ezgif-3-ea1d484d39

@johndoknjas
Copy link
Contributor

I think this would be a great feature to have in the module!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants