3

我目前正在尝试为奥赛罗制作一个好的 AI,并使用 Minimax 算法做到了这一点。然而,当我尝试使用 alpha-beta 剪枝进行更深入的搜索时,该算法似乎运行得很糟糕。我用 Wiki 和 Berkely.edu 等其他来源检查了它,我认为我已经正确实现了它,但我仍然找不到问题。

def alphabeta(board, player, a, b, lev):
        h = heur(board, player)
        if lev == 0:
                return h, None
        poss = get_legal_moves(board, player)
        if len(poss) == 0:
                return h, None
        move = 0
        for x in poss:
                cpboard = board[:]
                cpboard[x] = player
                bracket(cpboard, player, x)
                a1, q = alphabeta(cpboard, opponent_color(player), a, b, lev-1)
                if player is me:
                        if a1 > a:
                                a, move = a1, x
                else:
                        if a1 < b:
                                b, move = a1, x
                if b <= a:
                        break
        if player is me:
                return a, move
        else:
                return b, move
4

2 回答 2

2

您的 alpha-beta 代码可能是错误的。请注意当玩家“通过转弯”(即没有可用的动作)时会发生什么,因此,我的代码中有一个棘手的错误。

您是否调用了切换 alpha 和 beta 值的递归?我的工作方式是这样的(Java代码):

private float minimax(OthelloBoard board, OthelloMove best, float alpha, float beta, int depth)
{
    float bestResult = -Float.MAX_VALUE;
    OthelloMove garbage = new OthelloMove();

    int state = board.getState();
    int currentPlayer = board.getCurrentPlayer();

    if (state == OthelloBoard.STATE_DRAW)
        return 0.0f;
    if ((state == OthelloBoard.STATE_BLACK_WINS) && (currentPlayer == OthelloBoard.BLACK))                    
        return INFINITY;        
    if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.WHITE))
        return INFINITY;
    if ((state == OthelloBoard.STATE_BLACK_WINS) && (currentPlayer == OthelloBoard.WHITE))
        return -INFINITY;
    if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.BLACK))
        return -INFINITY;

    if (depth == maxDepth)
        return OthelloHeuristics.eval(currentPlayer, board);

    ArrayList<OthelloMove> moves = board.getAllMoves(currentPlayer);

    for (OthelloMove mv : moves)
    {            
        board.makeMove(mv);
        alpha = - minimax(board, garbage, -beta, -alpha, depth + 1);
        board.undoMove(mv);

        if (beta <= alpha)
            return alpha;
        if (alpha > bestResult)
        {                
            best.setFlipSquares(mv.getFlipSquares());
            best.setIdx(mv.getIdx());        
            best.setPlayer(mv.getPlayer());
            bestResult = alpha;
        }
    }

     return bestResult;
}

调用是这样的:

 OthelloMove bestFound = new OthelloMove();
 int maxDepth = 8;
 minimax(board, bestFound, -Float.MAX_VALUE, Float.MAX_VALUE, maxDepth);
//Wait for Thread to finish
 board.makeMove(bestFound);

编辑:如果玩家没有可用的移动,getAllMoves() 返回一个“虚拟移动”,这根本不会改变棋盘,只需通过回合。

希望能帮助到你!

于 2012-02-28T20:25:21.770 回答
1

你的alphabeta实现对我来说看起来不错。由于 minimax 和 alphabeta 在正确实施时会产生相同的结果,因此您应该能够使用旧的 minimax 代码作为对 alphabeta 的检查,至少对于适度的搜索深度。如果在搜索相同的游戏树时他们的结果不同,那么你就知道你做错了什么。

但最有可能的是,糟糕的发挥是你的“heur”评估功能的结果。

于 2012-01-16T08:05:41.983 回答