Python 从alphabeta框架收集和检索主要变体
我正在尝试用python编写一个国际象棋引擎,我可以找到给定位置的最佳移动,但我正在努力收集该位置的主要变化,以下是我迄今为止尝试的内容:Python 从alphabeta框架收集和检索主要变体,python,artificial-intelligence,chess,minimax,alpha-beta-pruning,Python,Artificial Intelligence,Chess,Minimax,Alpha Beta Pruning,我正在尝试用python编写一个国际象棋引擎,我可以找到给定位置的最佳移动,但我正在努力收集该位置的主要变化,以下是我迄今为止尝试的内容: def alphabeta(board, alpha, beta, depth, pvtable): if depth == 0: return evaluate.eval(board) for move in board.legal_moves: board.push(move) scor
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[depth-1]=str(move)
来追加移动,但最后我发现pvtable
包含随机的不一致移动,比如开始位置的['g1h3','g8h6','h3g5','d8g5']
我知道有人问过类似的问题,但我仍然不知道如何解决这个问题。我认为当搜索再次达到相同深度时(在游戏树的另一个分支中),您的移动会被覆盖 本网站很好地解释了如何检索主要变量: 应用于您的代码示例,应该是这样的(我没有测试它):
def alphabeta(电路板、alpha、beta、深度、pline):
行=[]
如果深度=0:
返回evaluate.eval(董事会)
对于搬入板。法律_移动:
板。推(动)
分数=-alphabeta(板,-beta,-alpha,深度-1,线)
board.pop()
如果得分>=beta:
返回贝塔
如果分数>α:
阿尔法=分数
pline[:]=[str(移动)]+行
返回阿尔法
谢谢,我已经阅读了这一页,很有趣,但是我无法将其翻译成python代码,因为我的C语言背景是0,你能给我举个例子吗?非常感谢,我刚刚将其编辑为if score>alpha:alpha=score pline[:]=[str(move)]+line
,看起来还可以,但仍然不确定这是否正确。我已经根据你的评论更新了我的答案。为什么你仍然认为这种方法是不正确的?如果你还有其他问题,就给我写一封邮件。对不起,我的意思是我需要做更多的测试来检查我得到的是否是一致的和相关的动作。