RabbitsAndPheasantsSat.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;
19 
24 public class RabbitsAndPheasantsSat {
25  static {
26  System.loadLibrary("jniortools");
27  }
28 
29  public static void main(String[] args) throws Exception {
30  // Creates the model.
31  CpModel model = new CpModel();
32  // Creates the variables.
33  IntVar r = model.newIntVar(0, 100, "r");
34  IntVar p = model.newIntVar(0, 100, "p");
35  // 20 heads.
36  model.addEquality(LinearExpr.sum(new IntVar[] {r, p}), 20);
37  // 56 legs.
38  model.addEquality(LinearExpr.scalProd(new IntVar[] {r, p}, new long[] {4, 2}), 56);
39 
40  // Creates a solver and solves the model.
41  CpSolver solver = new CpSolver();
42  CpSolverStatus status = solver.solve(model);
43 
44  if (status == CpSolverStatus.FEASIBLE) {
45  System.out.println(solver.value(r) + " rabbits, and " + solver.value(p) + " pheasants");
46  }
47  }
48 }
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
Constraint addEquality(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:153
In a field of rabbits and pheasants, there are 20 heads and 56 legs.
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 LinearExpr scalProd(IntVar[] variables, long[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:33
static void main(String[] args)
static LinearExpr sum(IntVar[] variables)
Creates a sum expression.
Definition: LinearExpr.java:28
CpSolverStatus solve(CpModel model)
Solves the given module, and returns the solve status.
Definition: CpSolver.java:33