Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在minimax函数内生成国际象棋移动的转置表_Python_Chess - Fatal编程技术网

Python 在minimax函数内生成国际象棋移动的转置表

Python 在minimax函数内生成国际象棋移动的转置表,python,chess,Python,Chess,背景 我正在尝试优化我的国际象棋极小极大算法,到目前为止已经使用了alpha-beta修剪。我还使用换位表实现了迭代深化,以便以后进行优化。到目前为止,我在一些python代码中得到的基本信息是: # Original call def ai_make_move(gamestate): for depth in range(1, max_depth): move, evaluation = minimax(gamestate, depth, -math.inf, math

背景

我正在尝试优化我的国际象棋极小极大算法,到目前为止已经使用了alpha-beta修剪。我还使用换位表实现了迭代深化,以便以后进行优化。到目前为止,我在一些python代码中得到的基本信息是:

# Original call
def ai_make_move(gamestate):
    for depth in range(1, max_depth):
        move, evaluation = minimax(gamestate, depth, -math.inf, math.inf, maximizing_player)

# Minimax
def minimax(gamestate, depth, -math.inf, math.inf, maximizing_player):
    board.is_human_turn = not maximizing_player
    children = board.get_all_possible_moves()

    if depth == 0 or board.is_draw or board.is_check_mate:
        return None, evaluate(board)

    best_move = children[0]

    if maximizing_player:
        max_eval = -math.inf
        for child in children:
            board.make_move(child)
            current_eval = minimax(board, depth - 1, alpha, beta, False)[1]
            board.unmake_move()

            if current_eval > max_eval:
                max_eval = current_eval
                best_move = child
            alpha = max(alpha, current_eval)

            if beta <= alpha:
                break

        return best_move, max_eval

    else:
        min_eval = math.inf
        for child in children:
            board.make_move(child)
            current_eval = minimax(board, depth - 1, alpha, beta, True)[1]
            board.unmake_move()

            if current_eval < min_eval:
                min_eval = current_eval
                best_move = child
            beta = min(beta, current_eval)

            if beta <= alpha:
                break
        return best_move, min_eval
出于某种原因,引擎做出了完全不合理的动作,在当前位置上是不合法的,而且似乎不起作用

我尝试过的

我已核实:

  • 我的Zobrist是用正确的方法计算的
  • make_move和unmake_move函数按其应有的方式工作(在玩人对人游戏时)
  • 在没有换位表的概念的情况下,AI似乎在工作,并且在玩好且有效的动作
问题


我不知道出了什么问题,也不知道这是否是正确的方法。非常感谢您的指导

你不能假设引擎在较浅深度进行的评估是准确的,因为位置可能会随着下一步的移动而改变,例如简单地捕捉你的女王。这就是为什么不计算之前搜索到的移动,您可以先从表中进行移动排序并计算之前迭代中最有希望的移动,而且alpha-beta剪枝通常比从固定深度搜索更快

# Minimax
def minimax(gamestate, depth, -math.inf, math.inf, maximizing_player):

    if gamestate.zobrist_key in gamestate.transposition_table:
        children = gamestate.transposition_table[gamestate.zobrist_key]
    else:
        board.is_human_turn = not maximizing_player
        children = board.get_all_possible_moves()

    if depth == 0 or board.is_draw or board.is_check_mate:
        return None, evaluate(board)

   ......