DefaultWeightHandler

Namespace: Itinero
We found 10 examples in language CSharp for this search. You will see 36 fragments of code.

        /// <summary>
        /// Returns the default weight handler.
        /// </summary>
        public static DefaultWeightHandler DefaultWeightHandler(this IProfileInstance profile, RouterBase router)
        {
            return router.GetDefaultWeightHandler(profile);
        }


        /// <summary>
        /// Returns the default weight handler.
        /// </summary>
        public static DefaultWeightHandler DefaultWeightHandler(this IProfileInstance profile, RouterBase router)
        {
            return router.GetDefaultWeightHandler(profile);
        }

        /// <summary>
        /// Calculates the weight for the given edge and distance.
        /// </summary>
        public static T Calculate<T>(this WeightHandler<T> handler, ushort edgeProfile, float distance)
            where T : struct
        {
            Factor factor;
            return handler.Calculate(edgeProfile, distance, out factor);
        }

        /// <summary>
        /// Returns true if weigh1 > weight2 according to the weight handler.
        /// </summary>
        public static bool IsLargerThan<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) > handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 > weight2 according to the weight handler.
        /// </summary>
        public static bool IsLargerThanOrEqual<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) >= handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 smaller than weight2 according to the weight handler.
        /// </summary>
        public static bool IsSmallerThan<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) < handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 smaller than weight2 according to the weight handler.
        /// </summary>
        public static bool IsSmallerThanOrEqual<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) <= handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 smaller than metric according to the weight handler.
        /// </summary>
        public static bool IsSmallerThan<T>(this WeightHandler<T> handler, T weight1, float metric)
            where T : struct
        {
            return handler.GetMetric(weight1) < metric;
        }

        /// <summary>
        /// Tests routing a graph with one edge.
        /// </summary>
        [Test]
        public void TestOneEdge()
        {
            var weightHandler = new DefaultWeightHandler(null);
            var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size,
                weightHandler.MetaSize);
            graph.AddEdge(0, 1, 100, true, Constants.NO_VERTEX);

            var dykstra = new Itinero.Algorithms.Contracted.Dual.Dykstra<float>(graph, weightHandler, 0, false, float.MaxValue);
            dykstra.WasFound += (p, v, w) =>
            { 
                 if (v == 0)
                 {
                     Assert.AreEqual(0, w);
                 }
                 else if(v == 1)
                 {
                     Assert.AreEqual(100, w);
                 }

                 return false;
            };
            dykstra.Run();

            Assert.AreEqual(true, dykstra.HasRun);
            Assert.AreEqual(true, dykstra.HasSucceeded);
        }

        /// <summary>
        /// Tests routing a graph with two edges.
        /// </summary>
        [Test]
        public void TestOneHopTwoEdges()
        {
            var weightHandler = new DefaultWeightHandler(null);
            var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size,
                weightHandler.MetaSize);
            graph.AddEdge(0, 1, 100, true, Constants.NO_VERTEX);
            graph.AddEdge(0, 2, 200, true, Constants.NO_VERTEX);

            var dykstra = new Itinero.Algorithms.Contracted.Dual.Dykstra<float>(graph, weightHandler, 0, false, float.MaxValue);
            dykstra.WasFound += (p, v, w) =>
            {
                if (v == 0)
                {
                    Assert.AreEqual(0, w);
                }
                else if (v == 1)
                {
                    Assert.AreEqual(100, w);
                }
                else if(v == 2)
                {
                    Assert.AreEqual(200, w);
                }

                return false;
            };
            dykstra.Run();

            Assert.AreEqual(true, dykstra.HasRun);
            Assert.AreEqual(true, dykstra.HasSucceeded);
        }

        /// <summary>
        /// Tests routing a graph with two edges.
        /// </summary>
        [Test]
        public void TestTwoHopTwoEdges()
        {
            var weightHandler = new DefaultWeightHandler(null);
            var graph = new DirectedMetaGraph(ContractedEdgeDataSerializer.Size,
                weightHandler.MetaSize);
            graph.AddEdge(0, 1, 100, true, Constants.NO_VERTEX);
            graph.AddEdge(1, 2, 200, true, Constants.NO_VERTEX);

            var dykstra = new Itinero.Algorithms.Contracted.Dual.Dykstra<float>(graph, weightHandler, 0, false, float.MaxValue);
            dykstra.WasFound += (p, v, w) =>
            {
                if (v == 0)
                {
                    Assert.AreEqual(0, w);
                }
                else if (v == 1)
                {
                    Assert.AreEqual(100, w);
                }
                else if (v == 2)
                {
                    Assert.AreEqual(300, w);
                }

                return false;
            };
            dykstra.Run();

            Assert.AreEqual(true, dykstra.HasRun);
            Assert.AreEqual(true, dykstra.HasSucceeded);
        }


        /// <summary>
        /// Executes the actual run of the algorithm.
        /// </summary>
        protected override void DoRun(CancellationToken cancellationToken)
        {
            // search sources.
            _sourceSearches = new OneToMany<T>[_sources.Length];
            for (var i = 0; i < _sources.Length; i++)
            {
                _sourceSearches[i] = new OneToMany<T>(_routerDb, _weightHandler, _getRestrictions, _sources[i], _targets, _maxSearch);
                _sourceSearches[i].Run(cancellationToken);
            }

            this.HasSucceeded = true;
        }

        /// <summary>
        /// Gets the best weight for the source/target at the given index.
        /// </summary>
        /// <returns></returns>
        public T GetBestWeight(int source, int target)
        {
            this.CheckHasRunAndHasSucceeded();

            var path = _sourceSearches[source].GetPath(target);
            if (path != null)
            {
                return path.Weight;
            }
            return _weightHandler.Infinite;
        }

        /// <summary>
        /// Gets the path from source to target.
        /// </summary>
        public EdgePath<T> GetPath(int source, int target)
        {
            this.CheckHasRunAndHasSucceeded();

            var path = _sourceSearches[source].GetPath(target);
            if (path != null)
            {
                return path;
            }
            return null;
        }


        /// <summary>
        /// Executes the actual run of the algorithm.
        /// </summary>
        protected override void DoRun(CancellationToken cancellationToken)
        {
            // search sources.
            _sourceSearches = new OneToMany<T>[_sources.Length];
            for (var i = 0; i < _sources.Length; i++)
            {
                _sourceSearches[i] = new OneToMany<T>(_routerDb, _weightHandler, _sources[i], _targets, _maxSearch);
                _sourceSearches[i].Run(cancellationToken);
            }

            this.HasSucceeded = true;
        }

        /// <summary>
        /// Gets the best weight for the source/target at the given index.
        /// </summary>
        /// <returns></returns>
        public T GetBestWeight(int source, int target)
        {
            this.CheckHasRunAndHasSucceeded();

            var path = _sourceSearches[source].GetPath(target);
            if(path != null)
            {
                return path.Weight;
            }
            return _weightHandler.Infinite;
        }

        /// <summary>
        /// Gets the path from source to target.
        /// </summary>
        public EdgePath<T> GetPath(int source, int target)
        {
            this.CheckHasRunAndHasSucceeded();

            var path = _sourceSearches[source].GetPath(target);
            if (path != null)
            {
                return path;
            }
            return null;
        }

        /// <summary>
        /// Tries to get the path from source to target.
        /// </summary>
        public bool TryGetPath(int source, int target, out EdgePath<T> path)
        {
            this.CheckHasRunAndHasSucceeded();

            return _sourceSearches[source].TryGetPath(target, out path);
        }

        
        /// <summary>
        /// Executes the algorithm.
        /// </summary>
        protected sealed override void DoRun(CancellationToken cancellationToken)
        {
            // run mass resolver if needed.
            if (!_massResolver.HasRun)
            {
                _massResolver.Run(cancellationToken);
            }

            // create error and resolved point management data structures.
            _correctedResolvedPoints = _massResolver.RouterPoints;
            _errors = new Dictionary<int, RouterPointError>(_correctedResolvedPoints.Count);
            _correctedIndices = new List<int>(_correctedResolvedPoints.Count);
            for (var i = 0; i < _correctedResolvedPoints.Count; i++)
            {
                _correctedIndices.Add(i);
            }
            
            // calculate matrix.
            var nonNullInvalids = new HashSet<int>();
            var locations = _correctedResolvedPoints.ToArray();
            var weightsResult = _router.TryCalculateWeight(_profile, _weightHandler, locations, locations, 
                nonNullInvalids, nonNullInvalids, _settings);
            _weights = weightsResult.Value;

            // take into account the non-null invalids now.
            if (nonNullInvalids.Count > 0)
            { // shrink lists and add errors.
                foreach (var invalid in nonNullInvalids)
                {
                    _errors[invalid] = new RouterPointError()
                    {
                        Code = RouterPointErrorCode.NotRoutable,
                        Message = "Location could not routed to or from."
                    };
                }

                _correctedResolvedPoints = _correctedResolvedPoints.ShrinkAndCopyList(nonNullInvalids);
                _correctedIndices = _correctedIndices.ShrinkAndCopyList(nonNullInvalids);
                _weights = _weights.SchrinkAndCopyMatrix(nonNullInvalids);
            }
            this.HasSucceeded = true;
        }
        
        /// <summary>
        /// Returns the corrected index, or the index in the weight matrix for the given routerpoint index.
        /// </summary>
        /// <param name="resolvedIdx">The index of the resolved point.</param>
        /// <returns>The index in the weight matrix, -1 if this point is in error.</returns>
        public int CorrectedIndexOf(int resolvedIdx)
        {
            this.CheckHasRunAndHasSucceeded();

            return _correctedIndices.IndexOf(resolvedIdx);
        }
        
        /// <summary>
        /// Returns the router point index that represents the given weight in the weight matrix.
        /// </summary>
        /// <param name="weightIdx">The index in the weight matrix.</param>
        /// <returns>The router point index, always exists and always returns a proper value.</returns>
        public int OriginalIndexOf(int weightIdx)
        {
            this.CheckHasRunAndHasSucceeded();

            return _correctedIndices[weightIdx];
        }


        /// <summary>
        /// Executes the actual run of the algorithm.
        /// </summary>
        protected override void DoRun(CancellationToken cancellationToken)
        {
            // search sources.
            _sourceSearches = new OneToMany<T>[_sources.Length];
            for (var i = 0; i < _sources.Length; i++)
            {
                _sourceSearches[i] = new OneToMany<T>(_routerDb, _weightHandler, _getRestrictions, _sources[i], _targets, _maxSearch);
                _sourceSearches[i].Run(cancellationToken);
            }

            this.HasSucceeded = true;
        }

        /// <summary>
        /// Gets the best weight for the source/target at the given index.
        /// </summary>
        /// <returns></returns>
        public T GetBestWeight(int source, int target)
        {
            this.CheckHasRunAndHasSucceeded();

            var path = _sourceSearches[source].GetPath(target);
            if (path != null)
            {
                return path.Weight;
            }
            return _weightHandler.Infinite;
        }

        /// <summary>
        /// Gets the path from source to target.
        /// </summary>
        public EdgePath<T> GetPath(int source, int target)
        {
            this.CheckHasRunAndHasSucceeded();

            var path = _sourceSearches[source].GetPath(target);
            if (path != null)
            {
                return path;
            }
            return null;
        }


        /// <summary>
        /// Adds weight1 and weight2.
        /// </summary>
        public override float Add(float weight1, float weight2)
        {
            return weight1 + weight2;
        }

        /// <summary>
        /// Returns true if the given weight is bigger in any of it's fields compared to the other weight.
        /// </summary>
        public override bool IsLargerThanAny(float weight, float other)
        {
            return weight > other;
        }

        /// <summary>
        /// Multiplies the given weight with the given factor.
        /// </summary>
        public override float Multiply(float weight, float factor)
        {
            return weight * factor;
        }

        /// <summary>
        /// Divides the given weight by the given divider.
        /// </summary>
        public override float Divide(float weight, float divider)
        {
            return weight / divider;
        }

        /// <summary>
        /// Subtracts weight2 from weight1.
        /// </summary>
        public override float Subtract(float weight1, float weight2)
        {
            return weight1 - weight2;
        }

        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        public override int Compare(float x, float y)
        {
            return x.CompareTo(y);
        }

        /// <summary>
        /// Calculates the weight for the given edge and distance.
        /// </summary>
        public static T Calculate<T>(this WeightHandler<T> handler, ushort edgeProfile, float distance)
            where T : struct
        {
            Factor factor;
            return handler.Calculate(edgeProfile, distance, out factor);
        }

        /// <summary>
        /// Returns true if weigh1 > weight2 according to the weight handler.
        /// </summary>
        public static bool IsLargerThan<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) > handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 > weight2 according to the weight handler.
        /// </summary>
        public static bool IsLargerThanOrEqual<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) >= handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 smaller than weight2 according to the weight handler.
        /// </summary>
        public static bool IsSmallerThan<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) < handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 smaller than weight2 according to the weight handler.
        /// </summary>
        public static bool IsSmallerThanOrEqual<T>(this WeightHandler<T> handler, T weight1, T weight2)
            where T : struct
        {
            return handler.GetMetric(weight1) <= handler.GetMetric(weight2);
        }

        /// <summary>
        /// Returns true if weigh1 smaller than metric according to the weight handler.
        /// </summary>
        public static bool IsSmallerThan<T>(this WeightHandler<T> handler, T weight1, float metric)
            where T : struct
        {
            return handler.GetMetric(weight1) < metric;
        }

Itinero.Algorithms.Weights.DefaultWeightHandler : WeightHandler

Constructors :

public DefaultWeightHandler(Func<UInt16, Factor> getFactor = )

Methods :

public Single get_Zero()
public Single get_Infinite()
public Single Add(Single weight1 = , Single weight2 = )
public Single Subtract(Single weight1 = , Single weight2 = )
public Single Calculate(UInt16 edgeProfile = , Single distance = , Factor& factor = )
public WeightAndDir<Single> CalculateWeightAndDir(UInt16 edgeProfile = , Single distance = )
public WeightAndDir<Single> CalculateWeightAndDir(UInt16 edgeProfile = , Single distance = , Boolean& accessible = )
public Single Add(Single weight = , UInt16 edgeProfile = , Single distance = , Factor& factor = )
public Single GetMetric(Single weight = )
public Void AddEdge(DirectedMetaGraph graph = , UInt32 vertex1 = , UInt32 vertex2 = , UInt32 contractedId = , Nullable<Boolean> direction = , Single weight = )
public Void AddOrUpdateEdge(DirectedMetaGraph graph = , UInt32 vertex1 = , UInt32 vertex2 = , UInt32 contractedId = , Nullable<Boolean> direction = , Single weight = )
public Void AddOrUpdateEdge(DirectedDynamicGraph graph = , UInt32 vertex1 = , UInt32 vertex2 = , UInt32 contractedId = , Nullable<Boolean> direction = , Single weight = , UInt32[] s1 = , UInt32[] s2 = )
public Void AddEdge(DirectedDynamicGraph graph = , UInt32 vertex1 = , UInt32 vertex2 = , Nullable<Boolean> direction = , Single weight = )
public Single GetEdgeWeight(MetaEdge edge = , Nullable direction = )
public WeightAndDir<Single> GetEdgeWeight(MetaEdge edge = )
public UInt32 AddPathTree(PathTree tree = , UInt32 vertex = , Single weight = , UInt32 previous = )
public Void GetPathTree(PathTree tree = , UInt32 pointer = , UInt32& vertex = , Single& weight = , UInt32& previous = )
public Single GetEdgeWeight(EdgeEnumerator edge = , Nullable direction = )
public WeightAndDir<Single> GetEdgeWeight(EdgeEnumerator edge = )
public Single GetEdgeWeight(DynamicEdge edge = , Nullable direction = )
public Single GetEdgeWeight(EdgeEnumerator edge = , Nullable direction = )
public Boolean CanUse(ContractedDb db = )
public Int32 get_DynamicSize()
public Int32 get_MetaSize()
public Boolean IsSmallerThanAny(Single weight = , Single max = )
public WeightAndDir<Single> GetEdgeWeight(EdgeEnumerator edge = )
public WeightAndDir<Single> GetEdgeWeight(Edge edge = )
public WeightAndDir<Single> GetEdgeWeight(RoutingEdge edge = )
public UInt32 AddPathTree(PathTree tree = , UInt32 vertex = , WeightAndDir<Single> weight = , UInt32 previous = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods