2

我正在尝试使用 Python stockfish 库对一系列分位置进行评估。当解决方案在 x 中配合时,我的代码运行得很快。当解决方案是 x centipawns 时,它会运行很长时间。如何让stockfish 限制它考虑评估的时间?这是我的代码:

fish = Stockfish()
evals = []
fen = 'r1bqk2r/pp1pbppp/2n1pn2/2p5/2B1N3/4PN2/PPPP1PPP/R1BQ1RK1 b kq - 1 1'
fish.set_fen_position(fen)
fish.get_evaluation()
4

2 回答 2

0

我查看了代码,目前不支持使用时间获取分数。

我创建了一个可以控制时间限制的功能。该函数将返回 cp 和 mate 以及 bestmove 中的分数。

代码

from stockfish import Stockfish


VALUE_MATE = 32000


def mate_to_value(mate: int) -> int:
    """
    Convert mate number to value.
    """
    if mate > 0:
        v = VALUE_MATE - 2*mate + 1
    else:
        v = -VALUE_MATE - 2*mate

    return v


def my_get_evaluation(fish: Stockfish, fen: str, timems: int):
    """
    Evaluate the fen with fish at a given timems.
    Returns a dict of score {'cp': '49', 'mate': None} and move.
    """
    score = {}
    bestmove = '0000'

    fish.set_fen_position(fen)
    fish.get_best_move_time(timems)

    search_info = fish.info
    bestmove = search_info.split(' pv ')[1].split()[0]

    if 'score cp ' in search_info:
        score_cp = search_info.split('score cp ')[1].split()[0]
        score.update({'cp': int(score_cp), 'mate': None})
    elif 'score mate ' in search_info:
        score_mate = search_info.split('score mate ')[1].split()[0]
        score_cp = mate_to_value(int(score_mate))
        score.update({'cp': int(score_cp), 'mate': int(score_mate)})

    return score, bestmove

示例应用程序

def myprocess():
    evals = []
    only_score_cp = []
    bestmoves = []

    timems = 5000
    fens = [
        'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
        '1k6/7Q/2K5/8/8/8/8/8 w - - 0 1',
        '1k6/7Q/1K6/8/8/8/8/8 b - - 0 1'
    ]
    fish = Stockfish(r'F:\Chess\Engines\stockfish\sf14\sf14.exe')

    for fen in fens:
        score, bm = my_get_evaluation(fish, fen, timems)
        evals.append(score)
        bestmoves.append(bm)
        only_score_cp.append(score['cp'])

    for e in evals:
        print(e)
    print()

    for bm in bestmoves:
        print(bm)
    print()

    for s in only_score_cp:
        print(s)
    print()

    for f, m, e in zip(fens, bestmoves, evals):
        print(f'fen: {f}, move: {m}, score: {e}')


# Start
myprocess()

输出

{'cp': 49, 'mate': None}
{'cp': 31999, 'mate': 1}
{'cp': -31998, 'mate': -1}

e2e4
h7b7
b8c8

49
31999
-31998

fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1, move: e2e4, score: {'cp': 49, 'mate': None}
fen: 1k6/7Q/2K5/8/8/8/8/8 w - - 0 1, move: h7b7, score: {'cp': 31999, 'mate': 1}
fen: 1k6/7Q/1K6/8/8/8/8/8 b - - 0 1, move: b8c8, score: {'cp': -31998, 'mate': -1}
于 2021-12-17T06:04:05.427 回答
0

您可以使用python-chess库 ( $ pip instep python-chess) 并简单地执行此操作:

import chess
import chess.engine
engine = chess.engine.SimpleEngine.popen_uci("your/stockfish/path/here.exe")
fen = 'r1bqk2r/pp1pbppp/2n1pn2/2p5/2B1N3/4PN2/PPPP1PPP/R1BQ1RK1 b kq - 1 1'
board = chess.Board(fen)
evaluation = chess.engine.Limit(time=1))['score']
print(evaluation)
于 2021-08-07T21:38:34.727 回答