python中的最小最大算法
在 minmax 算法中,如何确定函数何时到达树的末尾并中断递归调用。
我创建了一个 max 函数,在其中调用 min 函数。在 min 函数中,我应该做什么?对于 max 函数,我只是返回最好成绩。
def maxAgent(gameState, depth):
if (gameState.isWin()):
return gameState.getScore()
actions = gameState.getLegalActions(0);
bestScore = -99999
bestAction = Directions.STOP
for action in actions:
if (action != Directions.STOP):
score = minAgent(gameState.generateSuccessor(0, action), depth, 1)
if (score > bestScore):
bestScore = score
bestAction = action
return bestScore
def minvalue(gameState,depth,agentIndex):
if (gameState.isLose()):
return gameState.getScore()
else:
finalstage = False
number = gameState.getNumAgents()
if (agentIndex == number-1):
finalstage = True
bestScore = 9999
for action in gameState.getLegalActions(agentIndex):
if(action != Directions.STOP
我不明白现在如何继续?我不允许设置树的深度限制。它必须是任意的。
In the minmax algorithm,How to determine when your function reaches the end of the tree and break the recursive calls.
I have made a max function in which I am calling the min function. In the min function , what shud I do?? For max function, I am just returning the bestscore.
def maxAgent(gameState, depth):
if (gameState.isWin()):
return gameState.getScore()
actions = gameState.getLegalActions(0);
bestScore = -99999
bestAction = Directions.STOP
for action in actions:
if (action != Directions.STOP):
score = minAgent(gameState.generateSuccessor(0, action), depth, 1)
if (score > bestScore):
bestScore = score
bestAction = action
return bestScore
def minvalue(gameState,depth,agentIndex):
if (gameState.isLose()):
return gameState.getScore()
else:
finalstage = False
number = gameState.getNumAgents()
if (agentIndex == number-1):
finalstage = True
bestScore = 9999
for action in gameState.getLegalActions(agentIndex):
if(action != Directions.STOP
I could not understand how to proceed now?I not allowed to set the limit of depth of tree. it has to be arbitrary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常你想要搜索直到某个递归深度(例如,下棋时提前 n 步)。因此,您应该将当前递归深度作为参数传递。当您的结果没有改善时,如果您可以毫不费力地确定这一点,您可以提前终止。
Usually you want to search until a certain recursion depth (e.g. n moves in advance, when playing chess). Therefore, you should pass the current recursion depth as a parameter. You may abort earlier when your results do not improve, if you can determine that with little effort.
基本上,您是在询问何时到达叶节点。
当您达到搜索的最大深度时,就会出现叶节点,或者出现终端节点(即结束游戏的位置)。
Basically, you're asking when you've reached a leaf node.
A leaf node occurs when you've reached the maximum depth for the search, or a terminal node (i.e. a position that ends the game).
我完全同意转租。但您可能还想考虑这一点:
有时您可能还认为您正在探索的当前分支值得更深入地探索。这将需要应用一些进一步的启发式方法。我不知道您的问题的解决方案需要多先进。这是因为我不知道问题出在哪里。
按照 Amber 的建议,尝试发布一些代码,以便我们知道您希望解决方案的复杂程度。此外,如果您解释了您知道/可以做多少,那么我们也许能够建议更有用的选项 - 您可能能够实际实现的事情,而不仅仅是您可能不知道如何或没有时间实现的惊人想法实施
I completely agree with relet. But you might want to consider this also:
There are times when you might also think that the current branch that you are exploring is worth exploring a little deeper. This will call for some further heuristics to be applied. I don't know how advanced the solution for your problem is required to be. This is because I don't know where the problem is coming from.
As suggested by Amber, try posting some code so we know how complex you want the solution to be. Further, if you explained how much you know/can_do, then we might be able to suggest more useful options - things that you might be able to realistically implement, rather than just amazing ideas that you may not know how to or have the time to implement
这个网站有 minimax 的帖子,尽管它是基于 IronPython 的:
http://blogs.microsoft.co.il/blogs/dhelper/archive/2009/07/13/getting-started-with-ironpython-part-4-minimax-algorithm.aspx
nofollow 说:
您可以通过使用 Negamax http://en.wikipedia.org/wiki/Negamax< /a>
This site has post of minimax, even though it is based on IronPython:
http://blogs.microsoft.co.il/blogs/dhelper/archive/2009/07/13/getting-started-with-ironpython-part-4-minimax-algorithm.aspx
It says:
You can avoid having two different functions by using Negamax http://en.wikipedia.org/wiki/Negamax