如何解决从极大极小到 Alpa-beta 修剪极小极大
我找到了一个 3 x 3 tic tac toe 游戏的 minimax 代码,我想将其更改为 Alpha-beta minimax,因为我打算创建一个 6 x 6 tic tac toe。
这是来自 this github 的原始代码:
def minimax(state,depth,player):
"""
AI function that choice the best move
:param state: current state of the board
:param depth: node index in the tree (0 <= depth <= 9),but never nine in this case (see iaturn() function)
:param player: an human or a computer
:return: a list with [the best row,best col,best score]
"""
if player == COMP:
best = [-1,-1,-infinity]
else:
best = [-1,+infinity]
if depth == 0 or game_over(state):
score = evaluate(state)
return [-1,score]
for cell in empty_cells(state):
x,y = cell[0],cell[1]
state[x][y] = player
score = minimax(state,depth - 1,-player)
state[x][y] = 0
score[0],score[1] = x,y
if player == COMP:
if score[2] > best[2]:
best = score # max value
else:
if score[2] < best[2]:
best = score # min value
return best
这是我添加 Alpha Beta 的尝试:
def minimax(state,alpha,beta,best score]
"""
if depth == 0 or game_over(state):
score = evaluate(state)
return [-1,score]
if player == COMP:
best = [-1,+infinity]
for cell in empty_cells(state):
x,y
if player == COMP:
if score[2] > best[2]:
best = score # max value
alpha = max(alpha,score[2]) #added alpha
if beta <= alpha:
break
else:
if score[2] < best[2]:
best = score # min value
beta= min(beta,score[2]) #added beta
if beta <= alpha:
break
return best
我是算法的新手,这里的代码模式与我在网上找到的许多模式不同。是否可以进行任何更改以将其转变为 Alpha-beta 修剪极小极大值?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。