Esempio n. 1
0
        public static Sudoku.Core.GrilleSudoku SolveSudoku(Sudoku.Core.GrilleSudoku s, BaseModel model)
        {
            var features = GetFeatures(s);

            while (true)
            {
                var output = model.Predict(features.reshape(1, 9, 9, 1));
                output = output.squeeze();
                var prediction = np.argmax(output, axis: 2).reshape(9, 9) + 1;
                var proba      = np.around(np.max(output, axis: new[] { 2 }).reshape(9, 9), 2);

                features = DeNormalize(features);
                var mask = features.@equals(0);
                if (((int)mask.sum()) == 0)
                {
                    break;
                }

                var probNew = proba * mask;
                var ind     = (int)np.argmax(probNew);
                var(x, y) = ((ind / 9), ind % 9);
                var val = prediction[x][y];
                features[x][y] = val;
                features       = Normalize(features);
            }

            return(GetSudoku(features));
        }
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 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. 4
0
        public int NbErrors(GrilleSudoku originalPuzzle)
        {
            // We use a large lambda expression to count duplicates in rows, columns and boxes
            var toReturn    = GrilleSudoku.TousLesVoisinages.Select(n => n.Select(nx => this.Cellules[nx])).Sum(n => n.GroupBy(x => x).Select(g => g.Count() - 1).Sum());
            var cellsToTest = this.Cellules.Select((c, i) => new { index = i, cell = c }).ToList();

            toReturn += cellsToTest.Count(x => originalPuzzle.Cellules[x.index] > 0 && originalPuzzle.Cellules[x.index] != x.cell); // Mask
            return(toReturn);
        }
Esempio n. 5
0
        public static Sudoku.Core.GrilleSudoku SolveSudoku(Sudoku.Core.GrilleSudoku s, BaseModel model)
        {
            var features = GetFeatures(s);

            while (true)
            {
                var output = model.Predict(features.reshape(1, 9, 9, 1));
                output = output.squeeze();
                var prediction = np.argmax(output, axis: 1).reshape(9, 9) + 1;
                var proba      = np.around(np.max(output, axis: new[] { 1 }).reshape(9, 9), 1);

                features = DeNormalize(features);
                var mask = features.@equals(0);
                if (((int)mask.sum()) == 0)
                {
                    break;
                }

                var probNew   = proba * mask;
                var threshold = 1.0;
                var ind_list  = np.argwhere(probNew >= threshold);

                while (ind_list.len == 0)
                {
                    threshold = threshold - 0.1;
                    if (threshold >= 0.5)
                    {
                        ind_list = np.argwhere(probNew >= threshold);

                        if (ind_list.len > 0)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                Console.WriteLine(ind_list);
                for (int i = 0; i < ind_list.len; i++)

                {
                    var x   = ind_list[i][0];
                    var y   = ind_list[i][1];
                    var val = prediction[x][y];
                    features[x][y] = val;
                }
                features = Normalize(features);
            }

            return(GetSudoku(features));
        }
Esempio n. 6
0
        BoolExpr GetPuzzleConstraint(Sudoku.Core.GrilleSudoku instance)
        {
            BoolExpr instance_c = z3Context.MkTrue();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (instance.GetCellule(i, j) != 0)
                    {
                        instance_c = z3Context.MkAnd(instance_c,
                                                     (BoolExpr)
                                                     z3Context.MkEq(X[i][j], z3Context.MkInt(instance.GetCellule(i, j))));
                    }
                }
            }
            return(instance_c);
        }
Esempio n. 7
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. 8
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));
                }
            }
        }
Esempio n. 10
0
 public static NDarray GetFeatures(Sudoku.Core.GrilleSudoku objSudoku)
 {
     return(Normalize(np.array(objSudoku.Cellules.ToArray()).reshape(9, 9)));
 }
Esempio n. 11
0
 public Sudoku.Core.GrilleSudoku Solve(Sudoku.Core.GrilleSudoku s)
 {
     return(NeuralNetHelper.SolveSudoku(s, model));
 }
Esempio n. 12
0
 public bool IsValid(GrilleSudoku originalPuzzle)
 {
     return(NbErrors(originalPuzzle) == 0);
 }