【发布时间】:2022-02-17 03:14:10
【问题描述】:
我是一名新程序员,目前使用 Chess.js 和 Chessboard.js 为我的国际象棋引擎编写一个 javascript alpha-beta 修剪 minimax 算法。我已经实现了一个带有移动排序的基本算法。目前,它在 8 秒内评估了大约 14000 个节点,这太慢了。我的算法有问题还是我没有实现优化?我的算法无法在合理的时间限制内处理比深度 4 更深的任何东西。谢谢你。 附言“跟踪评估”功能只是评估每个特定的移动,以避免对叶节点处的板进行全面评估,这种优化使我的程序加快了大约 50%,但现在仍然很慢。
function minimax(game, depth, distanceFromRoot, alpha, beta, gameEval) {//returns gameEval
if (depth === 0) {
nodeNum++;
if(game.turn() === 'b'){
return (-gameEval / 8);
}else{
return (gameEval / 8);
}
}
// run eval
var prevEval = gameEval;
var moves = game.moves();
moveOrdering(moves);
var bestMove = null;
var bestEval = null;
for (let i = 0; i < moves.length; i++) {
var gameCopy = new Chess()//dummy board to pass down
gameCopy.load(game.fen())
const moveInfo = gameCopy.move(moves[i])
var curGameCopy = new Chess()//static board to eval, before the move so we know which piece was taken if a capture occurs
curGameCopy.load(game.fen())
var curEval = trackingEval(curGameCopy, prevEval, moveInfo, moves[i]); //returns the OBJECTIVE eval for the current move for current move sequence
var evaluated = -minimax(gameCopy, depth - 1, distanceFromRoot + 1, -beta, -alpha, curEval);//pass down the current eval for that move
if (evaluated >= beta) {
return beta;
}
if (evaluated > alpha){
alpha = evaluated
bestMove = moves[i]
bestEval = evaluated;
if (distanceFromRoot === 0) {
bestEval = evaluated;
}
}
}
if(distanceFromRoot === 0){
setEval(-bestEval)
return bestMove;
}
return alpha;
}
【问题讨论】:
标签: javascript algorithm optimization chess alpha-beta-pruning