OR-Tools  7.1
CpIsFunSat.java
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 // [START program]
18 import com.google.ortools.sat.IntVar;
20 
22 public class CpIsFunSat {
23  static {
24  System.loadLibrary("jniortools");
25  }
26 
27  // [START solution_printing]
28  static class VarArraySolutionPrinter extends CpSolverSolutionCallback {
29  public VarArraySolutionPrinter(IntVar[] variables) {
30  variableArray = variables;
31  }
32 
33  @Override
34  public void onSolutionCallback() {
35  for (IntVar v : variableArray) {
36  System.out.printf(" %s = %d", v.getName(), value(v));
37  }
38  System.out.println();
39  solutionCount++;
40  }
41 
42  public int getSolutionCount() {
43  return solutionCount;
44  }
45 
46  private int solutionCount;
47  private final IntVar[] variableArray;
48  }
49  // [END solution_printing]
50 
51  public static void main(String[] args) throws Exception {
52  // Create the model.
53  CpModel model = new CpModel();
54 
55  // [START variables]
56  int base = 10;
57  IntVar c = model.newIntVar(1, base - 1, "C");
58  IntVar p = model.newIntVar(0, base - 1, "P");
59  IntVar i = model.newIntVar(1, base - 1, "I");
60  IntVar s = model.newIntVar(0, base - 1, "S");
61  IntVar f = model.newIntVar(1, base - 1, "F");
62  IntVar u = model.newIntVar(0, base - 1, "U");
63  IntVar n = model.newIntVar(0, base - 1, "N");
64  IntVar t = model.newIntVar(1, base - 1, "T");
65  IntVar r = model.newIntVar(0, base - 1, "R");
66  IntVar e = model.newIntVar(0, base - 1, "E");
67 
68  // We need to group variables in a list to use the constraint AllDifferent.
69  IntVar[] letters = new IntVar[] {c, p, i, s, f, u, n, t, r, e};
70  // [END variables]
71 
72  // [START constraints]
73  // Define constraints.
74  model.addAllDifferent(letters);
75 
76  // CP + IS + FUN = TRUE
77  model.addEquality(LinearExpr.scalProd(new IntVar[] {c, p, i, s, f, u, n, t, r, u, e},
78  new long[] {base, 1, base, 1, base * base, base, 1, -base * base * base,
79  -base * base, -base, -1}),
80  0);
81  // [END constraints]
82 
83  // [START solve]
84  // Create a solver and solve the model.
85  CpSolver solver = new CpSolver();
86  VarArraySolutionPrinter cb = new VarArraySolutionPrinter(letters);
87  solver.searchAllSolutions(model, cb);
88  // [END solve]
89 
90  System.out.println("Statistics");
91  System.out.println(" - conflicts : " + solver.numConflicts());
92  System.out.println(" - branches : " + solver.numBranches());
93  System.out.println(" - wall time : " + solver.wallTime() + " s");
94  System.out.println(" - solutions : " + cb.getSolutionCount());
95  }
96 }
97 // [END program]
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
Cryptarithmetic puzzle.
Definition: CpIsFunSat.java:22
Constraint addEquality(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:153
Wrapper around the SAT solver.
Definition: CpSolver.java:26
IntVar newIntVar(long lb, long ub, String name)
Creates an integer variable with domain [lb, ub].
Definition: CpModel.java:69
Constraint addAllDifferent(IntVar[] variables)
Adds.
Definition: CpModel.java:249
static void main(String[] args)
Definition: CpIsFunSat.java:51
long numConflicts()
Returns the number of conflicts created during search.
Definition: CpSolver.java:104
Main modeling class.
Definition: CpModel.java:40
static LinearExpr scalProd(IntVar[] variables, long[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:33
CpSolverStatus searchAllSolutions(CpModel model, CpSolverSolutionCallback cb)
Searches for all solutions of a satisfiability problem.
Definition: CpSolver.java:57
long numBranches()
Returns the number of branches explored during search.
Definition: CpSolver.java:99
Parent class to create a callback called at each solution.
double wallTime()
Returns the wall time of the search.
Definition: CpSolver.java:109