Пример #1
0
        public void TestFindingDirectionsBestFirstSearchRomania()
        {
            var solver  = new AStarSearchSolver <FindingDirectionsState>(_ => default(double));
            var problem = GetRomaniaProblem();

            TestSolver(solver, problem, 418);
        }
Пример #2
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);
        }
Пример #3
0
        public void TestNPuzzleAStarSearchHammingDistance()
        {
            var solver  = new AStarSearchSolver <NPuzzle>(NPuzzle.HammingDistance);
            var problem = new NPuzzleProblem(3, 3, "8 6 7 2 5 4 3 0 1");

            TestSolver(solver, problem, 27);
            // cannot really solve larger problems with this method
        }
Пример #4
0
        public void TestNPuzzleAStarSearchManhattanDistance()
        {
            var solver = new AStarSearchSolver <NPuzzle>(NPuzzle.ManhattanDistance);
            var problem = new NPuzzleProblem(3, 3, "8 6 7 2 5 4 3 0 1"); int expected = 27;

            //var problem = new NPuzzleProblem(3, 4, "3 7 9 11 4 8 10 0 5 2 1 6"); int expected = 37;
            //var problem = new NPuzzleProblem(4, 4, "14, 1, 9, 6, 4, 8, 12, 5, 7, 2, 3, 0, 10, 11, 13, 15"); int expected = 45;
            //var problem = new NPuzzleProblem(4, 4, "4, 3, 6, 13, 7, 15, 9, 0, 10, 5, 8, 11, 2, 12, 1, 14"); int expected = 50;
            TestSolver(solver, problem, expected);
        }
Пример #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,
            });
        }
Пример #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);
        }