Esempio n. 1
0
        public bool CreateCustomRoute(CustomRouteRequest route, out IList <RouteResponse> result)
        {
            var routes = _context.GradedRoutesWithIncludedRelatedData();
            var places = FindPlacesFromRoutes(routes);

            PlaceResponse start = route.Start;
            PlaceResponse end   = route.End;

            double distanceFromStart = FindClosestPlace(start, places, out Miejsce closestToStart);
            double distanceFromEnd   = FindClosestPlace(end, places, out Miejsce closestToEnd);

            if (distanceFromStart <= MAXIMUM_DISTANCE && distanceFromEnd <= MAXIMUM_DISTANCE)
            {
                var mountainGroup     = FindMountainGroup(closestToStart);
                var mountainGroupName = _context.GrupaGorska.First(m => m.Id == mountainGroup).Nazwa;
                routes = routes.Where(r => r.GrupaGorskaId == mountainGroup).ToList();
                places = FindPlacesFromRoutes(routes);

                //create graph from locations in a mountain group
                var graph = new BidirectionalGraph <PlaceVertex, Edge <PlaceVertex> >();
                var edgeCostDictionary = new Dictionary <Edge <PlaceVertex>, int>();
                PopulateGraph(graph, edgeCostDictionary, places, routes);

                //find path between start location and end location using A* Algorithm
                AStarShortestPathAlgorithm <PlaceVertex, Edge <PlaceVertex> > astar = new AStarShortestPathAlgorithm <PlaceVertex, Edge <PlaceVertex> >(graph, x => edgeCostDictionary[x], x => 0);
                var startVertex = new PlaceVertex(closestToStart.Id);
                var endVertex   = new PlaceVertex(closestToEnd.Id);

                if (astar.ComputeDistanceBetween(startVertex, endVertex, out var path))
                {
                    result = new List <RouteResponse>();
                    if (distanceFromStart >= MINIMUM_DISTANCE)
                    {
                        result.Add(RouteResponse.CreateCustomRoute(start, PlaceResponse.BuildFromModel(closestToStart), mountainGroupName, distanceFromStart));
                    }
                    result = result.Concat(CreateResponseListFromPath(path)).ToList();
                    if (distanceFromEnd >= MINIMUM_DISTANCE)
                    {
                        result.Add(RouteResponse.CreateCustomRoute(PlaceResponse.BuildFromModel(closestToEnd), end, mountainGroupName, distanceFromEnd));
                    }
                    return(true);
                }
                else
                {
                    result = new List <RouteResponse>();
                    return(false);
                }
            }
            else
            {
                result = new List <RouteResponse>();
                return(false);
            }
        }
Esempio n. 2
0
 private void PopulateGraph(BidirectionalGraph <PlaceVertex, Edge <PlaceVertex> > graph, Dictionary <Edge <PlaceVertex>, int> edgeCostDictionary, IList <Miejsce> places, IList <OdcinekPunktowany> routes)
 {
     foreach (var place in places)
     {
         var vertex = new PlaceVertex(place.Id);
         graph.AddVertex(vertex);
     }
     foreach (var route in routes)
     {
         var start = new PlaceVertex(route.PoczatekId, route.Id);
         var end   = new PlaceVertex(route.KoniecId, route.Id);
         Edge <PlaceVertex> edge = new Edge <PlaceVertex>(start, end);
         graph.AddEdge(edge);
         edgeCostDictionary.Add(edge, route.Punkty);
     }
 }