Пример #1
0
        public List <RouteResult> SearchRoute(City source, City end, double price, ParcelCategory category)
        {
            var citiesByName = context.City.Where(x => x.IsActive).ToList().ToDictionary(x => x.Name);
            var routes       = GetAllRoutes();
            var extraCharge  = category.ExtraCharge;
            var graph        = BuildGraph(routes);
            var result       = new List <RouteResult>();

            double WeightByTime(TaggedEdge <string, string> edge)
            {
                return(8);
            }

            IEnumerable <IEnumerable <TaggedEdge <string, string> > > shortestPath = null;

            try
            {
                shortestPath = graph.RankedShortestPathHoffmanPavley(WeightByTime, source.Name, end.Name, 4);
            }
            catch
            {
                return(result);
            }


            foreach (var shortPath in shortestPath)
            {
                var    parts       = new List <SegmentResult>();
                var    route       = new RouteResult();
                double time        = 0.0;
                double returnPrice = 0.0;
                foreach (var edge in shortPath)
                {
                    parts.Add(new SegmentResult
                    {
                        Departure = new Data.DataContracts.City.CityDTO {
                            Name = edge.Source.ToString()
                        },
                        Destination = new Data.DataContracts.City.CityDTO {
                            Name = edge.Target.ToString()
                        },
                        EstimatedDuration     = 8,
                        TransportationCompany = TransportationCompany.OA,
                        Price      = (Decimal)price,
                        TotalPrice = (Decimal)(price * (1 + extraCharge / 100)),
                        ExtraFee   = (Decimal)(price * extraCharge / 100),
                    });
                    time        += WeightByTime(edge);
                    returnPrice += price;
                }
                route.Segments      = parts;
                route.TotalDuration = time;
                route.TotalPrice    = returnPrice * (1 + extraCharge / 100);
                route.Departure     = new CityDTO {
                    Name = source.Name
                };
                route.Destination = new CityDTO {
                    Name = end.Name
                };
                route.GetDistinctTransportationCompany();

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