I’ve recently tried to understand how minimax can be optimised. I found a great code but I was asking myself: is this code working for a tree with only three different steps ?
I’m not sure I’ve understood precisely how alpha and beta are being updated.
Max
/ |
Min Min Min
/| | |
9 2 4 1 4 1 2
def max_value(board, alpha, beta):
"""
Returns the maximum value for the current player on the board
using alpha-beta pruning.
"""
if terminal(board):
return utility(board)
v = -math.inf
for action in actions(board):
v = max(v, min_value(result(board, action), alpha, beta))
alpha = max(alpha, v)
if alpha >= beta:
break
return v
def min_value(board, alpha, beta):
"""
Returns the minimum value for the current player on the board
using alpha-beta pruning.
"""
if terminal(board):
return utility(board)
v = math.inf
for action in actions(board):
v = min(v, max_value(result(board, action), alpha, beta))
beta = min(beta, v)
if alpha >= beta:
break
return v
def minimax(board):
"""
Returns the optimal action for the current player on the board
using the minimax algorithm with alpha-beta pruning.
"""
if terminal(board):
return None
if player(board) == X:
v = -math.inf
opt_action = None
for action in actions(board):
new_value = min_value(result(board, action), -math.inf, math.inf)
if new_value > v:
v = new_value
opt_action = action
return opt_action
elif player(board) == O:
v = math.inf
opt_action = None
for action in actions(board):
new_value = max_value(result(board, action), -math.inf, math.inf)
if new_value < v:
v = new_value
opt_action = action
return opt_action