gpt4 book ai didi

java - Minimax 算法不返回最佳移动

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:34:26 29 4
gpt4 key购买 nike

我正在使用带 alpha-beta 剪枝的极小极大算法编写黑白棋引擎。它工作正常,但我发现了以下问题:

当算法发现位置丢失时,它会按预期返回 -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 步之后丢失)时,上面的最佳变量等于作为参数传递的空无效移动...为什么??

感谢您的帮助!

最佳答案

您的问题是您使用 -INFINITY 和 +INFINITY 作为输赢分数。您的赢/输分数应该高于/低于任何其他位置评估分数,但不等于您的无穷大值。这将保证即使在无可救药的情况下也会选择一步棋。

关于java - Minimax 算法不返回最佳移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9511814/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com