Esempio n. 1
0
        public void TestBasicFunctionality()
        {
            var q = new PriorityQueue<int>();
            Assert.AreEqual(0, q.Count);
            Assert.AreEqual(0, q.Capacity);
            Assert.Throws<InvalidOperationException>(() => q.Peek());
            Assert.Throws<InvalidOperationException>(() => q.Dequeue());

            q.Enqueue(5);
            q.Enqueue(2);
            q.Enqueue(4);

            Assert.AreEqual(3, q.Count);
            Assert.IsTrue(q.Capacity >= 3);

            Assert.IsTrue(q.Contains(2));
            Assert.IsFalse(q.Contains(3));

            Assert.AreEqual(3, q.ToArray().Length);
            CollectionAssert.AreEqual(q.ToArray(), q);

            Assert.AreEqual(2, q.Peek());

            Assert.AreEqual(2, q.Dequeue());
            Assert.AreEqual(4, q.Dequeue());
            Assert.AreEqual(5, q.Dequeue());
        }
Esempio n. 2
0
        public static void Main()
        {
            Node[] nodes = new Node[7];
            nodes[1] = new Node(1);
            nodes[2] = new Node(2);
            nodes[3] = new Node(3);
            nodes[4] = new Node(4);
            nodes[5] = new Node(5);
            nodes[6] = new Node(6);

            InitializeEdges(nodes);
            PriorityQueue<Edge> edges = new PriorityQueue<Edge>();
            bool[] usedNodes = new bool[7];
            Node startNode = nodes[1];
            usedNodes[startNode.Number] = true;
            foreach (var edge in startNode.Edges)
            {
                if (!edges.Contains(edge) && !usedNodes[edge.EndNode.Number])
                {
                    edges.Enqueue(edge);
                }
            }

            List<Edge> traversedEdges = new List<Edge>();

            while (edges.Count != 0)
            {
                Edge currentEdge = edges.Dequeue();
                if (!usedNodes[currentEdge.EndNode.Number])
                {
                    usedNodes[currentEdge.EndNode.Number] = true;
                    traversedEdges.Add(currentEdge);
                    foreach (var edge in currentEdge.EndNode.Edges)
                    {
                        if (!edges.Contains(edge) && !usedNodes[edge.EndNode.Number])
                        {
                            edges.Enqueue(edge);
                        }
                    }
                }
            }

            foreach (var edge in traversedEdges)
            {
                Console.WriteLine(edge);
            }
        }
Esempio n. 3
0
        public static Path findPath(Node startNode, Node destination)
        {
            List<Node> closedList = new List<Node>();
            PriorityQueue<Node> openList = new PriorityQueue<Node>();

            Dictionary<Node, int> gScore = new Dictionary<Node,int>();
            gScore[startNode] = 0;

            Dictionary<Node, int> fScore = new Dictionary<Node,int>();
            fScore[startNode] = startNode.HeuristicDistance(destination);

            Dictionary<Node, Node> prevNode = new Dictionary<Node, Node>();

            openList.Add(fScore[startNode], startNode);

            while (openList.Count > 0)
            {
                Node current = openList.RemoveMin();
                if (current.Equals(destination))
                {
                    return getPath(prevNode, destination);
                }
                else
                {
                    closedList.Add(current);
                    Node[] neighbours = current.GetNeighbours();
                    foreach (Node next in neighbours)
                    {
                        if (closedList.Contains(next))
                            continue;

                        int newGScore = gScore[current] + current.distanceBetween(next);

                        if (!openList.Contains(next) || newGScore < gScore[next])
                        {
                            prevNode[next] = current;
                            gScore[next] = newGScore;
                            fScore[next] = gScore[next] + next.HeuristicDistance(destination);
                            if (!openList.Contains(next))
                                openList.Add(fScore[next], next);
                        }
                    }
                }
            }

            return null;
        }
Esempio n. 4
0
        public Property Default_PriorityQueue_will_contain_all_added_items(long[] items)
        {
            var priorityQueue = new PriorityQueue<long>();
            foreach (var item in items)
                priorityQueue.Enqueue(item);

            return items.All(x => priorityQueue.Contains(x)).Label($"Expected priority queue to contain [{string.Join(";", items)}] " +
                                                                   $"but instead was [{string.Join(";", priorityQueue)}] (unsorted)");

        }
Esempio n. 5
0
        public static SearchResult Search(AbstractNode initialNode, IKnowledgeBase kb)
        {
            var frontier = new PriorityQueue<AbstractNode>();
            var explored = new List<AbstractState>();
            var statesSearched = 0; //Bliver kun brugt af os af ren interesse
            var end = initialNode.Target;
            frontier.Add(initialNode);
            explored.Add(initialNode.State);

            while (frontier.Count > 0)
            {
                // Chooses the lowest-cost node in the frontier
                var currentNode = frontier.Pop();
                statesSearched++;
                if (currentNode.State.Equals(end))
                    return new SearchResult(currentNode, statesSearched, true);

                var actions = kb.ActionsForNode(currentNode);
            //Explore /expand the current node
                foreach (var action in actions)
                {
                    var child = kb.Resolve(currentNode, action, end);
                    //System.Console.WriteLine("Frontier.Count: " + frontier.Count);

                    if (!explored.Contains(child.State) && !frontier.Contains(child))
                    {
                        explored.Add(child.State);
                        frontier.Add(child);

                    }
                    else if(true)
                    {
                        for (int i = 0; i < frontier.Count; i++)
                        {
                            var frontierNode = frontier[i];
                            if (frontierNode.State.Equals(child.State) && frontierNode.PathCost > child.PathCost)
                            {
                                frontier[i] = child;
                                break;
                            }
                        }
                    }
                }
            }

            return new SearchResult(null, statesSearched, false);
        }
Esempio n. 6
0
File: Program.cs Progetto: sseng/ai
        static List<Node> getChildren(Node current, List<Node> closedList, PriorityQueue<Node> openList)
        {
            string gameString = current.GameString;
            List<Node> children = new List<Node>();

            for (int i = 0; i < gameString.Length - 1; i++)
            {
                char[] charString = gameString.ToCharArray();

                char temp = charString[i];
                charString[i] = charString[i+1];
                charString[i+1] = temp;

                string s = new string(charString);
                Node childNode = new Node(s);
                childNode.G = current.G + 1;
                childNode.Parent = current;

                //if openlist and closed list does not contain new node, add to children
                if (!openList.Contains(childNode) && !closedList.Contains(childNode) && !childNode.Equals(current) )
                    children.Add(childNode);
            }
            return children;
        }
Esempio n. 7
0
        public void ContainsTest()
        {
            PriorityQueue<int> actual = new PriorityQueue<int> { 45, 12, 1, 0 };

            Assert.IsTrue(actual.Contains(12));
        }
Esempio n. 8
0
 public void ContainsTest(int[] elemList)
 {
     PriorityQueue<int> actual = new PriorityQueue<int>(elemList);
     for(int i = 0; i < elemList.Length; i++)
         PexAssert.IsTrue(actual.Contains(elemList[i]));
 }
Esempio n. 9
0
        public List<MapTile> GetPath(Point start, Point goal) {
            var startTile = GetTile(start);
            var goalTile = GetTile(goal);

            // check that the start and goal positions are valid, and are not the same
            if (!Within(start) || !Within(goal) || start == goal || startTile == null || goalTile == null) {
                return new List<MapTile>();
            }
            // Check that start and goal are walkable and that a path can exist between them
            if (startTile.Set != goalTile.Set) {
                return new List<MapTile>();
            }


            // reset costs
            foreach (var t in _tiles) {
                t.F = t.G = float.MaxValue;
            }
            var open = new PriorityQueue<MapTile>(_tiles.Length);
            var closed = new HashSet<MapTile>();

            startTile.G = 0;
            startTile.F = h(start, goal);

            open.Enqueue(startTile, startTile.F);

            MapTile current = null;
            while (open.Any() && current != goalTile) {
                current = open.Dequeue();
                closed.Add(current);
                for (var i = 0; i < 8; i++) {
                    var edge = current.Edges[i];

                    if (edge == null) {
                        continue;
                    }
                    var neighbor = edge.Node2;
                    var cost = current.G + edge.Cost;



                    if (open.Contains(neighbor) && cost < neighbor.G) {
                        open.Remove(neighbor);
                    }
                    if (closed.Contains(neighbor) && cost < neighbor.G) {
                        closed.Remove(neighbor);
                    }
                    if (!open.Contains(neighbor) && !closed.Contains(neighbor)) {
                        neighbor.G = cost;
                        var f = cost + h(neighbor.MapPosition, goal);
                        open.Enqueue(neighbor, f);
                        neighbor.Parent = current;

                    }
                }
            }
            System.Diagnostics.Debug.Assert(current == goalTile);
            var path = new List<MapTile>();


            while (current != startTile) {
                path.Add(current);
                current = current.Parent;
            }
            path.Reverse();
            return path;
        }
Esempio n. 10
0
        private SearchState Step(PriorityQueue<Node> open)
        {
            Node n, child;
            n = open.Dequeue();
            n.visited = true;
            if(n.Equals(goal)){
                thePath = MakePath(n);
                bestCost = n.cost;
                return SearchState.REACHED_THE_GOAL;
            }

            while(n.HasMoreChildren){
                child = n.GetNextChildren();
                if(child.visited) continue;
                int newCost = n.cost + Cost(n, child);
                if(open.Contains(child) && child.cost <= newCost) continue;
                child.parent = n;
                child.cost = newCost;
                if(!open.Contains(child)) open.Enqueue(child);
                else open.FullRefresh();
            }
            return SearchState.STILL_SEARCH;
        }
Esempio n. 11
0
        void UpdateGraphWithDistancesFromNode(Node source)
        {
            PriorityQueue<Node> unVisitedNodes = new PriorityQueue<Node>(Nodes); // Time to create a min heap - O(n)

            // Does this update the value of 'source' in Nodes ?
            source.Distance = 0;

            while (!unVisitedNodes.Empty()) // O(n)
            {
                Node current = unVisitedNodes.Peek();

                if (current.Distance == Constants.INFINITY)
                {
                    break;
                }

                foreach (Node neighbor in current.Neighbors) // O(nm)
                {
                    if (unVisitedNodes.Contains(neighbor))
                    {
                        int tentative = 0;
                        Edge edge = FindEdge(current, neighbor);  // O(nml)
                        tentative = current.Distance + edge.Cost;
                        if (tentative < neighbor.Distance)
                        {
                            neighbor.Distance = tentative;
                            neighbor.Previous = current;
                        }
                    }
                }

                unVisitedNodes.Dequeue();
            }
        }
        public void WorkPriorityQueue()
        {
            PriorityQueue<string> q1 = new PriorityQueue<string>();
            PriorityQueue<int> q2 = new PriorityQueue<int>();
            PriorityQueue<Group> q3 = new PriorityQueue<Group>();

            q1.Enqueue(new List<string> { "A", "B", "C" }, 5);
            q1.Enqueue(new List<string> { "F", "G", "H" }, 1);
            q1.Enqueue(new List<string> { "X", "V", "B" }, 0);
            q1.Enqueue(new List<string> { "Q", "W", "E" }, 1);
            q1.Enqueue("Z", 1);

            q2.Enqueue(new List<int> { 4, 5, 6 }, 5);
            q2.Enqueue(new List<int> { 1, 2, 3 }, 1);
            q2.Enqueue(new List<int> { 7, 8, 9 }, 0);
            q2.Enqueue(new List<int> { 0, 3, 5 }, 1);
            q2.Enqueue(9, 2);

            q3.Enqueue(new Group("CS1", 7), 1);
            q3.Enqueue(new Group("CS2", 14), 3);
            q3.Enqueue(new Group("CS3", 24), 5);
            q3.Enqueue(new Group("CS4", 31), 1);

            Console.WriteLine("Dequeue in Q1: " + q1.Dequeue());
            Console.WriteLine("First in Q1: " + q1.First());
            Console.WriteLine("First with priority = 1 in Q1: " + q1.First(1));
            Console.WriteLine("Last in Q1: " + q1.Last());
            Console.WriteLine("Last with priority = 1 in Q1: " + q1.Last(1));
            Console.WriteLine("Count in Q1: " + q1.Count);
            Console.WriteLine("Count with priority = 1 in Q1: " + q1.GetCount(1));

            Console.WriteLine("__________________________________________________");

            Console.WriteLine("Dequeue in Q2: " + q2.Dequeue());
            Console.WriteLine("First in Q2: " + q2.First());
            Console.WriteLine("First with priority = 1 in Q2: " + q2.First(1));
            Console.WriteLine("Last in Q2: " + q2.Last());
            Console.WriteLine("Last with priority = 1 in Q2: " + q2.Last(1));
            Console.WriteLine("Count in Q2: " + q2.Count);
            Console.WriteLine("Count with priority = 1 in Q2: " + q2.GetCount(1));

            Console.WriteLine("__________________________________________________");

            Console.WriteLine("Dequeue in Q3: " + q3.Dequeue().Title);
            Console.WriteLine("First in Q3: " + q3.First().Title);
            Console.WriteLine("First with priority = 1 in Q3: " + q3.First(1).Title);
            Console.WriteLine("Last in Q3: " + q3.Last().Title);
            Console.WriteLine("Last with priority = 1 in Q3: " + q3.Last(1).Title);
            Console.WriteLine("Count in Q3: " + q3.Count);
            Console.WriteLine("Count with priority = 1 in Q3: " + q3.GetCount(1));

            Console.WriteLine("__________________________________________________");
            bool q = q1.Contains(new Tuple<string, int>("7", 1));
            Console.WriteLine("Contains('7', 1) in Q1: " + q);
            q = q1.Contains(new Tuple<string, int>("Z", 1));
            Console.WriteLine("Contains('Z', 1) in Q1: " + q);

            q2.Clear();
            Console.WriteLine("Clear in Q2: " + q2.Count);

            Tuple<string, int>[] array = new Tuple<string, int>[15];

            q1.CopyTo(array, 0);

            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("Array[{0}] = ( {1}  {2})", i, array[i].Item2, array[i].Item1);
            }

            Console.ReadLine();
        }
Esempio n. 13
0
        public void TestRemove1()
        {
            var queue = new PriorityQueue<string> { "string", "anotherString" };

            var result = queue.Remove("string");

            Assert.IsTrue(result);
            Assert.AreEqual(1, queue.Count);
            Assert.IsFalse(queue.Contains("string"));
        }
Esempio n. 14
0
        public void TestPeek1()
        {
            var queue = new PriorityQueue<string> { "string", "anotherString" };

            var poll = queue.Peek();

            Assert.AreEqual(2, queue.Count);
            Assert.AreEqual("anotherString", poll);
            Assert.IsTrue(queue.Contains("anotherString"));
        }
Esempio n. 15
0
        public void TestPoll1()
        {
            var queue = new PriorityQueue<string> { "string", "anotherString" };

            var poll = queue.Poll();

            Assert.AreEqual(1, queue.Count);
            Assert.AreEqual("anotherString", poll);
            Assert.IsFalse(queue.Contains("anotherString"));
        }