Files
ortools-clone/ortools/sat/samples/SearchForAllSolutionsSampleSat.java

85 lines
2.7 KiB
Java
Raw Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-11-15 15:20:50 -08:00
// [START program]
2020-05-26 09:30:42 +02:00
package com.google.ortools.sat.samples;
import com.google.ortools.Loader;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
/** Code sample that solves a model and displays all solutions. */
public class SearchForAllSolutionsSampleSat {
2018-11-15 15:20:50 -08:00
// [START print_solution]
static class VarArraySolutionPrinter extends CpSolverSolutionCallback {
public VarArraySolutionPrinter(IntVar[] variables) {
variableArray = variables;
}
@Override
public void onSolutionCallback() {
2018-11-10 23:56:52 +01:00
System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, wallTime());
for (IntVar v : variableArray) {
System.out.printf(" %s = %d%n", v.getName(), value(v));
}
solutionCount++;
}
2018-11-10 23:56:52 +01:00
public int getSolutionCount() {
return solutionCount;
}
private int solutionCount;
private final IntVar[] variableArray;
}
2018-11-15 15:20:50 -08:00
// [END print_solution]
public static void main(String[] args) throws Exception {
Loader.loadNativeLibraries();
// Create the model.
2018-11-15 15:20:50 -08:00
// [START model]
CpModel model = new CpModel();
2018-11-15 15:20:50 -08:00
// [END model]
// Create the variables.
2018-11-15 15:20:50 -08:00
// [START variables]
int numVals = 3;
IntVar x = model.newIntVar(0, numVals - 1, "x");
IntVar y = model.newIntVar(0, numVals - 1, "y");
IntVar z = model.newIntVar(0, numVals - 1, "z");
2018-11-15 15:20:50 -08:00
// [END variables]
// Create the constraints.
2018-11-15 15:20:50 -08:00
// [START constraints]
model.addDifferent(x, y);
2018-11-15 15:20:50 -08:00
// [END constraints]
// Create a solver and solve the model.
2018-11-15 15:20:50 -08:00
// [START solve]
CpSolver solver = new CpSolver();
2018-11-10 23:56:52 +01:00
VarArraySolutionPrinter cb = new VarArraySolutionPrinter(new IntVar[] {x, y, z});
// Tell the solver to enumerate all solutions.
solver.getParameters().setEnumerateAllSolutions(true);
// And solve.
CpSolverStatus unusedStatus = solver.solve(model, cb);
2018-11-15 15:20:50 -08:00
// [END solve]
System.out.println(cb.getSolutionCount() + " solutions found.");
}
}
2018-11-15 15:20:50 -08:00
// [END program]