Пример #1
0
        public void TestSolution()
        {
            int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal);

            //! -1
            int[] s1 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 };

            NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1);

            //! 2
            int[] s2 = { 1, 2, 3, 4, 9, 6, 7, 5, 8 };

            NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2);

            //! 1
            int[] s3 = { 1, 2, 3, 4, 6, 9, 7, 5, 8 };

            NPuzzleState <int[]> s3State = new NPuzzleState <int[]>(s3);

            //! 2
            int[] s4 = { 1, 2, 9, 4, 6, 3, 7, 5, 8 };

            NPuzzleState <int[]> s4State = new NPuzzleState <int[]>(s4);

            //! -1
            int[] s5 = { 1, 9, 2, 4, 6, 3, 7, 5, 8 };

            NPuzzleState <int[]> s5State = new NPuzzleState <int[]>(s5);

            //! -1
            int[] s6 = { 9, 1, 2, 4, 6, 3, 7, 5, 8 };

            NPuzzleState <int[]> s6State = new NPuzzleState <int[]>(s6);

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> rootNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(goalState);

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node1 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s1), rootNode, -1, 1);

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node2 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s2), node1, 2, 1);

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node3 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s3), node2, 1, 1);

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node4 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s4), node3, 2, 1);

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node5 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s5), node4, -1, 1);

            SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node6 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s6), node5, -1, 1);

            List <int> solution = node6.Solution();

            Assert.AreEqual(solution[0], -1);

            Assert.AreEqual(solution[1], -1);

            Assert.AreEqual(solution[2], 2);
        }
Пример #2
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);
            }
        }