微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Minimax和Alpha Beta修剪没有获得成功 评估功能 Minimax

如何解决Minimax和Alpha Beta修剪没有获得成功 评估功能 Minimax

我在python中实现了带有alpha beta修剪的minimax算法,但是它返回了非常愚蠢的动作。

我似乎找不到该错误,我很确定是问题所在是alpa_beta函数。它不会返回非法动作或类似动作,但会返回非常差的动作。

在我检查过的所有实现中,我都没办法修复它,我们的代码几乎相同,但是我的功能无法正常工作。

评估功能

def evaluate_medium(Boxes,linesX,linesY,is_maximizing,move):
    # Check if there's a winner
    if winner(Boxes) is True:
        score = 5000
    elif winner(Boxes) is False:
        score = -5000
    else:
        # I pass the played move
        # If the move can complete a Box then the score is +25 for max and -25 for min player
        if is_maximizing and Box_completed(linesX,move):
            score = +25
        elif not is_maximizing and Box_completed(linesX,move):
            score = -25
        else:
            if is_maximizing and safe_to_play(linesX,move):
                score = +10
            elif not is_maximizing and safe_to_play(linesX,move):
                score = -10
            else:
                score = 0

    return [move[0],move[1],score,move[2]]

Minimax

def alpha_beta(Boxes,m,n,depth,move,alpha,beta):
    # I get all the possible moves
    moves = possible_moves(linesX,linesY)

    if depth == 0 or len(moves) == 0:
        return evaluate_medium(Boxes,move)

    if is_maximizing:
        # [I coordinate,J coordinate,if it's a Horizontal or Vertical line]
        max_eval = [-1,-1,-infinity,None]
        for move in moves:
            i,j,is_x = move[0],move[2]

            # Make a copy of the list
            currentX = list.copy(linesX)
            currentY = list.copy(linesY)

            # Make the move
            if is_x:
                currentX[i][j] = True
            else:
                currentY[i][j] = True
            
            # Call the algorithm recursively,decrease the depth and call False on is_maximizing
            evaluation = alpha_beta(Boxes,currentX,currentY,depth - 1,False,beta)

            if is_x:
                currentX[i][j] = None
            else:
                currentY[i][j] = None

            # If it's a better move than the best then it's the new best move
            if evaluation[2] > max_eval[2]:
                max_eval = evaluation

            # Pruning check
            alpha = max(alpha,max_eval[2])
            if beta <= alpha:
                break

        return max_eval
    else:
        min_eval = [-1,infinity,move[2]

            currentX = list.copy(linesX)
            currentY = list.copy(linesY)

            if is_x:
                currentX[i][j] = False
            else:
                currentY[i][j] = False

            evaluation = alpha_beta(Boxes,True,beta)

            if is_x:
                currentX[i][j] = None
            else:
                currentY[i][j] = None

            if evaluation[2] < min_eval[2]:
                min_eval = evaluation
            beta = min(beta,min_eval[2])
            if beta <= alpha:
                break

        return min_eval

对此我一直坚持很长时间,非常需要帮助。谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。