コード例 #1
0
 public BaseGraphSearchAlgo(SparseGraph graph, int source, int target)
 {
     m_Graph   = graph;
     m_iSource = source;
     m_iTarget = target;
     m_bFound  = false;
 }
コード例 #2
0
        //--------------------------- CreateSimpleGrid --------------------------
        //
        //  creates a graph based on a grid layout. This function requires the
        //  dimensions of the environment and the number of cells required horizontally
        //  and vertically
        //-----------------------------------------------------------------------------
        public static void Helper_CreateGrid(SparseGraph graph,
                                             int cySize,
                                             int cxSize,
                                             int NumCellsY,
                                             int NumCellsX)
        {
            //need some temporaries to help calculate each node center
            double CellWidth  = (double)cySize / (double)NumCellsX;
            double CellHeight = (double)cxSize / (double)NumCellsY;

            double midX = CellWidth / 2;
            double midY = CellHeight / 2;

            //first create all the nodes
            for (int row = 0; row < NumCellsY; ++row)
            {
                for (int col = 0; col < NumCellsX; ++col)
                {
                    graph.AddNode(new NavGraphNode(graph.GetNextFreeNodeIndex(),
                                                   new Vector2D(midX + (col * CellWidth),
                                                                midY + (row * CellHeight))));
                }
            }
            //now to calculate the edges. (A position in a 2d array [x][y] is the
            //same as [y*NumCellsX + x] in a 1d array). Each cell has up to eight
            //neighbours.
            for (int row = 0; row < NumCellsY; ++row)
            {
                for (int col = 0; col < NumCellsX; ++col)
                {
                    Helper_AddAllNeighboursToGridNode(graph, row, col, NumCellsX, NumCellsY);
                }
            }
        }
コード例 #3
0
            public EdgeIterator(SparseGraph graph, int node)
            {
                mGraph     = graph;
                mNodeIndex = node;

                enumeratorPosition = -1;
            }
コード例 #4
0
        //------------ Helper_AddAllNeighboursToGridNode ------------------
        //
        //  use to add the eight neighboring edges of a graph node that
        //  are positioned in a grid layout
        //------------------------------------------------------------------------
        public static void Helper_AddAllNeighboursToGridNode(SparseGraph graph,
                                                             int row,
                                                             int col,
                                                             int NumCellsX,
                                                             int NumCellsY)
        {
            for (int i = -1; i < 2; ++i)
            {
                for (int j = -1; j < 2; ++j)
                {
                    int nodeX = col + j;
                    int nodeY = row + i;

                    //skip if equal to this node
                    if ((i == 0) && (j == 0))
                    {
                        continue;
                    }

                    //check to see if this is a valid neighbour
                    if (ValidNeighbour(nodeX, nodeY, NumCellsX, NumCellsY))
                    {
                        //calculate the distance to this node
                        Vector2D PosNode      = graph.GetNode(row * NumCellsX + col).Pos;
                        Vector2D PosNeighbour = graph.GetNode(nodeY * NumCellsX + nodeX).Pos;

                        double dist = PosNode.Distance(PosNeighbour);

                        NavGraphEdge NewEdge;

                        //this neighbour is okay so it can be added
                        NewEdge = new NavGraphEdge(row * NumCellsX + col,
                                                   nodeY * NumCellsX + nodeX,
                                                   dist);
                        graph.AddEdge(NewEdge);

                        //if graph is not a diagraph then an edge needs to be added going
                        //in the other direction
                        if (!graph.isDigraph())
                        {
                            NewEdge = new NavGraphEdge(nodeY * NumCellsX + nodeX,
                                                       row * NumCellsX + col,
                                                       dist);
                            graph.AddEdge(NewEdge);
                        }
                    }
                }
            }
        }
コード例 #5
0
        public Graph_SearchDijkstra(SparseGraph graph, int source, int target)
            : base(graph, source, target)
        {
            int numNodes = m_Graph.NumNodes();

            m_ShortestPathTree = new List <NavGraphEdge>(numNodes);
            m_SearchFrontier   = new List <NavGraphEdge>(numNodes);
            m_CostToThisNode   = new List <double>(numNodes);

            for (int i = 0; i < numNodes; i++)
            {
                m_ShortestPathTree.Add(null);
                m_SearchFrontier.Add(null);
                m_CostToThisNode.Add(0);
            }

            m_bFound = Search();
        }
コード例 #6
0
        public Graph_SearchDFS(SparseGraph graph, int source, int target)
            : base(graph, source, target)
        {
            int numNodes = m_Graph.NumNodes();

            m_Visited = new List <int>(numNodes);
            m_Route   = new List <int>(numNodes);

            for (int i = 0; i < numNodes; i++)
            {
                m_Visited.Add((int)NodeState.unvisited);
                m_Route.Add((int)NodeState.no_parent_assigned);
            }

            m_SpanningTree = new List <NavGraphEdge>();

            m_bFound = Search();
        }
コード例 #7
0
        //  this searchs a graph using the distance between the target node and the
        //  currently considered node as a heuristic.
        //  This search is more commonly known as A* (pronounced Ay-Star)
        public Graph_SearchAStar(SparseGraph graph, int source, int target, CalculateHeuristic functionCalculate)
            : base(graph, source, target)
        {
            funcPointer = functionCalculate;

            int numNodes = m_Graph.NumNodes();

            m_ShortestPathTree = new List <NavGraphEdge>(numNodes);
            m_SearchFrontier   = new List <NavGraphEdge>(numNodes);
            m_GCosts           = new List <double>(numNodes);
            m_FCosts           = new List <double>(numNodes);

            for (int i = 0; i < numNodes; i++)
            {
                m_ShortestPathTree.Add(null);
                m_SearchFrontier.Add(null);
                m_GCosts.Add(0.0);
                m_FCosts.Add(0.0);
            }

            m_bFound = Search();
        }
コード例 #8
0
        public static void Helper_WeightNavGraphNodeEdges(SparseGraph graph, int node, double weight)
        {
            //make sure the node is present
            Debug.Assert(node < graph.NumNodes(), "Node can not exist in graph!");

            //set the cost for each edge
            SparseGraph.EdgeIterator EdgeItr = new SparseGraph.EdgeIterator(graph, node);

            while (EdgeItr.MoveNext())
            {
                //calculate the distance between nodes
                double dist = Vector2D.Vec2DDistance(graph.GetNode(EdgeItr.Current.From).Pos,
                                                     graph.GetNode(EdgeItr.Current.To).Pos);

                //set the cost of this edge
                graph.SetEdgeCost(EdgeItr.Current.From, EdgeItr.Current.To, dist * weight);

                //if not a digraph, set the cost of the parallel edge to be the same
                if (!graph.isDigraph())
                {
                    graph.SetEdgeCost(EdgeItr.Current.To, EdgeItr.Current.From, dist * weight);
                }
            }
        }
コード例 #9
0
            public NodeIterator(SparseGraph graph)
            {
                mGraph = graph;

                enumeratorPosition = -1;
            }