예제 #1
0
파일: Dykstra.cs 프로젝트: amseet/Orion
        /// <summary>
        /// Initializes and resets.
        /// </summary>
        public void Initialize()
        {
            // algorithm always succeeds, it may be dealing with an empty network and there are no targets.
            this.HasSucceeded = true;

            // intialize dykstra data structures.
            _visits = new Dictionary <uint, EdgePath <T> >();
            _heap   = new BinaryHeap <EdgePath <T> >(1000);

            // queue all sources.
            foreach (var source in _sources)
            {
                if (_getRestriction != null)
                {
                    var restriction = _getRestriction(source.Vertex);
                    if (restriction != Constants.NO_VERTEX)
                    {
                        continue;
                    }
                }

                _heap.Push(source, _weightHandler.GetMetric(source.Weight));
            }

            // gets the edge enumerator.
            _edgeEnumerator = _graph.GetEdgeEnumerator();
        }
예제 #2
0
 /// <summary>
 /// Creates a new edge keeping the current state of the given enumerator.
 /// </summary>
 internal Edge(Graph.EdgeEnumerator enumerator)
 {
     this.Id           = enumerator.Id;
     this.To           = enumerator.To;
     this.From         = enumerator.From;
     this.DataInverted = enumerator.DataInverted;
     this.Data         = enumerator.Data;
 }
예제 #3
0
 public void Initialize()
 {
     this.HasSucceeded = true;
     this._factors     = new Dictionary <uint, Factor>();
     this._visits      = new Dictionary <uint, Path>();
     this._heap        = new BinaryHeap <Path>(1000U);
     foreach (Path source in this._sources)
     {
         this._heap.Push(source, source.Weight);
     }
     this._edgeEnumerator = this._graph.GetEdgeEnumerator();
 }
예제 #4
0
        protected override void DoRun()
        {
            bool?nullable = new bool?();
            Dictionary <ushort, Factor> dictionary = new Dictionary <ushort, Factor>();

            Graph.EdgeEnumerator edgeEnumerator = this._source.GetEdgeEnumerator();
            for (uint vertex = 0; vertex < this._source.VertexCount; ++vertex)
            {
                edgeEnumerator.MoveTo(vertex);
                edgeEnumerator.Reset();
                while (edgeEnumerator.MoveNext())
                {
                    float  distance;
                    ushort profile;
                    EdgeDataSerializer.Deserialize(edgeEnumerator.Data0, out distance, out profile);
                    Factor factor = Factor.NoFactor;
                    if (!dictionary.TryGetValue(profile, out factor))
                    {
                        factor = this._getFactor(profile);
                        dictionary[profile] = factor;
                    }
                    if ((double)factor.Value != 0.0)
                    {
                        bool?direction = new bool?();
                        if ((int)factor.Direction == 1)
                        {
                            direction = new bool?(true);
                            if (edgeEnumerator.DataInverted)
                            {
                                direction = new bool?(false);
                            }
                        }
                        else if ((int)factor.Direction == 2)
                        {
                            direction = new bool?(false);
                            if (edgeEnumerator.DataInverted)
                            {
                                direction = new bool?(true);
                            }
                        }
                        uint data = ContractedEdgeDataSerializer.Serialize(distance * factor.Value, direction);
                        int  num  = (int)this._target.AddEdge(edgeEnumerator.From, edgeEnumerator.To, data, 4294967294U);
                    }
                }
            }
            this.HasSucceeded = true;
        }
예제 #5
0
        /// <summary>
        /// Initializes and resets.
        /// </summary>
        public void Initialize()
        {
            // algorithm always succeeds, it may be dealing with an empty network and there are no targets.
            this.HasSucceeded = true;

            // intialize dykstra data structures.
            _visits      = new HashSet <uint>();
            _pointerHeap = new BinaryHeap <uint>();
            _pathTree    = new PathTree();

            // initialize the edge enumerator.
            _edgeEnumerator = _graph.GetEdgeEnumerator();

            // queue source.
            if (!_source.Edge1.IsNoEdge)
            {
                _pointerHeap.Push(_weightHandler.AddPathTree(_pathTree, _source.Edge1.Raw, _source.Weight1, uint.MaxValue), 0);
            }
            if (!_source.Edge2.IsNoEdge)
            {
                _pointerHeap.Push(_weightHandler.AddPathTree(_pathTree, _source.Edge2.Raw, _source.Weight2, uint.MaxValue), 0);
            }
        }
예제 #6
0
        /// <summary>
        /// Runs the island detection.
        /// </summary>
        protected override void DoRun()
        {
            _enumerator  = _routerDb.Network.GeometricGraph.Graph.GetEdgeEnumerator();
            _vertexFlags = new SparseLongIndex();
            var vertexCount = _routerDb.Network.GeometricGraph.Graph.VertexCount;

            // precalculate all edge types for the given profiles.
            _canTraverse = new HashSet <ushort>();
            for (ushort p = 0; p < _routerDb.EdgeProfiles.Count; p++)
            {
                if (this.CanTraverse(p))
                {
                    _canTraverse.Add(p);
                }
            }

            var  island = (ushort)1;
            uint lower  = 0;

            while (true)
            {
                // find the first vertex without an island assignment.
                var vertex = uint.MaxValue;
                for (uint v = lower; v < vertexCount; v++)
                {
                    if (_islands[v] == 0)
                    {
                        lower  = v;
                        vertex = v;
                        break;
                    }
                }

                if (vertex == uint.MaxValue)
                { // no more islands left.
                    break;
                }

                // expand island until no longer possible.
                var current = vertex;
                _islands[vertex] = island;
                _vertexFlags.Add(vertex);
                vertex = this.Expand(vertex, island);

                if (vertex == uint.MaxValue)
                { // expanding failed, still just the source vertex, this is an island of one.
                    _islands[current] = SINGLETON_ISLAND;
                }
                else
                {
                    while (vertex != uint.MaxValue)
                    {
                        _islands[vertex] = island;
                        _vertexFlags.Add(vertex);

                        vertex = this.Expand(vertex, island);

                        if (vertex < current)
                        {
                            current = vertex;
                        }

                        if (vertex == uint.MaxValue)
                        {
                            while (current < vertexCount)
                            {
                                if (_islands[current] == island &&
                                    !_vertexFlags.Contains(current))
                                { // part of island but has not been used to expand yet.
                                    vertex = current;
                                    break;
                                }
                                current++;
                            }
                        }
                    }

                    // island was no singleton, move to next island.
                    island++;
                }
            }
        }
예제 #7
0
 internal EdgeEnumerator(GeometricGraph graph, Graph.EdgeEnumerator enumerator)
 {
     _graph      = graph;
     _enumerator = enumerator;
 }
예제 #8
0
        /// <summary>
        /// Initializes and resets.
        /// </summary>
        public void Initialize()
        {
            // algorithm always succeeds, it may be dealing with an empty network and there are no targets.
            this.HasSucceeded = true;

            // initialize a dictionary of speeds per edge profile.
            _factors = new Dictionary <uint, Factor>();

            // intialize dykstra data structures.
            _visits           = new Dictionary <long, EdgePath <T> >();
            _heap             = new BinaryHeap <EdgePath <T> >(1000);
            _edgeRestrictions = new Dictionary <EdgePath <T>, LinkedRestriction>();

            // initialize the edge enumerator.
            _edgeEnumerator = _graph.GetEdgeEnumerator();

            // queue all sources.
            foreach (var source in _sources)
            {
                var queue = true;
                if (_getRestriction != null && source.Edge != Constants.NO_EDGE)
                {
                    var sourceVertex                    = _edgeEnumerator.GetSourceVertex(source.Edge);
                    var sourceVertexRestrictions        = _getRestriction(sourceVertex);
                    LinkedRestriction linkedRestriction = null;
                    if (sourceVertexRestrictions != null)
                    {
                        foreach (var restriction in sourceVertexRestrictions)
                        {
                            if (restriction != null &&
                                restriction.Length > 1)
                            {
                                var targetVertex = _edgeEnumerator.GetTargetVertex(source.Edge);
                                if (restriction.Length == 2)
                                {     // a restriction of two, an edge is forbidden.
                                    if (restriction[1] == targetVertex)
                                    { // don't queue this edge, it's forbidden.
                                        queue = false;
                                        break;
                                    }
                                }
                                else
                                {     // a restriction bigger than two, check if this edge is the first one.
                                    if (restriction[1] == targetVertex)
                                    { // this edge is the first, queue the restriction too.
                                        linkedRestriction = new LinkedRestriction()
                                        {
                                            Restriction = restriction.SubArray(1, restriction.Length - 1),
                                            Next        = linkedRestriction
                                        };
                                        _edgeRestrictions[source] = linkedRestriction;
                                    }
                                }
                            }
                        }
                    }
                }
                if (queue)
                {
                    _heap.Push(source, _weightHandler.GetMetric(source.Weight));
                }
            }
        }
예제 #9
0
 /// <summary>
 /// Moves the enumerator to the edge represented by the given directed edge id.
 /// </summary>
 public static void MoveToEdge(this Graph.EdgeEnumerator enumerator, DirectedEdgeId edgeId)
 {
     enumerator.MoveToEdge(edgeId.EdgeId);
 }
예제 #10
0
 public bool Step()
 {
     this._current = (Path)null;
     if (this._heap.Count > 0)
     {
         this._current = this._heap.Pop();
         while (this._current != null && this._visits.ContainsKey(this._current.Vertex) && this._heap.Count != 0)
         {
             this._current = this._heap.Pop();
         }
     }
     if (this._current == null || this._visits.ContainsKey(this._current.Vertex))
     {
         return(false);
     }
     this._visits[this._current.Vertex] = this._current;
     if (this.WasFound != null && this.WasFound(this._current.Vertex, this._current.Weight))
     {
         return(false);
     }
     this._edgeEnumerator.MoveTo(this._current.Vertex);
     while (this._edgeEnumerator.MoveNext())
     {
         Graph.EdgeEnumerator edgeEnumerator = this._edgeEnumerator;
         uint to = edgeEnumerator.To;
         if (this._current.From != null && (int)this._current.From.Vertex == (int)to)
         {
             if (this.WasEdgeFound != null && this.WasEdgeFound(this._current.Vertex, edgeEnumerator.Id, this._current.Weight))
             {
                 return(false);
             }
         }
         else if (this._visits.ContainsKey(to))
         {
             if (this.WasEdgeFound != null && this.WasEdgeFound(this._current.Vertex, edgeEnumerator.Id, this._current.Weight))
             {
                 return(false);
             }
         }
         else
         {
             float  distance;
             ushort profile;
             EdgeDataSerializer.Deserialize(edgeEnumerator.Data0, out distance, out profile);
             Factor factor = Factor.NoFactor;
             if (!this._factors.TryGetValue((uint)profile, out factor))
             {
                 factor = this._getFactor(profile);
                 this._factors.Add((uint)profile, factor);
             }
             if ((double)factor.Value > 0.0 && ((int)factor.Direction == 0 || !this._backward && (int)factor.Direction == 1 != edgeEnumerator.DataInverted || this._backward && (int)factor.Direction == 1 == edgeEnumerator.DataInverted))
             {
                 float num = this._current.Weight + distance * factor.Value;
                 if ((double)num < (double)this._sourceMax)
                 {
                     this._heap.Push(new Path(to, num, this._current), num);
                 }
             }
         }
     }
     return(true);
 }