OR-Tools  7.1
SolveWithTimeLimitSampleSat.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 
17 import com.google.ortools.sat.IntVar;
18 
21  static {
22  System.loadLibrary("jniortools");
23  }
24 
25  public static void main(String[] args) throws Exception {
26  // Create the model.
27  CpModel model = new CpModel();
28  // Create the variables.
29  int numVals = 3;
30 
31  IntVar x = model.newIntVar(0, numVals - 1, "x");
32  IntVar y = model.newIntVar(0, numVals - 1, "y");
33  IntVar z = model.newIntVar(0, numVals - 1, "z");
34  // Create the constraint.
35  model.addDifferent(x, y);
36 
37  // Create a solver and solve the model.
38  CpSolver solver = new CpSolver();
39  solver.getParameters().setMaxTimeInSeconds(10.0);
40  CpSolverStatus status = solver.solve(model);
41 
42  if (status == CpSolverStatus.FEASIBLE) {
43  System.out.println("x = " + solver.value(x));
44  System.out.println("y = " + solver.value(y));
45  System.out.println("z = " + solver.value(z));
46  }
47  }
48 }
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
SatParameters.Builder getParameters()
Returns the builder of the parameters of the SAT solver for modification.
Definition: CpSolver.java:119
Main modeling class.
Definition: CpModel.java:40
Constraint addDifferent(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:220
Solves a problem with a time limit.
CpSolverStatus solve(CpModel model)
Solves the given module, and returns the solve status.
Definition: CpSolver.java:33