Esempio n. 1
0
        public Search_AStar(SparseGraph <TNode, TEdge> Graph, IHeuristic <SparseGraph <TNode, TEdge> > Heuristic, int Source, int Target)
        {
            this.Graph     = Graph;
            this.bFound    = false;
            this.Heuristic = Heuristic;

            SourceNodeIndex = Source;
            TargetNodeIndex = Target;

            int ActiveNodeCount = Graph.ActiveNodeCount();
            int NodeCount       = Graph.NodeCount();

            ShortestPathTree = new List <TEdge>(NodeCount);
            SearchFrontier   = new List <TEdge>(NodeCount);
            CostToThisNode   = new List <double>(NodeCount);
            GCosts           = new List <double>(NodeCount);
            FCosts           = new List <double>(NodeCount);

            // not sure i need to initialize these...nt);
            for (int i = 0; i < NodeCount; i++)
            {
                ShortestPathTree.Insert(i, null);
                SearchFrontier.Insert(i, null);
                CostToThisNode.Insert(i, 0);
                FCosts.Insert(i, 0);
                GCosts.Insert(i, 0);
            }

            TimeSlicedQ = new IndexedPriorityQueueLow <double>(FCosts, Graph.NodeCount());
            TimeSlicedQ.Insert(SourceNodeIndex);
        }
Esempio n. 2
0
        public ESearchStatus CycleOnce()
        {
            if (SourceNodeIndex > Graph.NodeCount())
            {
                return(ESearchStatus.TargetNotFound);
            }

            if (TimeSlicedQ.IsEmpty())
            {
                return(ESearchStatus.TargetNotFound);
            }

            int NextClosestNode = TimeSlicedQ.Pop();

            ShortestPathTree[NextClosestNode] = SearchFrontier[NextClosestNode];

            if (NextClosestNode == TargetNodeIndex)
            {
                return(ESearchStatus.TargetFound);
            }

            foreach (var Edge in Graph.Edges[NextClosestNode])
            {
                double HCost = Heuristic.Calculate(Graph, TargetNodeIndex, Edge.ToNodeIndex);
                double GCost = GCosts[NextClosestNode] + Edge.EdgeCost;

                if (SearchFrontier[Edge.ToNodeIndex] == null)
                {
                    FCosts[Edge.ToNodeIndex] = GCost + HCost;
                    GCosts[Edge.ToNodeIndex] = GCost;
                    TimeSlicedQ.Insert(Edge.ToNodeIndex);
                    SearchFrontier[Edge.ToNodeIndex] = Edge;
                }
                else if (GCost < GCosts[Edge.ToNodeIndex] &&
                         (ShortestPathTree[Edge.ToNodeIndex] == null))
                {
                    FCosts[Edge.ToNodeIndex] = GCost + HCost;
                    GCosts[Edge.ToNodeIndex] = GCost;
                    TimeSlicedQ.ChangePriority(Edge.ToNodeIndex);
                    SearchFrontier[Edge.ToNodeIndex] = Edge;
                }
            }

            return(ESearchStatus.SearchIncomplete);
        }
Esempio n. 3
0
        public GraphSearchDFS(SparseGraph <GraphNode, GraphEdge> Graph, int Source, int Target)
        {
            this.Graph           = Graph;
            this.bFound          = false;
            this.SourceNodeIndex = Source;
            this.TargetNodeIndex = Target;

            SpanningTree = new List <GraphEdge>();

            VisitedNodes = new List <int>();
            for (int i = 0; i < Graph.NodeCount(); i++)
            {
                VisitedNodes.Insert(i, (int)NodeStatus.Unvisited);
            }

            Route = new List <int>(Graph.NodeCount());
            for (int i = 0; i < Graph.NodeCount(); i++)
            {
                Route.Insert(i, (int)NodeStatus.NoParentAssigned);
            }
        }
Esempio n. 4
0
        public bool Search()
        {
            var Q = new IndexedPriorityQueueLow <double>(CostToThisNode, Graph.NodeCount());

            Q.Insert(SourceNodeIndex);

            while (!Q.IsEmpty())
            {
                int NextClosestNode = Q.Pop();

                ShortestPathTree[NextClosestNode] = SearchFrontier[NextClosestNode];

                if (NextClosestNode == TargetNodeIndex)
                {
                    bFound = true;
                    return(true);
                }

                foreach (var Edge in Graph.Edges[NextClosestNode])
                {
                    double NewCost = CostToThisNode[NextClosestNode] + Edge.EdgeCost;

                    if (SearchFrontier[Edge.ToNodeIndex] == null)
                    {
                        CostToThisNode[Edge.ToNodeIndex] = NewCost;
                        Q.Insert(Edge.ToNodeIndex);
                        SearchFrontier[Edge.ToNodeIndex] = Edge;
                    }
                    else if ((NewCost < CostToThisNode[Edge.ToNodeIndex]) &&
                             (ShortestPathTree[Edge.ToNodeIndex] == null))
                    {
                        CostToThisNode[Edge.ToNodeIndex] = NewCost;
                        Q.ChangePriority(Edge.ToNodeIndex);
                        SearchFrontier[Edge.ToNodeIndex] = Edge;
                    }
                }
            }

            return(false);
        }
Esempio n. 5
0
        public GraphSearchDijkstra(SparseGraph <GraphNode, GraphEdge> Graph, int Source, int Target)
        {
            this.Graph  = Graph;
            this.bFound = false;

            SourceNodeIndex = Source;
            TargetNodeIndex = Target;

            int ActiveNodeCount = Graph.ActiveNodeCount();
            int NodeCount       = Graph.NodeCount();

            ShortestPathTree = new List <GraphEdge>(NodeCount);
            SearchFrontier   = new List <GraphEdge>(NodeCount);
            CostToThisNode   = new List <double>(ActiveNodeCount);

            for (int i = 0; i < NodeCount; i++)
            {
                ShortestPathTree.Insert(i, null);
                SearchFrontier.Insert(i, null);
                CostToThisNode.Insert(i, 0);
            }
        }