python中的AlphaBeta修剪算法不进行修剪

2024-09-24 22:24:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在评估国际象棋的位置,其实并不重要。我插入了打印检查,以查看可以修剪多少条路径,但没有打印任何内容,这意味着我没有真正修剪任何内容

我已经理解了算法并严格遵循伪代码。有人知道出了什么问题吗

def alphabeta(self,node,depth,white, alpha,beta):
    ch = Chessgen()
    if(depth == 0 or self.is_end(node)):
        return self.stockfish_evaluation(node.board)


    if (white):
        value = Cp(-10000)
        for child in ch.chessgen(node):
            value = max(value, self.alphabeta(child,depth-1,False, alpha,beta))
            alpha = max(alpha, value)
            if (alpha >= beta):
                print("Pruned white")
                break
        return value

    else:
        value = Cp(10000)
        for child in ch.chessgen(node):
            value = min(value, self.alphabeta(child,depth-1,True, alpha,beta))
            beta = min(beta,value)
            if(beta <= alpha):
                print("Pruned black")
                break
        return value

Tags: selfalphanodechild内容forreturnif
1条回答
网友
1楼 · 发布于 2024-09-24 22:24:23

你的伪代码是什么? 我发现的那个代码有点不同:

由于我没有您的完整代码,因此无法运行:

def alphabeta(self,node,depth,white, alpha,beta):
       ch = Chessgen() ### can you do the init somewhere else to speed up the code ?
       if(depth == 0 or self.is_end(node)):
           return self.stockfish_evaluation(node.board)


       if (white):
           value = Cp(-10000)
           for child in ch.chessgen(node):
               value = max(value, self.alphabeta(child,depth-1,False, alpha,beta))
               if (value >= beta):
                   print("Pruned white")
                   return value
               alpha = max(alpha, value)
           return value

       else:
           value = Cp(10000)
           for child in ch.chessgen(node):
               value = min(value, self.alphabeta(child,depth-1,True, alpha,beta))
               if(value <= alpha):
                   print("Pruned black")
                   return value
               beta = min(beta,value)
           return value

一个完整的简单国际象棋程序可以在这里找到: https://andreasstckl.medium.com/writing-a-chess-program-in-one-day-30daff4610ec

相关问题 更多 >