partial sync with main (without the routing part)
This commit is contained in:
@@ -18,11 +18,13 @@ 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;
|
||||
import com.google.ortools.sat.LinearExpr;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/** Solves an optimization problem and displays all intermediate solutions. */
|
||||
public class SolveAndPrintIntermediateSolutionsSampleSat {
|
||||
public final class SolveAndPrintIntermediateSolutionsSampleSat {
|
||||
// [START print_solution]
|
||||
static class VarArraySolutionPrinterWithObjective extends CpSolverSolutionCallback {
|
||||
public VarArraySolutionPrinterWithObjective(IntVar[] variables) {
|
||||
@@ -46,6 +48,27 @@ public class SolveAndPrintIntermediateSolutionsSampleSat {
|
||||
private int solutionCount;
|
||||
private final IntVar[] variableArray;
|
||||
}
|
||||
|
||||
static class BestBoundCallback implements Consumer<Double> {
|
||||
public BestBoundCallback() {
|
||||
bestBound = 0.0;
|
||||
numImprovements = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Double bound) {
|
||||
bestBound = bound;
|
||||
numImprovements++;
|
||||
}
|
||||
|
||||
public double getBestBound() {
|
||||
return bestBound;
|
||||
}
|
||||
|
||||
double bestBound;
|
||||
int numImprovements;
|
||||
}
|
||||
|
||||
// [END print_solution]
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
@@ -79,10 +102,18 @@ public class SolveAndPrintIntermediateSolutionsSampleSat {
|
||||
CpSolver solver = new CpSolver();
|
||||
VarArraySolutionPrinterWithObjective cb =
|
||||
new VarArraySolutionPrinterWithObjective(new IntVar[] {x, y, z});
|
||||
solver.solve(model, cb);
|
||||
solver.getParameters().setNumWorkers(1);
|
||||
solver.getParameters().setLinearizationLevel(2);
|
||||
BestBoundCallback bestBoundCallback = new BestBoundCallback();
|
||||
|
||||
solver.setBestBoundCallback(bestBoundCallback);
|
||||
CpSolverStatus unusedStatus = solver.solve(model, cb);
|
||||
// [END solve]
|
||||
|
||||
System.out.println(cb.getSolutionCount() + " solutions found.");
|
||||
System.out.println("solution count: " + cb.getSolutionCount());
|
||||
System.out.println("best bound count: " + bestBoundCallback.numImprovements);
|
||||
}
|
||||
|
||||
private SolveAndPrintIntermediateSolutionsSampleSat() {}
|
||||
}
|
||||
// [END program]
|
||||
|
||||
Reference in New Issue
Block a user