如何停止此算法循环?

时间:2018-04-21 09:44:36

标签: c# algorithm unity3d genetic-algorithm stl-algorithm

我试图在我的程序中获得Negmax算法。我明白了,但无论如何我对算法也有点怀疑。但是当currentDepth达到深度最大值之后,它必须停止这个循环。请帮帮我解决这个问题?

换句话说,如果当前深度与最大深度相比较大,则必须停止循环。但这不会发生在这里,请帮我停止这个循环。提前谢谢。

然而,这是问题,我也提出了一些方法,但它没有用,因此,我删除并作为评论放在我实验过程中。这是我的代码的一部分:

if(currentGameDepth == maximumGameDepth)
        {

           // I included some methods, but it won't execute to out of the loop
            // I tried to return Mathf.Infinite(), but it is not work..
            // please help me
           //-----------------------------------------------------------------

        }

完整代码如下: - 在这里你可以看到,两个地方添加NegamaxAlgorithm关键字: Negmax算法在这里: -

private static float NegamaxAlgorithm(Piece game, float maximumGameDepth, float currentGameDepth, GamesModel moves)
{ 
    if(currentGameDepth == maximumGameDepth)
    {

       // I included some methods, but it won't execute to out of the loop
        // I tried to return Mathf.Infinite(), but it is not work..
        // please help me
       //-----------------------------------------------------------------

    }

    bestGameMove = null;
    float bestGameScore = Mathf.NegativeInfinity;
    foreach (GamesModel m in MovesList1)
    {
        Moves move = Piece.AccordaingToEvaluationWhiteSide(m,game);
        float recursedGameScore;
        Moves currentGameMove = move;
        recursedGameScore = NegamaxAlgorithm(game , maximumGameDepth , currentGameDepth + 1, currentGameMove);
        float currentGameScore = -recursedGameScore;
        if (currentGameScore > bestGameScore)
        {
            bestGameScore = currentGameScore;
            bestGameMove = m;
        }
    }
    return bestGameScore;
}

...

1 个答案:

答案 0 :(得分:3)

问题可能是您使用浮点数来表示整数深度,并且==比较由于浮点精度问题而失败。

尝试类似:

private static float NegamaxAlgorithm(Piece game, 
     int maximumGameDepth, int currentGameDepth, GamesModel moves)
{ 
    if(currentGameDepth >= maximumGameDepth)
    {
        // terminate recursion
        return float.MinValue;
    }
    else
    {
       // ... use recursion to calculate next level ...
       return bestGameScore;
    }

}