如何解决Minimax递归超时
我正在尝试为pacman游戏编写minimax算法。我在递归上遇到问题-有时我的算法“正常运行”(它不会崩溃,但仍返回错误的值),然后有时会崩溃并给我递归错误,提示已超过最大递归深度。有人可以告诉我我在这里做错了什么吗?谢谢!
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,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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。