Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中alpha-beta修剪算法出错_Python_Algorithm_Artificial Intelligence_Pruning - Fatal编程技术网

python中alpha-beta修剪算法出错

python中alpha-beta修剪算法出错,python,algorithm,artificial-intelligence,pruning,Python,Algorithm,Artificial Intelligence,Pruning,在下面的修剪中,返回的alpha是正确的,而beta保持不变,我做错了什么? 它是一个在底部节点具有以下值的树 tree = [[[5, 1, 2], [8, -8, -9]], [[9, 4, 5], [-3, 4, 3]]] root = 0 pruned = 0 def children(branch, depth, alpha, beta): global tree global root global pruned i = 0 for chil

在下面的修剪中,返回的alpha是正确的,而beta保持不变,我做错了什么? 它是一个在底部节点具有以下值的树

tree = [[[5, 1, 2], [8, -8, -9]], [[9, 4, 5], [-3, 4, 3]]]
root = 0
pruned = 0

def children(branch, depth, alpha, beta):
    global tree
    global root
    global pruned
    i = 0
    for child in branch:
        if type(child) is list:
            (nalpha, nbeta) = children(child, depth + 1, alpha, beta)
            if depth % 2 == 1:
                beta = nalpha if nalpha < beta else beta
            else:
                alpha = nbeta if nbeta > alpha else alpha
            branch[i] = alpha if depth % 2 == 0 else beta
            i += 1
        else:
            if depth % 2 == 0 and alpha < child:
                alpha = child
            if depth % 2 == 1 and beta > child:
                beta = child
            if alpha >= beta:
                pruned += 1
                break
    if depth == root:
        tree = alpha if root == 0 else beta
    return (alpha, beta)

def alphabeta(in_tree=tree, start=root, lower=-15, upper=15):
    global tree
    global pruned
    global root

    (alpha, beta) = children(tree, start, lower, upper)

    if __name__ == "__main__":
        print ("(alpha, beta): ", alpha, beta)
        print ("Result: ", tree)
        print ("Times pruned: ", pruned)

    return (alpha, beta, tree, pruned)


if __name__ == "__main__":
    alphabeta()

此代码从一开始就打印错误的字母和beta,因此这是一种更传统的方法。我没有仔细检查,但我知道这是正确的方法。变量p对“位置”进行编码。只有当所有树分支的深度相同时,代码才会准确。在这种情况下,这就是为什么深度变量设置为3。要使它在任何树上运行,还需要做一些工作

tree = [[[0,1,2],[-1,2,5],[-2,2,0]],[[-2,-1,-3],[-4,-3,-1],[1,2,8]],[[4,6,1],[1,7,-1],[-2,-4,1]]]

side = 1
alpha = -1000
beta = 1000
depth = 3

p = []
def getLengthLoL(l, address):
    item = l
    for index in address:
        item = item[index]
    return len(item) if isinstance(item, list) else item

def makeMove(move):
    global side
    if side:
        side = 0
    else:
        side = 1
    p.append(move)

def backMove(move):
    global side
    if side:
        side = 0
    else:
        side = 1
    p.pop()

def evaluation(score):
    if side==0:
        return -1*score
    else:
        return score 

def minmax( alpha, beta, depth ):
    if depth==0:
        return evaluation(getLengthLoL(tree,p))
    moves = getLengthLoL(tree,p)
    for move in range(int(moves)):
        makeMove(move)
        val = -1*minmax(-beta,-alpha,depth-1)
        backMove(move)
        if val >= beta:
            return beta;        
        if val > alpha:
            alpha = val;
    return alpha        

myScore = minmax(alpha,beta,depth)
print myScore

使用您的代码后,虽然答案是正确的,但我无法打印beta版,您将如何进行?我相信betacuts将(高度)取决于您的移动顺序-您访问叶节点的顺序。如果val>=beta:betacuts.append(val),您可以在算法期间拥有一个全局列表betacuts[]并将其追加到。然后在末尾打印betacuts。
tree = [[[0,1,2],[-1,2,5],[-2,2,0]],[[-2,-1,-3],[-4,-3,-1],[1,2,8]],[[4,6,1],[1,7,-1],[-2,-4,1]]]

side = 1
alpha = -1000
beta = 1000
depth = 3

p = []
def getLengthLoL(l, address):
    item = l
    for index in address:
        item = item[index]
    return len(item) if isinstance(item, list) else item

def makeMove(move):
    global side
    if side:
        side = 0
    else:
        side = 1
    p.append(move)

def backMove(move):
    global side
    if side:
        side = 0
    else:
        side = 1
    p.pop()

def evaluation(score):
    if side==0:
        return -1*score
    else:
        return score 

def minmax( alpha, beta, depth ):
    if depth==0:
        return evaluation(getLengthLoL(tree,p))
    moves = getLengthLoL(tree,p)
    for move in range(int(moves)):
        makeMove(move)
        val = -1*minmax(-beta,-alpha,depth-1)
        backMove(move)
        if val >= beta:
            return beta;        
        if val > alpha:
            alpha = val;
    return alpha        

myScore = minmax(alpha,beta,depth)
print myScore