コード例 #1
0
ファイル: Model.cs プロジェクト: souka1/Optimization
        public IEnumerable <GridCell[]> Solve(IEnumerable <GridCell> hints)
        {
            var context = Microsoft.SolverFoundation.Services.SolverContext.GetContext();
            var model   = context.CreateModel();

            model.Name = "Sudoku";

            var digits = Microsoft.SolverFoundation.Services.Domain.IntegerRange(1, 9);
            var grid   = model.CreateVariables(digits);

            model.CreateConstraints(grid);
            model.AddHints(model.Decisions.ToArray(), hints);

            var solution = context.Solve(new Microsoft.SolverFoundation.Services.ConstraintProgrammingDirective());

            var results = new List <GridCell[]>();

            while (solution.Quality != Microsoft.SolverFoundation.Services.SolverQuality.Infeasible)
            {
                if (results.Count() > MAX_SOLUTIONS)
                {
                    throw new TooManySolutionsException(MAX_SOLUTIONS);
                }

                var solutionDetails = new GridCell[81];
                for (int i = 0; i < 81; i++)
                {
                    byte xLoc  = Convert.ToByte(i % 9);
                    byte yLoc  = Convert.ToByte(i / 9);
                    var  value = Convert.ToByte(solution.Decisions.ToArray()[i].GetValues().Single().Single());
                    solutionDetails[i] = GridCell.Create(xLoc, yLoc, value);
                }
                results.Add(solutionDetails);

                solution.GetNext();
            }

            if (!results.Any())
            {
                throw new NoFeasibleSolutionException();
            }


            var report = solution.GetReport(Microsoft.SolverFoundation.Services.ReportVerbosity.SolverDetails);

            this.SolverName = report.SolverType.FullName;

            return(results);
        }
コード例 #2
0
        public IEnumerable <GridCell[]> Solve(IEnumerable <GridCell> hints)
        {
            var model = new Solver("CPSolver");

            IntVar[] x = CreateVariables(model); // Create variables
            CreateConstraints(model, x);         // Create constraints
            AddHints(model, x, hints);           // Add solution hints (sudoku given values)

            DecisionBuilder decisionBuilder    = model.MakePhase(x, Solver.INT_VAR_DEFAULT, Solver.INT_VALUE_DEFAULT);
            var             optimizationStatus = model.Solve(decisionBuilder);

            if (!optimizationStatus)
            {
                throw new NoFeasibleSolutionException();
            }

            // Iterate solutions
            var results = new List <GridCell[]>();

            while (model.NextSolution())
            {
                if (results.Count() > MAX_SOLUTIONS)
                {
                    throw new TooManySolutionsException(MAX_SOLUTIONS);
                }

                var solution = new GridCell[81];

                for (int i = 0; i < 81; i++)
                {
                    byte xLoc = Convert.ToByte(i % 9);
                    byte yLoc = Convert.ToByte(i / 9);
                    solution[i] = GridCell.Create(xLoc, yLoc, Convert.ToByte(x[i].Value()));
                }

                results.Add(solution);
            }

            this.SolverName = model.GetType().FullName;

            return(results);
        }
コード例 #3
0
 public static void AddHint(this IList <GridCell> hints, byte x, byte y, byte value)
 {
     hints.Add(GridCell.Create(x, y, value));
 }