OR-Tools  7.1
StepFunctionSampleSat.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 
18 import com.google.ortools.sat.IntVar;
22 
24 public class StepFunctionSampleSat {
25  static {
26  System.loadLibrary("jniortools");
27  }
28 
29  public static void main(String[] args) throws Exception {
30  // Create the CP-SAT model.
31  CpModel model = new CpModel();
32 
33  // Declare our primary variable.
34  IntVar x = model.newIntVar(0, 20, "x");
35 
36  // Create the expression variable and implement the step function
37  // Note it is not defined for var == 2.
38  //
39  // - 3
40  // -- -- --------- 2
41  // 1
42  // -- --- 0
43  // 0 ================ 20
44  //
45  IntVar expr = model.newIntVar(0, 3, "expr");
46 
47  // expr == 0 on [5, 6] U [8, 10]
48  Literal b0 = model.newBoolVar("b0");
49  model.addLinearExpressionInDomain(x, Domain.fromValues(new long[] {5, 6, 8, 9, 10}))
50  .onlyEnforceIf(b0);
51  model.addEquality(expr, 0).onlyEnforceIf(b0);
52 
53  // expr == 2 on [0, 1] U [3, 4] U [11, 20]
54  Literal b2 = model.newBoolVar("b2");
55  model
57  x, Domain.fromIntervals(new long[][] {{0, 1}, {3, 4}, {11, 20}}))
58  .onlyEnforceIf(b2);
59  model.addEquality(expr, 2).onlyEnforceIf(b2);
60 
61  // expr == 3 when x = 7
62  Literal b3 = model.newBoolVar("b3");
63  model.addEquality(x, 7).onlyEnforceIf(b3);
64  model.addEquality(expr, 3).onlyEnforceIf(b3);
65 
66  // At least one bi is true. (we could use a sum == 1).
67  model.addBoolOr(new Literal[] {b0, b2, b3});
68 
69  // Search for x values in increasing order.
70  model.addDecisionStrategy(new IntVar[] {x},
73 
74  // Create the solver.
75  CpSolver solver = new CpSolver();
76 
77  // Force the solver to follow the decision strategy exactly.
78  solver.getParameters().setSearchBranching(SatParameters.SearchBranching.FIXED_SEARCH);
79 
80  // Solve the problem with the printer callback.
81  solver.searchAllSolutions(model, new CpSolverSolutionCallback() {
82  public CpSolverSolutionCallback init(IntVar[] variables) {
83  variableArray = variables;
84  return this;
85  }
86 
87  @Override
88  public void onSolutionCallback() {
89  for (IntVar v : variableArray) {
90  System.out.printf("%s=%d ", v.getName(), value(v));
91  }
92  System.out.println();
93  }
94 
95  private IntVar[] variableArray;
96  }.init(new IntVar[] {x, expr}));
97  }
98 }
void onlyEnforceIf(Literal lit)
Adds a literal to the constraint.
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
IntVar newBoolVar(String name)
Creates a Boolean variable with the given name.
Definition: CpModel.java:85
Link integer constraints together.
Constraint addLinearExpressionInDomain(LinearExpr expr, Domain domain)
Adds.
Definition: CpModel.java:134
static Domain fromValues(long[] values)
Definition: Domain.java:54
SatParameters.Builder getParameters()
Returns the builder of the parameters of the SAT solver for modification.
Definition: CpSolver.java:119
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17
Main modeling class.
Definition: CpModel.java:40
static Domain fromIntervals(long[][] intervals)
Definition: Domain.java:58
static void main(String[] args)
CpSolverStatus searchAllSolutions(CpModel model, CpSolverSolutionCallback cb)
Searches for all solutions of a satisfiability problem.
Definition: CpSolver.java:57
Parent class to create a callback called at each solution.