コード例 #1
0
        public void Solve_BacktrackSolver_NoSolution(int n)
        {
            var problem  = NQueens.CreateProblem(n);
            var solver   = new RecursiveBacktrackSolver <int, int>();
            var solution = solver.Solve(problem, CancellationToken.None);

            Assert.IsNull(solution);
        }
コード例 #2
0
        public void Solve_BacktrackSolver_VariablesReverseOrder()
        {
            var problem  = NQueens.CreateProblem();
            var solver   = new RecursiveBacktrackSolver <int, int>(variableSelectionStrategy: new ComparerVariableSelectionStrategy <int, int>((a, b) => b.UserObject.CompareTo(a.UserObject)));
            var solution = solver.Solve(problem, CancellationToken.None);

            var expected = new Dictionary <int, int>
            {
                { 1, 4 }, { 2, 2 }, { 3, 7 }, { 4, 3 }, { 5, 6 }, { 6, 8 }, { 7, 5 }, { 8, 1 },
            };

            CollectionAssert.AreEquivalent(expected, solution.AsReadOnlyDictionary().OrderBy(kv => kv.Key.UserObject).ToDictionary(kv => kv.Key.UserObject, kv => kv.Value));
        }
コード例 #3
0
        public void Solve_BacktrackSolver()
        {
            var problem  = NQueens.CreateProblem();
            var solver   = new RecursiveBacktrackSolver <int, int>();
            var solution = solver.Solve(problem, CancellationToken.None);

            var expected = new Dictionary <int, int>
            {
                { 1, 1 }, { 2, 5 }, { 3, 8 }, { 4, 6 }, { 5, 3 }, { 6, 7 }, { 7, 2 }, { 8, 4 },
            };

            CollectionAssert.AreEquivalent(expected, solution.AsReadOnlyDictionary().OrderBy(kv => kv.Key.UserObject).ToDictionary(kv => kv.Key.UserObject, kv => kv.Value));
        }
コード例 #4
0
        public void Solve_BacktrackSolver_MinimumRemainingValueVariableSelectionStrategy()
        {
            var problem  = NQueens.CreateProblem();
            var solver   = new RecursiveBacktrackSolver <int, int>(variableSelectionStrategy: new MinimumRemainingValueVariableSelectionStrategy <int, int>());
            var solution = solver.Solve(problem, CancellationToken.None);

            Assert.IsNotNull(solution);

            // TODO: Not always consistent, need to figure out why.
            //var expected = new Dictionary<int, int>
            //{
            //    {1, 1}, {2, 5}, {3, 8}, {4, 6}, {5, 3}, {6, 7}, {7, 2}, {8, 4},
            //};
            //CollectionAssert.AreEquivalent(expected, solution.AsReadOnlyDictionary().OrderBy(kv => kv.Key.UserObject).ToDictionary(kv => kv.Key.UserObject, kv => kv.Value));
        }