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

take longer time compare with chess module #95

Closed
sugizo opened this issue May 30, 2022 · 5 comments
Closed

take longer time compare with chess module #95

sugizo opened this issue May 30, 2022 · 5 comments

Comments

@sugizo
Copy link

sugizo commented May 30, 2022

stockfish python module take longer time compare with chess module using the same stockfish version

can compare my code using stockfish python module on #94
with

import chess
import chess.pgn
import time
from IPython.display import display, HTML, clear_output

def engine_analyse(app_path, board, nodes):
    #engine = chess.engine.SimpleEngine.popen_uci(app_path)
    #info = engine.analyse(board, chess.engine.Limit(time = 9.9, depth = 9, 
    #                                                nodes = nodes, mate = 9) )
    #engine.quit()
    with chess.engine.SimpleEngine.popen_uci(app_path) as engine:
        info = engine.analyse(board, chess.engine.Limit(time = 9.9, depth = 9, 
                                                        nodes = nodes, mate = 9) )
    clear_output(wait = True)
    return info

def stockfish_analyse(board):
    app_path = "engine/stockfish"
    nodes = 9
    info = engine_analyse(app_path, board, nodes)
    return info

def who(player):
    return "White" if player == chess.WHITE else "Black"

def display_board(board, use_svg):
    if use_svg:
        return board._repr_svg_()
    else:
        return "<pre>" + str(board) + "</pre>"

def play_pgn(file_pgn, analyse, sleep = 9.9, visual = "svg"):
    """
    visual: "simple" | "svg" | None
    """
    use_svg = (visual == "svg")
    pgn = open(file_pgn)
    read_game = chess.pgn.read_game(pgn)
    read_game_mainline_moves = read_game.mainline_moves()
    board = read_game.board()
    game = chess.pgn.Game()

    try:
        for uci in read_game_mainline_moves:
            board.push(uci)

            if board.is_checkmate():
                html = """
                <table>
                <tr>
                <th rowspan = "7" style = "width:45%%;text-align: center">%s</th>
                <th style = "text-align: left">Board</th>
                <td style = "text-align: left"><pre>%s</pre></td>
                </tr>
                <tr>
                <th style = "text-align: left">Move</th>
                <td style = "text-align: left">UCI : %s</td>
                </tr>
                </table>
                """ % (board._repr_svg_(), board, uci)
            else:
                info = analyse(board)
                if info:
                    score = info['score']
                    info_pv_uci = [info_pv.uci() for info_pv in info['pv'] ]
                    pv = ", ".join(info_pv_uci)
                    nodes = info['nodes']
                    depth = info['depth']
                    info_time = info['time']
                else: 
                    score = ''
                    pv = ''
                    nodes = ''
                    depth = ''
                    info_time = ''
                html = """
                <table>
                <tr>
                <th rowspan = "12" style = "width:45%%;text-align: center">%s</th>
                <th style = "text-align: left">Board</th>
                <td style = "text-align: left"><pre>%s</pre></td>
                </tr>
                <tr>
                <th style = "text-align: left">Move</th>
                <td style = "text-align: left">UCI : %s</td>
                </tr>
                <tr>
                <th style = "text-align: left">Score</th>
                <td style = "text-align: left">%s</td>
                </tr>
                <tr>
                <th style = "text-align: left">PV</th>
                <td style = "text-align: left">%s</td>
                </tr>
                <tr>
                <th style = "text-align: left">Depth</th>
                <td style = "text-align: left">%s</td>
                </tr>
                <tr>
                <th style = "text-align: left">Nodes</th>
                <td style = "text-align: left">%s</td>
                </tr>
                <tr>
                <th style = "text-align: left">Time</th>
                <td style = "text-align: left">%s</td>
                </tr>
                </table>
                """ % (board_stop, board, uci, score, pv, depth, nodes, info_time)

            if visual is not None:
                if visual == "svg":
                    clear_output(wait = True)

                display(HTML(html) )

                if visual == "svg":
                    time.sleep(sleep)

    except KeyboardInterrupt:
        msg = "Game Interrupted!"
        return (msg, board)

    if board.is_checkmate():
        msg = "Checkmate : " + who(not board.turn) + " Wins!"
    else:
        msg = "Draw"
    
    if visual is not None:
        print(msg)
        print('PGN File :', file_pgn)

play_pgn("pgn/chess_openings/a/A00 - Amar Gambit.pgn", stockfish_analyse, sleep = 0.0)

when using stockfish module set the parameter to 0 in hope to have faster result
and when using chess python module set the parameter to 9 in hope to have longer

but the result is stockfish python module still get the longer time
tested using default parameter in stockfish python module but still get the longer time

@johndoknjas
Copy link
Contributor

Many of the custom parameters you chose in your code for #94 don't make the engine stronger. E.g., you're using the minimum for Threads, Skill Level, Hash. One or more of these could have an influence on engine speed. E.g., lowering Skill Level makes SF increase the value for MultiPV, in order for it to have sub-optimal moves to pick from.

Also, you're using depth 9 for the python chess module, but that's still very low. Running Stockfish directly, it gets to depth 9 in an instant. So it'd be better to test both python modules using a depth value of at least 20. What was the time difference when you did your tests?

@sugizo
Copy link
Author

sugizo commented May 31, 2022

already tried different parameter, and
the default

import stockfish 
stockfish.get_parameters()
    stockfish = Stockfish(path = engine, depth = 18, 
                          parameters = {'Contempt': 0, 'Debug Log File': '', 'Hash': 1024, 
                                        'Min Split Depth': 0, 'Minimum Thinking Time': 20, 
                                        'Move Overhead': 10, 'MultiPV': 1, 'Ponder': 'false', 
                                        'Skill Level': 20, 'Slow Mover': 100, 'Threads': 1, 
                                        'UCI_Chess960': 'false', 'UCI_Elo': 1350, 
                                        'UCI_LimitStrength': 'false'} )

but still got the same result, that's why before post an issue, already try to mix some parameter value and perhaps can change the result, but the result is same (stockfish python module take longer)

question
what is the parameter both of chess and stockfish, to get the same result ?

@johndoknjas
Copy link
Contributor

For taking longer, do you mean looping forever, or just slower in general?

To get comparable results, I'd recommend not sending in any parameters for the python stockfish module, just set depth to something like 25. For python chess, maybe just set hash equal to 1024, and also depth to 25. Use defaults for everything else.

@sugizo
Copy link
Author

sugizo commented May 31, 2022

in this issue is talk about just stockfish engine not it's derivates

$ ~/Downloads/chess/engine/stockfish -v
Stockfish 11 64 by T. Romstad, M. Costalba, J. Kiiski, G. Linscott

so it's never looping forever when using stockfish engine with stockfish python module, it's just slower comparable when the same stockfish app above version 11 is used by chess module

@kieferro
Copy link
Contributor

kieferro commented Mar 7, 2023

Hello, this project is no longer maintained in this repo but on this fork. (For more information about this please look here).

It is not possible to transfer the entire discussion here. However, when we go through the remaining issues, we will get back to you and either create a proxy issue or find another solution.

Just be aware that the project will no longer be updated here.

@sugizo sugizo closed this as completed May 16, 2023
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

No branches or pull requests

3 participants