예제 #1
0
        public static ISolver GetSolver()
        {
            // FIXME: Allow control over which solver is used

            // FIXME: Relative paths are fragile!
            // Look in the directory of the symbooglix binaries for solver
            var pathToSolver = "../../../Symbooglix/bin/{0}/z3".Replace('/', Path.DirectorySeparatorChar);

            // Depending on the build type choose which directory we search for the Solver
            #if DEBUG
            pathToSolver = String.Format(pathToSolver, "Debug");
            #else
            pathToSolver = String.Format(pathToSolver, "Release");
            #endif

            if (!File.Exists(pathToSolver))
            {
                pathToSolver += ".exe";
            }

            if (!File.Exists(pathToSolver))
            {
                Assert.Fail("Could not find solver at \"{0}\"", pathToSolver);
            }

            var solver = new SimpleSolver(new Z3SMTLIBSolver(/*useNamedAttributes=*/ true, pathToSolver, /*persistentProcess=*/ true, true));
            solver.SetTimeout(10);
            return(solver);
        }
예제 #2
0
        protected override ISolverBundle Solver(SlotClusterFixed <ConstrainedSlot, CylinderBrain> cluster)
        {
            typedSolver             = new SimpleSolver <ConstrainedSlot, ConstrainedSlotInfos, CylinderBrain>();
            typedSolver.slotCluster = cluster;
            solver = typedSolver;

            return(solver);
        }
        /// <summary>
        /// Solve the board
        /// </summary>
        /// <param name="boardData">the board to solve</param>
        /// <returns>true if a solution can be found</returns>
        public bool Solve(int[,] boardData)
        {
            if (SimpleSolver.IsConsistent(boardData))
            {
                this.SolveInternal(boardData);

                return(this.solutionCount > 0);
            }

            return(false);
        }
예제 #4
0
 public void TestInitialize()
 {
     if (Rc == null)
     {
         Rc = new Cube();
     }
     if (Solver == null)
     {
         Solver = new SimpleSolver(Rc);
     }
 }
예제 #5
0
        public async Task SolveTest(string path, int solutionsCount)
        {
            var absPath = Path.Combine(TestContext.CurrentContext.TestDirectory, path);

            var sudoku = await SudokuAdapter.ReadFromFileAsync(absPath);

            var solver = new SimpleSolver();

            var actualSolutionsCount = solver.CountSolutions(sudoku);

            Assert.AreEqual(solutionsCount, actualSolutionsCount);
        }
예제 #6
0
        public async Task TestTopNSolutions(string path, int solutionsLimit, Enums.TopType type, int expectedDifficulty)
        {
            var absPath = Path.Combine(TestContext.CurrentContext.TestDirectory, path);

            var sudoku = await SudokuAdapter.ReadFromFileAsync(absPath);

            var solver = new SimpleSolver();

            var solutions = solver.GetTopNSolutions(sudoku, solutionsLimit, type);

            Assert.AreEqual(solutionsLimit, solutions.Count);
            Assert.AreEqual(expectedDifficulty, solutions.First().DifficultyPoints);
        }
예제 #7
0
        public IActionResult Index(Dictionary <string, string> grid)
        {
            var model = new HomeViewModel {
                Grid = grid
            };
            var solver = new SimpleSolver(determineBoardSize(grid));
            var board  = convertBoard(grid);

            if (solver.Solve(board))
            {
                model.Grid = convertBoard(solver.GetFirstSolution());
            }

            return(View(model));
        }
예제 #8
0
        public void CanSolveBoards()
        {
            int i = 0;

            foreach (var board in GetBoards())
            {
                i++;
                var result = SimpleSolver.Solve(board);
                Assert.True(result.Solved);
            }


            //var result = Solver.Solve(GetBoards()[0]);
            //Assert.True(result.IsSolved);
        }
        public SudokuGrid CreateGrid(Difficulty difficulty)
        {
            ISudokuSolver solver = new SimpleSolver(seed: _seed, asGenerator: true);
            var           grid   = new SudokuGrid();

            PreAddToGrid(ref grid);

            var(success, solvedGrid) = solver.Solve(grid);

            // Minimize risk of getting a similar looking puzzle
            RandomizeLayout(ref solvedGrid);
            // Remove elements to match the difficulty
            RemoveElements(ref solvedGrid, difficulty);

            SetEnabledStates(ref solvedGrid);

            return(solvedGrid);
        }
예제 #10
0
파일: Form1.cs 프로젝트: ilyakom/Sudoku
        private async void SolveButton_Click(object sender, EventArgs e)
        {
            var solvedSudoku = await Task.Run(() =>
            {
                var solver = new SimpleSolver();
                return(solver.SolveSudoku(_model.CurrentSudoku));
            });


            if (solvedSudoku == null)
            {
                MessageBox.Show(@"This Sudoku does not have any solution");
            }
            else
            {
                _model.CurrentSudoku = solvedSudoku;
                UpdateLabelsUi();
            }
        }
예제 #11
0
        public static Executor GetExecutor(Program p, IStateScheduler scheduler = null, ISolver solver = null, bool useConstantFolding = false)
        {
            if (scheduler == null)
            {
                scheduler = new DFSStateScheduler();
            }

            if (solver == null)
            {
                solver = new SimpleSolver(new DummySolver());
            }

            IExprBuilder builder = new SimpleExprBuilder(/*immutable=*/ true);

            if (useConstantFolding)
            {
                builder = new ConstantFoldingExprBuilder(new ConstantCachingExprBuilder(builder));
            }

            Executor e = new Executor(p, scheduler, solver, builder, new SimpleSymbolicPool());

            return(e);
        }
예제 #12
0
        private int RemoveElementsRandomly(ref SudokuGrid grid, int toRemove)
        {
            if (toRemove == 0)
            {
                return(toRemove);
            }

            // Remove cells at random while attempting to keep a single unique solution:
            // Remove a cell at random, and attempt to solve the puzzle using remaining numbers,
            // If not possible the removal is a success. If possible, reset the value and move on.

            var values = Enumerable.Range(0, 80).ToList();

            values.Shuffle(_seed);

            var solver = new SimpleSolver();

            foreach (var cell in values)
            {
                var x = cell % 9;
                var y = cell / 9;

                if (grid.Grid[x, y].Value == 0)
                {
                    continue;
                }

                var originalValue = grid.Grid[x, y].Value;

                var complementNumbers = Enumerable.Range(1, 9).Except(new List <int>(grid.Grid[x, y].Value));
                var ambiguous         = false;

                foreach (var number in complementNumbers)
                {
                    var validValuesForCell = grid.ValidValues(x, y);

                    if (validValuesForCell.Contains(number))
                    {
                        // No need to check if it is not possible to place the number
                        continue;
                    }

                    grid.Grid[x, y].Value = number;

                    var tempGrid = grid.DeepClone();
                    var(success, _) = solver.Solve(tempGrid);
                    if (success)
                    {
                        //Console.WriteLine($"Failed check for ({x},{y}) for value {number}. Original: {originalValue}");
                        grid.Grid[x, y].Value = originalValue;
                        ambiguous             = true;
                        break;
                    }
                }

                if (ambiguous == false)
                {
                    Console.WriteLine($"Success check for ({x},{y}). Original: {originalValue}");
                    grid.Grid[x, y].Value = 0;
                    toRemove--;

                    if (toRemove == 0)
                    {
                        return(toRemove);
                    }
                }
            }

            return(toRemove);
        }