【问题标题】:How to fix the min max algorithm if it tends to self-destruct?如果它倾向于自毁,如何修复最小最大算法?
【发布时间】:2020-07-19 04:51:08
【问题描述】:

我正在尝试使用 MinMax 为跳棋游戏编写 AI 算法。在最终测试之前一切都很好......电脑选择任何一块,然后他会来找我的家伙。他真想快点死。这是我的代码:

public MoveAndPoints MinMax(Dictionary<int, Field> gameBoard, string myColor, Boolean maximizingPlayer, int depth)
    {
        MoveAndPoints bestValue = new MoveAndPoints();
        if (0 == depth)
        {
            return new MoveAndPoints(((myColor == enemyColor) ? 1 : -1) * evaluateGameBoard(gameBoard, myColor), bestValue.move);
        }
            
        MoveAndPoints val = new MoveAndPoints();
        if (maximizingPlayer)
        {
            bestValue.points = int.MinValue;
            foreach (Move move in GetPossibleMoves(gameBoard, myColor))
            {
                gameBoard = ApplyMove(gameBoard, move);
                bestValue.move = move;
                val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), false, depth - 1);
                bestValue.points = Math.Max(bestValue.points, val.points);
                gameBoard = RevertMove(gameBoard, move);
            }
            return bestValue;
        }
        else
        {
            bestValue.points = int.MaxValue;
            foreach (Move move in GetPossibleMoves(gameBoard, myColor))
            {
                gameBoard = ApplyMove(gameBoard, move);
                val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), true, depth - 1);
                bestValue.points = Math.Min(bestValue.points, val.points);
                gameBoard = RevertMove(gameBoard, move);
            }
            return bestValue;
        }
    }

这是我的启发:

public int evaluateGameBoard(Dictionary<int, Field> gameBoard, string color)
{
    return Extend.GetNumberOfPieces(gameBoard, color) - Extend.GetNumberOfPieces(gameBoard, Extend.GetEnemyPlayerColor(color));
}

例如,我有这样的场景:


他选择死亡而不是移动任何其他地方:

如果我自己选择死亡,他只会杀了我,因为他必须这样做,因为这是他唯一的举动。 有人可以帮我解决这个问题吗?我认为问题出在 MinMax 内部。我几乎可以肯定这不是很成问题,但我真的找不到它:(

这是我的树的深度 3:

【问题讨论】:

  • 查找“alpha beta pruning”
  • 是的,我知道,但我想看看他,因为 MinMax 会以某种方式工作。

标签: c# algorithm artificial-intelligence minmax


【解决方案1】:

我没有你所有的代码,所以我无法完全验证这些。但是,我相信至少你的一个问题是你正在混合玩家之间的颜色。在您的 MinMax 调用中,您为两个位置传递了相同的颜色:

这是最大化播放器内部的调用:

val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), false, depth - 1);

这是另一个调用:

val = MinMax(gameBoard, Extend.GetEnemyPlayerColor(myColor), true, depth - 1);

无论你是否最大化,你都正确地切换,但你不应该在玩家之间切换颜色。

在实践中确实想要跟踪玩家是谁在根,因为所有的评估都是相对于根玩家的。在这种情况下,您只需在整个过程中传递颜色,并且除了在评估函数中之外,永远不要使用敌人的颜色。

【讨论】:

  • 嗯,我完全不明白。我的意思是为什么我shouldn't 切换颜色?如果我不会,那么我将只模拟 1 名球员的动作,而不注意 2 号球员的去向。你能给我解释一下吗?
  • @Brarord 你必须更换球员才能让运动发挥作用,但你不想改变你的评估方式。也就是说,根玩家正在从他们的角度进行搜索和评估,因此所有状态都应该从根玩家的最大玩家的角度进行评估。
  • @Brarord 我建议添加另一个不变的变量rootColor,然后在调用evaluateGameBoard 时使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多