c# - 理解 Minimax 算法

标签 c# algorithm artificial-intelligence minimax alpha-beta-pruning

我正在尝试为两人 8x8 棋盘游戏创建一个 AI 对手。经过研究,我发现 Minimax 算法足以胜任这项工作。我创建的 AI 对手将与其他 AI 对手或人类对战。

我对极小极大算法的理解有疑问。

我试图只创建一个 AI 对手,但在网上找到的解释说我需要为两个玩家(最小玩家和最大玩家)编写代码,正如我从下面的伪代码中理解的那样。

MinMax (GamePosition game) {
  return MaxMove (game);
}

MaxMove (GamePosition game) {
  if (GameEnded(game)) {
    return EvalGameState(game);
  }
  else {
    best_move < - {};
    moves <- GenerateMoves(game);
    ForEach moves {
       move <- MinMove(ApplyMove(game));
       if (Value(move) > Value(best_move)) {
          best_move < - move;
       }
    }
    return best_move;
  }
}

MinMove (GamePosition game) {
  best_move <- {};
  moves <- GenerateMoves(game);
  ForEach moves {
     move <- MaxMove(ApplyMove(game));
     if (Value(move) > Value(best_move)) {
        best_move < - move;
     }
  }

  return best_move;
}

我可以进一步理解,最大玩家将是我要开发的 AI,最小玩家是对手。

我的问题是为什么我必须为最小玩家和最大玩家编写代码以返回最佳着法?

下面给出的伪代码是基于C#的。

提前致谢。

最佳答案

你只需要在最坏的情况下为两个玩家搜索最佳解决方案,这就是为什么它被称为 minmax,你不需要更多:

function minimax( node, depth )     
   if node is a terminal node or depth <= 0:        
       return the heuristic value of node  

   α = -∞    
   foreach child in node:          
      α = max( a, -minimax( child, depth-1 ) )  

   return α

node是一个游戏位置, 节点中的 child 是下一个 Action (来自所有可用 Action 的列表), 深度是两个玩家一起搜索的最大移动。

您可能无法在 8x8 棋盘上运行所有可能的 Action (取决于您有多少下一步的选择) 例如,如果每个人都有 8 种不同的可能 Action ,并且游戏在 40 次 Action 后结束(应该是最坏的情况),那么您将获得 8^40 位置。计算机将需要数十年甚至更长的时间来解决它,这就是为什么你需要深度参数和使用启发式函数(例如随机森林树) 无需检查所有选项即可知道游戏位置有多好。

一个更好的 minmax 算法是 Alpha-Beta 修剪,一旦找到目标(β 参数)就完成搜索:

function negamax( node, depth, α, β, color )  
   if node is a terminal node or depth = 0     
       return color * the heuristic value of node  

   foreach child of node          
       value = -negamax( child, depth-1, -β, -α, -color )  

   if value ≥ β                
      return value /** Alpha-Beta cut-off */  

  if value ≥ α       
     α = value              

  return α

最好先使用没有很多位置的游戏(例如井字游戏)。

关于c# - 理解 Minimax 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29041413/

相关文章:

c# - 启动 IIS Express 时发生错误 - Visual Studio 2017

r - 由于 kd-tree 最近邻搜索中的最大半径导致意外减速

c# - 如何使用 Microsoft Fakes 来填充异步任务方法?

c# - 十六进制转十进制

algorithm - 将时间戳转换为字母数字

machine-learning - 使用前馈神经网络代替 LSTM?

haskell - 在haskell中绑定(bind)计算的运行时间

python - 什么算法适用于这个简单的机器学习问题?

c# - 验证由几个字节组成的位掩码

regex - 从字符串中删除模式