将Minimax转换为Negamax(python)

时间:2014-06-10 06:30:32

标签: python algorithm artificial-intelligence minimax negamax

我正在制作一名奥赛罗球员,并使用alpha-beta修剪实现了minimax算法。然后我在网上做了大量关于最好的研究,并一直听到关于" negamax"他们都使用的算法。似乎大多数人认为negamax比minimax更快(我认为因为它不会在最小和最大玩家之间切换?),所以我想把我的minimax算法变成negamax,如果那样的话不太难。

我想知道人们是否对使用negamax的速度有多快了解,以及如何将我的极小极大代码转换为值得欣赏的negamax算法的任何提示或代码!

这是我的极小极大算法:

def minimax(Board, maximizingPlayer, depth, count):
     # maximizing player has 'B' and minimizing 'W'
     if maximizingPlayer: player, opp = Board.player, Board.opp
     else: player, opp = Board.opp, Board.player

     moves_list = Board.get_moves_list(player, opp)
     best_move = (-1,-1)

     # base case
     if ( depth==0 or moves_list == [] ):
         best_score, parity, mobility, stability = Board.evaluate()
         best_move = (-1, -1)
         return best_score, best_move, count

     # maximizing player
     if maximizingPlayer:
           best_score = float("-inf")
           for move in moves_list:
                new_board = deepcopy(Board)
                new_board.play_legal_move(move[0], move[1], player, opp, flip=True)
                the_score, the_move, count = minimax(new_board, False, depth-1, count+1)
                best_score = max(best_score, the_score)
                if (the_score == best_score):
                    best_move = move

           return best_score, best_move, count
     # minimzing player
     else:
           best_score = float("inf")
           for move in moves_list:
                new_board = deepcopy(Board)
                new_board.play_legal_move(move[0], move[1], player, opp, flip=True)
                the_score, the_move, count = minimax(new_board, True, depth-1, count+1)
                best_score = min(best_score, the_score)
                if (the_score == best_score):
                    best_move = move

           return best_score, best_move, count

因为我收到了关于我的Alpha-beta修剪的回复,所以这就是:

def alphabeta(Board, maximizingPlayer, depth, count, alpha, beta):
     # maximizing player has 'B' and minimizing 'W'
     if maximizingPlayer: player, opp = Board.player, Board.opp
     else: player, opp = Board.opp, Board.player

     moves_list = Board.get_moves_list(player, opp)
     best_move = (-1,-1)

     # base case
     if ( depth==0 or moves_list == [] ):
         best_score, parity, mobility, stability = Board.evaluate()
         best_move = (-1, -1)
         return best_score, best_move, count

     # maximizing player
     if maximizingPlayer:
           best_score = float("-inf")
           for move in moves_list:
                new_board = deepcopy(Board)
                new_board.play_legal_move(move[0], move[1], player, opp, flip=True)
                the_score, the_move, count = alphabeta(new_board, False, depth-1, count+1, alpha, beta)
                if (the_score > alpha):
                    alpha = the_score
                    best_move = move
                if beta <= alpha: break

           return alpha, best_move, count
     # minimzing player
     else:
           best_score = float("inf")
           for move in moves_list:
                new_board = deepcopy(Board)
                new_board.play_legal_move(move[0], move[1], player, opp, flip=True)
                the_score, the_move, count = alphabeta(new_board, True, depth-1, count+1, alpha, beta)
                if (the_score < beta):
                    beta = the_score
                    best_move = move
                if beta <= alpha: break

           return beta, best_move, count

1 个答案:

答案 0 :(得分:4)

我认为现在你已经实现了minimax它已经足够好但是你需要在minimax中实现最重要的优化alpha-beta修剪,这将是对代码的一个简单改变,并且速度有了非常显着的提高。

修改: -

注意到你已经使用了alpha-beta,所以你可以实现negamax,但是你没有切换它的想法是不正确的,但它减少了minimax的代码(我怀疑速度的显着提高)。这里的想法是,一个玩家的移动点总是-ve另一个玩家,但是相同的大小允许你计算max(a,b)= -min(-a,-b)。

这里的简单翻译是: -

score = -negamax(depth-1,-player)
best = max(score,best)

这些只是使用negamax评估minimax的行

在这里你不需要另外评估最小值和最大值,但是给予minplayer的分数总是对正面玩家的负面这一事实是足够的,这样你总是可以评估最大值以获得正确的分数。

注意: -

这在速度方面并不是一个重要的优化,但是使代码简单易读,所以它值得,但不幸的是你需要擦除很多代码才能将代码转换为negamax,所以我建议不要这样做。