【发布时间】:2012-03-01 06:46:29
【问题描述】:
我正在编写一个使用带有 alpha-beta 修剪的 minimax 的 Othello 引擎。 它工作正常,但我发现以下问题:
当算法发现一个位置丢失时,它按预期返回-INFINITY,但在 在这种情况下,我无法跟踪“最佳”移动...该位置已经丢失,但无论如何它应该返回一个有效的移动(最好是一个存活时间更长的移动,就像好的国际象棋引擎一样)。
代码如下:
private float minimax(OthelloBoard board, OthelloMove best, float alpha, float beta, int depth)
{
OthelloMove garbage = new OthelloMove();
int currentPlayer = board.getCurrentPlayer();
if (board.checkEnd())
{
int bd = board.countDiscs(OthelloBoard.BLACK);
int wd = board.countDiscs(OthelloBoard.WHITE);
if ((bd > wd) && currentPlayer == OthelloBoard.BLACK)
return INFINITY;
else if ((bd < wd) && currentPlayer == OthelloBoard.BLACK)
return -INFINITY;
else if ((bd > wd) && currentPlayer == OthelloBoard.WHITE)
return -INFINITY;
else if ((bd < wd) && currentPlayer == OthelloBoard.WHITE)
return INFINITY;
else
return 0.0f;
}
//search until the end? (true during end game phase)
if (!solveTillEnd )
{
if (depth == maxDepth)
return OthelloHeuristics.eval(currentPlayer, board);
}
ArrayList<OthelloMove> moves = board.getAllMoves(currentPlayer);
for (OthelloMove mv : moves)
{
board.makeMove(mv);
float score = - minimax(board, garbage, -beta, -alpha, depth + 1);
board.undoMove(mv);
if(score > alpha)
{
//Set Best move here
alpha = score;
best.setFlipSquares(mv.getFlipSquares());
best.setIdx(mv.getIdx());
best.setPlayer(mv.getPlayer());
}
if (alpha >= beta)
break;
}
return alpha;
}
我称之为:
AI ai = new AI(board, maxDepth, solveTillEnd);
//create empty (invalid) move to hold best move
OthelloMove bestMove = new OthelloMove();
ai.bestFound = bestMove;
ai.minimax(board, bestMove, -INFINITY, INFINITY, 0);
//dipatch a Thread
new Thread(ai).start();
//wait for thread to finish
OthelloMove best = ai.bestFound();
当搜索一个丢失的位置(例如,想象它在 10 步之后丢失)时,上面的最佳变量等于作为参数传递的空无效移动...为什么??
感谢您的帮助!
【问题讨论】:
-
请阅读faq和How to Ask,并提出更具体的问题。另外,您的问题不完整;您没有显示类
AI的定义,这对问题非常关键 -
问题是概念性的,而不是代码问题。我提供的代码足以解决我认为的问题。但无论如何,谢谢,我会阅读这篇文章以了解更多信息。
标签: java algorithm search minimax