首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Othello Alpha-Beta修剪不好的python

Othello Alpha-Beta修剪不好的python
EN

Stack Overflow用户
提问于 2012-01-12 04:36:12
回答 2查看 3.5K关注 0票数 3

我目前正在尝试为奥赛罗制作一个好的人工智能,并且已经使用了Minimax算法。然而,当我尝试使用alpha-beta剪枝进行更深层次的搜索时,算法似乎表现得很糟糕。我与维基和Berkely.edu等其他来源进行了检查,我认为我已经正确地实现了它,但我仍然找不到问题所在。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-29 04:25:22

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

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

代码语言:javascript
复制
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;
}

调用如下所示:

代码语言:javascript
复制
 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()会返回一个“虚拟移动”,它根本不会改变棋盘,只需传递该轮。

希望它能帮上忙!

票数 2
EN

Stack Overflow用户

发布于 2012-01-16 16:05:42

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

但最有可能的是,糟糕的游戏是由"heur“评估函数中的某些东西造成的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8826230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档