I am writing a basic chess AI using a minimax algorithm. I implemented alpha-beta pruning which seemed to work fine. Here's the code:
def move(self, board):
moves = {}
for move in board.legal_moves:
board.push(move)
moves[move] = self.evaluate_move(board, 1, float("-inf"), float("inf"))
board.pop()
best_moves = []
for key in moves.keys():
if moves[key] == max(moves.values()):
best_moves.append(key)
chosen_move = random.choice(best_moves)
return chosen_move
def evaluate_move(self, board, depth, alpha, beta):
if depth % 2: # if depth is odd ie. minimizing player
extremepoints = float("inf")
else:
extremepoints = float("-inf")
if depth < self.depth_limit and (not board.is_game_over()):
for move in board.legal_moves:
board.push(move)
if depth % 2: # if depth is odd ie. minimizing player
points = self.evaluate_move(board, depth+1, alpha, beta)
extremepoints = min(extremepoints, points)
beta = min(beta, points)
if alpha >= beta:
board.pop()
break
else:
points = self.evaluate_move(board, depth+1, alpha, beta)
extremepoints = max(extremepoints, points)
alpha = max(alpha, points)
if beta <= alpha:
board.pop()
break
board.pop()
else:
return self.evaluate_position(board)
return extremepoints
However, while watching this video I realized, that I might be losing out on potential performance. At that point in the video, alpha gets set at the very top of the tree and it's being given to all other first-level moves. My implementation doesn't do this and instead gives each first-level move the value -inf for alpha. I tried to fix this by doing:
def move(self, board):
alpha = float("-inf")
beta = float("inf")
moves = {}
for move in board.legal_moves:
board.push(move)
moves[move] = self.evaluate_move(board, 1, alpha, beta) # Change here
alpha = max(alpha, moves[move])
board.pop()
best_moves = []
for key in moves.keys():
if moves[key] == max(moves.values()):
best_moves.append(key)
chosen_move = random.choice(best_moves)
return chosen_move
The problem is, this resulted in a worse AI. It's way faster, but loses every time to an AI without this "fix". However while browsing Stack Overflow I found the link to this implementation, that seems to be doing it the same way I am.
So, my question is: Am I already doing alpha-beta pruning to the furthest extent possible and no changes are needed, or, is there something wrong with the way I'm implementing the fix?