使用带有Alpha-Beta修剪的MinMax找到最佳移动

Ste*_*TNT 6 java algorithm artificial-intelligence minmax alpha-beta-pruning

我正在为游戏开发AI,我想使用MinMax算法和Alpha-Beta修剪.

我对它是如何工作有一个粗略的想法,但我仍然无法从头开始编写代码,所以我花了最近两天在网上寻找某种伪代码.

我的问题是,我在网上发现的每个伪代码似乎都是基于找到最佳移动的价值,而我需要返回最佳移动而不是数字.

我当前的代码基于这个伪代码(源代码)

minimax(level, player, alpha, beta){  // player may be "computer" or "opponent"
    if (gameover || level == 0)
       return score
    children = all valid moves for this "player"
    if (player is computer, i.e., max's turn){
       // Find max and store in alpha
       for each child {
          score = minimax(level - 1, opponent, alpha, beta)
          if (score > alpha) alpha = score
          if (alpha >= beta) break;  // beta cut-off
       }
       return alpha
    } else (player is opponent, i.e., min's turn)
       // Find min and store in beta
       for each child {
          score = minimax(level - 1, computer, alpha, beta)
          if (score < beta) beta = score
          if (alpha >= beta) break;  // alpha cut-off
       }
       return beta
    }
}

// Initial call with alpha=-inf and beta=inf
minimax(2, computer, -inf, +inf)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,此代码返回一个数字,我想这需要使一切正常(因为在递归期间使用返回的数字).

所以我认为我可以使用外部变量来存储最佳移动,这就是我改变之前代码的方式:

minimax(level, player, alpha, beta){  // player may be "computer" or "opponent"
    if (gameover || level == 0)
       return score
    children = all valid moves for this "player"
    if (player is computer, i.e., max's turn){
       // Find max and store in alpha
       for each child {
          score = minimax(level - 1, opponent, alpha, beta)
          if (score > alpha) {
              alpha = score
              bestMove = current child // ROW THAT I ADDED TO UPDATE THE BEST MOVE
          }
          if (alpha >= beta) break;  // beta cut-off
       }
       return alpha
    } else (player is opponent, i.e., min's turn)
       // Find min and store in beta
       for each child {
          score = minimax(level - 1, computer, alpha, beta)
          if (score < beta) beta = score
          if (alpha >= beta) break;  // alpha cut-off
       }
       return beta
    }
}

// Initial call with alpha=-inf and beta=inf
minimax(2, computer, -inf, +inf)
Run Code Online (Sandbox Code Playgroud)

现在,这对我来说是有意义的,因为我们需要更新最佳动作,只有当玩家轮到你并且移动比之前更好时.

所以,虽然我认为这是正确的(即使我不是100%肯定),也有一个java实现更新bestMove甚至在这种score < beta情况下,我不明白为什么.

尝试使用该实现导致我的代码选择最佳移动来自对立的玩家,这似乎是不正确的(假设我是黑人玩家,我正在寻找我能做到的最佳动作我期待一个"黑色"的举动,而不是一个"白色"举动.

我不知道我的伪代码(第二个)是否是使用带有alpha-beta修剪的MinMax找到最佳移动的正确方法,或者我是否需要在分数<beta情况下更新最佳移动.

如果你愿意的话,请随意提出任何新的和更好的伪代码,我没有任何约束,如果它比我的更好,我不介意重写一些代码.

编辑:

由于我无法理解回复,我想也许问题不会问我想知道什么,所以我想在这里写得更好.

只要我想为一个玩家获得最佳动作,并且每次我需要一个新动作时,这个玩家(即最大化者)都会被传递给MinMax函数(这样就可以minmax(2, black, a, b)返回黑色玩家的最佳动作,同时minmax(2, white, a ,b)返回对于白人玩家来说最好的一个),你如何改变第一个伪代码(或源代码中的java实现)来存储这个给定的最佳移动?

编辑2:

让我们看看我们是否可以这样做.

这是我的实施,请你告诉我它是否正确?

//PlayerType is an enum with just White and Black values, opponent() returns the opposite player type
protected int minMax(int alpha, int beta, int maxDepth, PlayerType player) {        
    if (!canContinue()) {
        return 0;
    }
    ArrayList<Move> moves = sortMoves(generateLegalMoves(player));
    Iterator<Move> movesIterator = moves.iterator();
    int value = 0;
    boolean isMaximizer = (player.equals(playerType)); // playerType is the player used by the AI        
    if (maxDepth == 0 || board.isGameOver()) {
        value = evaluateBoard();
        return value;
    }
    while (movesIterator.hasNext()) {
        Move currentMove = movesIterator.next();
        board.applyMove(currentMove);
        value = minMax(alpha, beta, maxDepth - 1, player.opponent());
        board.undoLastMove();
        if (isMaximizer) {
            if (value > alpha) {
                selectedMove = currentMove;
                alpha = value;
            }
        } else {
            if (value < beta) {
                beta = value;
            }
        }
        if (alpha >= beta) {
            break;
        }
    }
    return (isMaximizer) ? alpha : beta;
}
Run Code Online (Sandbox Code Playgroud)

编辑3:

基于@ Codor的回答/评论的新实现

private class MoveValue {
    public Move move;
    public int value;

    public MoveValue() {
        move = null;
        value = 0;
    }

    public MoveValue(Move move, int value) {
        this.move = move;
        this.value = value;
    }

    @Override
    public String toString() {
        return "MoveValue{" + "move=" + move + ", value=" + value + '}';
    }

}

protected MoveValue minMax(int alpha, int beta, int maxDepth, PlayerType player) {
    if (!canContinue()) {
        return new MoveValue();
    }
    ArrayList<Move> moves = sortMoves(generateLegalMoves(player));
    Iterator<Move> movesIterator = moves.iterator();
    MoveValue moveValue = new MoveValue();
    boolean isMaximizer = (player.equals(playerType));
    if (maxDepth == 0 || board.isGameOver()) {            
        moveValue.value = evaluateBoard();
        return moveValue;
    }
    while (movesIterator.hasNext()) {
        Move currentMove = movesIterator.next();
        board.applyMove(currentMove);
        moveValue = minMax(alpha, beta, maxDepth - 1, player.opponent());
        board.undoLastMove();
        if (isMaximizer) {
            if (moveValue.value > alpha) {
                selectedMove = currentMove;
                alpha = moveValue.value;
            }
        } else {
            if (moveValue.value < beta) {
                beta = moveValue.value;
                selectedMove = currentMove;
            }
        }
        if (alpha >= beta) {
            break;
        }
    }
    return (isMaximizer) ? new MoveValue(selectedMove, alpha) : new MoveValue(selectedMove, beta);
}
Run Code Online (Sandbox Code Playgroud)

我不知道我是否做对了,或者我做错了什么,但我回到了问题时我遇到的问题:

调用minMax(Integer.MIN_VALUE, Integer.MAX_VALUE, 1, PlayerType.Black)返回一个只能由白人玩家完成的移动,这不是我需要的.

对于给定的球员,我需要最好的举动,而不是整板的最佳举动.

Ste*_*TNT 6

经过一些研究和大量时间浪费解决这个问题后,我想出了这个似乎有用的解决方案.

private class MoveValue {

    public double returnValue;
    public Move returnMove;

    public MoveValue() {
        returnValue = 0;
    }

    public MoveValue(double returnValue) {
        this.returnValue = returnValue;
    }

    public MoveValue(double returnValue, Move returnMove) {
        this.returnValue = returnValue;
        this.returnMove = returnMove;
    }

}


protected MoveValue minMax(double alpha, double beta, int maxDepth, MarbleType player) {       
    if (!canContinue()) {
        return new MoveValue();
    }        
    ArrayList<Move> moves = sortMoves(generateLegalMoves(player));
    Iterator<Move> movesIterator = moves.iterator();
    double value = 0;
    boolean isMaximizer = (player.equals(playerType)); 
    if (maxDepth == 0 || board.isGameOver()) {            
        value = evaluateBoard();            
        return new MoveValue(value);
    }
    MoveValue returnMove;
    MoveValue bestMove = null;
    if (isMaximizer) {           
        while (movesIterator.hasNext()) {
            Move currentMove = movesIterator.next();
            board.applyMove(currentMove);
            returnMove = minMax(alpha, beta, maxDepth - 1, player.opponent());
            board.undoLastMove();
            if ((bestMove == null) || (bestMove.returnValue < returnMove.returnValue)) {
                bestMove = returnMove;
                bestMove.returnMove = currentMove;
            }
            if (returnMove.returnValue > alpha) {
                alpha = returnMove.returnValue;
                bestMove = returnMove;
            }
            if (beta <= alpha) {
                bestMove.returnValue = beta;
                bestMove.returnMove = null;
                return bestMove; // pruning
            }
        }
        return bestMove;
    } else {
        while (movesIterator.hasNext()) {
            Move currentMove = movesIterator.next();
            board.applyMove(currentMove);
            returnMove = minMax(alpha, beta, maxDepth - 1, player.opponent());
            board.undoLastMove();
            if ((bestMove == null) || (bestMove.returnValue > returnMove.returnValue)) {
                bestMove = returnMove;
                bestMove.returnMove = currentMove;
            }
            if (returnMove.returnValue < beta) {
                beta = returnMove.returnValue;
                bestMove = returnMove;
            }
            if (beta <= alpha) {
                bestMove.returnValue = alpha;
                bestMove.returnMove = null;
                return bestMove; // pruning
            }
        }
        return bestMove;
    }   
}
Run Code Online (Sandbox Code Playgroud)