SolveAndPrintIntermediateSolutionsSampleSat.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 
23  static {
24  System.loadLibrary("jniortools");
25  }
26 
27  // [START print_solution]
28  static class VarArraySolutionPrinterWithObjective extends CpSolverSolutionCallback {
29  public VarArraySolutionPrinterWithObjective(IntVar[] variables) {
30  variableArray = variables;
31  }
32 
33  @Override
34  public void onSolutionCallback() {
35  System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, wallTime());
36  System.out.printf(" objective value = %f%n", objectiveValue());
37  for (IntVar v : variableArray) {
38  System.out.printf(" %s = %d%n", v.getName(), value(v));
39  }
40  solutionCount++;
41  }
42 
43  public int getSolutionCount() {
44  return solutionCount;
45  }
46 
47  private int solutionCount;
48  private final IntVar[] variableArray;
49  }
50  // [END print_solution]
51 
52  public static void main(String[] args) throws Exception {
53  // Create the model.
54  // [START model]
55  CpModel model = new CpModel();
56  // [END model]
57 
58  // Create the variables.
59  // [START variables]
60  int numVals = 3;
61 
62  IntVar x = model.newIntVar(0, numVals - 1, "x");
63  IntVar y = model.newIntVar(0, numVals - 1, "y");
64  IntVar z = model.newIntVar(0, numVals - 1, "z");
65  // [END variables]
66 
67  // Create the constraint.
68  // [START constraints]
69  model.addDifferent(x, y);
70  // [END constraints]
71 
72  // Maximize a linear combination of variables.
73  // [START objective]
74  model.maximize(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {1, 2, 3}));
75  // [END objective]
76 
77  // Create a solver and solve the model.
78  // [START solve]
79  CpSolver solver = new CpSolver();
80  VarArraySolutionPrinterWithObjective cb =
81  new VarArraySolutionPrinterWithObjective(new IntVar[] {x, y, z});
82  solver.solveWithSolutionCallback(model, cb);
83  // [END solve]
84 
85  System.out.println(cb.getSolutionCount() + " solutions found.");
86  }
87 }
88 // [END program]
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
Solves an optimization problem and displays all intermediate solutions.
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
Main modeling class.
Definition: CpModel.java:40
static LinearExpr scalProd(IntVar[] variables, long[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:33
void maximize(LinearExpr expr)
Adds a maximization objective of a linear expression.
Definition: CpModel.java:967
CpSolverStatus solveWithSolutionCallback(CpModel model, CpSolverSolutionCallback cb)
Solves a problem and passes each solution found to the callback.
Definition: CpSolver.java:39
Parent class to create a callback called at each solution.
Constraint addDifferent(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:220