public void Test1TwoPoints()
        {
            // build test case.
            var router    = new RouterMock();
            var locations = new Coordinate[] {
                new Coordinate(0, 0),
                new Coordinate(1, 1)
            };
            var matrixAlgorithm = new WeightMatrixAlgorithm <float>(router, Vehicle.Car.Fastest(), new DefaultWeightHandlerMock(),
                                                                    new Itinero.Algorithms.Search.MassResolvingAlgorithm(router, new Itinero.Profiles.Profile[] { Vehicle.Car.Fastest() }, locations, null));

            // run.
            matrixAlgorithm.Run();

            Assert.IsNotNull(matrixAlgorithm);
            Assert.IsTrue(matrixAlgorithm.HasRun);
            Assert.IsTrue(matrixAlgorithm.HasSucceeded);
            Assert.AreEqual(0, matrixAlgorithm.Errors.Count);
            var matrix = matrixAlgorithm.Weights;

            Assert.IsNotNull(matrix);
            Assert.AreEqual(0, matrix[0][0]);
            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(locations[0], locations[1]), matrix[0][1], 0.1);
            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(locations[1], locations[0]), matrix[1][0], 0.1);
            Assert.AreEqual(0, matrix[1][1]);
            Assert.AreEqual(2, matrixAlgorithm.RouterPoints.Count);
            Assert.AreEqual(0, matrixAlgorithm.CorrectedIndexOf(0));
            Assert.AreEqual(1, matrixAlgorithm.CorrectedIndexOf(1));
        }
        public void Test3TwoPoints()
        {
            // build test case.
            var invalidSet = new HashSet <int>();

            invalidSet.Add(1);
            var router    = new RouterMock(invalidSet);
            var locations = new Coordinate[] {
                new Coordinate(0, 0),
                new Coordinate(1, 1)
            };
            var matrixAlgorithm = new WeightMatrixAlgorithm <float>(router, Vehicle.Car.Fastest(), new DefaultWeightHandlerMock(),
                                                                    new Itinero.Algorithms.Search.MassResolvingAlgorithm(router, new Itinero.Profiles.Profile[] { Vehicle.Car.Fastest() }, locations, null));

            // run.
            matrixAlgorithm.Run();

            Assert.IsNotNull(matrixAlgorithm);
            Assert.IsTrue(matrixAlgorithm.HasRun);
            Assert.IsTrue(matrixAlgorithm.HasSucceeded);
            Assert.AreEqual(1, matrixAlgorithm.Errors.Count);
            Assert.IsTrue(matrixAlgorithm.Errors.ContainsKey(1));
            var matrix = matrixAlgorithm.Weights;

            Assert.IsNotNull(matrix);
            Assert.AreEqual(0, matrix[0][0]);
            Assert.AreEqual(1, matrixAlgorithm.RouterPoints.Count);
            Assert.AreEqual(0, matrixAlgorithm.CorrectedIndexOf(0));
            Assert.AreEqual(0, matrixAlgorithm.OriginalIndexOf(0));

            // build test case.
            invalidSet.Clear();
            invalidSet.Add(0);
            router    = new RouterMock(invalidSet);
            locations = new Coordinate[] {
                new Coordinate(0, 0),
                new Coordinate(1, 1)
            };
            matrixAlgorithm = new WeightMatrixAlgorithm <float>(router, Vehicle.Car.Fastest(), new DefaultWeightHandlerMock(),
                                                                new Itinero.Algorithms.Search.MassResolvingAlgorithm(router, new Itinero.Profiles.Profile[] { Vehicle.Car.Fastest() }, locations, null));

            // run.
            matrixAlgorithm.Run();

            Assert.IsNotNull(matrixAlgorithm);
            Assert.IsTrue(matrixAlgorithm.HasRun);
            Assert.IsTrue(matrixAlgorithm.HasSucceeded);
            Assert.AreEqual(1, matrixAlgorithm.Errors.Count);
            Assert.IsTrue(matrixAlgorithm.Errors.ContainsKey(0));
            matrix = matrixAlgorithm.Weights;
            Assert.IsNotNull(matrix);
            Assert.AreEqual(0, matrix[0][0]);
            Assert.AreEqual(1, matrixAlgorithm.RouterPoints.Count);
            Assert.AreEqual(0, matrixAlgorithm.CorrectedIndexOf(1));
            Assert.AreEqual(1, matrixAlgorithm.OriginalIndexOf(0));
        }
Esempio n. 3
0
        public void Test1TwoPointsWithMatching()
        {
            // build test case.
            var router    = new RouterMock(new AttributeCollection(new Attribute("highway", "residential")));
            var locations = new Coordinate[] {
                new Coordinate(0, 0),
                new Coordinate(1, 1)
            };
            var matrixAlgorithm = new WeightMatrixAlgorithm(router, Vehicle.Car.Fastest(), new DefaultWeightHandlerMock(), locations,
                                                            (edge, i) =>
            {
                return(true);
            });

            // run.
            matrixAlgorithm.Run();

            Assert.IsNotNull(matrixAlgorithm);
            Assert.IsTrue(matrixAlgorithm.HasRun);
            Assert.IsTrue(matrixAlgorithm.HasSucceeded);
            Assert.AreEqual(0, matrixAlgorithm.Errors.Count);
            var matrix = matrixAlgorithm.Weights;

            Assert.IsNotNull(matrix);
            Assert.AreEqual(0, matrix[0][0]);
            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(locations[0], locations[1]), matrix[0][1], 0.1);
            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(locations[1], locations[0]), matrix[1][0], 0.1);
            Assert.AreEqual(0, matrix[1][1]);
            Assert.AreEqual(2, matrixAlgorithm.RouterPoints.Count);
            Assert.AreEqual(0, matrixAlgorithm.IndexOf(0));
            Assert.AreEqual(1, matrixAlgorithm.IndexOf(1));

            // build test case.
            router    = new RouterMock(new AttributeCollection(new Attribute("highway", "primary")));
            locations = new Coordinate[] {
                new Coordinate(0, 0),
                new Coordinate(1, 1)
            };
            matrixAlgorithm = new WeightMatrixAlgorithm(router, Vehicle.Car.Fastest(), locations,
                                                        (edge, i) =>
            {
                return(false);
            });

            // run.
            matrixAlgorithm.Run();

            Assert.IsNotNull(matrixAlgorithm);
            Assert.IsTrue(matrixAlgorithm.HasRun);
            Assert.IsTrue(matrixAlgorithm.HasSucceeded);
            Assert.AreEqual(2, matrixAlgorithm.Errors.Count);
            Assert.AreEqual(LocationErrorCode.NotResolved, matrixAlgorithm.Errors[0].Code);
            Assert.AreEqual(LocationErrorCode.NotResolved, matrixAlgorithm.Errors[1].Code);
        }
Esempio n. 4
0
        public void Test2TwoPoints()
        {
            // build test case.
            var router    = new RouterMock();
            var locations = new Coordinate[] {
                new Coordinate(0, 0),
                new Coordinate(180, 1)
            };
            var matrixAlgorithm = new WeightMatrixAlgorithm(router, Vehicle.Car.Fastest(), new DefaultWeightHandlerMock(), locations, null);

            // run.
            matrixAlgorithm.Run();

            Assert.IsNotNull(matrixAlgorithm);
            Assert.IsTrue(matrixAlgorithm.HasRun);
            Assert.IsTrue(matrixAlgorithm.HasSucceeded);
            Assert.AreEqual(1, matrixAlgorithm.Errors.Count);
            Assert.IsTrue(matrixAlgorithm.Errors.ContainsKey(1));
            var matrix = matrixAlgorithm.Weights;

            Assert.IsNotNull(matrix);
            Assert.AreEqual(0, matrix[0][0]);
            Assert.AreEqual(1, matrixAlgorithm.RouterPoints.Count);
            Assert.AreEqual(0, matrixAlgorithm.IndexOf(0));
            Assert.AreEqual(0, matrixAlgorithm.LocationIndexOf(0));

            // build test case.
            locations = new Coordinate[] {
                new Coordinate(180, 0),
                new Coordinate(1, 1)
            };
            matrixAlgorithm = new WeightMatrixAlgorithm(router, Vehicle.Car.Fastest(), new DefaultWeightHandlerMock(), locations, null);

            // run.
            matrixAlgorithm.Run();

            Assert.IsNotNull(matrixAlgorithm);
            Assert.IsTrue(matrixAlgorithm.HasRun);
            Assert.IsTrue(matrixAlgorithm.HasSucceeded);
            Assert.AreEqual(1, matrixAlgorithm.Errors.Count);
            Assert.IsTrue(matrixAlgorithm.Errors.ContainsKey(0));
            matrix = matrixAlgorithm.Weights;
            Assert.IsNotNull(matrix);
            Assert.AreEqual(0, matrix[0][0]);
            Assert.AreEqual(1, matrixAlgorithm.RouterPoints.Count);
            Assert.AreEqual(0, matrixAlgorithm.IndexOf(1));
            Assert.AreEqual(1, matrixAlgorithm.LocationIndexOf(0));
        }
Esempio n. 5
0
        public static void Main(string[] args)
        {
            Download.ToFile("http://files.itinero.tech/data/itinero/routerdbs/planet/europe/belgium.c.cf.routerdb", "belgium.c.cf.routerdb").Wait();

            var routerDb = RouterDb.Deserialize(File.OpenRead("belgium.c.cf.routerdb"));
            var router   = new Router(routerDb);

            var locations = new List <Coordinate>(new Coordinate[]
            {
                new Coordinate(51.270453873703080f, 4.8008108139038080f),
                new Coordinate(51.264197451065370f, 4.8017120361328125f),
                new Coordinate(51.267446600889850f, 4.7830009460449220f),
                new Coordinate(51.260733228426076f, 4.7796106338500980f),
                new Coordinate(51.256489871317920f, 4.7884941101074220f),
                new Coordinate(51.270964016530680f, 4.7894811630249020f)
            });

            // METHOD1: quick and easy for high-quality data already on the road network.
            // calculate drive time in seconds between all given locations.
            var resolved      = router.Resolve(Vehicle.Car.Fastest(), locations.ToArray(), 150);
            var invalidPoints = new HashSet <int>();
            var matrix        = router.CalculateWeight(Vehicle.Car.Fastest(), resolved, invalidPoints);

            // METHOD2: most datasets contain large numbers of unconfirmed locations that may be too far from the road network or contain errors.
            //          this method can handle coordinates sets that contains errors.

            // let's add a location that's in the middle of nowhere.
            var invalidLocation = new Coordinate(51.275689280878694f, 4.7779369354248040f);

            locations.Insert(3, invalidLocation);

            // for advanced applications there is a helper class
            var matrixCalculator = new WeightMatrixAlgorithm(router, Vehicle.Car.Fastest(), locations.ToArray());

            matrixCalculator.Run();

            // there is some usefull output data here now.
            var weights = matrixCalculator.Weights;             // the weights, in this case seconds travel time.
            var errors  = matrixCalculator.Errors;              // some locations could be unreachable, this contains details about those locations.

            resolved = matrixCalculator.RouterPoints.ToArray(); // the resolved routerpoints, you can use these later without the need to resolve again.

            // when there are failed points, the weight matrix is smaller, use these functions to map locations from the original array to succeeded points.
            var newIndex = matrixCalculator.MassResolver.ResolvedIndexOf(4); // returns the index of the original location in the weight matrix.
            var oldIndex = matrixCalculator.MassResolver.LocationIndexOf(5); // returns the index of the weight matrix point in the original locations array.
        }
        /// <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()));
        }