Unity A *寻路崩溃

时间:2016-07-03 18:23:40

标签: c# artificial-intelligence unity5 path-finding

所以我试图实现A *算法。基本逻辑几乎已经完成,但有一个问题我无法解决。

当请求路径Unity停止响应并且我的PC一直变慢,直到它挂起并且我必须强制关闭。

以下是简化代码:

public static List<Node> ReturnPath(Vector3 pointA, Vector3 pointB)
{
    /* A Bunch of initializations */

    while (!pathFound) 
    {
        //add walkable neighbours to openlist
        foreach (Node n in GetNeighbours(currentNode)) 
        {
            if (!n.walkable)
                continue;
            n.gCost = currentNode.gCost + GetManDist (currentNode, n);
            n.hCost = GetManDist (n, B);
            openList.Add (n);
            n.parent = currentNode;

        }

        //check openList for lowest fcost or lower hCost
        for (int i = 0; i < openList.Count; i++) 
        {
            if ((openList [i].fCost < currentNode.fCost) || (openList [i].fCost == currentNode.fCost && openList [i].hCost < currentNode.hCost)) 
            {
                //make the currentNode = node with lowest fcost
                currentNode = openList [i];
            }
            openList.Remove (currentNode);
            if(!closedList.Contains(currentNode))
                closedList.Add (currentNode);
        }

        //Check if the currentNode it destination
        if (currentNode == B) 
        {
            Debug.Log ("Path Detected");
            path = RetracePath (A, B);
            pathFound = true;
        }

    }
    return path;
}

只要目标是两个节点,这就可以正常工作。如果不止于此,则会出现上述问题。这是为什么?我的第一个猜测是我在openList中投入太多。

注意:我将一个30 x 30单位平台(地板)分成1x1个方块,称为&#34;节点&#34; GetManDist()获取2个节点之间的曼哈顿距离。

更新:这是工作代码。评论太长了

    public List<Node> ReturnPath(Vector3 pointA, Vector3 pointB)
{
    List<Node> openList = new List<Node>();
    List<Node> closedList = new List<Node>();
    List<Node> path = new List<Node> ();

    Node A = ToNode (pointA);
    Node B = ToNode (pointB);
    Node currentNode;

    bool pathFound = false;

    A.hCost = GetManDist(A, B);
    A.gCost = 0;

    openList.Add (A);

    while (!pathFound) // while(openList.Count > 0) might be better because it handles the possibility of a non existant path
    {
        //Set to arbitrary element
        currentNode = openList [0];

        //Check openList for lowest fcost or lower hCost
        for (int i = 0; i < openList.Count; i++) 
        {
            if ((openList [i].fCost < currentNode.fCost) || ((openList [i].fCost == currentNode.fCost && openList [i].hCost < currentNode.hCost))) 
            {
                //Make the currentNode = node with lowest fcost
                currentNode = openList [i];
            }
        }

        //Check if the currentNode is destination
        if (currentNode.hCost == 0) //Better than if(currentNode == B)
        {
            path = RetracePath (A, B);
            pathFound = true;
        }

        //Add walkable neighbours to openlist
        foreach (Node n in GetNeighbours(currentNode)) 
        {
            //Avoid wasting time checking unwalkable and those already checked
            if (!n.walkable || closedList.Contains(n))
                continue;

            //Movement Cost to neighbour
            int newGCost = currentNode.gCost + GetManDist (currentNode, n);

            //Calculate g_Cost, Update if new g_cost to neighbour is less than an already calculated one
            if (n.gCost > newGCost || !openList.Contains (n)) 
            {
                n.gCost = newGCost;
                n.hCost = GetManDist (n, B);
                n.parent = currentNode; //So we can retrace
                openList.Add (n);
            }
        }
        //We don't need you no more...
        openList.Remove (currentNode);

        //Avoid redundancy of nodes in closedList
        if(!closedList.Contains(currentNode))
            closedList.Add (currentNode);

    }

    return path;
}

1 个答案:

答案 0 :(得分:1)

问题在于currentNode的值。由于我们正在针对 currentNode 检查 开放列表 中具有最低f_Cost或更低h_Cost的节点,当路径寻找遇到墙壁时,它必须返回或转弯,这导致f_Cost和h_Cost(都大于currentNode)的增加。因此,openlist中没有任何节点具有较低的f_Cost / h_Cost导致无限循环。简单的解决方案是每次将currentNode设置为openList中的任意元素。

添加

currentNode = openlist[0];

在循环开始时。