在黑白棋游戏中的Alpha-beta

时间:2014-01-21 21:40:07

标签: android artificial-intelligence minimax reversi

我正在Reversi游戏中实现AI的MiniMax算法,我正在尝试添加Alpha-Beta prunning方法。如果我得到正确的话,AI应该选择与标准MiniMax完全相同的动作,只需缩短时间,这要归功于切断分支。然而,AI以非常愚蠢的方式播放,我试图检查所有分支,但找不到原因。没有alpha-beta的实现工作正常,为记录。这是功能:

public Field alphaBeta (GameBoard gb, int depth, boolean player, int alpha, int beta)
{
    /** maximum depth of search reached, we stop */
    if(depth >= max_depth) return null;

    //player = (depth+1)%2 + 1;

    /** getting a list of moves to chose from */
    ArrayList <Field> moves = findAllPossibleMoves(gb, player); 

    Field best_move = null;

    /** iterating over all possible moves, to find the best one */      
    for (int i=moves.size();--i>=0;)
    {
        /** board to simulate moves */
        GameBoard temp_board = new GameBoard(gb);
        /** getting the current move */
        Field move = moves.get(i);      
        /** simulating the move for the current node */
        game.move(move, temp_board, player);
        //Log.i("board", "Depth:"+depth+" Player:"+player+" Move:"+i+" Rating:"+evaluate(temp_board));
        Log.i("board", ""+moves.get(i).getX()+","+moves.get(i).getY());         
        temp_board.printBoard();
        Field best_deep_move = alphaBeta (gb, depth + 1, !player, alpha, beta);

        if(best_deep_move != null)
        {
            move.setRating(best_deep_move.getRating());
        }       

        /** if the maximum depth is reached, we have a null, so we evaluate */
        else 
        {
            move.setRating(evaluate (temp_board, game.getActive_player()));
        }
  if (depth%2==0) // max
        {
            alpha = Math.max(alpha, move.getRating());
        }
        else
        {
            beta = Math.min(beta, move.getRating()); 
        }   


        /** if we are not the deepest possible, we get the rating from the lower node */

        if(best_move == null) 
        {
            best_move = move;           
        }           
        else
        {   
            Log.i("update", "Current move rating:"+move.getRating());
            Log.i("update", "New move rating:"+best_move.getRating());
            if (depth%2==0)
            {
                Log.i("update", "MAX player");
                /** for us, we look for the maximum */
                if (best_move.getRating() < move.getRating()) 
                {
                    best_move = move;   
                    best_move.alpha = alpha;
                }
                if( beta <= alpha)
                {
                    break;
                }

            }
            else
            {
                Log.i("update", "MIN player");
                /** for the opponent, we look for the minimum */
                if (best_move.getRating() > move.getRating()) 
                {
                    best_move = move;   
                    best_move.beta = beta;
                }
                if( beta <= alpha)
                {
                    break;
                }
            }
            Log.i("update", "Updated move rating"+best_move.getRating());
        }
    }


    return best_move;
}

整个代码非常庞大,所以我不会在这里粘贴它。只要询问一下你认为应该看的一些函数/类,然后立即粘贴它们。我很感激任何提示。

0 个答案:

没有答案