【问题标题】:Facing performance problems implementing minimax for a chess game面临为国际象棋游戏实现极小极大的性能问题
【发布时间】:2015-07-03 19:34:00
【问题描述】:

我正在尝试为一个小象棋游戏实现极小极大算法。也许我的前提是错误的,这不是应该尝试的。是吗?

程序可以运行,但存在很大的性能问题:

  • 深度 = 0、1 或 2,结果是即时的。
  • 深度 = 3 结果需要 15 秒。
  • 深度 = 4 - 还没有结果。

这是我的实现:

private Move findBestMove(Chessboard chessboard, int depth,
        boolean maximizingPlayer) {
    if (depth == 0) {
        return new Move(chessboard.calculateHeuristicValue());
    } else {
        Move bestMove;
        if (maximizingPlayer) {
            bestMove = new Move(Integer.MIN_VALUE);
            for (Move possibleMove : findAllPossibleMoves(chessboard,
                    !(maximizingPlayer ^ whiteTurn))) {
                Move move = findBestMove(
                        possibleMove.getResultChessboard(), depth - 1,
                        !maximizingPlayer);
                if (move.getValue() > bestMove.getValue()) {
                    possibleMove.setValue(move.getValue());
                    bestMove = possibleMove;
                }
            }
        } else {
            bestMove = new Move(Integer.MAX_VALUE);
            for (Move possibleMove : findAllPossibleMoves(chessboard,
                    !(maximizingPlayer ^ whiteTurn))) {
                Move move = findBestMove(
                        possibleMove.getResultChessboard(), depth - 1,
                        !maximizingPlayer);
                if (move.getValue() < bestMove.getValue()) {
                    possibleMove.setValue(move.getValue());
                    bestMove = possibleMove;
                }
            }
        }
        return bestMove;
    }
}

可能在算法的实现或对象的设计或它们的使用中存在错误。我不能把手指放在它上面。因此,在尝试优化代码或调整程序的内存配置之前,我想确保没有我忽略的重大问题。

注意:没有内存分析经验。

【问题讨论】:

  • 极小极大是指数的。为了减少探索的分支数量,您可以使用字母修剪

标签: java performance recursion artificial-intelligence minimax


【解决方案1】:

在国际象棋中,第一步有 20 种可能性(棋子 16 种,马 4 种)。

为简单起见,我们假设接下来的动作也是如此。

  • 对于深度 1,MinMax 仅考虑 20 步。
  • 对于深度 2,MinMax 考虑 20 个动作和该动作的 20 个答案,400 个可能的动作,没问题。
  • 对于深度 3,MinMax 考虑 20^3 = 8.000 次可能的移动。在您的机器上已经 15 秒了。
  • 对于深度 4,MinMax 考虑 20^4 = 160.000 次可能的移动。这将比前一个花费大约 20 倍的时间......

只是搜索空间变得太大 - 它随着输入(深度)大小呈指数增长。时间复杂度为 O(20^depth)。

但是,我们不必搜索所有空间来找到真正好的动作。

Alpha-beta pruning 是 MinMax 的流行优化。

如果这还不够,我会考虑切换到完全其他的算法 - 例如Monte Carlo Tree Search(使用 UCT)。

移动数据库也可以提供帮助 - 无需计算您已经准备好的(预先计算的)策略的第一个移动。

1997 年击败 Kasparov 的著名 Deep Blue 使用了它们,您可以查看它还使用了什么 here

【讨论】:

    猜你喜欢
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多