/// <summary>
        /// Returns  a  path
        /// </summary>
        /// <returns>a path or null if the target is not reachable from the source</returns>
        internal IEnumerable<VisibilityVertex> GetPath(bool shrinkEdgeLength) {
            var pq = new GenericBinaryHeapPriorityQueue<VisibilityVertex>();

            _source.Distance = 0;
            _target.Distance = double.PositiveInfinity;
            pq.Enqueue(_source, H(_source));

            while (!pq.IsEmpty()) {
                double hu;
                var u = pq.Dequeue(out hu);
                if (hu >= _target.Distance)
                    break;

                foreach (var e in u.OutEdges) {
                    if (PassableOutEdge(e)) {
                        var v = e.Target;
                        ProcessNeighbor(pq, u, e, v);
                    }
                }

                foreach (var e in u.InEdges) {
                    if (PassableInEdge(e)) {
                        var v = e.Source;
                        ProcessNeighbor(pq, u, e, v);
                    }
                }

            }
            return _visGraph.PreviosVertex(_target) == null
                ? null
                : CalculatePath(shrinkEdgeLength);
        }
        private bool InitPath(VertexEntry[] sourceVertexEntries, VisibilityVertexRectilinear source, VisibilityVertexRectilinear target)
        {
            if ((source == target) || !InitEntryDirectionsAtTarget(target))
            {
                return(false);
            }
            this.Target = target;
            this.Source = source;
            double cost = this.TotalCostFromSourceToVertex(0, 0) + HeuristicDistanceFromVertexToTarget(source.Point, Direction.None);

            if (cost >= this.upperBoundOnCost)
            {
                return(false);
            }

            // This path starts lower than upperBoundOnCost, so create our structures and process it.
            this.queue           = new GenericBinaryHeapPriorityQueue <VertexEntry>();
            this.visitedVertices = new List <VisibilityVertexRectilinear> {
                source
            };

            if (sourceVertexEntries == null)
            {
                EnqueueInitialVerticesFromSource(cost);
            }
            else
            {
                EnqueueInitialVerticesFromSourceEntries(sourceVertexEntries);
            }
            return(this.queue.Count > 0);
        }
Пример #3
0
        void ProcessNeighbor(GenericBinaryHeapPriorityQueue <VisibilityVertex> pq, VisibilityEdge l,
                             VisibilityVertex v)
        {
            var len = l.Length;
            var c   = _current.Distance + len;

            if (c >= upperBound)
            {
                return;
            }
            if (targets.Contains(v))
            {
                upperBound    = c;
                closestTarget = v;
            }
            if (v != sources && _visGraph.PreviosVertex(v) == null)
            {
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                pq.Enqueue(v, c);
            }
            else if (c < v.Distance)
            {
                //This condition should never hold for the dequeued nodes.
                //However because of a very rare case of an epsilon error it might!
                //In this case DecreasePriority will fail to find "v" and the algorithm will continue working.
                //Since v is not in the queue changing its .Distance will not mess up the queue.
                //Changing v.Prev is fine since we come up with a path with an insignificantly
                //smaller distance.
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                pq.DecreasePriority(v, c);
            }
        }
 void ProcessNeighbor(GenericBinaryHeapPriorityQueue<VisibilityVertex> pq, VisibilityEdge l,
                      VisibilityVertex v) {
     var len = l.Length;
     var c = current.Distance + len;
     if (c >= upperBound)
         return;
     if (targets.Contains(v)) {
         upperBound = c;
         closestTarget = v;
     }
     if (v != source && _visGraph.PreviosVertex(v) == null) {
         v.Distance = c;
         _visGraph.SetPreviousEdge(v, l);
         pq.Enqueue(v, c);
     } else if (c < v.Distance) {
         //This condition should never hold for the dequeued nodes.
         //However because of a very rare case of an epsilon error it might!
         //In this case DecreasePriority will fail to find "v" and the algorithm will continue working.
         //Since v is not in the queue changing its .Distance will not mess up the queue.
         //Changing v.Prev is fine since we come up with a path with an insignificantly
         //smaller distance.
         v.Distance = c;
         _visGraph.SetPreviousEdge(v, l);
         pq.DecreasePriority(v, c);
     }
 }
Пример #5
0
        /// <summary>
        /// Returns  a  path
        /// </summary>
        /// <returns>a path or null if the target is not reachable from the source</returns>
        internal IEnumerable <VisibilityVertex> GetPath()
        {
            var pq = new GenericBinaryHeapPriorityQueue <VisibilityVertex>();

            foreach (var v in sources)
            {
                v.Distance = 0;
                pq.Enqueue(v, 0);
            }
            while (!pq.IsEmpty())
            {
                _current = pq.Dequeue();
                if (targets.Contains(_current))
                {
                    break;
                }

                foreach (var e in _current.OutEdges.Where(PassableOutEdge))
                {
                    ProcessNeighbor(pq, e, e.Target);
                }

                foreach (var e in _current.InEdges.Where(PassableInEdge))
                {
                    ProcessNeighbor(pq, e, e.Source);
                }
            }

            return(_visGraph.PreviosVertex(_current) == null ? null : CalculatePath());
        }
        void ProcessNeighbor(GenericBinaryHeapPriorityQueue <VisibilityVertex> pq, VisibilityVertex u, VisibilityEdge l, VisibilityVertex v)
        {
            var len = l.Length;
            var c   = u.Distance + len;

            if (v != source && _visGraph.PreviosVertex(v) == null)
            {
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                if (v != target)
                {
                    pq.Enqueue(v, H(v));
                }
            }
            else if (c < v.Distance)     //This condition should never hold for the dequeued nodes.
            //However because of a very rare case of an epsilon error it might!
            //In this case DecreasePriority will fail to find "v" and the algorithm will continue working.
            //Since v is not in the queue changing its .Distance will not influence other nodes.
            //Changing v.Prev is fine since we come up with the path with an insignificantly
            //smaller distance.
            {
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                if (v != target)
                {
                    pq.DecreasePriority(v, H(v));
                }
            }
        }
 void Cleanup() {
     costToTarget = double.PositiveInfinity;
     bestEdgeIntoTarget = null;
     prev.Clear();
     queue = new GenericBinaryHeapPriorityQueue<Node>();
     queue.Enqueue(source, 0);
 }
Пример #8
0
        /// <summary>
        /// Grow hubs
        /// </summary>
        bool GrowHubs(bool useHalfEdgesAsIdealR)
        {
            var queue = new GenericBinaryHeapPriorityQueue <Station>();

            foreach (var v in metroGraphData.VirtualNodes())
            {
                queue.Enqueue(v, -CalculatePotential(v, useHalfEdgesAsIdealR));
            }

            bool progress = false;

            //choose a hub with the greatest potential
            while (!queue.IsEmpty())
            {
                double  hu;
                Station v = queue.Dequeue(out hu);
                if (hu >= 0)
                {
                    break;
                }

                //grow the hub
                if (TryGrowHub(v, useHalfEdgesAsIdealR))
                {
                    queue.Enqueue(v, -CalculatePotential(v, useHalfEdgesAsIdealR));
                    progress = true;
                }
            }
            return(progress);
        }
Пример #9
0
 void Cleanup()
 {
     costToTarget       = double.PositiveInfinity;
     bestEdgeIntoTarget = null;
     prev.Clear();
     queue = new GenericBinaryHeapPriorityQueue <Node>();
     queue.Enqueue(source, 0);
 }
 private void Cleanup()
 {
     foreach (var v in this.visitedVertices)
     {
         v.RemoveVertexEntries();
     }
     this.visitedVertices.Clear();
     this.queue = null;
     this.TestClearIterations();
 }
 void InitEventQueue()
 {
     Queue = new GenericBinaryHeapPriorityQueue <LinkedPoint>();
     foreach (var vertPoint in VerticalPoints)
     {
         Queue.Enqueue(vertPoint, Low(vertPoint));
     }
     //a horizontal point will appear in the queue after a vertical point
     // with the same coordinate low coorinate
     foreach (var horizPoint in HorizontalPoints)
     {
         Queue.Enqueue(horizPoint, horizPoint.Point.Y);
     }
 }
        /// <summary>
        /// Returns  a  path
        /// </summary>
        /// <returns>a path or null if the target is not reachable from the source</returns>
        internal IEnumerable <VisibilityVertex> GetPath(bool shrinkEdgeLength)
        {
            var pq = new GenericBinaryHeapPriorityQueue <VisibilityVertex>();

            _source.Distance = 0;
            _target.Distance = double.PositiveInfinity;
            pq.Enqueue(_source, H(_source));

            while (!pq.IsEmpty())
            {
                double hu;
                var    u = pq.Dequeue(out hu);
                if (hu >= _target.Distance)
                {
                    break;
                }

                foreach (var e in u.OutEdges)
                {
                    if (PassableOutEdge(e))
                    {
                        var v = e.Target;
                        if (u != _source && u.isReal)
                        {
                            ProcessNeighbor(pq, u, e, v, 1000);
                        }
                        else
                        {
                            ProcessNeighbor(pq, u, e, v);
                        }
                    }
                }

                foreach (var e in u.InEdges)
                {
                    if (PassableInEdge(e))
                    {
                        var v = e.Source;
                        ProcessNeighbor(pq, u, e, v);
                    }
                }
            }
            return(_visGraph.PreviosVertex(_target) == null
                ? null
                : CalculatePath(shrinkEdgeLength));
        }
        /// <summary>
        /// Returns  a  path
        /// </summary>
        /// <returns>a path or null if the target is not reachable from the source</returns>
        internal IEnumerable<VisibilityVertex> GetPath() {
            var pq = new GenericBinaryHeapPriorityQueue<VisibilityVertex>();
            source.Distance = 0;
            pq.Enqueue(source, 0);
            while (!pq.IsEmpty()) {
                current = pq.Dequeue();
                if (targets.Contains(current))
                    break;

                foreach (var e in current.OutEdges.Where(PassableOutEdge))
                    ProcessNeighbor(pq, e, e.Target);

                foreach (var e in current.InEdges.Where(PassableInEdge))
                    ProcessNeighbor(pq, e, e.Source);
            }

            return _visGraph.PreviosVertex(current) == null ? null : CalculatePath();
        }
        void ProcessNeighbor(GenericBinaryHeapPriorityQueue <VisibilityVertex> pq, VisibilityVertex u, VisibilityEdge l, VisibilityVertex v, int penalty)
        {
            var len = l.Length + penalty;
            var c   = u.Distance + len;

            /*
             * if (_visGraph.visVertexToId[l.Source] < _g.N || _visGraph.visVertexToId[l.Target] < _g.N)
             * {
             *  if (!(l.Source == _source || l.Target == _source || l.Source == _target || l.Target == _target))
             *  {
             *      c = 500;
             *  }
             * }
             */
            // (v != _source && _visGraph.PreviosVertex(v) == null)

            if (v != _source && _visGraph.PreviosVertex(v) == null)
            {
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                if (v != _target)
                {
                    pq.Enqueue(v, H(v));
                }
            }
            else if (v != _source && c < v.Distance)
            { //This condition should never hold for the dequeued nodes.
                //However because of a very rare case of an epsilon error it might!
                //In this case DecreasePriority will fail to find "v" and the algorithm will continue working.
                //Since v is not in the queue changing its .Distance will not influence other nodes.
                //Changing v.Prev is fine since we come up with the path with an insignificantly
                //smaller distance.
                var prevV = _visGraph.PreviosVertex(v);
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                if (v != _target)
                {
                    pq.DecreasePriority(v, H(v));
                }
            }
        }
 List <SdBoneEdge> RouteEdgeWithGroups()
 {
     for (int i = 0; i < 2; i++)
     {
         SetLengthCoefficient();
         Queue = new GenericBinaryHeapPriorityQueue <SdVertex>();
         SetPortVerticesAndObstacles(CurrentEdgeGeometry.SourcePort, true, out sourceLoosePoly);
         SetPortVerticesAndObstacles(CurrentEdgeGeometry.TargetPort, false, out targetLoosePoly);
         List <SdBoneEdge> ret = RouteOnKnownSourceTargetVertices((CurrentEdgeGeometry.TargetPort.Location - CurrentEdgeGeometry.SourcePort.Location).Normalize(), i == 0);
         if (ret != null)
         {
             return(ret);
         }
         for (int j = 0; j < vertexArray.Length; j++)
         {
             vertexArray[j].SetPreviousToNull();
         }
     }
     //SplineRouter.ShowVisGraph(this.VisibilityGraph, ObstacleHierarchy.GetAllLeaves(), null, new[] { new LineSegment(
     // CurrentEdgeGeometry.SourcePort.Location, CurrentEdgeGeometry.TargetPort.Location)});
     throw new InvalidOperationException(); //cannot find a path
 }
        /// <summary>
        /// Grow hubs
        /// </summary>
        bool GrowHubs(bool useHalfEdgesAsIdealR) {
            var queue = new GenericBinaryHeapPriorityQueue<Station>();
            foreach (var v in metroGraphData.VirtualNodes()) {
                queue.Enqueue(v, -CalculatePotential(v, useHalfEdgesAsIdealR));
            }

            bool progress = false;
            //choose a hub with the greatest potential
            while (!queue.IsEmpty()) {
                double hu;
                Station v = queue.Dequeue(out hu);
                if (hu >= 0)
                    break;

                //grow the hub
                if (TryGrowHub(v, useHalfEdgesAsIdealR)) {
                    queue.Enqueue(v, -CalculatePotential(v, useHalfEdgesAsIdealR));
                    progress = true;
                }
            }
            return progress;
        }
 List<SdBoneEdge> RouteEdgeWithGroups() {
     for (int i = 0; i < 2; i++) {
         SetLengthCoefficient();
         Queue = new GenericBinaryHeapPriorityQueue<SdVertex>();
         SetPortVerticesAndObstacles(CurrentEdgeGeometry.SourcePort, true, out sourceLoosePoly);
         SetPortVerticesAndObstacles(CurrentEdgeGeometry.TargetPort, false, out targetLoosePoly);
         List<SdBoneEdge> ret = RouteOnKnownSourceTargetVertices((CurrentEdgeGeometry.TargetPort.Location - CurrentEdgeGeometry.SourcePort.Location).Normalize(), i == 0);
         if (ret != null)
             return ret;
         for (int j = 0; j < vertexArray.Length; j++) {
             vertexArray[j].SetPreviousToNull();
         }
     }
     //SplineRouter.ShowVisGraph(this.VisibilityGraph, ObstacleHierarchy.GetAllLeaves(), null, new[] { new LineSegment(
    // CurrentEdgeGeometry.SourcePort.Location, CurrentEdgeGeometry.TargetPort.Location)});
     throw new InvalidOperationException(); //cannot find a path
 }
 void InitEventQueue() {
     Queue = new GenericBinaryHeapPriorityQueue<LinkedPoint>();
     foreach (var vertPoint in VerticalPoints)
         Queue.Enqueue(vertPoint, Low(vertPoint));
     //a horizontal point will appear in the queue after a vertical point 
     // with the same coordinate low coorinate
     foreach (var horizPoint in HorizontalPoints)
         Queue.Enqueue(horizPoint, horizPoint.Point.Y);
 }
        void ProcessNeighbor(GenericBinaryHeapPriorityQueue<VisibilityVertex> pq, VisibilityVertex u, VisibilityEdge l, VisibilityVertex v) {
            var len = l.Length;
            var c = u.Distance + len;

            // (v != _source && _visGraph.PreviosVertex(v) == null)

            if (v != _source && _visGraph.PreviosVertex(v) == null) {
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                if (v != _target) {
                    pq.Enqueue(v, H(v));
                }
            } else if (v != _source && c < v.Distance) { //This condition should never hold for the dequeued nodes.
                //However because of a very rare case of an epsilon error it might!
                //In this case DecreasePriority will fail to find "v" and the algorithm will continue working.
                //Since v is not in the queue changing its .Distance will not influence other nodes.
                //Changing v.Prev is fine since we come up with the path with an insignificantly
                //smaller distance.
                var prevV = _visGraph.PreviosVertex(v);
                v.Distance = c;
                _visGraph.SetPreviousEdge(v, l);
                if (v != _target)
                    pq.DecreasePriority(v, H(v));
            }
        }