こんにちは、次のコードの例としてチェスを使用して、アルファ ベータ プルーニング アルゴリズムを理解しようとしています。
def minimax(position, depth):
"""Returns a tuple (score, bestmove) for the position at the given depth"""
if depth == 0 or position.is_checkmate() or position.is_draw():
return (position.evaluate(), None)
else:
if position.to_move == "white":
bestscore = -float("inf")
bestmove = None
for move in position.legal_moves():
new_position = position.make_move(move)
score, move = minimax(new_position, depth - 1)
if score > bestscore: # white maximizes her score
bestscore = score
bestmove = move
return (bestscore, bestmove)
else:
bestscore = float("inf")
bestmove = None
for move in position.legal_moves():
new_position = position.make_move(move)
score, move = minimax(new_position, depth - 1)
if score < bestscore: # black minimizes his score
bestscore = score
bestmove = move
return (bestscore, bestmove)
私が入手したブログへのリンクは次のとおりです: LINK (強調表示された構文が好きな場合は、リンクからコードを表示できます)
私が理解していないのは、アルファベータの剪定では、ツリーの上位に移動すると、アルファ変数とベータ変数の値が時々変化する必要があるということです。私の問題を説明する写真を添付しました-手順1)、2)、および3)は理解していますが、4)の手順はわかりません。4) ステップは図のように見えるはずですが、そのステップで値が変化するコードで何が起こっているのかわかりません。私はコードを注意深くたどりましたが、何らかの理由で 4) のステップで a = 5 と b = 5 になってしまいました。