【问题标题】:Alpha-beta pruningα-β修剪
【发布时间】:2014-01-16 05:20:26
【问题描述】:

我为我的 Android 黑白棋游戏实现了以下 MiniMax 算法:

@Override
public Field findBestMove(GameBoard gb, int depth, boolean player) 
{   
/** 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=0; i<moves.size(); i++)
{
    /** 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();
    /** getting to the next inferior node */            
    Field best_deep_move = findBestMove (temp_board, depth + 1, !player);           

    /** if the maximum depth is reached, we have a null, so we evaluate */
    if (best_deep_move == null) 
    {
        move.setRating(evaluate (temp_board));
    }
    /** if we are not the deepest possible, we get the rating from the lower node */
    else 
    {
        move.setRating(best_deep_move.getRating());         
        Log.i("eval", ""+best_deep_move.getRating());
    }           
    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;

                }

        }
        else
        {
            Log.i("update", "MIN player");
            /** for the opponent, we look for the minimum */
            if (best_move.getRating() > move.getRating())
            { 


                best_move = move;

            }
        }
        Log.i("update", "Updated move rating"+best_move.getRating());
    }
}

return best_move;

}

我已经熟悉了理论上的 Alpha-Beta 修剪,尽管我在将这些知识应用到这个算法中时遇到了一些麻烦。提前致谢

【问题讨论】:

  • 太好了。你有问题要问吗?

标签: java android algorithm minimax reversi


【解决方案1】:

需要对您的代码进行以下更改以实现 alpha-beta 修剪:-

  1. 传一个参数public Field findBestMove(GameBoard gb, int depth, boolean player,int aplha_beta)

  2. 如果当前 best_move 永远不会影响先前深度的 alpha_beta,则停止递归。

    if(player == max && best_move!=null && aplha_beta <= best_move.getRating()) {
    
               return(best_move);
     }
    
     if(player == min && best_move!=null && alpha_beta >= best_move.getRating()) {
    
            return(best_move);
     }
    
     Field best_deep_move = findBestMove(temp_board,depth+1,!player,best_move.getRating());
    

【讨论】:

  • 非常感谢,我一定会试试这个。但是,只是一个小问题,alpha_beta 到底是什么?在我尝试的大多数实现中,都有单独的 alpha 和 beta 参数,这是在给定时间代表两个变量吗?
  • @MichałSzydłowski 我只使用了一个变量,因为我们需要上一关的 alpha 或 beta 取决于玩家,而不是两者,当前的 alpha 或 beta 是你的 best_move.getRating()。所以我已经使用它进行了必要的比较。
  • 另外,我应该如何初始化那个参数,它应该是正无穷还是负无穷?
  • @MichałSzydłowski 如果最大玩家开始,则 +infinity 否则最小玩家 -infinity
  • @MichałSzydłowski 如果未初始化,则为 max player pass -infinity 和 min player pass +infinity
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 2020-01-14
  • 1970-01-01
相关资源
最近更新 更多