negamax 算法的这种实现是否正确
我正在尝试实现 negamax 算法,这就是我认为应该是这样的:
public Move getBestMove(Board board){
List<Move> possibleMoves = board.getPossibleMoves();
Move optimalMove;
int maxScore;
foreach(Move move in possibleMoves){
Board newBoard = board.clone();
newBoard.makeMove(move);
int score = negamax(newBoard, DEPTH, Integer.MAX, Integer.MIN, 1);
if (score > maxScore){
optimalMove = move;
maxScore = score;
}
}
}
以及相应的 negamax 函数
public int negamax(Board board, int depth, int alpha, int beta, int sign){
if(depth == null || board.getPossibleMovesNumber(colour) == 0){
return calculateBoardFunction(board);
}
else{
List<Move> possibleMoves = board.getPossibleMoves();
foreach(Move move in possibleMoves){
Board newBoard = board.clone();
newBoard.makeMove(move);
alpha = Math.max(alpha, -negamax(newBoard, depth-1, -beta, -alpha, -sign);
if(alpha >= beta){
break;
}
}
return alpha;
}
是的,我知道它没有编译,但我只是想对其进行伪编码。
编辑
calculateBoardFunction(Board board)将始终评估棋盘的最佳移动计算颜色。
另外,我试图使其通用,因此它对于每个游戏(国际象棋,黑白棋,围棋)等都一样......(但这不是问题的一部分)
我还使用维基百科的 negamax 伪代码作为示例。但使用该代码我>>认为>>我可以使用正确的启发值很好地创建游戏树。但我在 getBestMove 函数中添加代码的原因是为了找出实际上最好的移动方式。
但我不确定我是否能做到这一点。
I'm trying to implement the negamax algorithm, and this is how I thought it should be:
public Move getBestMove(Board board){
List<Move> possibleMoves = board.getPossibleMoves();
Move optimalMove;
int maxScore;
foreach(Move move in possibleMoves){
Board newBoard = board.clone();
newBoard.makeMove(move);
int score = negamax(newBoard, DEPTH, Integer.MAX, Integer.MIN, 1);
if (score > maxScore){
optimalMove = move;
maxScore = score;
}
}
}
And the corresponding negamax function
public int negamax(Board board, int depth, int alpha, int beta, int sign){
if(depth == null || board.getPossibleMovesNumber(colour) == 0){
return calculateBoardFunction(board);
}
else{
List<Move> possibleMoves = board.getPossibleMoves();
foreach(Move move in possibleMoves){
Board newBoard = board.clone();
newBoard.makeMove(move);
alpha = Math.max(alpha, -negamax(newBoard, depth-1, -beta, -alpha, -sign);
if(alpha >= beta){
break;
}
}
return alpha;
}
Yeah I know it's not compiling but I'm just trying to pseudocode it a bit.
Edit
The calculateBoardFunction(Board board) will ALWAYS evaluate the board for the color that the best move is calculated for.
Also, i've tried to make it generic, so it works the same for every game (chess, reversi, go) etc... (but that's not part of the question)
Also I used the wikipedia's negamax pseudocode as an example. But using that code I >>think<< I could create the game tree very well, with the correct heuristics values. but the reason I have the code in the getBestMove
function, is to figure out what move is actually the best.
But im not sure if I can do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来或多或少是正确的。存在打印错误(
-sign
而不是-colour
),并且您需要每次通过循环克隆板(或使用unmakeMove
,但是你一开始就不需要克隆)。但除此之外,逻辑看起来是正确的。在现实世界中,您可能希望在尝试之前以某种方式对动作进行排序。这可能会导致所有 Beta 截止的巨大加速。
This looks more or less right. There is a misprint (
-sign
instead of-colour
), and you need to clone the board every time through the loop (or useunmakeMove
, but then you don't need a clone in the first place). But apart from this, the logic looks correct.In the real world, you would want to sort the moves somehow before trying them out. This can result in a huge speed-up, from all the beta cutoffs.