Esempio n. 1
0
        protected Sudoku.Core.GrilleSudoku SolveOriginalCleanup(Sudoku.Core.GrilleSudoku instance)
        {
            BoolExpr instance_c = GetPuzzleConstraint(instance);
            var      z3Solver   = GetSolver();

            z3Solver.Assert(GenericContraints);
            z3Solver.Assert(instance_c);

            if (z3Solver.Check() == Status.SATISFIABLE)
            {
                Model m = z3Solver.Model;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (instance.GetCellule(i, j) == 0)
                        {
                            instance.SetCell(i, j, ((IntNum)m.Evaluate(X[i][j])).Int);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed to solve sudoku");
            }
            return(instance);
        }
Esempio n. 2
0
        protected Sudoku.Core.GrilleSudoku SolveWithScope(Sudoku.Core.GrilleSudoku instance)
        {
            var z3Solver = GetReusableSolver();

            z3Solver.Push();
            BoolExpr instance_c = GetPuzzleConstraint(instance);

            z3Solver.Assert(instance_c);

            if (z3Solver.Check() == Status.SATISFIABLE)
            {
                Model m = z3Solver.Model;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (instance.GetCellule(i, j) == 0)
                        {
                            instance.SetCell(i, j, ((IntNum)m.Evaluate(X[i][j])).Int);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed to solve sudoku");
            }
            z3Solver.Pop();
            return(instance);
        }
Esempio n. 3
0
        protected Sudoku.Core.GrilleSudoku SolveWithSubstitutions(Sudoku.Core.GrilleSudoku instance)
        {
            var substExprs = new List <Expr>();
            var substVals  = new List <Expr>();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (instance.GetCellule(i, j) != 0)
                    {
                        substExprs.Add(X[i][j]);
                        substVals.Add(z3Context.MkInt(instance.GetCellule(i, j)));
                    }
                }
            }
            BoolExpr instance_c = (BoolExpr)GenericContraints.Substitute(substExprs.ToArray(), substVals.ToArray());

            var z3Solver = GetSolver();

            z3Solver.Assert(instance_c);

            if (z3Solver.Check() == Status.SATISFIABLE)
            {
                Model m = z3Solver.Model;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (instance.GetCellule(i, j) == 0)
                        {
                            instance.SetCell(i, j, ((IntNum)m.Evaluate(X[i][j])).Int);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed to solve sudoku");
            }
            return(instance);
        }
Esempio n. 4
0
        protected Sudoku.Core.GrilleSudoku SolveOriginalVersion(Sudoku.Core.GrilleSudoku instance)
        {
            BoolExpr instance_c = z3Context.MkTrue();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    instance_c = z3Context.MkAnd(instance_c,
                                                 (BoolExpr)
                                                 z3Context.MkITE(z3Context.MkEq(z3Context.MkInt(instance.GetCellule(i, j)), z3Context.MkInt(0)),
                                                                 z3Context.MkTrue(),
                                                                 z3Context.MkEq(X[i][j], z3Context.MkInt(instance.GetCellule(i, j)))));
                }
            }

            Solver s = z3Context.MkSolver();

            s.Assert(GenericContraints);
            s.Assert(instance_c);

            if (s.Check() == Status.SATISFIABLE)
            {
                Model m = s.Model;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        instance.SetCell(i, j, ((IntNum)m.Evaluate(X[i][j])).Int);
                    }
                }
                return(instance);
            }
            else
            {
                Console.WriteLine("Failed to solve sudoku");
                return(instance);
            }
        }
        public void Solve(Sudoku.Core.GrilleSudoku s)
        {
            var sRows = new List <string>();
            var cols  = Enumerable.Range(0, 9);

            for (int i = 0; i < 9; i++)
            {
                sRows.Add(string.Concat(cols.Select(c => s.GetCellule(i, c))
                                        .Select(val => val == 0 ? " " : val.ToString())));
            }
            //var grid = new Grid(ImmutableList.Create(
            //    "6 4 9 7 3",
            //    "  3    6 ",
            //    "       18",
            //    "   18   9",
            //    "     43  ",
            //    "7   39   ",
            //    " 7       ",
            //    " 4    8  ",
            //    "9 8 6 4 5"));

            var grid         = new Grid(ImmutableList.Create(sRows.ToArray()));
            var internalRows = BuildInternalRowsForGrid(grid);
            var dlxRows      = BuildDlxRows(internalRows);
            var solutions    = new Dlx()
                               .Solve(dlxRows, d => d, r => r)
                               .Where(solution => VerifySolution(internalRows, solution))
                               .ToImmutableList();
            var grille = SolutionToGrid(internalRows, solutions[0]);

            for (int rowIndex = 0; rowIndex < 9; rowIndex++)
            {
                for (int coIndex = 0; coIndex < 9; coIndex++)
                {
                    s.SetCell(rowIndex, coIndex, grille.ValueAt(rowIndex, coIndex));
                }
            }
        }