Exemple #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) {
            }
        }
Exemple #2
0
        public void TestNPuzzleProblemResult()
        {
            int[] state = { 7, 6, 4, 9, 8, 2, 5, 3, 1 };
            NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(state);

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

            NPuzzleState <int[]> r1State = new NPuzzleState <int[]>(r1);

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

            NPuzzleState <int[]> r2State = new NPuzzleState <int[]>(r2);

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

            NPuzzleState <int[]> r3State = new NPuzzleState <int[]>(r3);

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

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

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

            NPuzzleState <int[]> s2r1State = new NPuzzleState <int[]>(s2r1);

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

            NPuzzleState <int[]> s2r2State = new NPuzzleState <int[]>(s2r2);

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

            NPuzzleState <int[]> s2r3State = new NPuzzleState <int[]>(s2r3);

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

            CollectionAssert.AreEquivalent(r1State, problem.Result(initialState, -2));

            CollectionAssert.AreEquivalent(r2State, problem.Result(initialState, 2));

            CollectionAssert.AreEquivalent(r3State, problem.Result(initialState, 2));

            CollectionAssert.AreEquivalent(s2r1State, problem.Result(s2State, 2));

            CollectionAssert.AreEquivalent(s2r2State, problem.Result(s2State, -2));

            CollectionAssert.AreEquivalent(s2r3State, problem.Result(s2State, 1));
            Assert.Throws <NPuzzleUtils.ResultAcceptableActionException>(() => problem.Result(initialState, 0));
        }