SimpleCpProgram.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]
15 // [START import]
19 import java.util.logging.Logger;
20 // [END import]
21 
23 public class SimpleCpProgram {
24  static {
25  System.loadLibrary("jniortools");
26  }
27 
28  private static final Logger logger = Logger.getLogger(SimpleCpProgram.class.getName());
29 
30  public static void main(String[] args) throws Exception {
31  // Instantiate the solver.
32  // [START solver]
33  Solver solver = new Solver("CpSimple");
34  // [END solver]
35 
36  // Create the variables.
37  // [START variables]
38  final long numVals = 3;
39  final IntVar x = solver.makeIntVar(0, numVals - 1, "x");
40  final IntVar y = solver.makeIntVar(0, numVals - 1, "y");
41  final IntVar z = solver.makeIntVar(0, numVals - 1, "z");
42  // [END variables]
43 
44  // Constraint 0: x != y..
45  // [START constraints]
46  solver.addConstraint(solver.makeAllDifferent(new IntVar[] {x, y}));
47  logger.info("Number of constraints: " + solver.constraints());
48  // [END constraints]
49 
50  // Solve the problem.
51  // [START solve]
52  final DecisionBuilder db = solver.makePhase(
54  // [END solve]
55 
56  // Print solution on console.
57  // [START print_solution]
58  int count = 0;
59  solver.newSearch(db);
60  while (solver.nextSolution()) {
61  ++count;
62  logger.info(
63  String.format("Solution: %d\n x=%d y=%d z=%d", count, x.value(), y.value(), z.value()));
64  }
65  solver.endSearch();
66  logger.info("Number of solutions found: " + solver.solutions());
67  // [END print_solution]
68 
69  // [START advanced]
70  logger.info(String.format("Advanced usage:\nProblem solved in %d ms\nMemory usage: %d bytes",
71  solver.wallTime(), Solver.memoryUsage()));
72  // [END advanced]
73  }
74 }
75 // [END program]
Simple CP Program.
Constraint makeAllDifferent(IntVar[] vars)
Definition: Solver.java:1122
IntVar makeIntVar(long min, long max, String name)
Definition: Solver.java:427
DecisionBuilder makePhase(IntVar[] vars, int var_str, int val_str)
Definition: Solver.java:1785
static void main(String[] args)
void newSearch(DecisionBuilder db, SearchMonitor[] monitors)
Definition: Solver.java:291