0

Tree

Hello, I'm trying to understand the alpha beta pruning algorithm using chess as an example from the following code:

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)

Here's the link to the blog I got it from: LINK (you can view the code from the link if you like highlighted syntax)

What I don't understand is that in alpha beta pruning the value of alpha and beta variable must change sometimes when you go higher up in a tree. I attached a picture explaining my problem - while I understand the steps 1), 2) and 3), I don't get the 4) step. I know that the 4) step should look like on the picture but I don't know what's going on in the code at that step that the values change. I followed the code carefully but for some reason I ended up with a = 5 and b = 5 in the 4) step which is ridiculous because that would mean that the branch on the right would get removed which is obviously wrong.

Vincent
  • 273
  • 1
  • 2
  • 9
  • Where did you get the picture from? It looks to me incorrect. – ABCD Jan 05 '15 at 03:23
  • I'm not so sure step 4 is correct. The min node wants something smaller than 5 (upper bound), not bigger. – ABCD Jan 05 '15 at 03:25
  • I made the picture myself. :) The numbers passed by are correct since I took them from a picture from wikipedia's article about the algorithm but the values of alpha-beta are added by me so there may be a mistake. But I don't see any mistake since above step 4 there is a max node (a square) and the choice is made between 5 (on the left) and 4 (on the right). And a MAX node above them wants a bigger value so I think alpha should be set to 5 which is a lower bound. – Vincent Jan 05 '15 at 08:56
  • I used this side http://cs.ucla.edu/~rosen/161/notes/alphabeta.html for learning, so you may want to take a look at it. You can find there a similar example of a tree with alpha and beta values marked. You can start reading the article from the following phrase: "Since it is a max node, we now know that it's value will be greater than or equal to 3, so we change alpha to 3" – Vincent Jan 05 '15 at 08:59

1 Answers1

0

I think your reasoning in your comments is not correct. From your comments, your implicitly believe the search goes to the right branch of tree then back to the left branch of the tree, which is of course incorrect.

Your logic is wrong because at the (5) non-leaf node on the left branch of the tree, the search has only visited the nodes underneath (the leaf nodes (5) and (4). It has not visited the nodes on the right branch of the tree and therefore has no idea what the value will be. Therefore your comment

"there is a max node (a square) and the choice is made between 5 (on the left) and 4 (on the right). And a MAX node above them wants a bigger value so I think alpha should be set to 5 which is a lower bound." is not correct.

It's wrong because only the root node (the max node) knows the value 4 on the right, but it can only be done AFTER step 4. In fact, it can only be done at the end of the search, after all the nodes in the right branch of the tree are visited.

ABCD
  • 7,914
  • 9
  • 54
  • 90