如何提高Cheima.js的Minimax性能?

时间:2018-05-27 14:27:24

标签: javascript chess chessboard.js


我正在尝试使用带有alpha-beta修剪的Minimax算法使用Chess.js和Chessboard.js编写国际象棋引擎。问题是算法需要很长时间来执行所有评估并决定移动,即使使用depth=3。我怎样才能提高性能?

以下是我的minimax实施:

var minimax = function (depth, game, alpha, beta, isAIplaying) {

  // end branch: simply evaluate and return the current score of the board
  if (depth === 0) {
    return evaluateBoard(game);
  }

  // list of all the possible moves in current status
  var newGameMoves = game.moves();
  if (isAIplaying) {
      var bestMove = -9999;
      for (var i = 0; i < newGameMoves.length; i++) {
          game.move(newGameMoves[i]);
          bestMove = Math.max(bestMove, minimax(depth - 1, game, !isAIplaying));
          game.undo();
          alpha = Math.max(alpha, bestMove);
          if (beta <= alpha) {
            return bestMove;
          }
      }
      return bestMove;
  } else {
      var bestMove = 9999;
      for (var i = 0; i < newGameMoves.length; i++) {
          game.move(newGameMoves[i]);
          bestMove = Math.min(bestMove, minimax(depth - 1, game, !isAIplaying));
          game.undo();
          beta = Math.min(beta, bestMove);
          if (beta <= alpha) {
           return bestMove;
          }
      }
      return bestMove;
  }
};

在下面的代码安静中调用该函数,Black必须决定采取哪一步:

var possibleMoves = game.moves(); 

// game over
if (possibleMoves.length === 0) 
  return;

var best_score = -9999;
var bestMoveFound;

for(var i=0; i<possibleMoves.length; ++i){
  game.move(possibleMoves[i]); // make one possible move

   // minimax: take the current status of the game
  // it is not Black's turn
  var score = minimax(3, game, -10000, 10000, false);

  // if I get a better score then I store it
  if(score >= best_score){
    best_score = score;
    bestMoveFound = i;
  }

  // undo move
  game.undo();
}

// make the best move (update both the game and the board on the screen)
game.move(possibleMoves[bestMoveFound]);
board.position(game.fen());

以下是评估函数:

var evaluateBoard = function(current_game) {
    var status = current_game.fen();

    var score = 0;

    // calculate score for the current situation
    var fen_idx = 0;
    var piece = status[fen_idx];
    while(piece != ' '){
      switch(piece){
        case 'p': score += pawn; break;
        case 'n': score += knight; break;
        case 'b': score += bishop; break;
        case 'r': score += tower; break;
        case 'q': score += queen; break;
        case 'k': score += king; break;
        case 'P': score -= pawn; break;
        case 'N': score -= knight; break;
        case 'B': score -= bishop; break;
        case 'R': score -= tower; break;
        case 'Q': score -= queen; break;
        case 'K': score -= king; break;
        default: break;
      }

      ++fen_idx;
      piece = status[fen_idx];
    }

    return score;
};

pawnbishop和其他是简单的常量,p表示黑色棋子,而P表示白色。你有什么想法加快表现吗?

1 个答案:

答案 0 :(得分:0)

评估FEN-String的想法看起来很快,但是您的程序必须为游戏树的每次离开计算FEN-String。这确实很慢,因为它必须在64个正方形内循环扫描每个图形以及每个空白的正方形。您不应该这样做,而是将主板上的图形保存在列表中,以便计算与源代码的模拟量。

如果在板上取下一块或更换一块,则最好具有所有和平的总和,而仅增加或减去一个值。因此,您的评估功能不需计算,引擎将更快。以我的经验,如果您的评估功能只是将两侧的零件值进行比较,则您应该在2秒内达到7级深度。