【发布时间】: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