Python Forum
Alphabeta pruning error
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alphabeta pruning error
#1
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()
Reply
#2
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020