Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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 极小极大递归超时_Python_Algorithm_Recursion_Minimax_Minmax - Fatal编程技术网

Python 极小极大递归超时

Python 极小极大递归超时,python,algorithm,recursion,minimax,minmax,Python,Algorithm,Recursion,Minimax,Minmax,我正在尝试为一个吃豆人游戏编写一个极大极小算法。我对递归有问题-有时我的算法“工作”(它没有崩溃,但仍然返回错误的值),然后有时它崩溃并给我一个递归错误,说超过了最大递归深度。有人能解释一下我做错了什么吗?谢谢大家! def minimax(self, gamestate, depth, agent): if depth == self.depth or gamestate.isWin() or gamestate.isLose(): return se

我正在尝试为一个吃豆人游戏编写一个极大极小算法。我对递归有问题-有时我的算法“工作”(它没有崩溃,但仍然返回错误的值),然后有时它崩溃并给我一个递归错误,说超过了最大递归深度。有人能解释一下我做错了什么吗?谢谢大家!

 def minimax(self, gamestate, depth, agent):
        if depth == self.depth or gamestate.isWin() or gamestate.isLose():
            return self.evaluationFunction(gamestate), Directions.STOP
        best_move = None

        if agent == 0: ## Pacman is max
            best_val = float('-inf')
        else: ## ghosties are min
            best_val = float('inf')

        actions = gamestate.getLegalActions(agent)
        for action in actions:
            next_agent = agent + 1  ## quit moving me! ## this goes here to set next_agent to be same as agent each iteration, because it also gets changed below
            successor = gamestate.generateSuccessor(agent, action) ## generate successors gamestate (ie new board)
            if next_agent == gamestate.getNumAgents():
                next_agent = 0  ## Once we reach the last agent, set back to 0
                depth += 1  ## increment depth

            v = self.minimax(successor, depth, next_agent)

            ## here is where we set the max and min values based on the agent
            if agent == 0: ## pacman - max
                if v > best_val:
                    best_val = v
                    best_move = action
            else: ## ghost - min
                if v < best_val:
                    best_val = v
                    best_move = action
        return best_move, best_val
def minimax(自身、游戏状态、深度、代理):
如果depth==self.depth或gamestate.isWin()或gamestate.isLose():
返回self.evaluationFunction(游戏状态),Directions.STOP
最佳移动=无
如果代理==0:##Pacman为最大值
最佳值=浮点('-inf')
否则:##鬼魂是最小的
最佳值=浮动('inf')
actions=gamestate.getLegaLaactions(代理)
行动中的行动:
下一个代理=代理+1##停止移动我!#这是为了在每次迭代中将下一个_代理设置为与代理相同,因为它在下面也会被更改
继任者=游戏状态。生成接受者(代理、动作)##生成继任者游戏状态(即新板)
如果next_agent==gamestate.getNumAgents():
next_agent=0##一旦到达最后一个agent,将其设置回0
深度+=1##增量深度
v=自最小最大值(后继、深度、下一个代理)
##这里是我们根据代理设置最大值和最小值的地方
如果代理==0:##pacman-max
如果v>最佳值:
最佳值=v
最佳动作=行动
其他:##鬼-敏
如果v<最佳值:
最佳值=v
最佳动作=行动
返回最佳移动,最佳值

您是否尝试在其中添加一些
打印
语句以查看它在做什么?如何知道算法返回错误的值?您可以考虑增加Python中的递归限制。您应该使用alpha-beta修剪来减少节点(和分支)的数量,因为搜索空间是指数型的。我知道它不会返回最佳值,因为这个赋值来自Berkeley,并且带有自动加载器,可以根据测试用例运行代码。它向您展示了最理想的移动,以及您返回的移动。如果这些不相等,则不会返回最佳节点。此外,alpha-beta修剪是下一个问题。我们不为这个问题实现它,只是minimax!