22 System.loadLibrary(
"jniortools");
26 public VarArraySolutionPrinterWithLimit(
IntVar[] variables,
int limit) {
27 variableArray = variables;
28 solutionLimit = limit;
32 public void onSolutionCallback() {
33 System.out.printf(
"Solution #%d: time = %.02f s%n", solutionCount, wallTime());
34 for (
IntVar v : variableArray) {
35 System.out.printf(
" %s = %d%n", v.getName(), value(v));
38 if (solutionCount >= solutionLimit) {
39 System.out.printf(
"Stop search after %d solutions%n", solutionLimit);
44 public int getSolutionCount() {
48 private int solutionCount;
49 private final IntVar[] variableArray;
50 private final int solutionLimit;
53 public static void main(String[] args)
throws Exception {
65 VarArraySolutionPrinterWithLimit cb =
66 new VarArraySolutionPrinterWithLimit(
new IntVar[] {x, y, z}, 5);
69 System.out.println(cb.getSolutionCount() +
" solutions found.");
70 if (cb.getSolutionCount() != 5) {
71 throw new RuntimeException(
"Did not stop the search correctly.");
Code sample that solves a model and displays a small number of solutions.
static void main(String[] args)