コード例 #1
0
        public ActionResult SearchRoute(Shippment model)
        {
            var shippment = new Shippment();
            var Model     = new Shippment();

            var categories = DbHelper.GetAllCategoriesFromDb();

            Model.Categories = GetCategoryListItems(categories);

            var cities = DbHelper.GetAllCities();

            Model.CitiesFrom = GetCityListItems(cities);
            Model.CitiesTo   = GetCityListItems(cities);

            if (ModelState.IsValid)
            {
                shippment.Category = model.Category;
                shippment.Weight   = model.Weight;
                shippment.CityFrom = model.CityFrom;
                shippment.CityTo   = model.CityTo;

                CalculatedRoute calculatedRoute      = null;
                CalculatedRoute calculatedRouteCheap = null;
                try
                {
                    calculatedRoute = RouteCalculator.Calculate(model.Category, model.Weight,
                                                                DbHelper.GetCityByName(model.CityFrom), DbHelper.GetCityByName(model.CityTo), true);

                    calculatedRouteCheap = RouteCalculator.Calculate(model.Category, model.Weight,
                                                                     DbHelper.GetCityByName(model.CityFrom), DbHelper.GetCityByName(model.CityTo), false);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                shippment.searchedSections      = calculatedRoute;
                shippment.searchedSectionsCheap = calculatedRouteCheap;

                shippment.Categories = GetCategoryListItems(categories);
                shippment.CitiesFrom = GetCityListItems(cities);
                shippment.CitiesTo   = GetCityListItems(cities);
                if (shippment.searchedSections != null && shippment.searchedSections.Route != null)
                {
                    for (int i = 0; i < shippment.searchedSections.Route.Count; i++)
                    {
                        TempData["price" + i]    = shippment.searchedSections.Route[i].Price.ToString();
                        TempData["duration" + i] = shippment.searchedSections.Route[i].Duration.ToString();
                        TempData["from" + i]     = shippment.searchedSections.Route[i].From.Name;
                        TempData["to" + i]       = shippment.searchedSections.Route[i].To.Name;
                    }
                    TempData["price"]    = shippment.searchedSections.Price;
                    TempData["duration"] = shippment.searchedSections.Duration;
                }

                return(View("index", shippment));
            }

            return(View("index", Model));
        }
コード例 #2
0
        public static List <CalculatedRoute> calculateKRoutes(string origin, string destination, List <string> cities, List <CachedSection> cachedSections, bool fastest, int k)
        {
            var graphWeightTuple = CreateBidirectionalGraph(cities, cachedSections, fastest);

            var pathAlgorithm = new HoffmanPavleyRankedShortestPathAlgorithm <string, TaggedEdge <string> >(graphWeightTuple.Item1, e => graphWeightTuple.Item2[e]);

            pathAlgorithm.ShortestPathCount = k;
            pathAlgorithm.Compute(origin, destination);

            var result = new List <CalculatedRoute>();

            foreach (IEnumerable <TaggedEdge <string> > path in pathAlgorithm.ComputedShortestPaths)
            {
                var route = new CalculatedRoute();
                route.Route = new List <CachedSection>();
                decimal totalPrice    = 0;
                float   totalDuration = 0;
                foreach (TaggedEdge <string> section in path)
                {
                    var cachedSection = new CachedSection();
                    cachedSection.Price    = section.Price;
                    totalPrice            += section.Price;
                    cachedSection.Duration = section.Duration;
                    totalDuration         += section.Duration;

                    cachedSection.Provider = section.Provider;
                    cachedSection.Category = section.Category;
                    cachedSection.Weight   = section.Weight;
                    cachedSection.From     = new City(section.Source);
                    cachedSection.To       = new City(section.Target);

                    route.Route.Add(cachedSection);
                }
                route.Price    = totalPrice;
                route.Duration = totalDuration;

                result.Add(route);
            }
            return(result);
        }