Beispiel #1
0
        public void TestPop()
        {
            Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > f = GetPriorityQueue();
            int c = f.Count();
            int i = 0;

            int[] heurs = new int[f.Count()];

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node;
            while (f.Count() > 0)
            {
                node     = f.Pop();
                heurs[i] = NPuzzleHeuristics.ManhattanDistance(node);
                i++;
            }

            for (int j = 0; j < i - 1; j++)
            {
                Assert.True(heurs[j] <= heurs[j + 1]);
            }
            Assert.AreEqual(i, c);
            Assert.IsNull(f.Pop());
        }
Beispiel #2
0
        public void TestAppendToo()
        {
            Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >   handler  = Heuristics.NPuzzleHeuristics.ManhattanDistance;
            Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler);
            int size = 9;

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int>[] nodeArray = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> [100];
            SearchTreeNode.Node <NPuzzleState <int[]>, int, int>   node;
            for (int i = 0; i < 100; i++)
            {
                NPuzzleState <int[]> istate = NPuzzleUtils.GenerateInitState(size);
                node         = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(istate);
                nodeArray[i] = node;
                //int heur = NPuzzleHeuristics.ManhattanDistance(node);
                frontier.Append(node);
            }

            for (int i = 0; i < 100; i++)
            {
                int heur = NPuzzleHeuristics.ManhattanDistance(nodeArray[i]);
                Assert.True(frontier.InPriorityQueue(nodeArray[i]));
            }
            int j = 0;

            int[] heurArray = new int[100];

            while (frontier.Count() > 0)
            {
                node         = frontier.Pop();
                heurArray[j] = NPuzzleHeuristics.ManhattanDistance(node);
                j++;
            }

            for (j = 0; j < 99; j++)
            {
                Assert.True(heurArray[j] <= heurArray[j + 1]);
            }
        }
Beispiel #3
0
        /*! Test removing elements from the frontier
         */
        public void TestCount()
        {
            Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > f        = GetPriorityQueue();
            SortedList <int, List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > >   frontier = f.GetPriorityQueue();

            Assert.AreEqual(f.Count(), 10);
            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = f.Pop();
            int[] s = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Assert.AreEqual(node.state, s);
            Assert.AreEqual(f.Count(), 9);
            int[] s1 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 };
            //! md = 1
            int[] s7 = { 1, 2, 3, 4, 5, 9, 7, 8, 6 };
            //! 2, md = 2
            int[] s2 = { 1, 2, 3, 4, 9, 6, 7, 5, 8 };
            node = f.Pop();
            Assert.AreEqual(node.state, s1);
            Assert.AreEqual(f.Count(), 8);

            node = f.Pop();
            Assert.AreEqual(node.state, s7);
            Assert.AreEqual(f.Count(), 7);

            node = f.Pop();
            Assert.AreEqual(node.state, s2);
            Assert.AreEqual(f.Count(), 6);

            node = f.Pop();
            Assert.AreEqual(f.Count(), 5);
            node = f.Pop();
            Assert.AreEqual(f.Count(), 4);
            node = f.Pop();
            Assert.AreEqual(f.Count(), 3);
            node = f.Pop();
            Assert.AreEqual(f.Count(), 2);
            node = f.Pop();
            Assert.AreEqual(f.Count(), 1);
            node = f.Pop();
            Assert.AreEqual(f.Count(), 0);
        }