コード例 #1
0
        public void TestNPuzzleInitialState()
        {
            int[] goal    = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] initial = { 1, 2, 3, 4, 5, 6, 7, 9, 8 };
            NPuzzleState <int[]> goalState    = new NPuzzleState <int[]>(goal);
            NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(initial);


            try {
                Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initialState);
                Assert.NotNull(problem);
                Assert.NotNull(problem.InitialState);
                Assert.AreEqual(problem.InitialState, initialState);
                Problem.AbstractProblem <NPuzzleState <int[]>, int, int> p2 = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initialState);
                Assert.NotNull(p2);
                Assert.True(p2.GoalTest(goalState));
                Assert.AreEqual(p2.Result(initialState, 1), goalState);
                List <int> results = new List <int>();
                results.Add(1);
                results.Add(-1);
                results.Add(2);
                Assert.AreEqual(p2.Actions(initialState), results);
            } catch (NPuzzleUtils.InvalidNPuzzleStatesException ex) {
            }
        }
コード例 #2
0
        public void TestNPuzzleProblemActions()
        {
            int[] initial = { 1, 2, 3, 4, 5, 6, 7, 9, 8 };

            NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(initial);

            Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = CreateProblem(initialState);
            List <int> actions  = problem.Actions(initialState);
            List <int> expected = new List <int>();

            expected.Add(-1);
            expected.Add(1);
            expected.Add(2);
            CollectionAssert.AreEquivalent(expected, actions);
            int[] initial2 = { 9, 7, 4, 2, 6, 3, 5, 8, 1 };

            NPuzzleState <int[]> initialState2 = new NPuzzleState <int[]>(initial2);

            problem  = CreateProblem(initialState2);
            actions  = problem.Actions(initialState2);
            expected = new List <int>();
            expected.Add(-2);
            expected.Add(1);
            CollectionAssert.AreEquivalent(expected, actions);
            int[] initial3 = { 2, 4, 8, 6, 3, 9, 7, 1, 5 };

            NPuzzleState <int[]> initialState3 = new NPuzzleState <int[]>(initial3);

            problem  = CreateProblem(initialState3);
            actions  = problem.Actions(initialState3);
            expected = new List <int>();
            expected.Add(-2);
            expected.Add(2);
            expected.Add(-1);
            CollectionAssert.AreEquivalent(expected, actions);
            int[] initial4 = { 2, 4, 8, 6, 9, 3, 7, 1, 5 };
            NPuzzleState <int[]> initialState4 = new NPuzzleState <int[]>(initial4);

            problem  = CreateProblem(initialState4);
            actions  = problem.Actions(initialState4);
            expected = new List <int>();
            expected.Add(-2);
            expected.Add(2);
            expected.Add(-1);
            expected.Add(1);
            CollectionAssert.AreEquivalent(expected, actions);
        }
コード例 #3
0
        public void TestNPuzzleProblemCreation()
        {
            int[] goal    = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] initial = { 1, 2, 3, 4, 5, 6, 7, 9, 8 };
            NPuzzleState <int[]> goalState    = new NPuzzleState <int[]>(goal);
            NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(initial);

            Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initialState);
            problem.Actions(goalState);

            Assert.Throws <NPuzzleUtils.InvalidNPuzzleStatesException>(() => new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(initialState, goalState));
        }