BubbleUp() private method

private BubbleUp ( int index ) : void
index int
return void
示例#1
0
    public List <Node> Astar(Node start, Node goal)
    {
        if (start == null || goal == null)
        {
            return(null);
        }

        if (start == goal)
        {
            return(new List <Node>()
            {
                start
            });
        }

        // initialize pathfinding variables
        foreach (Node node in _ObjectManager.Map.nodes)
        {
            node.gScore        = int.MaxValue;
            node.fScore        = int.MaxValue;
            node.parent        = null;
            node.isInOpenSet   = false;
            node.isInClosedSet = false;
        }

        MinHeap openSet = new MinHeap(start);

        start.gScore = 0;
        start.fScore = start.gScore + Heuristic_cost_estimate(start, goal);

        while (openSet.heap.Count > 1)
        {
            // get closest node
            Node current = openSet.GetRoot();

            // if its the goal, return
            if (current == goal)
            {
                return(Reconstruct_path(start, goal));
            }

            // look at the neighbors of the node
            foreach (Node neighbor in current.getDiagnalNeighbors())
            {
                // ignore the ones that are unwalkable or are in the closed set
                if (neighbor != null && neighbor.isWalkable && !neighbor.isInClosedSet)
                {
                    // if the new gscore is lower replace it
                    int tentativeGscore = current.gScore + 14 + UnityEngine.Random.Range(0, 8);
                    if (!neighbor.isInOpenSet || tentativeGscore < neighbor.gScore)
                    {
                        neighbor.parent = current;
                        neighbor.gScore = tentativeGscore;
                        neighbor.fScore = neighbor.gScore + Heuristic_cost_estimate(neighbor, goal);
                    }

                    // if neighbor's not in the open set add it
                    if (!neighbor.isInOpenSet)
                    {
                        openSet.BubbleUp(neighbor);
                    }
                }
            }

            // look at the neighbors of the node
            foreach (Node neighbor in current.getCloseNeighbors())
            {
                // ignore the ones that are unwalkable or are in the closed set
                if (neighbor != null && neighbor.isWalkable && !neighbor.isInClosedSet)
                {
                    // if the new gscore is lower replace it
                    int tentativeGscore = current.gScore + 10 + UnityEngine.Random.Range(0, 8);
                    if (!neighbor.isInOpenSet || tentativeGscore < neighbor.gScore)
                    {
                        neighbor.parent = current;
                        neighbor.gScore = tentativeGscore;
                        neighbor.fScore = neighbor.gScore + Heuristic_cost_estimate(neighbor, goal);
                    }

                    // if neighbor's not in the open set add it
                    if (!neighbor.isInOpenSet)
                    {
                        openSet.BubbleUp(neighbor);
                    }
                }
            }
        }
        // Fail
        return(null);
    }
示例#2
0
    public List<Node> Astar(Node start, Node goal)
    {
        if (start == null || goal == null)
            return null;

        if (start == goal)
        {
            return new List<Node>()
            {
                start
            };
        }

        // initialize pathfinding variables
        foreach (Node node in _ObjectManager.Map.nodes) {
            node.gScore = int.MaxValue;
            node.fScore = int.MaxValue;
            node.parent = null;
            node.isInOpenSet = false;
            node.isInClosedSet = false;
        }

        MinHeap openSet = new MinHeap (start);

        start.gScore = 0;
        start.fScore = start.gScore + Heuristic_cost_estimate (start, goal);

        while (openSet.heap.Count > 1) {
            // get closest node
            Node current = openSet.GetRoot ();

            // if its the goal, return
            if (current == goal)
                return Reconstruct_path (start, goal);

            // look at the neighbors of the node
            foreach (Node neighbor in current.getDiagnalNeighbors()) {
                // ignore the ones that are unwalkable or are in the closed set
                if (neighbor != null && neighbor.isWalkable && !neighbor.isInClosedSet) {

                    // if the new gscore is lower replace it
                    int tentativeGscore = current.gScore + 14 + UnityEngine.Random.Range (0, 8);
                    if (!neighbor.isInOpenSet || tentativeGscore < neighbor.gScore) {

                        neighbor.parent = current;
                        neighbor.gScore = tentativeGscore;
                        neighbor.fScore = neighbor.gScore + Heuristic_cost_estimate (neighbor, goal);
                    }

                    // if neighbor's not in the open set add it
                    if (!neighbor.isInOpenSet) {
                        openSet.BubbleUp (neighbor);
                    }
                }
            }

            // look at the neighbors of the node
            foreach (Node neighbor in current.getCloseNeighbors()) {
                // ignore the ones that are unwalkable or are in the closed set
                if (neighbor != null && neighbor.isWalkable && !neighbor.isInClosedSet) {

                    // if the new gscore is lower replace it
                    int tentativeGscore = current.gScore + 10 + UnityEngine.Random.Range (0, 8);
                    if (!neighbor.isInOpenSet || tentativeGscore < neighbor.gScore) {

                        neighbor.parent = current;
                        neighbor.gScore = tentativeGscore;
                        neighbor.fScore = neighbor.gScore + Heuristic_cost_estimate (neighbor, goal);
                    }

                    // if neighbor's not in the open set add it
                    if (!neighbor.isInOpenSet) {
                        openSet.BubbleUp (neighbor);
                    }
                }
            }

        }
        // Fail
        return null;
    }