以下是多智能体pacman的minimax算法的代码片段,其中有多个鬼(最小玩家)。
def min_max(self, gamestate, current_depth, min_count):
if current_depth == 1:
return (self.evaluationFunction(gamestate),None)
# if max node
if min_count == 0:
min_count = gamestate.getNumAgents() - 1
legal_actions = gamestate.getLegalActions(0)
max_list = []
for action in legal_actions:
max_list.append((self.min_max(gamestate.generateSuccessor(0, action), current_depth - 1, min_count), action))
return max(max_list, key = itemgetter(0))
# if min nodes...
else:
legal_actions = gamestate.getLegalActions(min_count)
min_list = []
for action in legal_actions:
min_list.append((self.min_max(gamestate.generateSuccessor(min_count, action),current_depth - 1, min_count - 1), action))
print(current_depth,min_count)
return min(min_list, key = itemgetter(0))
pacman_move = self.min_max(gameState, self.depth * (no_of_ghosts + 1) + 1, 0)[1]
但是我收到以下错误: ValueError:min()arg是一个空序列。
感谢任何帮助
答案 0 :(得分:0)
您在最后一步没有任何法律行动 - 这就是min_list
为空的原因。
>>> min([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence
因此处理以下情况:
if not legal_actions:
# pass? win? lose?
在执行min()