【问题标题】:Implementing Iterative Deepening with minimax algorithm with alpha beta pruning PYTHON使用带有 alpha beta 修剪 PYTHON 的 minimax 算法实现迭代深化
【发布时间】:2021-06-04 05:00:49
【问题描述】:

我已经使用 alpha beta pruning 实现了一个 NegaMax 算法(它只是 minimax 算法的一个较短版本)。现在我想实现迭代深化,以便我可以找到每个深度的最佳移动,然后根据前一层的分数对树下的节点重新排序,以便我的字母修剪更有效。

这是我到目前为止所做的:

InitialDEPTH = 1

def findBestMove(gs, validMoves):
    global nextMove
    global InitialDEPTH 
    nextMove = None
    
    for d in range(2):
        CurrentDEPTH = InitialDEPTH + d
        findMoveNegaMaxAlphaBeta(gs, validMoves, CurrentDEPTH, -CHECKMATE, CHECKMATE, 1 if gs.whiteToMove else -1)
    
    return nextMove    

这里的 gs 是随着每一步移动而变化的游戏状态,它包含关于该点游戏的所有信息,例如是否可以进行易位或是否有可能的过路移动。我的 negamax 算法如下所示:

def findMoveNegaMaxAlphaBeta(gs, validMoves, depth, alpha, beta, turnMultiplier):
    global nextMove
    if depth == 0 :
       return turnMultiplier * scoreBoard(gs)    

    maxScore = -CHECKMATE

    # I have a felling i need to add some code here to make it work
    for move in validMoves :
        gs.makeMove(move)
        nextMoves = gs.getValidMoves()
        score = -findMoveNegaMaxAlphaBeta(gs, nextMoves, depth - 1 , -beta, -alpha, -turnMultiplier)
        if score > maxScore:
            maxScore = score
            if depth == DEPTH :
                nextMove = move
        gs.undoMove() 
        if maxScore > alpha:   # This is were pruning happens
            alpha = maxScore
        if alpha >= beta :
            break    

    return maxScore   

如何将时间约束函数添加到此代码中,以便它仅在提到的时间结束时返回最佳移动,而不是在此之前。

此外,我如何在每个深度之后对节点重新排序,以便在下一个深度进行有效修剪。我为此编写了某种函数,但我不知道如何实现它。我写的函数:

def sorting(move):
    gs.makeMove(move)
    score = scoreBoard(gs)
    gs.undoMove()

    return turnMultiplier * score
validMoves.sort(key = sorting)
    

【问题讨论】:

  • 您能否就您在实施迭代深化时遇到的特定问题提出问题?在您的问题中,我没有看到实施它的尝试。你肯定在网上找到了所有的信息和伪代码……问题出在哪里?
  • 我编辑了我的问题并尝试实施它。

标签: python chess minimax alpha-beta-pruning


【解决方案1】:

在我看来,您有 2 个问题,我将尝试回答:

  1. 如何将时间约束函数添加到此代码中,以便它仅在所述时间结束时返回最佳移动,而不是在此之前。

所以你想搜索每一步的特定秒数而不是搜索特定的深度?这很容易实现,您所要做的就是使迭代加深到某个较大的深度,然后将当前时间与每 x 个节点的搜索开始时间进行比较。像这样的:

import time

start_time = time.time()
move_time = 5  # 5 seconds per move
for depth in range(100):
    ...
    score, move = negamax()
    
    # Only save move if you haven't aborted the search at current depth due to time out.
    if move:
        best_score, best_move = score, move

def negamax():
    if time.time() - start_time > move_time:
        return None, None


    ....
    return score, move
  1. 另外,如何在每个深度之后重新排序节点,以便在下一个深度进行有效修剪。

我不知道您要对当前的排序做什么。下面是 negamax 框架通常的样子:

def negamax():
    if depth = 0:
        return evaluation()

    valid_moves = gs.get_valid_moves()

    # Here you sort the moves
    sorted_valid_moves = sort(valid_moves)

    for move in sorted_valid_moves():
        gs.make_move()
        score = -negamax(...)
        gs.unmake_move()

您可以根据多个标准对移动进行排序,您可以阅读更多关于如何实施它们的信息here

【讨论】:

    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多