Esempio n. 1
0
        public GraphSearchResult GetShortestPath(int sourceVertexID, int targetVertexID)
        {
            mSourceVertexID = mVertexDictionary[sourceVertexID];
            mTargetVertexID = mVertexDictionary[targetVertexID];

            mTries = 0;

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var vertex = GetShortestPathUsingDijkstraAlgorithm();

            stopwatch.Stop();
            var result = new GraphSearchResult(vertex, stopwatch.Elapsed, mTries);

            ResetVertices();

            return(result);
        }
Esempio n. 2
0
        private RoutingPath GetRoutingPath(ApplicationDbContext myContext, string lon1, string lat1, int level1, string lon2, string lat2, int level2)
        {
            List <GraphDataItem> dtEdges   = new List <GraphDataItem>();
            RoutingPath          totalPath = null;

            Point3D sourceToFind = new Point3D {
                Latitude = Decimal.Parse(lat1), Type = Point3D.PointType.PATH_POINT, Level = level1, Longitude = Decimal.Parse(lon1)
            };
            Point3D targetToFind = new Point3D {
                Latitude = Decimal.Parse(lat2), Type = Point3D.PointType.PATH_POINT, Level = level2, Longitude = Decimal.Parse(lon2)
            };
            Point3D source = myContext.Points.ToList().Where(x => x.Level == level1 && x.Latitude == sourceToFind.Latitude && x.Longitude == sourceToFind.Longitude).FirstOrDefault();
            Point3D target = myContext.Points.ToList().Where(x => x.Level == level2 && x.Latitude == targetToFind.Latitude && x.Longitude == targetToFind.Longitude).FirstOrDefault();

            if (source == null)
            {
                source = sourceToFind;
                source.setWktAndGeometry();
            }
            if (target == null)
            {
                target = targetToFind;
                target.setWktAndGeometry();
            }
            var pathPoints = myContext.Points.Where(x => x.Type == Point3D.PointType.PATH_POINT | x.Type == Point3D.PointType.LEVEL_CONNECTION);

            //double minToTarget = double.MaxValue;
            //Point3D closestTarget = null;
            foreach (LineStringDTO item in myContext.LineStrings.Include("Source").Include("Target").ToList())
            {
                //if (item.connectorType != ConnectorType.NONE)
                //  System.Diagnostics.Debugger.Break();
                dtEdges.Add(new GraphDataItem
                {
                    Name           = item.Name,
                    Cost           = item.Distance,
                    EdgeID         = (int)item.Id,
                    IsReverse      = item.BiDirectional,
                    ReverseCost    = item.BiDirectional ? item.Distance : double.PositiveInfinity,
                    SourceVertexID = (int)item.Source.Id,
                    TargetVertexID = (int)item.Target.Id
                });
                //edges.Add(item.toGeneric());
            }
            List <IHasEntrances> sourceContainers = new List <IHasEntrances>();
            List <IHasEntrances> targetContainers = new List <IHasEntrances>();

            //GetStoreContainersOfPoint(myContext, source, target, sourceContainers, targetContainers);

            List <POI> containersSource = poisRep.GetContainerByLocation(lon1, lat1, level1);

            foreach (var item in containersSource)
            {
                if (item is IHasEntrances)
                {
                    sourceContainers.Add((IHasEntrances)item);
                }
            }
            List <POI> containersTarget = poisRep.GetContainerByLocation(lon2, lat2, level2);

            foreach (var item in containersTarget)
            {
                if (item is IHasEntrances)
                {
                    targetContainers.Add((IHasEntrances)item);
                }
            }
            //point is inside store - need to add the edge to the entrance first
            List <GraphDataItem> edgesFromSourceToGraph = GetAdditionalEdgeOfSourceOrTargetToNearest(myContext, sourceContainers, source, pathPoints, false);

            if (edgesFromSourceToGraph == null)
            {
                return(getRoutingPathFromDataGraphItems(myContext, source, target, dtEdges, true));
            }
            else
            {
                dtEdges.AddRange(edgesFromSourceToGraph);
            }

            List <GraphDataItem> edgesFromGraphToTarget = GetAdditionalEdgeOfSourceOrTargetToNearest(myContext, targetContainers, target, pathPoints, true);

            if (edgesFromGraphToTarget == null)
            {
                return(getRoutingPathFromDataGraphItems(myContext, source, target, dtEdges, true));
            }
            else
            {
                dtEdges.AddRange(edgesFromGraphToTarget);
            }
            //dtEdges.AddRange(GetAdditionalEdgeOfSourceOrTargetToNearest(myContext, targetContainers, target, pathPoints, true));

            //LineString3D<Point3D> targetToNearestPath = new LineString3D<Point3D>
            //{
            //    Level = target.Level,
            //    Source = target,
            //    Target = closestTarget,
            //    Distance = minToTarget
            //};
            //targetToNearestPath.setWktAndLocationG();
            ////List<Point3D> fromandTo = new List<Point3D> { source, target };
            ////pathPoints.Concat(fromandTo);
            ////List<LineString3D<Point3D>> edges = new List<LineString3D<Point3D>>();

            //dtEdges.Add(new GraphDataItem { Cost = targetToNearestPath.Distance, EdgeID = (int)targetToNearestPath.Id, IsReverse = false, SourceVertexID = (int)targetToNearestPath.Source.Id, TargetVertexID = (int)targetToNearestPath.Target.Id });
            //edges.Add(sourceToNearestPath);
            //edges.Add(targetToNearestPath);
            Graph2            graph2 = new Graph2(dtEdges);
            GraphSearchResult result = graph2.GetShortestPath((int)source.Id, (int)target.Id);

            if (result == null || result.Count() == 0)
            {
                //totalPath = getRoutingPathFromDataGraphItems(myContext, source, target, dtEdges, true);
            }
            else
            {
                totalPath = getRoutingPathFromDataGraphItems(myContext, source, target, result, false);
            }

            return(totalPath);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            int nodesPerSide = 12;
            var graph        = new BidirectionalGraph <int, Edge <int> >();

            // add start
            graph.AddVertex(0);

            // add odd vertices
            for (int i = 0; i < nodesPerSide; i++)
            {
                graph.AddVertex(i * 2 + 1);
            }

            // add even vertices
            for (int i = 1; i < nodesPerSide + 1; i++)
            {
                graph.AddVertex(i * 2);
            }

            // add end
            graph.AddVertex(nodesPerSide * 2 + 1);

            // conditionally connect odd side
            for (int i = 0; i < nodesPerSide; i++)
            {
                graph.AddEdge(new Edge <int>(0, i * 2 + 1));

                for (int j = i + 1; j < nodesPerSide; j++)
                {
                    graph.AddEdge(new Edge <int>(i * 2 + 1, j * 2 + 1));
                }

                // add edge to final
                graph.AddEdge(new Edge <int>(i * 2 + 1, nodesPerSide * 2 + 1));
            }

            // conditionally connect even side
            for (int i = 1; i < nodesPerSide + 1; i++)
            {
                graph.AddEdge(new Edge <int>(0, i * 2));

                for (int j = i + 1; j < nodesPerSide + 1; j++)
                {
                    graph.AddEdge(new Edge <int>(i * 2, j * 2));
                }

                // add edge to final
                graph.AddEdge(new Edge <int>(i * 2, nodesPerSide * 2 + 1));
            }

            // save graph
            var graphSerializer = new GraphMLSerializer <int, Edge <int>, BidirectionalGraph <int, Edge <int> > >();

            using (var xmlWriter = XmlWriter.Create("graph.xml"))
            {
                graphSerializer.Serialize(xmlWriter, graph, x => x.ToString(), x => x.ToString());
            }

            // generate odd result set
            var datuh = GetForwardConnectedPresenceVectors(nodesPerSide);

            var expectedPaths = new List <List <int> >();

            foreach (var vector in datuh)
            {
                var path     = new List <int>();
                var pathEven = new List <int>();
                path.Add(0);
                pathEven.Add(0);

                var index2 = 0;
                foreach (var shouldAdd in vector)
                {
                    if (shouldAdd)
                    {
                        path.Add(index2 * 2 + 1);
                        pathEven.Add((index2 + 1) * 2);
                    }

                    index2++;
                }

                path.Add(nodesPerSide * 2 + 1);
                pathEven.Add(nodesPerSide * 2 + 1);
                expectedPaths.Add(path);
                if (pathEven.Count > 2)
                {
                    expectedPaths.Add(pathEven);
                }
            }

            var search = new GraphSearchResult()
            {
                Start     = 0,
                End       = nodesPerSide * 2 + 1,
                CodePaths = new GraphCodePathResult[expectedPaths.Count]
            };

            var index = 0;

            foreach (var path in expectedPaths)
            {
                var codePathResult = new GraphCodePathResult();
                var ids            = string.Join(",", path);
                codePathResult.Ids      = ids;
                search.CodePaths[index] = codePathResult;
                index++;
            }

            var model = new GraphResultsModel()
            {
                Searches = new GraphSearchResult[] { search }
            };

            var serializer = new XmlSerializer(typeof(GraphResultsModel));

            using (var fs = new FileStream("results.xml", FileMode.Create, FileAccess.ReadWrite))
            {
                serializer.Serialize(fs, model);
            }

            Console.ReadKey();
        }