【问题标题】:Adding Alpha Beta pruning to Negamax in Java在 Java 中为 Negamax 添加 Alpha Beta 修剪
【发布时间】:2016-04-11 11:12:01
【问题描述】:

我正在用 Java 制作一个国际象棋游戏,并且(我认为)已经成功地为 AI 玩家实现了 Negamax。我在为此添加 alpha beta 修剪以改进算法时遇到了一些麻烦。我已经尝试过遵循教程和示例代码,但无法理解它是如何工作的。

以下是我目前必须获得最佳移动的代码:

private Move getBestMove() {
    System.out.println("Getting best move");
    System.out.println("Thinking...");

    List<Move> validMoves = generateMoves(true);
    int bestResult = Integer.MIN_VALUE;
    Move bestMove = null;

    for (Move move : validMoves) {

        executeMove(move);
        System.out.println("Evaluating: " + move);

        int evaluationResult = -evaluateNegaMax(this.lookForward, "", Integer.MIN_VALUE, Integer.MAX_VALUE);
        undoMove(move);

        if (evaluationResult > bestResult) {
            bestResult = evaluationResult;
            bestMove = move;
        }
    }
    System.out.println("Done thinking! The best move is: " + bestMove);
    return bestMove;
}

这是我尝试在我的(工作)negamax 方法中添加 aplha-beta 修剪:

public int evaluateNegaMax(int lookForward, String indent, int alpha, int beta) {

    if (lookForward <= 0
            || this.chessGame.getGameState() == ChessGame.GAME_STATE_WHITE_WON
            || this.chessGame.getGameState() == ChessGame.GAME_STATE_BLACK_WON) {

        return evaluateState();
    }

    List<Move> moves = generateMoves(false);

    for (Move currentMove : moves) {

        System.out.println(indent + "Handling move: " + currentMove + " : " + alpha);
        if (currentMove == null) {
            continue;
        }

        executeMove(currentMove);

        alpha = Math.max(alpha, -evaluateNegaMax(lookForward-1, "    ", -beta, -alpha));

        if (alpha > beta) {
            break;
        }

        undoMove(currentMove);
    }
    return alpha;
}

最后是控制台的样子

Starting game flow
Looking 2 moves aheadExecuted: E/2 -> E/4
Tested 0 moves
Getting best move
Thinking...
Evaluating: B/8 -> A/6
Handling move: B/1 -> A/3 : -2147483648
    Handling move: A/8 -> B/8 : -2147483647
Handling move: B/1 -> C/3 : 2
    Handling move: B/8 -> A/8 : -2147483647
    Handling move: A/6 -> B/4 : -3
    Handling move: A/6 -> C/5 : -3
    Handling move: G/8 -> F/6 : -2
Handling move: D/1 -> E/2 : 2
    Handling move: B/8 -> A/8 : -2147483647
Handling move: D/1 -> F/3 : 2
    Handling move: A/8 -> B/8 : -2147483647
    Handling move: A/8 -> B/8 : -2147483647
    Handling move: F/6 -> E/4 : -32
    Handling move: F/6 -> G/4 : -17
    Handling move: F/6 -> D/5 : -17
Handling move: G/1 -> E/2 : 2
    Handling move: B/1 -> A/3 : -2147483647
    Handling move: B/1 -> C/3 : -29
    Handling move: E/1 -> F/1 : -28
    Handling move: E/2 -> G/1 : -19
    Handling move: E/2 -> C/3 : -19
    Handling move: E/2 -> G/3 : -19
    Handling move: E/2 -> D/4 : -19
Handling move: G/1 -> F/3 : 19
    Handling move: A/8 -> B/8 : -2147483647
Handling move: G/1 -> H/3 : 19
    Handling move: B/8 -> B/2 : -2147483647
Exception in thread "Thread-2" java.lang.NullPointerException
    at Chess.logic.ChessGame.movePiece(ChessGame.java:166)
    at Chess.ai.AiPlayerHandler.executeMove(AiPlayerHandler.java:158)
    at Chess.ai.AiPlayerHandler.evaluateNegaMax(AiPlayerHandler.java:84)
    at Chess.ai.AiPlayerHandler.getBestMove(AiPlayerHandler.java:47)
    at Chess.ai.AiPlayerHandler.getMove(AiPlayerHandler.java:31)
    at Chess.logic.ChessGame.waitForMove(ChessGame.java:125)
    at Chess.logic.ChessGame.startGame(ChessGame.java:95)
    at Chess.logic.ChessGame.run(ChessGame.java:338)
    at java.lang.Thread.run(Thread.java:745)

任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

  • 您能澄清一下您的问题吗?是如何使用 negamax 实现 alpha-beta 搜索?目前,您似乎在要求某人在您的代码中为您实现它,这太宽泛了。
  • 基本上这就是我要问的。我确实尝试过自己实施它,但它只是返回了完全错误的结果。根据我的研究,应该只需要添加几行代码即可添加到我目前拥有的内容中。也许我应该在没有任何尝试的情况下发布我的尝试而不是工作代码。我现在把它改过来。感谢您的评论。
  • 似乎该错误不在您显示的代码中。为什么会出现空指针异常?这是你必须调查的。没有代码,我们无法为您解决这个问题。
  • @Rémi 在我将 alpha-beta 部分添加到它之前,已经使用了 evaluateNegaMax() 方法。它很好地打印了整棵树。错误发生在我添加的内容中。如果有帮助,我可以添加原始方法。只是不喜欢贴一堵代码墙并期望有人知道我在说什么。

标签: java artificial-intelligence chess alpha-beta-pruning negamax


【解决方案1】:

我想我已经成功了。如果有人关注此问题正在等待回复,则代码如下:

    public int evaluateNegaMax(int depth, String indent, int alpha, int beta) {
    if (depth <= 0
            || this.chessGame.getGameState() == ChessGame.GAME_STATE_WHITE_WON
            || this.chessGame.getGameState() == ChessGame.GAME_STATE_BLACK_WON) {

        return evaluateState();
    }

    List<Move> moves = generateMoves(false);
    int bestValue = Integer.MIN_VALUE;

    for (Move currentMove : moves) {

        executeMove(currentMove);
        int value = -evaluateNegaMax(depth - 1, indent + "    ", -beta, -alpha);
        System.out.println(indent + "Handling move: " + currentMove + " : " + value);
        undoMove(currentMove);
        counter++;

        if (value > bestValue) {
            bestValue = value;
        }

        if (bestValue > alpha) {
            alpha = bestValue;
        }

        if (bestValue >= beta) {
            break;
        }
    }
    System.out.println(indent + "max: " + alpha);
    return alpha;
}

【讨论】:

    猜你喜欢
    • 2012-11-13
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多