如何停止此算法循环?
How do I stop this algorithm loop?
我试图在我的程序中使用 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;
}
..
问题可能是您使用浮点数表示整数深度,并且 ==
比较因浮点精度问题而失败。
试试这样的东西:
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;
}
}
我试图在我的程序中使用 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;
}
..
问题可能是您使用浮点数表示整数深度,并且 ==
比较因浮点精度问题而失败。
试试这样的东西:
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;
}
}