Exemplo n.º 1
0
        public void TestReverseIntSort()
        {
            var items = Enumerable.Range(1, 100).ToList().Shuffle();

            var priorityQueue = new PriorityQueue<int>(true) { items };

            Assert.That(priorityQueue.Peek(), Is.EqualTo(100));
            Assert.That(priorityQueue.ToList(), Is.EquivalentTo(Enumerable.Range(1, 100).Reverse().ToList()));
        }
Exemplo n.º 2
0
        public void TestSortWithDuplicateKeys()
        {
            var items = Enumerable.Range(1, 1000)
                .Select(x => x % 10).ToList()
                .Shuffle();

            var priorityQueue = new PriorityQueue<int>() { items };

            Assert.That(priorityQueue.Peek(), Is.EqualTo(0));
            Assert.That(priorityQueue.ToList(), Is.EquivalentTo(items.OrderBy(x => x).ToList()));
        }
Exemplo n.º 3
0
        static void Main()
        {
            // example structure as binary heap
            // -4444 parent root at the beginning with children -5 and 1
            // -5 with children 5 and 50, 1 with children 4 and 8
            // 5 with children 999 and 19
            //                                   -4444
            //                                -5        1
            //                             5      50    4  8
            //                         999   19                     

            PriorityQueue<int> queue = new PriorityQueue<int>();

            queue.Enqueue(5);
            queue.Enqueue(19);
            queue.Enqueue(-4444);
            queue.Enqueue(1);
            queue.Enqueue(50);
            queue.Enqueue(4);
            queue.Enqueue(8);
            queue.Enqueue(999);
            queue.Enqueue(-5);

            Print(queue);


            Console.WriteLine(queue.Dequeue());

            // all are for testing
            // Console.WriteLine();
            // Print(queue);

            Console.WriteLine(queue.Dequeue());

            // Console.WriteLine();
            // Print(queue);

            Console.WriteLine(queue.Dequeue());
            Console.WriteLine("----------------");

            // Console.WriteLine();
            // Print(queue);

            Console.WriteLine("Peek: " + queue.Peek());

            Console.WriteLine(queue.Count);

            queue.Clear();

            Console.WriteLine(queue.Count);
        }
Exemplo n.º 4
0
        public void TestAdd()
        {
            var items = Enumerable.Range(100, 1000).ToList().Shuffle();

            var priorityQueue = new PriorityQueue<int>() { items };

            Enumerable.Range(1, 10).ToList().ForEach(priorityQueue.Add);
            Enumerable.Range(10000, 10).ToList().ForEach(priorityQueue.Add);

            ((List<int>)items).AddRange(Enumerable.Range(1, 10));
            ((List<int>)items).AddRange(Enumerable.Range(10000, 10));

            Assert.That(priorityQueue.Peek(), Is.EqualTo(1));
            Assert.That(priorityQueue.ToList(), Is.EquivalentTo(items.OrderBy(x => x).ToList()));
        }
Exemplo n.º 5
0
        public ArrayList BranchAndBound(City[] Cities, ArrayList Route, double BSSF)
        {
            Node startNode = new Node(Cities);
            startNode.initStaticMembers();
            PriorityQueue pQ = new PriorityQueue(startNode);
            ArrayList bbRoute = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();

            //while ((pQ.Size() > 0) && (pQ.Peek() < BSSF) && (timer.Elapsed.TotalMinutes < 10))
            while ((pQ.Size() > 0) && (pQ.Peek() < BSSF) && (timer.Elapsed.TotalSeconds < 30))
            {
                // Keep track of the largest size of the queue
                if (pQ.Size() > Node.maxNodesCreated)
                {
                    Node.maxNodesCreated = pQ.Size();
                }

                startNode = pQ.DeleteMinimum();
                //startNode.matrix2Table();

                // Check for a solution
                if (startNode.includedEdges == Cities.Length)
                {
                    ArrayList tempRoute = new ArrayList();

                    if (!startNode.exited.Contains(-1))
                    {
                        int index = 0;

                        while (tempRoute.Count < Cities.Length)
                        {
                            tempRoute.Add(Cities[startNode.exited[index]]);
                            index = startNode.exited[index];
                        }

                        BSSF = startNode.lowerBound;
                        bbRoute = tempRoute;
                        Node.numSolutions++;
                    }
                }

                Node incNode = new Node(startNode);
                Node excNode = new Node(startNode);
                Node maxInclude = null;
                Node maxExclude = null;

                double maxDiff = -1;
                double diff = 0;
                int maxRow = 0;
                int maxCol = 0;

                for (int row = 0; row < startNode.matrixSize; row++)
                {
                    for (int col = 0; col < startNode.matrixSize; col++)
                    {
                        if (startNode.rcMatrix[row, col] == 0)
                        {
                            Node includeNode = new Node(incNode, true, row, col);
                            Node excludeNode = new Node(excNode, false, row, col);
                            diff = excludeNode.lowerBound - includeNode.lowerBound;
                            Node.numStatesCreated += 2;

                            if (diff > maxDiff)
                            {
                                maxDiff = diff;
                                maxRow = row;
                                maxCol = col;
                                maxInclude = new Node(includeNode);
                                maxExclude = new Node(excludeNode);
                            }
                        }
                    }
                }

                if (maxInclude != null && maxInclude.lowerBound < BSSF)
                {
                    pQ.Insert(maxInclude);
                }
                else
                {
                    Node.pruneCount++;
                }

                if (maxExclude != null && maxExclude.lowerBound < BSSF)
                {
                    pQ.Insert(maxExclude);
                }
                else
                {
                    Node.pruneCount++;
                }
            }

            timer.Stop();
            Node.timeElapsed = timer.ElapsedMilliseconds;

            if (bbRoute == null)
            {
                return Route;
            }
            else
            {
                // Add the rest of the queue to the pruned count
                Node.pruneCount += pQ.Size();
                return bbRoute;
            }
        }
Exemplo n.º 6
0
        public static bool GetShortestPath(GridNode i_start, GridNode i_end, bool isDiagonal, out List<GridNode> o_path, out List<GridNode> o_open)
        {
            o_path = new List<GridNode>();
            o_open = new List<GridNode>();

            IDictionary<GridNode, GridNode> path = new Dictionary<GridNode, GridNode>();
            IDictionary<GridNode, float> open = new Dictionary<GridNode, float>();

            PriorityQueue<float, GridNode> priorityQueue = new PriorityQueue<float, GridNode>(30 * 30, Comparer<float>.Default);

            priorityQueue.Add(new KeyValuePair<float, GridNode>(GetDistance(i_start, i_end, isDiagonal), i_start));
            open.Add(new KeyValuePair<GridNode, float>(i_start, 0));

            bool succeed = false;
            while (!priorityQueue.IsEmpty)
            {
                KeyValuePair<float, GridNode> current = priorityQueue.Peek();
                GridNode currentNode = current.Value;
                float currentCost;
                if (!open.TryGetValue(currentNode, out currentCost))
                {
                    Debug.Assert(false);
                }

                if (currentNode == i_end)
                {
                    succeed = true;
                    break;
                }

                foreach (GridNode neighbour in currentNode.GetNeighbors(isDiagonal))
                {
                    if (neighbour.Passable)
                    {
                        float newCost = currentCost + GetCost(currentNode, neighbour, isDiagonal);
                        float oldCost;
                        if (!open.TryGetValue(neighbour, out oldCost))
                        {
                            path.Add(neighbour, currentNode);
                            open.Add(neighbour, newCost);
                            float dist = GetDistance(neighbour, i_end, isDiagonal);
                            priorityQueue.Add(new KeyValuePair<float, GridNode>(newCost + dist, neighbour));
                        }
                        else if (newCost < oldCost)
                        {
                            path[neighbour] = currentNode;
                            open[neighbour] = newCost;
                            float dist = GetDistance(neighbour, i_end, isDiagonal);
                            priorityQueue.Remove(new KeyValuePair<float, GridNode>(oldCost + dist, neighbour));
                            priorityQueue.Add(new KeyValuePair<float, GridNode>(newCost + dist, neighbour));
                        }
                    }
                }

                priorityQueue.Remove(current);
            }

            if (succeed)
            {

                GridNode currentNode = i_end;
                o_path.Add(currentNode);
                while (path.ContainsKey(currentNode))
                {
                    currentNode = path[currentNode];
                    o_path.Add(currentNode);
                }
                o_path.Reverse();

            }

            o_open = open.Keys.ToList();

            return succeed;
        }
Exemplo n.º 7
0
        public void Count()
        {
            var pq = new PriorityQueue<int>();
              Assert.AreEqual(0, pq.Count);

              pq.Enqueue(123);
              Assert.AreEqual(1, pq.Count);

              pq.Enqueue(31);
              pq.Enqueue(64);
              Assert.AreEqual(3, pq.Count);

              for (int i = 0; i < 30; i++)
            pq.Enqueue(i);

              pq.Peek();
              pq.Dequeue();
              pq.Dequeue();
              Assert.AreEqual(31, pq.Count);

              pq.Clear();
              Assert.AreEqual(0, pq.Count);
        }
Exemplo n.º 8
0
        public void Test()
        {
            var pq = new PriorityQueue<int>();

              var random = new Random();
              for (int i = 0; i < 100; i++)
              {
            var value = random.Next(0, 20);

            pq.Enqueue(value);
            pq.Validate();
              }

              var max = int.MaxValue;
              for (int i = 0; i < 100; i++)
              {
            Assert.AreEqual(100 - i, pq.Count);

            var peek = pq.Peek();
            var value = pq.Dequeue();

            Assert.AreEqual(peek, value);
            Assert.IsTrue(max >= value);
            max = value;

            pq.TrimExcess();
              }
        }