Example #1
0
        /// <summary>
        /// Converts a simple VRP solution into a solution containing the actual routes.
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="solution"></param>
        /// <param name="points"></param>
        protected Route[] ConvertSolution(Vehicle vehicle, int[][] solution, RouterPoint[] points)
        {
            var routes = new Route[solution.Length];

            for (int routeIdx = 0; routeIdx < solution.Length; routeIdx++)
            {
                // concatenate the route(s).
                Route tsp = null;
                Route route;
                for (int idx = 0; idx < solution[routeIdx].Length - 1; idx++)
                {
                    route = _router.Calculate(Vehicle.Car, points[solution[routeIdx][idx]],
                                              points[solution[routeIdx][idx + 1]]);
                    if (route != null && route.Segments.Length > 0)
                    {
                        if (tsp == null)
                        { // first route = start
                            tsp = route;
                        }
                        else
                        { // concatenate.
                            tsp = Route.Concatenate(tsp, route);
                        }
                    }
                }

                // concatenate the route from the last to the first point again.
                route = _router.Calculate(vehicle, points[solution[routeIdx][solution[routeIdx].Length - 1]],
                                          points[solution[routeIdx][0]]);
                if (route.Segments.Length > 0)
                {
                    tsp = Route.Concatenate(tsp, route);
                }

                // set the result.
                routes[routeIdx] = tsp;

                if (routes[routeIdx] != null)
                { // route exists!
                    var tags          = new List <RouteTags>();
                    var customerCount = new RouteTags();
                    customerCount.Key   = "customer_count";
                    customerCount.Value = solution[routeIdx].Length.ToString();
                    tags.Add(customerCount);
                    routes[routeIdx].Tags = tags.ToArray();

                    // set the correct vehicle type.
                    routes[routeIdx].Vehicle = vehicle.UniqueName;
                }
            }

            return(routes);
        }
Example #2
0
        internal static void Save(Stream stream, Route route)
        {
            GpxDocument gpxDocument = new GpxDocument((IXmlSource) new XmlStreamSource(stream));
            gpxType     gpxType     = new gpxType();

            gpxType.trk = new trkType[1];
            List <wptType> wptTypeList1 = new List <wptType>();
            trkType        trkType      = new trkType();
            List <wptType> wptTypeList2 = new List <wptType>();

            trkType.trkseg = new trksegType[1];
            trksegType trksegType = new trksegType();

            for (int index1 = 0; index1 < route.Segments.Count; ++index1)
            {
                RouteSegment segment = route.Segments[index1];
                if (segment.Points != null)
                {
                    for (int index2 = 0; index2 < segment.Points.Length; ++index2)
                    {
                        RouteStop point     = segment.Points[index2];
                        RouteTags routeTags = point.Tags == null ? (RouteTags)null : ((IEnumerable <RouteTags>)point.Tags).FirstOrDefault <RouteTags>((Func <RouteTags, bool>)(x => x.Value == "name"));
                        wptTypeList2.Add(new wptType()
                        {
                            lat  = (Decimal)point.Latitude,
                            lon  = (Decimal)point.Longitude,
                            name = routeTags == null ? string.Empty : routeTags.Value
                        });
                    }
                }
                wptTypeList1.Add(new wptType()
                {
                    lat = (Decimal)segment.Latitude,
                    lon = (Decimal)segment.Longitude
                });
            }
            trksegType.trkpt  = wptTypeList1.ToArray();
            trkType.trkseg[0] = trksegType;
            gpxType.trk[0]    = trkType;
            gpxType.wpt       = wptTypeList2.ToArray();
            gpxDocument.Gpx   = (object)gpxType;
            gpxDocument.Save();
        }