我正在尝试用Python编写一个国际象棋引擎,我可以找到给定位置的最佳移动,但我正在努力收集该位置的主要变化,以下是我迄今为止尝试过的:
def alphabeta(board, alpha, beta, depth, pvtable):
if depth == 0:
return evaluate.eval(board)
for move in board.legal_moves:
board.push(move)
score = -alphabeta(board, -beta, -alpha, depth - 1, pvtable)
board.pop()
if score >= beta:
return beta
if score > alpha:
alpha = score
pvtable[depth-1] = str(move)
return alpha
我正在使用pvtable[深度 - 1] = str(move)
来附加移动,但最终我发现 pvtable
包含随机不一致的移动,例如['g1h3', 'g8h6', 'h3g5', 'd8g5']
为起始位置。
我知道已经有人问过类似的问题,但我仍然不知道如何解决这个问题。
最佳答案
我认为当搜索再次达到相同深度(在游戏树的不同分支中)时,您的 Action 会被覆盖。
这个网站很好地解释了如何检索主要变体:https://web.archive.org/web/20071031100114/http://www.brucemo.com:80/compchess/programming/pv.htm
应用于您的代码示例,它应该是这样的(我没有测试它):
def alphabeta(board, alpha, beta, depth, pline):
line = []
if depth == 0:
return evaluate.eval(board)
for move in board.legal_moves:
board.push(move)
score = -alphabeta(board, -beta, -alpha, depth - 1, line)
board.pop()
if score >= beta:
return beta
if score > alpha:
alpha = score
pline[:] = [str(move)] + line
return alpha
关于python - 从 Alphabeta 框架中收集和检索主要变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346591/