OR-Tools  7.1
SimpleSatProgram.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;
19 
21 public class SimpleSatProgram {
22  static {
23  System.loadLibrary("jniortools");
24  }
25 
26  public static void main(String[] args) throws Exception {
27  // Create the model.
28  // [START model]
29  CpModel model = new CpModel();
30  // [END model]
31 
32  // Create the variables.
33  // [START variables]
34  int numVals = 3;
35 
36  IntVar x = model.newIntVar(0, numVals - 1, "x");
37  IntVar y = model.newIntVar(0, numVals - 1, "y");
38  IntVar z = model.newIntVar(0, numVals - 1, "z");
39  // [END variables]
40 
41  // Create the constraints.
42  // [START constraints]
43  model.addDifferent(x, y);
44  // [END constraints]
45 
46  // Create a solver and solve the model.
47  // [START solve]
48  CpSolver solver = new CpSolver();
49  CpSolverStatus status = solver.solve(model);
50  // [END solve]
51 
52  if (status == CpSolverStatus.FEASIBLE) {
53  System.out.println("x = " + solver.value(x));
54  System.out.println("y = " + solver.value(y));
55  System.out.println("z = " + solver.value(z));
56  }
57  }
58 }
59 // [END program]
Minimal CP-SAT example to showcase calling the solver.
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
long value(IntVar var)
Returns the value of a variable in the last solution found.
Definition: CpSolver.java:79
Main modeling class.
Definition: CpModel.java:40
static void main(String[] args)
Constraint addDifferent(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:220
CpSolverStatus solve(CpModel model)
Solves the given module, and returns the solve status.
Definition: CpSolver.java:33