Python Alpha-beta修剪不';t输出最佳可能的移动
这是一个tic-tac-toe游戏的alpha-beta修剪算法的实现,问题是它没有给我最好的结果Python Alpha-beta修剪不';t输出最佳可能的移动,python,alpha-beta-pruning,Python,Alpha Beta Pruning,这是一个tic-tac-toe游戏的alpha-beta修剪算法的实现,问题是它没有给我最好的结果 def alpha_beta_policy(board, p): def recurse(b, alpha, beta, player): if is_game_over(b) != False: return (utility(b), 'none') if player == 1: value =
def alpha_beta_policy(board, p):
def recurse(b, alpha, beta, player):
if is_game_over(b) != False:
return (utility(b), 'none')
if player == 1:
value = (float('inf') * -1, (-100, -100))
for i in range(len(legal_moves(b))):
recursion = (recurse(make_move(b, player, legal_moves(b)[i], False), alpha, beta, player*-1)[0], legal_moves(b)[i])
value = max(value, recursion)
alpha = max(alpha, value[0])
if alpha >= beta:
break # cut-off
return value
elif player == -1:
value = (float('inf'), (100, 100))
for i in range(len(legal_moves(b))):
recursion = (recurse(make_move(b, player, legal_moves(b)[i], False), alpha, beta, player*-1)[0], legal_moves(b)[i])
value = min(value, recursion)
beta = min(beta, value[0])
if beta <= alpha:
break # cut-off
return value
else:
raise Error('ERROR: Invalid player selection.')
value, action = recurse(board, float('inf')*-1, float('inf'), p)
print(f'alpha-beta says action = {action} and value = {value}')
return action
def alpha_beta_策略(董事会,p):
def递归(b、alpha、beta、player):
如果游戏结束了(b)!=错误:
返回(实用程序(b),“无”)
如果player==1:
值=(浮点('inf')*-1,(-100,-100))
对于范围内的i(len(legal_moves(b)):
递归=(递归(使移动(b,玩家,合法移动(b)[i],False),alpha,beta,玩家*-1)[0],合法移动(b)[i])
值=最大值(值,递归)
alpha=最大值(alpha,值[0])
如果α>=β:
断线
返回值
elif播放器==-1:
值=(浮点('inf'),(100100))
对于范围内的i(len(legal_moves(b)):
递归=(递归(使移动(b,玩家,合法移动(b)[i],False),alpha,beta,玩家*-1)[0],合法移动(b)[i])
值=最小值(值,递归)
β=最小值(β,值[0])
如果β