6 * 6 Tic-Tac-Toe 上的极小极大 Alpha beta 修剪

问题描述

我正在尝试在我的班级中构建 Alpha-beta 修剪算法,但我仍在努力。 我希望函数的返回是 x 的位置、y 的位置和分数的列表。原始代码是极小极大,没有我从 this github 得到的 Alpha-beta 剪枝。

这是没有 Alpha-beta 剪枝代码的原始极小极大值:

def minimax(state,depth,player):
    """
    AI function that choice the best move
    :param state: current state of the board
    :param depth: node index in the tree (0 <= depth <= 9),but never nine in this case (see iaturn() function)
    :param player: an human or a computer
    :return: a list with [the best row,best col,best score]
    """
    if player == COMP:
        best = [-1,-1,-infinity]
    else:
        best = [-1,+infinity]

    if depth == 0 or game_over(state):
        score = evaluate(state)
        return [-1,score]

    for cell in empty_cells(state):
        x,y = cell[0],cell[1]
        state[x][y] = player
        score = minimax(state,depth - 1,-player)
        state[x][y] = 0
        score[0],score[1] = x,y

        if player == COMP:
            if score[2] > best[2]:
                best = score  # max value
        else:
            if score[2] < best[2]:
                best = score  # min value

    return best

这是我修改并设法接近我想要的:

from math import inf as infinity
from random import choice
import platform
    
def minimax(state,alpha,beta,player):
    """ 
    state: current state of the board
    player: integer,1 is COMp while -1 is HUMAN
    return a list of [x position,y position,score]
    """
    

    if depth == 0 or game_over(state):
        score = evaluate(state)  # evaluate is a function to see the state of the current board; 
                                   computer win : +1,human win : -1,draw : 0
        return [-1,score]
    
    if player==COMP: 
       
        best = [-1,-infinity]
  
        # empty_cells(state) find the current available positions on the board
        for cell in empty_cells(state): 
            x,cell[1]
            state[x][y] = player  
            val = minimax(state,-player)              
            best = max(best[2],val[2])
            alpha = max(alpha,val[2])
  
            # Alpha Beta Pruning 
            if beta <= alpha: 
                break 
           
        return best 
       
    else:
        best = [-1,+infinity]
  
        # empty_cells(state) find the current available positions on the board
        for cell in empty_cells(state): 
            x,cell[1]
            state[x][y] = player
           
            val = minimax(state,player)
                             
            best = min(best[2],val[2]) 
            beta = min(beta,val[2]) 
  
            # Alpha Beta Pruning 
            if beta <= alpha: 
                break  
        return best

我尝试在原始代码添加 Alpha-beta 剪枝,但它的模式与我从互联网上找到的许多极小极大值不同,所以我尝试用我熟悉的模式编写新的。对这两个代码中的任何一个实施 Alpha beta 修剪的建议将非常适用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)