Rework alphabeta function

This commit is contained in:
Yorick Barbanneau 2023-12-20 01:14:53 +01:00
parent aa9d13d587
commit 46e83a63ce

View file

@ -64,8 +64,6 @@ class MinmaxPlayerEngine(PlayerEngine):
def get_move(self, board): def get_move(self, board):
super().get_move(board) super().get_move(board)
def get_best_move(board):
value = -math.inf value = -math.inf
nodes = 1 nodes = 1
leafs = 0 leafs = 0
@ -124,30 +122,32 @@ class AlphabetaPlayerEngine(PlayerEngine):
def get_move(self, board): def get_move(self, board):
super().get_move(board) super().get_move(board)
self.logger.debug("Enter AlphaBeta function")
alpha = -math.inf alpha = -math.inf
beta = math.inf beta = math.inf
nodes = 1 nodes = 1
leafs = 0 leafs = 0
move = [] move = []
value = -math.inf all_moves = board.legal_moves()
for m in board.legal_moves(): # random.shuffle(all_moves)
for m in all_moves:
board.push(m) board.push(m)
v, n, l = self.checkAlphaBeta(board, False, self.options['depth'] - 1, alpha, beta) value, n, l = self.checkAlphaBeta(board, False, self.options['depth'] - 1, alpha, beta)
board.pop() board.pop()
alpha = max(alpha,v)
nodes += n nodes += n
leafs += l leafs += l
if alpha >= value: if value >= alpha:
value = alpha alpha = value
move = m move = m
self.logger.debug("found a better move: {} (heuristic:{})".format( self.logger.debug("\t-> found a better move: {} | heuristic:{})".format(
move, move,
alpha alpha
)) ))
self.logger.debug("Tree statistics:\n\tnodes:{}\n\tleafs:{}".format( self.logger.info("Tree statistics:\n\tnodes:{}\n\tleafs:{}\n\theuristic:{}".format(
nodes, nodes,
leafs leafs,
alpha
)) ))
return move return move
@ -160,36 +160,38 @@ class AlphabetaPlayerEngine(PlayerEngine):
return self.heuristic.get(board, self.player), nodes, leafs return self.heuristic.get(board, self.player), nodes, leafs
if friend_move: if friend_move:
value = -math.inf all_moves = board.legal_moves()
for m in board.legal_moves(): random.shuffle(all_moves)
for m in all_moves:
board.push(m) board.push(m)
v, n, l = self.checkAlphaBeta(board, False, depth - 1, alpha, beta) v, n, l = self.checkAlphaBeta(board, False, depth - 1, alpha, beta)
board.pop() board.pop()
alpha = max(value,v) alpha = max(alpha,v)
nodes += n nodes += n
leafs += l leafs += l
if alpha >= beta: if alpha >= beta:
# self.logger.debug("Alpha pruning - alpha:{} / beta:{}".format( self.logger.debug("\t - Beta pruning - alpha:{} / beta:{}".format(
# alpha, alpha,
# beta beta
# )) ))
return beta, nodes, leafs return beta, nodes, leafs
return alpha, nodes, leafs return alpha, nodes, leafs
else: else:
value = math.inf all_moves = board.legal_moves()
for m in board.legal_moves(): random.shuffle(all_moves)
for m in all_moves:
board.push(m) board.push(m)
v, n, l = self.checkAlphaBeta(board, True, depth - 1, alpha, beta) v, n, l = self.checkAlphaBeta(board, True, depth - 1, alpha, beta)
board.pop(); board.pop();
beta = min(beta,v) beta = min(beta, v)
nodes += n nodes += n
leafs += l leafs += l
if alpha >= beta: if alpha >= beta:
# self.logger.debug("Beta pruning - alpha:{} / beta:{}".format( self.logger.debug("\t - Alpha pruning | alpha:{} | beta:{}".format(
# alpha, alpha,
# beta beta
# )) ))
return alpha, nodes, leafs return alpha, nodes, leafs
return beta, nodes, leafs return beta, nodes, leafs