【发布时间】:2020-03-16 03:31:58
【问题描述】:
我正在尝试创建一个 minimax 类型的 AI,它会经历 4 层动作,并尝试根据某种启发式方法挑选出可能的最佳动作。如果我到达一个非法移动的节点,那么事情就在我的状态机中,然后我返回值 None 而不是我的启发式函数将给出的正常点值。在我的 minimax 函数中处理这个问题时,我有点不确定如何以最好的方式处理它。到目前为止,它看起来像这样,并且想知道这是否有意义。
def ai_min_max(board, ai_mancala, player_mancala, ai_choices, player_choices, target_depth, cur_depth, maxTurn, position):
#base case where we call our heuristic function to tell us what the value of this state is
if cur_depth == target_depth :
#return the heuristic value for this state
return first_heuristic(board, ai_mancala, player_mancala, ai_choices, player_choices, position)
#if we are currently on a level where we are maximizing our function
if maxTurn :
#set the value to negative infinity
max_eval = float("-inf")
#go through the 10 possible choices you can make
for x in range(len(ai_choices)) :
new_position = position + [x]
my_eval = ai_min_max(board, ai_mancala, player_mancala, ai_choices, player_choices, target_depth, cur_depth +1, False, new_position)
#update the current max only if we have a valid movement, if not then do not update
if my_eval is not None:
max_eval = max(max_eval, my_eval)
if max_eval == float("-inf") :
return float("inf")
return max_eval
#if it is the minimizing player's turn
else :
min_eval = float("inf")
for x in range(len(player_choices)) :
new_position = position + [x]
my_eval = ai_min_max(board, ai_mancala, player_mancala, ai_choices, player_choices, target_depth, cur_depth +1, True, new_position)
if my_eval is not None:
min_eval = min(min_eval, my_eval)
#if there were no valid moves
if min_eval == float("inf") :
return float("-inf")
return min_eval
【问题讨论】:
标签: python-3.x artificial-intelligence numpy-ndarray minmax