Python中的beta修剪
我试图在Connect Four类型的游戏中实现一台电脑播放器。 alpha-beta修剪似乎是实现这一目标的最佳方法,但我似乎无法弄清楚我做错了什么。
以下是我提出的代码。 它从一个初始的根状态开始。 对于每一个可能的有效移动(如果不发生修剪),该算法:对状态进行深层复制,更新状态(增加深度,切换转动,添加一块,设置启发式值),并将此新状态添加到根的后继列表。
如果新状态不是叶(即在最大深度处),则递归地继续。 如果它是叶子,算法会检查根值和适当的本地alpha / beta值并相应更新。 在检查了所有可能的有效选项后,算法返回适当的本地alpha / beta值。
至少,这是我的意图。 每次运行都返回一个0值。这里要求的是初始化代码:
class GameState:
def __init__(self, parentState = None):
# copy constructor
if not(parentState == None):
self.matrix = copy.deepcopy(parentState.matrix)
self.successor = copy.deepcopy(parentState.successor)
self.depth = parentState.depth
self.turn = parentState.turn
self.alpha = parentState.alpha
self.beta = parentState.beta
self.connects = copy.deepcopy(parentState.connects)
self.value = parentState.value
self.algo_value = parentState.value
self.solution = parentState.solution
# new instance
else:
# empty board
self.matrix = [[0 for y in xrange(6)] for x in xrange(7)]
## USED WHEN GROWING TREE
self.successor = [] # empty list
self.depth = 0 # start at root
self.turn = 1 # game starts on user's turn
## USED WHEN SEARCHING FOR SOLUTION
self.alpha = float("-inf")
self.beta = float("+inf")
self.connects = [0, 0, 0] # connects in state
self.algo_value = float("-inf")
self.value = 0 # alpha-beta value of connects
self.solution = False # connect four
def alphabeta(root):
if root.depth < MAX_EXPANSION_DEPTH:
# pass down alpha/beta
alpha = root.alpha
beta = root.beta
# for each possible move
for x in range(7):
# ALPHA-BETA PRUNING
# if root is MAXIMIZER
if (root.turn == 2) and (root.algo_value > beta): print "beta prune"
# if root is MINIMIZER
elif (root.turn == 1) and (root.algo_value < alpha): print "alpha prune"
# CANNOT prune
else:
# if move legal
if (checkMove(root, x)):
# CREATE NEW STATE
root.successor.append(GameState(root))
working_state = root.successor[-1]
# update state
working_state.successor = []
working_state.depth += 1
working_state.turn = (working_state.turn % 2) + 1
cons = dropPiece(working_state, x, working_state.turn)
# update state values
# MAXIMIZER
if working_state.turn == 2:
working_state.value = ((cons[0]*TWO_VAL)+(cons[1]*THREE_VAL)+(cons[2]*FOUR_VAL)) + root.value
working_state.algo_value = float("-inf")
# MINIMIZER
else:
working_state.value = ((-1)*((cons[0]*TWO_VAL)+(cons[1]*THREE_VAL)+(cons[2]*FOUR_VAL))) + root.value
working_state.algo_value = float("inf")
# if NOT a leaf node
if (working_state.depth < MAX_EXPANSION_DEPTH):
# update alpha/beta values
working_state.alpha = alpha
working_state.beta = beta
ret = alphabeta(working_state)
# if MAXIMIZER
if (root.turn == 2):
if (ret > root.algo_value): root.algo_value = ret
if (ret > alpha): alpha = ret
# if MINIMIZER
else:
if (ret < root.algo_value): root.algo_value = ret
if (ret < beta): beta = ret
# if leaf, return value
else:
if root.turn == 2:
if (working_state.value > root.algo_value): root.algo_value = working_state.value
if working_state.value > alpha: alpha = working_state.value
else:
if (working_state.value < root.algo_value): root.algo_value = working_state.value
if working_state.value < beta: beta = working_state.value
if root.turn == 2: return alpha
else: return beta
解决了这个问题。 在上面的算法中,我检查了循环移到下一个后继者(默认的algo_values是各自的最大值和最小值)后的修剪。
相反,该算法应检查每个后继列表中的第一个节点,更新其algo_value,然后检查是否修剪后继列表中的其余节点。
链接地址: http://www.djcxy.com/p/56309.html