static void Main() { // Creates the model. // [START model] CpModel model = new CpModel(); // [END model] // Creates the variables. // [START variables] int num_vals = 3; IntVar x = model.NewIntVar(0, num_vals - 1, "x"); IntVar y = model.NewIntVar(0, num_vals - 1, "y"); IntVar z = model.NewIntVar(0, num_vals - 1, "z"); // [END variables] // Adds a different constraint. // [START constraints] model.Add(x != y); // [END constraints] // Creates a solver and solves the model. // [START solve] CpSolver solver = new CpSolver(); VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, y, z }); // Search for all solutions. solver.StringParameters = "enumerate_all_solutions:true"; // And solve. solver.Solve(model, cb); // [END solve] Console.WriteLine($"Number of solutions found: {cb.SolutionCount()}"); }
static void Main() { // Create the CP-SAT model. CpModel model = new CpModel(); // Declare our primary variable. IntVar x = model.NewIntVar(0, 20, "x"); // Create the expression variable and implement the step function // Note it is not defined for var == 2. // // - 3 // -- -- --------- 2 // 1 // -- --- 0 // 0 ================ 20 // IntVar expr = model.NewIntVar(0, 3, "expr"); // expr == 0 on [5, 6] U [8, 10] ILiteral b0 = model.NewBoolVar("b0"); model.AddLinearExpressionInDomain(x, Domain.FromValues(new long[] { 5, 6, 8, 9, 10 })) .OnlyEnforceIf(b0); model.Add(expr == 0).OnlyEnforceIf(b0); // expr == 2 on [0, 1] U [3, 4] U [11, 20] ILiteral b2 = model.NewBoolVar("b2"); model .AddLinearExpressionInDomain( x, Domain.FromIntervals(new long[][] { new long[] { 0, 1 }, new long[] { 3, 4 }, new long[] { 11, 20 } })) .OnlyEnforceIf(b2); model.Add(expr == 2).OnlyEnforceIf(b2); // expr == 3 when x == 7 ILiteral b3 = model.NewBoolVar("b3"); model.Add(x == 7).OnlyEnforceIf(b3); model.Add(expr == 3).OnlyEnforceIf(b3); // At least one bi is true. (we could use a sum == 1). model.AddBoolOr(new ILiteral[] { b0, b2, b3 }); // Search for x values in increasing order. model.AddDecisionStrategy(new IntVar[] { x }, DecisionStrategyProto.Types.VariableSelectionStrategy.ChooseFirst, DecisionStrategyProto.Types.DomainReductionStrategy.SelectMinValue); // Create the solver. CpSolver solver = new CpSolver(); // Force solver to follow the decision strategy exactly. solver.StringParameters = "search_branching:FIXED_SEARCH"; VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, expr }); solver.SearchAllSolutions(model, cb); }
static void Main() { // Creates the model. // [START model] CpModel model = new CpModel(); // [END model] // Creates the variables. // [START variables] int num_vals = 3; IntVar x = model.NewIntVar(0, num_vals - 1, "x"); IntVar y = model.NewIntVar(0, num_vals - 1, "y"); IntVar z = model.NewIntVar(0, num_vals - 1, "z"); // [END variables] // Adds a different constraint. // [START constraints] model.Add(x != y); // [END constraints] // Creates a solver and solves the model. // [START solve] CpSolver solver = new CpSolver(); VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, y, z }); solver.SearchAllSolutions(model, cb); // [END solve] Console.WriteLine(String.Format("Number of solutions found: {0}", cb.SolutionCount())); }
static void Main() { long earliness_date = 5; long earliness_cost = 8; long lateness_date = 15; long lateness_cost = 12; // Create the CP-SAT model. CpModel model = new CpModel(); // Declare our primary variable. IntVar x = model.NewIntVar(0, 20, "x"); // Create the expression variable and implement the piecewise linear // function. // // \ / // \______/ // ed ld // long large_constant = 1000; IntVar expr = model.NewIntVar(0, large_constant, "expr"); // First segment. IntVar s1 = model.NewIntVar(-large_constant, large_constant, "s1"); model.Add(s1 == earliness_cost * (earliness_date - x)); // Second segment. IntVar s2 = model.NewConstant(0); // Third segment. IntVar s3 = model.NewIntVar(-large_constant, large_constant, "s3"); model.Add(s3 == lateness_cost * (x - lateness_date)); // Link together expr and x through s1, s2, and s3. model.AddMaxEquality(expr, new IntVar[] { s1, s2, s3 }); // Search for x values in increasing order. model.AddDecisionStrategy( new IntVar[] { x }, DecisionStrategyProto.Types.VariableSelectionStrategy.ChooseFirst, DecisionStrategyProto.Types.DomainReductionStrategy.SelectMinValue); // Create the solver. CpSolver solver = new CpSolver(); // Force solver to follow the decision strategy exactly. solver.StringParameters = "search_branching:FIXED_SEARCH"; VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, expr }); solver.SearchAllSolutions(model, cb); }
public void Solve() { // Creates the model. CpModel model = new CpModel(); // Creates the variables. int num_vals = 9; //Creat the Sudoku grid IntVar[][] grid = new IntVar[9][]; for (int k = 0; k < 9; k++) { grid[k] = new IntVar[9]; } //get initial sudoku grid and put the 9 possibilities in Empty cells for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (Sudoku.getCaseInitialSudoku(i, j) == 0) { grid[i][j] = model.NewIntVar(1, num_vals, "C" + i.ToString() + j.ToString()); } else { grid[i][j] = model.NewIntVar(Sudoku.getCaseInitialSudoku(i, j), Sudoku.getCaseInitialSudoku(i, j), "C" + i.ToString() + j.ToString()); } } } // Adds a different constraint. for (int k = 0; k < 9; k++) { //All the ligne might have a different number in each cells model.AddAllDifferent(GetRow(grid, k)); //All the columns might have a different number in each cells model.AddAllDifferent(GetColumn(grid, k)); } //All 9 Regions might have a different number in each cells for (int region = 0; region < 9; region++) { model.AddAllDifferent(GetRegion(grid, region)); } // Creates a solver and solves the model. CpSolver solver = new CpSolver(); VarArraySolutionPrinter cb = new VarArraySolutionPrinter(grid); solver.SearchAllSolutions(model, cb); int[][] values = cb.getValues(); Sudoku.setSudoku(values); }
// [END solution_printer] // Solve the CP+IS+FUN==TRUE cryptarithm. static void Main() { // Constraint programming engine // [START model] CpModel model = new CpModel(); // [START model] // [START variables] int kBase = 10; IntVar c = model.NewIntVar(1, kBase - 1, "C"); IntVar p = model.NewIntVar(0, kBase - 1, "P"); IntVar i = model.NewIntVar(1, kBase - 1, "I"); IntVar s = model.NewIntVar(0, kBase - 1, "S"); IntVar f = model.NewIntVar(1, kBase - 1, "F"); IntVar u = model.NewIntVar(0, kBase - 1, "U"); IntVar n = model.NewIntVar(0, kBase - 1, "N"); IntVar t = model.NewIntVar(1, kBase - 1, "T"); IntVar r = model.NewIntVar(0, kBase - 1, "R"); IntVar e = model.NewIntVar(0, kBase - 1, "E"); // We need to group variables in a list to use the constraint AllDifferent. IntVar[] letters = new IntVar[] { c, p, i, s, f, u, n, t, r, e }; // [END variables] // [START constraints] // Define constraints. model.AddAllDifferent(letters); // CP + IS + FUN = TRUE model.Add(c * kBase + p + i * kBase + s + f * kBase * kBase + u * kBase + n == t * kBase * kBase * kBase + r * kBase * kBase + u * kBase + e); // [END constraints] // [START solve] // Creates a solver and solves the model. CpSolver solver = new CpSolver(); VarArraySolutionPrinter cb = new VarArraySolutionPrinter(letters); // Search for all solutions. solver.StringParameters = "enumerate_all_solutions:true"; // And solve. solver.Solve(model, cb); // [END solve] // [START statistics] Console.WriteLine("Statistics"); Console.WriteLine($" conflicts : {solver.NumConflicts()}"); Console.WriteLine($" branches : {solver.NumBranches()}"); Console.WriteLine($" wall time : {solver.WallTime()} s"); Console.WriteLine($" number of solutions found: {cb.SolutionCount()}"); // [END statistics] }
static void Main() { // Create the CP-SAT model. CpModel model = new CpModel(); // Declare our two primary variables. IntVar x = model.NewIntVar(0, 10, "x"); IntVar y = model.NewIntVar(0, 10, "y"); // Declare our intermediate boolean variable. IntVar b = model.NewBoolVar("b"); // Implement b == (x >= 5). model.Add(x >= 5).OnlyEnforceIf(b); model.Add(x < 5).OnlyEnforceIf(b.Not()); // Create our two half-reified constraints. // First, b implies (y == 10 - x). model.Add(y == 10 - x).OnlyEnforceIf(b); // Second, not(b) implies y == 0. model.Add(y == 0).OnlyEnforceIf(b.Not()); // Search for x values in increasing order. model.AddDecisionStrategy( new IntVar[] { x }, DecisionStrategyProto.Types.VariableSelectionStrategy.ChooseFirst, DecisionStrategyProto.Types.DomainReductionStrategy.SelectMinValue); // Create the solver. CpSolver solver = new CpSolver(); // Force solver to follow the decision strategy exactly. solver.StringParameters = "search_branching:FIXED_SEARCH"; VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, y, b }); solver.SearchAllSolutions(model, cb); }
static void Main() { // Creates the model. // [START model] CpModel model = new CpModel(); // [END model] // Creates the variables. // [START variables] int num_vals = 3; IntVar x = model.NewIntVar(0, num_vals - 1, "x"); IntVar y = model.NewIntVar(0, num_vals - 1, "y"); IntVar z = model.NewIntVar(0, num_vals - 1, "z"); // [END variables] // Creates the constraints. // [START constraints] model.Add(x != y); // [END constraints] // Solution hinting: x <- 1, y <- 2 model.AddHint(x, 1); model.AddHint(y, 2); // [START objective] model.Maximize(LinearExpr.ScalProd(new IntVar[] { x, y, z }, new int[] { 1, 2, 3 })); // [END objective] // Creates a solver and solves the model. // [START solve] CpSolver solver = new CpSolver(); VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, y, z }); CpSolverStatus status = solver.SolveWithSolutionCallback(model, cb); // [END solve] }
static void MinimalCpSatAllSolutions() { // Creates the model. CpModel model = new CpModel(); // Creates the variables. int num_vals = 3; IntVar x = model.NewIntVar(0, num_vals - 1, "x"); IntVar y = model.NewIntVar(0, num_vals - 1, "y"); IntVar z = model.NewIntVar(0, num_vals - 1, "z"); // Creates the constraints. model.Add(x != y); // Creates a solver and solves the model. CpSolver solver = new CpSolver(); VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, y, z }); solver.SearchAllSolutions(model, cb); Console.WriteLine(String.Format("Number of solutions found: {0}", cb.SolutionCount())); }
static void Main() { // Model. CpModel model = new CpModel(); // Variables. IntVar x = model.NewIntVar(0, 10, "x"); IntVar y = model.NewIntVar(0, 10, "y"); IntVar b = model.NewBoolVar("b"); // Implement b == (x >= 5). model.Add(x >= 5).OnlyEnforceIf(b); model.Add(x < 5).OnlyEnforceIf(b.Not()); // b implies (y == 10 - x). model.Add(y == 10 - x).OnlyEnforceIf(b); // not(b) implies y == 0. model.Add(y == 0).OnlyEnforceIf(b.Not()); // Search for x values in increasing order. model.AddDecisionStrategy( new IntVar[] { x }, DecisionStrategyProto.Types.VariableSelectionStrategy.ChooseFirst, DecisionStrategyProto.Types.DomainReductionStrategy.SelectMinValue); // Create a solver and solve with a fixed search. CpSolver solver = new CpSolver(); // Force solver to follow the decision strategy exactly. solver.StringParameters = "search_branching:FIXED_SEARCH"; VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] { x, y, b }); solver.SearchAllSolutions(model, cb); }