コード例 #1
0
        public void Should_dequeue_in_order()
        {
            var q = new FibonacciQueue <int, int>(i => i);

            q.Enqueue(3);
            q.Enqueue(1);
            q.Enqueue(0);
            q.Enqueue(4);
            q.Enqueue(9);
            q.Dequeue().Should().Be(0);
            q.Dequeue().Should().Be(1);
            q.Dequeue().Should().Be(3);
            q.Dequeue().Should().Be(4);
            q.Dequeue().Should().Be(9);
        }
コード例 #2
0
        public void Should_dequeue_massive_amount_in_order()
        {
            var q = new FibonacciQueue <int, int>(i => i);
            var r = new Random();

            for (int i = 0; i < 8192; i++)
            {
                q.Enqueue(r.Next());
            }

            var c = int.MinValue;

            while (q.Count > 0)
            {
                var i = q.Dequeue();
                i.Should().BeGreaterOrEqualTo(c);
                c = i;
            }
        }
コード例 #3
0
        private void OnBlackTarget([NotNull] TEdge edge)
        {
            Debug.Assert(edge != null);

            bool decreased = Relax(edge);

            if (decreased)
            {
                TVertex target   = edge.Target;
                double  distance = GetVertexDistance(target);

                OnTreeEdge(edge);
                _costs[target] = DistanceRelaxer.Combine(distance, CostHeuristic(target));
                _vertexQueue.Enqueue(target);
                VerticesColors[target] = GraphColor.Gray;
            }
            else
            {
                OnEdgeNotRelaxed(edge);
            }
        }
コード例 #4
0
        public List <T> FindPath(T start, T goal)
        {
            if (start == null)
            {
                return(null);
            }
            if (goal == null)
            {
                return(null);
            }
            this.Goal = goal;
            gScore    = new Dictionary <T, float>();
            fScore    = new Dictionary <T, float>();


            //var openset = new HashSet<T>();
            var openset   = new FibonacciQueue <T, float>(d => fScore[d]);
            var closedset = new HashSet <T>();

            cameFrom = new Dictionary <T, T>();

            //came_from := the empty map    // The map of navigated nodes.

            gScore[start] = 0;    // Cost from start along best known path.
            // Estimated total cost from start to goal through y.
            fScore[start] = gScore[start] + ConnectionProvider.GetHeuristicCostEstimate(start, goal);


            openset.Enqueue(start);
            onBegin();
            while (openset.Count > 0)
            {
                var current = openset.Dequeue();

                onSelectNode(current);

                if (StopCondition(current))
                {
                    return(reconstruct_reverse_path(cameFrom, current).Reverse().ToList());
                }

                if (current.Equals(goal))
                {
                    return(reconstruct_reverse_path(cameFrom, goal).Reverse().ToList());
                }

                closedset.Add(current); //add current to closedset

                foreach (var neighbor in ConnectionProvider.GetConnectedNodes(this, current))
                {
                    if (closedset.Contains(neighbor))
                    {
                        continue;
                    }

                    var tentative_g_score = gScore[current] + ConnectionProvider.GetCost(current, neighbor);
                    if (openset.Contains(neighbor) && !(tentative_g_score <= gScore[neighbor]))
                    {
                        continue;
                    }

                    cameFrom[neighbor] = current;
                    gScore[neighbor]   = tentative_g_score;
                    fScore[neighbor]   = gScore[neighbor] + ConnectionProvider.GetHeuristicCostEstimate(neighbor, goal);

                    if (openset.Contains(neighbor))
                    {
                        continue;
                    }

                    openset.Enqueue(neighbor);
                    onEnqueueNode(neighbor);
                }
            }
            return(null);
        }