Python Forum
Alphabeta pruning error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Alphabeta pruning error (/thread-3167.html)



Alphabeta pruning error - Zeflonex - May-02-2017

the beta yielded is not the expected one, i thinks its a problem in the modulo section , but  cannot pinpoint it exactly

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()



RE: Alphabeta pruning error - Joseph_f2 - May-08-2017

Your code does not appear to be complete, which makes helping you much harder. Please double check your posted source code and make sure it's the latest version you have.