Esempio n. 1
0
        /// <summary>
        /// Adds a contracted graph in the old (broken) way, to test backwards compat. with previous itinero versions.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="profile"></param>
        public static void AddContractedOldEdgeBased(this RouterDb db, Profile profile)
        {
            var weightHandler = profile.DefaultWeightHandlerCached(db);

            var contracted           = new DirectedDynamicGraph(weightHandler.DynamicSize);
            var directedGraphBuilder = new Itinero.Algorithms.Contracted.EdgeBased.DirectedGraphBuilder <float>(db.Network.GeometricGraph.Graph, contracted,
                                                                                                                weightHandler);

            directedGraphBuilder.Run();

            // contract the graph.
            var priorityCalculator = new Itinero.Algorithms.Contracted.EdgeBased.EdgeDifferencePriorityCalculator <float>(contracted, weightHandler,
                                                                                                                          new Itinero.Algorithms.Contracted.EdgeBased.Witness.DykstraWitnessCalculator <float>(weightHandler, 4, 64));

            priorityCalculator.DifferenceFactor = 5;
            priorityCalculator.DepthFactor      = 5;
            priorityCalculator.ContractedFactor = 8;
            var hierarchyBuilder = new Itinero.Algorithms.Contracted.EdgeBased.HierarchyBuilder <float>(contracted, priorityCalculator,
                                                                                                        new Itinero.Algorithms.Contracted.EdgeBased.Witness.DykstraWitnessCalculator <float>(weightHandler, int.MaxValue, 64), weightHandler, db.GetGetRestrictions(profile, null));

            hierarchyBuilder.Run();

            var contractedDb = new ContractedDb(contracted);

            db.AddContracted(profile, contractedDb);
        }
Esempio n. 2
0
        /// <summary>
        /// Calculates a many-to-many weight matrix using a dual edge-based graph.
        /// </summary>
        internal static Result <T[][]> CalculateManyToMany <T>(this ContractedDb contractedDb, RouterDb routerDb, Profile profile, WeightHandler <T> weightHandler,
                                                               RouterPoint[] sources, RouterPoint[] targets, T max, CancellationToken cancellationToken) where T : struct
        {
            if (!(contractedDb.HasNodeBasedGraph &&
                  contractedDb.NodeBasedIsEdgedBased))
            {
                throw new ArgumentOutOfRangeException("No dual edge-based graph was found!");
            }

            var dykstraSources = new DykstraSource <T> [sources.Length];

            for (var i = 0; i < sources.Length; i++)
            {
                dykstraSources[i] = sources[i].ToDualDykstraSource(routerDb, weightHandler, true);
            }

            var dykstraTargets = new DykstraSource <T> [targets.Length];

            for (var i = 0; i < targets.Length; i++)
            {
                dykstraTargets[i] = targets[i].ToDualDykstraSource(routerDb, weightHandler, true);
            }

            // calculate weights.
            var algorithm = new ManyToMany.VertexToVertexWeightAlgorithm <T>(contractedDb.NodeBasedGraph, weightHandler, dykstraSources, dykstraTargets, weightHandler.Infinite);

            algorithm.Run(cancellationToken);

            // extract the best weight for each edge pair.
            return(new Result <T[][]>(algorithm.Weights));
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new contracted graph and adds it to the router db for the given profile.
        /// </summary>
        public static void AddEdgeBasedContractedForTesting <T>(this RouterDb db, Profile profile, WeightHandler <T> weightHandler)
            where T : struct
        {
            // create the raw directed graph.
            ContractedDb contractedDb = null;

            var contracted           = new DirectedDynamicGraph(weightHandler.DynamicSize);
            var directedGraphBuilder = new Itinero.Algorithms.Contracted.EdgeBased.DirectedGraphBuilder <T>(db.Network.GeometricGraph.Graph, contracted,
                                                                                                            weightHandler);

            directedGraphBuilder.Run();

            // contract the graph.
            var priorityCalculator = new Itinero.Algorithms.Contracted.EdgeBased.EdgeDifferencePriorityCalculator <T>(contracted, weightHandler,
                                                                                                                      new Itinero.Algorithms.Contracted.EdgeBased.Witness.DykstraWitnessCalculator <T>(weightHandler, 4, 64));

            priorityCalculator.DifferenceFactor = 5;
            priorityCalculator.DepthFactor      = 5;
            priorityCalculator.ContractedFactor = 8;
            var hierarchyBuilder = new Itinero.Algorithms.Contracted.EdgeBased.HierarchyBuilder <T>(contracted, priorityCalculator,
                                                                                                    new Itinero.Algorithms.Contracted.EdgeBased.Witness.DykstraWitnessCalculator <T>(weightHandler, int.MaxValue, 64), weightHandler, db.GetGetRestrictions(profile, null));

            hierarchyBuilder.Run();

            contractedDb = new ContractedDb(contracted);

            // add the graph.
            db.AddContracted(profile, contractedDb);
        }
Esempio n. 4
0
 /// <summary>
 /// Checks if the given graph can be used with the weight handler.
 /// </summary>
 public static void CheckCanUse <T>(this WeightHandler <T> weightHandler, ContractedDb contractedDb)
     where T : struct
 {
     if (!weightHandler.CanUse(contractedDb))
     {
         throw new ArgumentException("Cannot used the given graph and the weight handler together. The data layouts don't match.");
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Creates a new contracted graph and adds it to the router db for the given profile.
        /// </summary>
        public static void AddContracted <T>(this RouterDb db, Profiles.Profile profile, WeightHandler <T> weightHandler, bool forceEdgeBased = false)
            where T : struct
        {
            // create the raw directed graph.
            ContractedDb contractedDb = null;

            lock (db)
            {
                if (forceEdgeBased)
                { // edge-based is needed when complex restrictions found.
                    var contracted           = new DirectedDynamicGraph(weightHandler.DynamicSize);
                    var directedGraphBuilder = new Itinero.Algorithms.Contracted.EdgeBased.DirectedGraphBuilder <T>(db.Network.GeometricGraph.Graph, contracted,
                                                                                                                    weightHandler);
                    directedGraphBuilder.Run();

                    // contract the graph.
                    var priorityCalculator = new Itinero.Algorithms.Contracted.EdgeBased.EdgeDifferencePriorityCalculator <T>(contracted, weightHandler,
                                                                                                                              new Itinero.Algorithms.Contracted.EdgeBased.Witness.DykstraWitnessCalculator <T>(weightHandler, int.MaxValue));
                    priorityCalculator.DifferenceFactor = 5;
                    priorityCalculator.DepthFactor      = 5;
                    priorityCalculator.ContractedFactor = 8;
                    var hierarchyBuilder = new Itinero.Algorithms.Contracted.EdgeBased.HierarchyBuilder <T>(contracted, priorityCalculator,
                                                                                                            new Itinero.Algorithms.Contracted.EdgeBased.Witness.DykstraWitnessCalculator <T>(weightHandler, int.MaxValue), weightHandler, db.GetGetRestrictions(profile, null));
                    hierarchyBuilder.Run();

                    contractedDb = new ContractedDb(contracted);
                }
                else
                { // vertex-based is ok when no complex restrictions found.
                    var contracted           = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size, weightHandler.MetaSize);
                    var directedGraphBuilder = new DirectedGraphBuilder <T>(db.Network.GeometricGraph.Graph, contracted, weightHandler);
                    directedGraphBuilder.Run();

                    // contract the graph.
                    var priorityCalculator = new EdgeDifferencePriorityCalculator(contracted,
                                                                                  new DykstraWitnessCalculator(int.MaxValue));
                    priorityCalculator.DifferenceFactor = 5;
                    priorityCalculator.DepthFactor      = 5;
                    priorityCalculator.ContractedFactor = 8;
                    var hierarchyBuilder = new HierarchyBuilder <T>(contracted, priorityCalculator,
                                                                    new DykstraWitnessCalculator(int.MaxValue), weightHandler);
                    hierarchyBuilder.Run();

                    contractedDb = new ContractedDb(contracted);
                }
            }

            // add the graph.
            lock (db)
            {
                db.AddContracted(profile, contractedDb);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Deserializes a database from the given stream.
        /// </summary>
        public static RouterDb Deserialize(Stream stream, RouterDbProfile profile)
        {
            // deserialize all basic data.
            // version1: OsmSharp.Routing state of layout.
            // version2: Added ShortcutsDbs.
            var version = stream.ReadByte();

            if (version != 1 && version != 2)
            {
                throw new Exception(string.Format("Cannot deserialize routing db: Invalid version #: {0}.", version));
            }

            var guidBytes = new byte[16];

            stream.Read(guidBytes, 0, 16);
            var guid = new Guid(guidBytes);

            var supportedProfiles = stream.ReadWithSizeStringArray();
            var metaDb            = stream.ReadWithSizeAttributesCollection();
            var shorcutsCount     = (int)0;

            if (version == 2)
            { // when version < 1 there are no shortcuts and thus no shortcut count.
                shorcutsCount = stream.ReadByte();
            }
            var contractedCount = stream.ReadByte();
            var profiles        = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var meta            = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var network         = RoutingNetwork.Deserialize(stream, profile == null ? null : profile.RoutingNetworkProfile);

            // create router db.
            var routerDb = new RouterDb(guid, network, profiles, meta, metaDb, supportedProfiles);

            // read all shortcut dbs.
            for (var i = 0; i < shorcutsCount; i++)
            {
                var shortcutsName = stream.ReadWithSizeString();
                var shorcutsDb    = ShortcutsDb.Deserialize(stream);
                routerDb._shortcutsDbs[shortcutsName] = shorcutsDb;
            }

            // read all contracted versions.
            for (var i = 0; i < contractedCount; i++)
            {
                var profileName = stream.ReadWithSizeString();
                var contracted  = ContractedDb.Deserialize(stream, profile == null ? null : profile.ContractedDbProfile);
                routerDb._contracted[profileName] = contracted;
            }
            return(routerDb);
        }
Esempio n. 7
0
        /// <summary>
        /// Reads a contracted graph from the given stream and adds it to this db.
        /// </summary>
        public void DeserializeAndAddContracted(Stream stream, ContractedDbProfile profile)
        {
            // first read and compare guids.
            var guidBytes = new byte[16];

            stream.Read(guidBytes, 0, 16);
            var guid = new Guid(guidBytes);

            if (guid != this.Guid)
            {
                throw new Exception("Cannot add this contracted graph, guid's do not match.");
            }
            var profileName = stream.ReadWithSizeString();
            var contracted  = ContractedDb.Deserialize(stream, profile);

            _contracted[profileName] = contracted;
        }
Esempio n. 8
0
 public override bool CanUse(ContractedDb db)
 {
     return(true);
 }
Esempio n. 9
0
 /// <summary>
 /// Returns true if the given contracted db can be used.
 /// </summary>
 public abstract bool CanUse(ContractedDb db);
Esempio n. 10
0
 /// <summary>
 /// Returns true if the given contracted db can be used.
 /// </summary>
 public sealed override bool CanUse(ContractedDb db)
 { // any current layout can be used with default weights.
     return(true);
 }
Esempio n. 11
0
        /// <summary>
        /// Deserializes a database from the given stream.
        /// </summary>
        public static RouterDb Deserialize(Stream stream, RouterDbProfile profile)
        {
            // deserialize all basic data.
            // version1: OsmSharp.Routing state of layout.
            // version2: Added ShortcutsDbs.
            // version3: Add advanced profile serialization.
            // version4: Added missing restriction dbs.
            var version = stream.ReadByte();

            if (version != 1 && version != 2 && version != 3 && version != 4)
            {
                throw new Exception(string.Format("Cannot deserialize routing db: Invalid version #: {0}.", version));
            }

            var guidBytes = new byte[16];

            stream.Read(guidBytes, 0, 16);
            var guid = new Guid(guidBytes);

            var supportedVehicleInstances = new List <Vehicle>();

            if (version <= 2)
            { // just contains vehicle names.
                var supportedVehicles = stream.ReadWithSizeStringArray();
                foreach (var vehicleName in supportedVehicles)
                {
                    Profile vehicleProfile;
                    if (Profile.TryGet(vehicleName, out vehicleProfile))
                    {
                        supportedVehicleInstances.Add(vehicleProfile.Parent);
                    }
                    else
                    {
                        Itinero.Logging.Logger.Log("RouterDb", Logging.TraceEventType.Warning, "Vehicle with name {0} was not found, register all vehicle profiles before deserializing the router db.",
                                                   vehicleName);
                    }
                }
            }
            else
            { // contains the full vehicles.
                var lengthBytes = new byte[4];
                stream.Read(lengthBytes, 0, 4);
                var size = BitConverter.ToInt32(lengthBytes, 0);
                for (var i = 0; i < size; i++)
                {
                    var vehicle = Vehicle.Deserialize(stream);
                    supportedVehicleInstances.Add(vehicle);
                    vehicle.Register();
                }
            }

            var metaDb        = stream.ReadWithSizeAttributesCollection();
            var shorcutsCount = (int)0;

            if (version >= 2)
            { // when version < 1 there are no shortcuts and thus no shortcut count.
                shorcutsCount = stream.ReadByte();
            }
            var contractedCount = stream.ReadByte();

            var restrictionDbCount = 0;

            if (version >= 4)
            {
                restrictionDbCount = stream.ReadByte();
            }

            var profiles = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var meta     = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var network  = RoutingNetwork.Deserialize(stream, profile == null ? null : profile.RoutingNetworkProfile);

            // create router db.
            var routerDb = new RouterDb(guid, network, profiles, meta, metaDb, supportedVehicleInstances.ToArray());

            // read all shortcut dbs.
            for (var i = 0; i < shorcutsCount; i++)
            {
                var shortcutsName = stream.ReadWithSizeString();
                var shorcutsDb    = ShortcutsDb.Deserialize(stream);
                routerDb._shortcutsDbs[shortcutsName] = shorcutsDb;
            }

            // read all contracted versions.
            for (var i = 0; i < contractedCount; i++)
            {
                var profileName = stream.ReadWithSizeString();
                var contracted  = ContractedDb.Deserialize(stream, profile == null ? null : profile.ContractedDbProfile);
                routerDb._contracted[profileName] = contracted;
            }

            // read all restriction dbs.
            for (var i = 0; i < restrictionDbCount; i++)
            {
                var restrictionDbName = stream.ReadWithSizeString();
                var restrictionDb     = RestrictionsDb.Deserialize(stream, profile == null ? null : profile.RestrictionDbProfile);
                routerDb._restrictionDbs[restrictionDbName] = restrictionDb;
            }

            return(routerDb);
        }
Esempio n. 12
0
 /// <summary>
 /// Tries to get a contracted version of the routing network for the given profile.
 /// </summary>
 public bool TryGetContracted(Profiles.Profile profile, out ContractedDb contracted)
 {
     return(_contracted.TryGetValue(profile.FullName, out contracted));
 }
Esempio n. 13
0
 /// <summary>
 /// Adds a contracted version of the routing network for the given profile.
 /// </summary>
 public void AddContracted(Profiles.Profile profile, ContractedDb contracted)
 {
     _contracted[profile.FullName] = contracted;
 }