1

I was looking at the post Strange behaviour in a function while implementing the alpha-beta pruning algorithm and the accepted answer, where it is stated: "Your rootAlphaBeta doesn't update the alpha value". I was wondering what the necessary addition to the code was.

nbro
  • 15,395
  • 32
  • 113
  • 196
Frank Epps
  • 569
  • 1
  • 7
  • 21

1 Answers1

6

For alpha-beta pruning to work, the alpha value needs to get propagated up to the top level of the depth first search. This can be achieved by initializing a variable to store alpha outside of the loop over the potential moves, storing the result of the call to alphaBeta() in it, and then using it as an argument to alphaBeta(). In code that will look like:

def rootAlphaBeta(self, board, rules, ply, player):
    """ Makes a call to the alphaBeta function. Returns the optimal move for a player at given ply. """
    best_move = None
    max_eval = float('-infinity')

    move_list = board.generateMoves(rules, player)
    alpha = float('infinity')
    for move in move_list:
        board.makeMove(move, player)
        alpha = -self.alphaBeta(board, rules, float('-infinity'), alpha, ply - 1, board.getOtherPlayer(player))
        board.unmakeMove(move, player)

        if alpha > max_eval:
            max_eval = alpha
            best_move = move

    return best_move
lennon310
  • 12,503
  • 11
  • 43
  • 61
seaotternerd
  • 6,298
  • 2
  • 47
  • 58
  • Isn't there some typo in your code? It looks like you are updating the beta value and not the alpha. – Björn Lindqvist Sep 28 '18 at 15:20
  • Do you mean that I'm updating beta but not alpha? Because I'm updating alpha on the second line of the for loop. I believe all of the updating of beta happens in the alphaBeta() function in the original question (here I'm just writing the rootAlphaBeta() function, since that's where the problem was). Disclaimer: I wrote this 5 years ago and haven't used alpha-beta pruning much since, so it's entirely possible I made a mistake. – seaotternerd Sep 28 '18 at 15:44