如何停止此算法循环?

问题描述 投票:0回答:1

我试图在我的程序中获得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;
}

..

c# algorithm unity3d genetic-algorithm stl-algorithm
1个回答
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;
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.