Пример #1
0
        public void TestHashSet()
        {
            HashSet <NPuzzleState <int[]> > explored = new HashSet <NPuzzleState <int[]> >();
            int size = 9, num = 100;

            NPuzzleState <int[]>[] states = new NPuzzleState <int[]> [num];
            NPuzzleState <int[]>   state;

            int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal);

            for (int i = 0; i < num; i++)
            {
                state     = NPuzzleUtils.GenerateInitState(size);
                states[i] = state;
                explored.Add(state);
            }
            Assert.False(explored.Contains(goalState));
            explored.Add(goalState);

            for (int i = 0; i < num; i++)
            {
                Assert.True(explored.Contains(states[i]));
            }
            Assert.True(explored.Contains(goalState));
        }
Пример #2
0
        public void TestNodeCreation()
        {
            int size = 9;
            //TODO: change NPuzzleUtils.GenerateInitState
            NPuzzleState <int[]> state = NPuzzleUtils.GenerateInitState(size);

            System.Diagnostics.Debug.WriteLine("State: ");
            System.Diagnostics.Debug.WriteLine(state.State.ToString());
            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(state);
        }
Пример #3
0
        public void TestSomething()
        {
            int size = 9;

            int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal);

            NPuzzleState <int[]> initial = NPuzzleUtils.GenerateInitState(size);

            Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> npuzzle = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initial);

            HashSet <NPuzzleState <int[]> > explored = new HashSet <NPuzzleState <int[]> >();

            //! first node in the frontier is the initialState
            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(npuzzle.InitialState);
        }
Пример #4
0
        public void TestAStarGraphSearch()
        {
            int size = 9;

            int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal);
            NPuzzleState <int[]> initial   = NPuzzleUtils.GenerateInitState(size);

            Assert.True(NPuzzleUtils.AcceptableState(initial.State));

            Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> npuzzle = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initial);

            Assert.AreEqual(npuzzle.InitialState, initial);
            Assert.AreEqual(npuzzle.GoalState, goal);

            //Heuristics.HeuristicFunction<NPuzzleState<int[]>,int, int> handler = Heuristics.NPuzzleHeuristics.ManhattanDistance;
            Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.AStarManhattanDistance;

            try
            {
                Assert.AreEqual(npuzzle.InitialState, initial);
                // Search.BestFirstGraphSearch<int[],int,int> bfgs = new Search.BestFirstGraphSearch<int[],int,int>(npuzzle, handler);
                SearchTreeNode.Node <NPuzzleState <int[]>, int, int> initialNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(npuzzle.InitialState);
//				Search.PriorityQueue<int, SearchTreeNode.Node<NPuzzleState<int[]>,int,int>> frontier = new Search.PriorityQueue<int, SearchTreeNode.Node<NPuzzleState<int[]>,int,int>>();

                Search.AStarGraphSearch <NPuzzleState <int[]>, int, int> asgs = new Search.AStarGraphSearch <NPuzzleState <int[]>, int, int>(npuzzle, handler);

                SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = asgs.Search();
                List <int> solution = node.Solution();
                Console.WriteLine("Printing solution to AStar:");
                solution.ForEach(delegate(int a) {
                    Console.Write("{0} ", a);
                });
                Console.WriteLine("");
            } catch (NPuzzleUtils.InvalidProblemException ex)
            {
                System.Console.WriteLine("There is an InvalidProblemException here doode");
                System.Console.WriteLine(ex.Message);
                throw ex;
            } catch (NPuzzleUtils.InvalidProblemPropertyException ex)
            {
                throw ex;
            } catch (System.NullReferenceException ex)
            {
                Console.WriteLine(ex);
            }
        }
Пример #5
0
        private Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > GetPriorityQueueToo()
        {
            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);
            }

            return(frontier);
        }
Пример #6
0
        public void GenerateInitStateTest()
        {
            NPuzzleState <int[]> state;
            int size = 9;

            state = NPuzzleUtils.GenerateInitState(size);
            Assert.That(state.State, Has.Exactly(1).EqualTo(size));

            Assert.That(state.State, Has.Exactly(1).EqualTo(size - 4));

            Assert.That(state.State, Has.Exactly(size - 1).LessThan(size));

            Assert.That(state.State, Has.Exactly(size - 1).GreaterThan(1));
            Assert.That(state.State, Has.Exactly(size - 4).GreaterThan(4));


            Assert.AreEqual(NPuzzleUtils.AcceptableState(state.State), true);
            Console.WriteLine(state.ToString());
        }
Пример #7
0
        public void TestGetIncumbent()
        {
            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++)
            {
                Assert.AreEqual(nodeArray[i], frontier.GetIncumbent(nodeArray[i]));
            }
        }
Пример #8
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]);
            }
        }