问题描述
我在python中实现了带有alpha beta修剪的minimax算法,但是它返回了非常愚蠢的动作。
我似乎找不到该错误,我很确定是问题所在是alpa_beta函数。它不会返回非法动作或类似动作,但会返回非常差的动作。
在我检查过的所有实现中,我都没办法修复它,我们的代码几乎相同,但是我的功能无法正常工作。
评估功能
def evaluate_medium(Boxes,linesX,linesY,is_maximizing,move):
# Check if there's a winner
if winner(Boxes) is True:
score = 5000
elif winner(Boxes) is False:
score = -5000
else:
# I pass the played move
# If the move can complete a Box then the score is +25 for max and -25 for min player
if is_maximizing and Box_completed(linesX,move):
score = +25
elif not is_maximizing and Box_completed(linesX,move):
score = -25
else:
if is_maximizing and safe_to_play(linesX,move):
score = +10
elif not is_maximizing and safe_to_play(linesX,move):
score = -10
else:
score = 0
return [move[0],move[1],score,move[2]]
Minimax
def alpha_beta(Boxes,m,n,depth,move,alpha,beta):
# I get all the possible moves
moves = possible_moves(linesX,linesY)
if depth == 0 or len(moves) == 0:
return evaluate_medium(Boxes,move)
if is_maximizing:
# [I coordinate,J coordinate,if it's a Horizontal or Vertical line]
max_eval = [-1,-1,-infinity,None]
for move in moves:
i,j,is_x = move[0],move[2]
# Make a copy of the list
currentX = list.copy(linesX)
currentY = list.copy(linesY)
# Make the move
if is_x:
currentX[i][j] = True
else:
currentY[i][j] = True
# Call the algorithm recursively,decrease the depth and call False on is_maximizing
evaluation = alpha_beta(Boxes,currentX,currentY,depth - 1,False,beta)
if is_x:
currentX[i][j] = None
else:
currentY[i][j] = None
# If it's a better move than the best then it's the new best move
if evaluation[2] > max_eval[2]:
max_eval = evaluation
# Pruning check
alpha = max(alpha,max_eval[2])
if beta <= alpha:
break
return max_eval
else:
min_eval = [-1,infinity,move[2]
currentX = list.copy(linesX)
currentY = list.copy(linesY)
if is_x:
currentX[i][j] = False
else:
currentY[i][j] = False
evaluation = alpha_beta(Boxes,True,beta)
if is_x:
currentX[i][j] = None
else:
currentY[i][j] = None
if evaluation[2] < min_eval[2]:
min_eval = evaluation
beta = min(beta,min_eval[2])
if beta <= alpha:
break
return min_eval
对此我一直坚持很长时间,非常需要帮助。谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)