【问题标题】:Alpha Beta pruning problems in Othello奥赛罗中的 Alpha Beta 修剪问题
【发布时间】:2012-02-27 07:45:32
【问题描述】:

我正在创建一个播放黑白棋的简单引擎,使用带有 alpha beta 切割的 minimax。 它玩得很好,但有时我会得到一个奇怪的索引超出范围异常(靠近 终局,总是)。

这是我的算法

private float minimax(OthelloBoard board, OthelloMove best, float alpha, float beta, int depth)
{
    calls++;

    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 Float.MAX_VALUE;
    if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.WHITE))
        return Float.MAX_VALUE;
    if ((state == OthelloBoard.STATE_BLACK_WINS) && (currentPlayer == OthelloBoard.WHITE))
        return -Float.MAX_VALUE;
    if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.BLACK))
        return -Float.MAX_VALUE;

    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;
}

在 makeMove 和 undoMove 中,我更新了游戏状态(黑胜、白胜、平局)。 我还在这些方法中切换播放器。当玩家没有动作时,我会做一个假人 在不改变棋盘的情况下移动,并切换玩家。

还有很多代码,但我认为问题发生在算法达到 游戏结束位置。当我将引擎设置为随机移动时不会发生此问题,因此问题应该是alpha beta算法。

这里是getAllMoves,这个调用getFlips:

   public ArrayList<OthelloMove> getAllMoves(int player)
   {
    ArrayList<OthelloMove> moves = new ArrayList<OthelloMove>();

    for (int i = 10; i < 90; i++) 
    {
        int col = i % 10;

        if (col != 0 && col != 9)            
        {
            if (cells[i] == EMPTY)
            {
                ArrayList<Integer> flips = getFlips(i, player);                    
                if (flips.size() > 0)
                {
                    OthelloMove mv = new OthelloMove();
                    mv.setFlipSquares(flips);
                    mv.setIdx(i);                        
                    mv.setPlayer(player);
                    moves.add(mv);
                }
            }
        }

    }                                     

    return moves;
}

这里是 getFlips。

    public ArrayList<Integer> getFlips(int idx, int player)
    {        
    int opponent = getOpponent(player);
    ArrayList<Integer> flips = new ArrayList<Integer>();

    if (cells[idx] != EMPTY)
        return flips;

    for (Integer dir : DIRECTIONS)
    {
        int distance = 1;
        int tempIdx = idx;

        while (cells[tempIdx += dir] == opponent)
            distance++;

        if ((cells[tempIdx] == player) && (distance > 1)) 
        {

            while (distance-- > 1)
            {                    
                tempIdx -= dir;
                flips.add(tempIdx);
            }                            
        }            
    }
    return flips;
}

这里是更新状态:

    public void updateState()
    {                   
    int opponent = getOpponent(currentPlayer);
    int playerMoves = getAllMoves(currentPlayer).size();
    int opponentMoves = getAllMoves(opponent).size();

    if ( ((playerMoves == 0) && (opponentMoves == 0)) ||  (emptyCells == 0))
    {                        
        int blackDiscs = countDiscs(BLACK);
        int whiteDiscs = countDiscs(WHITE);

        if (blackDiscs > whiteDiscs)
            state = STATE_BLACK_WINS;
        else if (blackDiscs < whiteDiscs)
            state = STATE_WHITE_WINS;
        else 
            state = STATE_DRAW;

    }                       

}

谢谢!

【问题讨论】:

  • 哇,伙计们在这里很快,谢谢。线程“Thread-3”中的异常 java.lang.ArrayIndexOutOfBoundsException: 100 at OthelloBoard.getFlips(OthelloBoard.java:119) at OthelloBoard.getAllMoves(OthelloBoard.java:85) at MinimaxOthello.minimax(MinimaxOthello.java:53) at MinimaxOthello .doMove(MinimaxOthello.java:23) 在 OthelloPanel.doLogic(OthelloPanel.java:118) 在 OthelloPanel.run(OthelloPanel.java:162) 在 java.lang.Thread.run(Thread.java:680)
  • 如您所见,它位于getFlips()。请添加此方法的相关代码。 getAllMoves() 可能也需要。
  • 问题出在while (cells[tempIdx += dir] ==对手)
  • cells 是一个 int[100],OthelloBoard 类的实例变量。我将它映射到一个 8x8 数组,只是为了澄清。谢谢!
  • 这意味着在执行 cells[tempIdx += dir] 之前,您必须检查 tempIdx + dir 是否小于 cells.length

标签: java algorithm search minimax


【解决方案1】:

我对游戏并不特别熟悉,但我认为这与该行中的事实帽子有关:

while (cells[tempIdx += dir] == opponent)

你也应该检查你没有出界,否则 - 如果棋盘末端还有对手,你将继续增加dir

尝试将此行更改为:

while (tempIdx + dir >= 0 && tempIdx + dir < cells.length && cells[tempIdx += dir] == opponent)

根据经验,通常在数组访问中,尤其是在循环中,通过显式检查长度来防止越界是一种很好的做法。

【讨论】:

  • 板外为零。当我关闭引擎时,while 循环可以正常工作,这是一个非常棘手的错误!我尝试你的代码并得到一个 java.lang.ArrayIndexOutOfBoundsException: -6 哦,我在一个单独的线程上运行它 =)
  • 不管外部是否为零,该数组在该检查中仍然有效,并且您超出了它的界限。所以,虽然 (tempIdx + dir >= 0 && tempIdx + dir
  • 我添加了&gt;=0 指示,我确实认为dir 可能是负面的。
  • 它不再崩溃,而是触发了过早的结束游戏。 updateState 方法是我猜的问题。
【解决方案2】:

找到问题了,谢谢。

错误是玩家无法移动,必须通过回合的情况。 棘手的是玩“鬼招”(即不会改变棋盘的招式),并且 切换玩家转向,这样 Minimax 甚至都不会注意到这种情况。

我正在这样做,但是在错误的地方!代码如下:

   public void makeMove (OthelloMove move)
   {                    
    int player = move.getPlayer();        
    ArrayList<Integer> flips = move.getFlipSquares();

    if (flips != null)
    {                                   
        int idx = move.getIdx();                    
        cells[idx] = player;
        for (Integer flip : flips)
            cells[flip] = player;        

        emptyCells--;    
        this.updatePhase();           
    }
    this.toogleCurrentPlayer();                    
}

public void undoMove (OthelloMove move)
{                    
    int player = move.getPlayer();        
    ArrayList<Integer> flips = move.getFlipSquares();
    int opponent = getOpponent(player);

    if (flips != null)
    {
        int idx = move.getIdx();

        cells[idx] = EMPTY;
        for (Integer flip : flips)
            cells[flip] = opponent;

        emptyCells++;                                         
        this.updatePhase();
    }
    this.toogleCurrentPlayer();         
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多