Exemple #1
0
        public void TestFindingDirectionsAStarSearchLimaLandmarksHeuristic()
        {
            var filename = @"../../../../datasets/Lima/Lima.osm.pbf";

            var(graph, nodes) = OpenStreetMapDataHelper.ExtractMapGraph(filename);
            var    nodeLocator = OpenStreetMapDataHelper.MakeNodeLocator(nodes);
            double latFrom     = -12.073457334109781;
            double lonFrom     = -77.16832519640246;
            double latTo       = -12.045889755060621;
            double lonTo       = -77.04266126523356;
            var    problem     = OpenStreetMapDataHelper.BuildMapProblem(graph, nodeLocator, latFrom, lonFrom, latTo, lonTo);
            var    goal        = problem.GetGoalState();
            // TODO: find a good way to choose landmarks
            var landmarks = new List <FindingDirectionsState>();

            landmarks.Add(nodeLocator.FindNearestNeighbor(new FindingDirectionsState(0, null, -11.9997645637738, -77.08525616778775)));
            landmarks.Add(nodeLocator.FindNearestNeighbor(new FindingDirectionsState(0, null, -12.0730474312380, -77.16825444354458)));
            //landmarks.Add(nodeLocator.FindNearestNeighbor(new FindingDirectionsState(0, null, -12.1900507635347, -76.97454096014161)));
            landmarks.Add(nodeLocator.FindNearestNeighbor(new FindingDirectionsState(0, null, -12.0160677109035, -76.87767227591303)));
            output.WriteLine("Landmarks selected: " + landmarks.Count);
            foreach (var landmark in landmarks)
            {
                output.WriteLine("    " + landmark.ToString());
            }
            var heuristic = HeuristicsHelper.MakeLandmarksHeuristic(goal, graph, landmarks);
            var solver    = new AStarSearchSolver <FindingDirectionsState>(heuristic);

            // real solution Final State (293 moves, 8.98118622895219 cost)
            // test solution Final State (361 moves, 10.7577504383769 cost)
            TestSolver(solver, problem, 8.98118622895219);
        }
Exemple #2
0
 public void TestBuildGraphNewYork()
 {
     var lima = OpenStreetMapDataHelper.ExtractMapProblem(
         NewYorkFilename, -12.073457334109781, -77.16832519640246,
         -12.045889755060621, -77.04266126523356
         );
 }
Exemple #3
0
        public void TestReadLimaPeru()
        {
            var osmData = OpenStreetMapDataHelper.ViewAllFileData(LimaFilename);
            int i       = 0;

            foreach (var item in osmData)
            {
                output.WriteLine(item);
                i++;
                if (i >= 10)
                {
                    break;
                }
            }
        }
Exemple #4
0
        public static void InitializeMapData(string city_name)
        {
            Log.WriteLine("Loading map data: " + city_name);
            var filename = Path.Combine($@"../datasets/{city_name}/{city_name}.osm.pbf");

            //var (graph, nodes) = OpenStreetMapDataHelper.ExtractMapGraph(filename);
            var(graph, segments, nodes) = OpenStreetMapDataHelper.ExtractIntersectionMapGraph(filename);
            MapController.mapGraph      = graph;
            MapController.segmentsGraph = segments;
            MapController.locations     = OpenStreetMapDataHelper.MakeNodeLocator(nodes);
            Log.WriteLine("Calculating heuristic");
            //MapController.double_heuristic = (from, goal) => DistanceHelper.Haversine(from.Latitude, from.Longitude, goal.Latitude, goal.Longitude);
            var random = new Random();

            // TODO: find a better way to choose landmarks
            MapController.landmarks        = nodes.OrderBy(m => random.Next()).Take(4).ToArray();
            MapController.double_heuristic = HeuristicsHelper.MakeLandmarksHeuristicDouble(mapGraph, MapController.landmarks);
            MapController.nodeCount        = nodes.Count;
            Log.WriteLine("Calculating bounding box");
            foreach (var node in nodes)
            {
                if (bbNorthWest == null)
                {
                    bbNorthWest = new LatLng {
                        lat = node.Latitude, lng = node.Longitude
                    }
                }
                ;
                bbNorthWest.lat = Math.Min(bbNorthWest.lat, node.Latitude);
                bbNorthWest.lng = Math.Min(bbNorthWest.lng, node.Longitude);
                if (bbSouthEast == null)
                {
                    bbSouthEast = new LatLng {
                        lat = node.Latitude, lng = node.Longitude
                    }
                }
                ;
                bbSouthEast.lat = Math.Max(bbSouthEast.lat, node.Latitude);
                bbSouthEast.lng = Math.Max(bbSouthEast.lng, node.Longitude);
            }
            Log.WriteLine("Map data loaded: " + city_name);
            Log.WriteLine("    Total map nodes: " + nodes.Count);
        }
Exemple #5
0
        public ActionResult <object> GetDirections(double latFrom, double lonFrom, double latTo, double lonTo)
        {
            var timer   = Stopwatch.StartNew();
            var problem = OpenStreetMapDataHelper.BuildMapProblem(mapGraph, locations, latFrom, lonFrom, latTo, lonTo);
            var goal    = problem.GetGoalState();
            Func <FindingDirectionsState, double> heuristic = from => double_heuristic(from, goal);
            var solver          = new AStarSearchSolver <FindingDirectionsState>(heuristic);
            var locationFinding = timer.Elapsed.TotalMilliseconds;

            timer = Stopwatch.StartNew();
            var           solution = solver.Solve(problem).Cast <FindingDirectionsState>().ToArray();
            List <LatLng> steps    = null;

            if (MapController.segmentsGraph == null)
            {
                steps = new List <LatLng>();
                for (int i = 1; i < solution.Length; i++)
                {
                    var parts = MapController.segmentsGraph.GetEdgeWeight(solution[i - 1], solution[i]);
                    steps.AddRange(parts.Select(m => new LatLng {
                        lat = m.Latitude, lng = m.Longitude
                    }));
                }
            }
            else
            {
                steps = solution
                        .Select(m => new LatLng {
                    lat = m.Latitude, lng = m.Longitude
                })
                        .ToList();
            }
            var itemsTime = timer.Elapsed.TotalMilliseconds;

            return(new {
                points = steps,
                problemDefineTime = locationFinding,
                directionsFindTime = itemsTime,
                statesEvaluated = solver.StatesEvaluated,
            });
        }
Exemple #6
0
        public void TestFindingDirectionsAStarSearchLimaStraightLineDistanceHeuristic()
        {
            var filename = @"../../../../datasets/Lima/Lima.osm.pbf";

            // var problem = OpenStreetMapDataHelper.ExtractMapProblem(
            //         filename,
            //         -12.073457334109781, -77.16832519640246,
            //         -12.045889755060621,-77.04266126523356
            //         // -12.041839850701912,-77.06330992167841,
            //         // -12.060138589193466,-77.01043821757685
            //     );
            var(graph, nodes) = OpenStreetMapDataHelper.ExtractMapGraph(filename);
            var    nodeLocator = OpenStreetMapDataHelper.MakeNodeLocator(nodes);
            double latFrom     = -12.073457334109781;
            double lonFrom     = -77.16832519640246;
            double latTo       = -12.045889755060621;
            double lonTo       = -77.04266126523356;
            var    problem     = OpenStreetMapDataHelper.BuildMapProblem(graph, nodeLocator, latFrom, lonFrom, latTo, lonTo);
            var    goal        = problem.GetGoalState();
            var    heuristic   = HeuristicsHelper.MakeStraightLineDistanceHeuristic(goal);
            var    solver      = new AStarSearchSolver <FindingDirectionsState>(heuristic);

            TestSolver(solver, problem, 8.98118622895219);
        }