OR-Tools  7.1
ChannelingSampleSat.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;
21 
23 public class ChannelingSampleSat {
24  static {
25  System.loadLibrary("jniortools");
26  }
27 
28  public static void main(String[] args) throws Exception {
29  // Create the CP-SAT model.
30  CpModel model = new CpModel();
31 
32  // Declare our two primary variables.
33  IntVar x = model.newIntVar(0, 10, "x");
34  IntVar y = model.newIntVar(0, 10, "y");
35 
36  // Declare our intermediate boolean variable.
37  IntVar b = model.newBoolVar("b");
38 
39  // Implement b == (x >= 5).
40  model.addGreaterOrEqual(x, 5).onlyEnforceIf(b);
41  model.addLessOrEqual(x, 4).onlyEnforceIf(b.not());
42 
43  // Create our two half-reified constraints.
44  // First, b implies (y == 10 - x).
45  model.addEquality(LinearExpr.sum(new IntVar[] {x, y}), 10).onlyEnforceIf(b);
46  // Second, not(b) implies y == 0.
47  model.addEquality(y, 0).onlyEnforceIf(b.not());
48 
49  // Search for x values in increasing order.
50  model.addDecisionStrategy(new IntVar[] {x},
53 
54  // Create the solver.
55  CpSolver solver = new CpSolver();
56 
57  // Force the solver to follow the decision strategy exactly.
58  solver.getParameters().setSearchBranching(SatParameters.SearchBranching.FIXED_SEARCH);
59 
60  // Solve the problem with the printer callback.
61  solver.searchAllSolutions(model, new CpSolverSolutionCallback() {
62  public CpSolverSolutionCallback init(IntVar[] variables) {
63  variableArray = variables;
64  return this;
65  }
66 
67  @Override
68  public void onSolutionCallback() {
69  for (IntVar v : variableArray) {
70  System.out.printf("%s=%d ", v.getName(), value(v));
71  }
72  System.out.println();
73  }
74 
75  private IntVar[] variableArray;
76  }.init(new IntVar[] {x, y, b}));
77  }
78 }
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
void onlyEnforceIf(Literal lit)
Adds a literal to the constraint.
Literal not()
Returns the negation of a boolean variable.
Constraint addEquality(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:153
static void main(String[] args)
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 addGreaterOrEqual(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:194
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 addLessOrEqual(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:168
void addDecisionStrategy(IntVar[] variables, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr)
Adds.
Definition: CpModel.java:979
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.
static LinearExpr sum(IntVar[] variables)
Creates a sum expression.
Definition: LinearExpr.java:28