Example #1
0
        /// <summary>
        /// Calculates TSP.
        /// </summary>
        public static Result <Route> TryCalculateTSP(this RouterBase router, Profile profile, Coordinate[] locations, int first = 0, int?last = null)
        {
            var tspRouter = new TSPRouter(new WeightMatrixAlgorithm(router, profile, locations), first, last);

            tspRouter.Run();
            if (!tspRouter.HasSucceeded)
            {
                return(new Result <Route>(tspRouter.ErrorMessage));
            }
            return(new Result <Route>(tspRouter.WeightMatrix.BuildRoute(tspRouter.Tour)));
        }
Example #2
0
        /// <summary>
        /// Calculates a router along the given coordinates.
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="coordinates"></param>
        /// <param name="complete"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public override Route GetRoute(Vehicle vehicle, GeoCoordinate[] coordinates, bool complete, bool sort)
        {
            Route route;

            if (sort)
            {
                var routerTsp = new TSPRouter(_router, vehicle, coordinates, 0, 0, new EAXSolver(new OsmSharp.Logistics.Solvers.GA.GASettings()
                {
                    CrossOverPercentage = 10,
                    ElitismPercentage   = 2,
                    PopulationSize      = 300,
                    MaxGenerations      = 100000,
                    MutationPercentage  = 0,
                    StagnationCount     = 100
                }));
                routerTsp.Run();

                if (!routerTsp.HasSucceeded)
                { // tsp-calculation has not succeeded.
                    throw new Exception(routerTsp.ErrorMessage);
                }

                // build the resulting routes.
                route = routerTsp.BuildRoute();
            }
            else
            { // concatenate routes.
                // resolve all points.
                var resolved = _router.Resolve(vehicle, 0.0075f, coordinates);

                // add id's to each resolved point.
                for (int idx = 0; idx < resolved.Length; idx++)
                {
                    if (resolved[idx] != null)
                    {
                        if (resolved[idx].Tags == null)
                        {
                            resolved[idx].Tags = new List <KeyValuePair <string, string> >();
                        }
                        resolved[idx].Tags.Add(new KeyValuePair <string, string>("index", idx.ToInvariantString()));
                    }
                }

                route = this.BuildRoute(vehicle, resolved, coordinates);
            }

            // TODO: implement complete boolean.
            return(route);
        }
        /// <summary>
        /// Calculates a route along the given locations.
        /// </summary>
        public Result <Route> Calculate(Profile profile, Coordinate[] locations, AttributeCollection[] attributes, bool?closed,
                                        Dictionary <string, object> parameters)
        {
            // create matrix calculator.
            var matrixCalculator = new WeightMatrixAlgorithm(
                _router, profile, locations, (edge, i) =>
            {
                if (attributes.Length > 0)
                {
                    var edgeTags = _router.Db.GetProfileAndMeta(
                        edge.Data.Profile, edge.Data.MetaId);
                    var locationTags = attributes[i];
                    string locationName;
                    string edgeName;
                    if (edgeTags.TryGetValue("name", out edgeName) &&
                        locationTags.TryGetValue("name", out locationName))
                    {
                        if (!string.IsNullOrWhiteSpace(edgeName) &&
                            !string.IsNullOrWhiteSpace(locationName))
                        {
                            if (edgeName.LevenshteinMatch(locationName, 90))
                            {
                                return(true);
                            }
                        }
                    }
                    return(false);
                }
                return(true);
            });

            matrixCalculator.SearchDistanceInMeter = 200;
            matrixCalculator.Run();

            // check success.
            if (!matrixCalculator.HasSucceeded)
            { // weight matrix calculation failed.
                return(new Result <Route>("Calculating weight matrix failed."));
            }

            // verify minimum results.
            if (matrixCalculator.RouterPoints.Count < 1 ||
                matrixCalculator.Errors.ContainsKey(0))
            {
                return(new Result <Route>("Calculating weight matrix failed."));
            }

            // create TSP router.
            int?last = locations.Length - 1;

            if (closed.HasValue && closed.Value)
            { // problem is closed.
                last = 0;
            }
            else if (closed.HasValue && !closed.Value)
            { // problem is open.
                last = null;
            }
            var tspRouter = new TSPRouter(_router, profile, locations, 0, last,
                                          new EAXSolver(new Itinero.Logistics.Solvers.GA.GASettings()
            {
                CrossOverPercentage = 10,
                ElitismPercentage   = 2,
                PopulationSize      = 300,
                MaxGenerations      = 100000,
                MutationPercentage  = 0,
                StagnationCount     = 100
            }), matrixCalculator);

            tspRouter.Run();

            // check success.
            if (!tspRouter.HasSucceeded)
            { // weight matrix calculation failed.
                return(new Result <Route>("Calculating final route failed."));
            }

            return(new Result <Route>(tspRouter.BuildRoute()));
        }
        /// <summary>
        /// Calculates a router along the given coordinates.
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="coordinates"></param>
        /// <param name="complete"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public override Route GetRoute(Vehicle vehicle, GeoCoordinate[] coordinates, bool complete, bool sort)
        {
            Route route;
            if (sort)
            {
                var routerTsp = new TSPRouter(_router, vehicle, coordinates, 0, 0, new EAXSolver(new OsmSharp.Logistics.Solvers.GA.GASettings()
                {
                    CrossOverPercentage = 10,
                    ElitismPercentage = 2,
                    PopulationSize = 300,
                    MaxGenerations = 100000,
                    MutationPercentage = 0,
                    StagnationCount = 100
                }));
                routerTsp.Run();

                if (!routerTsp.HasSucceeded)
                { // tsp-calculation has not succeeded.
                    throw new Exception(routerTsp.ErrorMessage);
                }

                // build the resulting routes.
                route = routerTsp.BuildRoute();
            }
            else
            { // concatenate routes.
                // resolve all points.
                var resolved = _router.Resolve(vehicle, 0.0075f, coordinates);

                // add id's to each resolved point.
                for (int idx = 0; idx < resolved.Length; idx++)
                {
                    if (resolved[idx] != null)
                    {
                        if (resolved[idx].Tags == null)
                        {
                            resolved[idx].Tags = new List<KeyValuePair<string, string>>();
                        }
                        resolved[idx].Tags.Add(new KeyValuePair<string, string>("index", idx.ToInvariantString()));
                    }
                }

                route = this.BuildRoute(vehicle, resolved, coordinates);
            }

            // TODO: implement complete boolean.
            return route;
        }