예제 #1
0
        public bool Search(Graph graph, IPathfindingNode source, IPathfindingNode target, IHeuristicCalculator hc)
        {
            pq.Add(source);

            while (pq.count > 0)
            {
                IPathfindingNode nextClosestNode = pq.PopFirst();
                if (searchFrontier.ContainsKey(nextClosestNode))
                {
                    GraphEdge nearestEdge = searchFrontier[nextClosestNode];
                    if (shortestPahTree.ContainsKey(nextClosestNode))
                    {
                        shortestPahTree[nextClosestNode] = nearestEdge;
                    }
                    else
                    {
                        shortestPahTree.Add(nextClosestNode, nearestEdge);
                    }
                }

                if (nextClosestNode == target)
                {
                    ConstructPath(source, target);
                    return(true);
                }

                List <GraphEdge> frontier = graph.GetFrontier(nextClosestNode);
                for (int i = 0; i < frontier.Count; i++)
                {
                    GraphEdge        frontierEdge = frontier[i];
                    IPathfindingNode neighbour    = frontierEdge.to;

                    float realCost      = nextClosestNode.RealNodeCost + frontierEdge.cost;
                    float heuristicCost = hc.Calculate(neighbour, target);
                    float totalCost     = nextClosestNode.TotalNodeCost + frontierEdge.cost;

                    if (searchFrontier.ContainsKey(neighbour) == false)
                    {
                        neighbour.TotalNodeCost = totalCost;
                        neighbour.RealNodeCost  = realCost;
                        searchFrontier.Add(neighbour, frontierEdge);
                        pq.Add(neighbour);
                    }
                    else if (realCost < neighbour.RealNodeCost)
                    {
                        neighbour.TotalNodeCost = totalCost;
                        neighbour.RealNodeCost  = realCost;
                        pq.MarkToSort();
                        searchFrontier[neighbour] = frontierEdge;
                    }
                }
            }
            return(false);
        }
예제 #2
0
        private void ConstructPath(IPathfindingNode source, IPathfindingNode target)
        {
            GraphEdge edge = shortestPahTree[target];

            while (edge.from != source)
            {
                shortestPath.Add(edge.from);
                edge = shortestPahTree[edge.from];
            }

            shortestPath.Reverse();
        }
예제 #3
0
 private GraphEdge FindEdge(IPathfindingNode from, IPathfindingNode to)
 {
     for (int i = 0; i < edges.Count; i++)
     {
         GraphEdge e = edges[i];
         if (e.from == from && e.to == to)
         {
             return(e);
         }
     }
     return(null);
 }
예제 #4
0
        public bool AddEdge(GraphEdge e)
        {
            if (edges.Contains(e))
            {
                return(false);
            }

            edges.Add(e);
            AddToFrontier(e);

            if (directed == false)
            {
                GraphEdge inverted = new GraphEdge(e.to, e.from, e.cost);
                edges.Add(inverted);
                AddToFrontier(inverted);
            }

            return(true);
        }
예제 #5
0
 public bool RemoveEdge(GraphEdge e)
 {
     if (edges.Remove(e))
     {
         List <GraphEdge> frontier = nodeFrontiers[e.from];
         frontier.Remove(e);
         if (directed == false)
         {
             GraphEdge inverted = FindEdge(e.to, e.from);
             if (inverted != null)
             {
                 edges.Remove(inverted);
                 frontier = nodeFrontiers[inverted.from];
                 frontier.Remove(inverted);
             }
         }
         return(true);
     }
     return(false);
 }