DotNet Reference

DotNet Reference

CpIsFunSat.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 
15 // [START program]
16 using System;
17 using Google.OrTools.Sat;
18 
19 // [START solution_printing]
21 {
22  public VarArraySolutionPrinter(IntVar[] variables)
23  {
24  variables_ = variables;
25  }
26 
27  public override void OnSolutionCallback()
28  {
29  {
30  foreach (IntVar v in variables_)
31  {
32  Console.Write(
33  String.Format(" {0}={1}", v.ShortString(), Value(v)));
34  }
35  Console.WriteLine();
36  solution_count_++;
37  }
38  }
39 
40  public int SolutionCount()
41  {
42  return solution_count_;
43  }
44 
45  private int solution_count_;
46  private IntVar[] variables_;
47 }
48 // [END solution_printing]
49 
50 public class CpIsFunSat
51 {
52  // Solve the CP+IS+FUN==TRUE cryptarithm.
53  static void Main()
54  {
55  // Constraint programming engine
56  CpModel model = new CpModel();
57 
58  // [START variables]
59  int kBase = 10;
60 
61  IntVar c = model.NewIntVar(1, kBase - 1, "C");
62  IntVar p = model.NewIntVar(0, kBase - 1, "P");
63  IntVar i = model.NewIntVar(1, kBase - 1, "I");
64  IntVar s = model.NewIntVar(0, kBase - 1, "S");
65  IntVar f = model.NewIntVar(1, kBase - 1, "F");
66  IntVar u = model.NewIntVar(0, kBase - 1, "U");
67  IntVar n = model.NewIntVar(0, kBase - 1, "N");
68  IntVar t = model.NewIntVar(1, kBase - 1, "T");
69  IntVar r = model.NewIntVar(0, kBase - 1, "R");
70  IntVar e = model.NewIntVar(0, kBase - 1, "E");
71 
72  // We need to group variables in a list to use the constraint AllDifferent.
73  IntVar[] letters = new IntVar[] {c, p, i, s, f, u, n, t, r, e};
74  // [END variables]
75 
76  // [START constraints]
77  // Define constraints.
78  model.AddAllDifferent(letters);
79 
80  // CP + IS + FUN = TRUE
81  model.Add(c * kBase + p + i * kBase + s + f * kBase * kBase + u * kBase + n ==
82  t * kBase * kBase * kBase + r * kBase * kBase + u * kBase + e);
83  // [END constraints]
84 
85  // [START solve]
86  // Creates a solver and solves the model.
87  CpSolver solver = new CpSolver();
89  solver.SearchAllSolutions(model, cb);
90  // [END solve]
91 
92  Console.WriteLine("Statistics");
93  Console.WriteLine($" - conflicts : {solver.NumConflicts()}");
94  Console.WriteLine($" - branches : {solver.NumBranches()}");
95  Console.WriteLine($" - wall time : {solver.WallTime()} s");
96  Console.WriteLine($" - number of solutions found: {cb.SolutionCount()}");
97  }
98 }
99 // [END program]
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
override string ShortString()
override void OnSolutionCallback()
Definition: CpIsFunSat.cs:27
IntVar NewIntVar(long lb, long ub, string name)
Definition: CpModel.cs:45
Constraint Add(BoundedLinearExpression lin)
Definition: CpModel.cs:104
CpSolverStatus SearchAllSolutions(CpModel model, SolutionCallback cb)
Definition: CpSolver.cs:52
long Value(LinearExpr e)
int SolutionCount()
Definition: CpIsFunSat.cs:40
Constraint AddAllDifferent(IEnumerable< IntVar > vars)
Definition: CpModel.cs:139
Definition: CpSolver.cs:20
VarArraySolutionPrinter(IntVar[] variables)
Definition: CpIsFunSat.cs:22
Definition: CpIsFunSat.cs:51
Definition: CpModel.pb.cs:12