2024-01-04 13:43:15 +01:00
|
|
|
// Copyright 2010-2024 Google LLC
|
2018-08-02 15:12:57 -07:00
|
|
|
// 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;
|
|
|
|
|
|
2020-09-11 03:07:18 +02:00
|
|
|
import com.google.ortools.Loader;
|
2018-08-03 16:42:45 -07:00
|
|
|
import com.google.ortools.sat.CpModel;
|
|
|
|
|
import com.google.ortools.sat.CpSolver;
|
|
|
|
|
import com.google.ortools.sat.CpSolverSolutionCallback;
|
|
|
|
|
import com.google.ortools.sat.IntVar;
|
2019-05-06 17:21:21 +02:00
|
|
|
import com.google.ortools.sat.LinearExpr;
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-11-15 14:32:20 -08:00
|
|
|
/** Solves an optimization problem and displays all intermediate solutions. */
|
2018-11-15 11:38:24 -08:00
|
|
|
public class SolveAndPrintIntermediateSolutionsSampleSat {
|
2018-11-15 15:20:50 -08:00
|
|
|
// [START print_solution]
|
2018-11-10 23:56:52 +01:00
|
|
|
static class VarArraySolutionPrinterWithObjective extends CpSolverSolutionCallback {
|
2018-08-28 11:19:49 +02:00
|
|
|
public VarArraySolutionPrinterWithObjective(IntVar[] variables) {
|
|
|
|
|
variableArray = variables;
|
|
|
|
|
}
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
@Override
|
|
|
|
|
public void onSolutionCallback() {
|
2018-11-10 23:56:52 +01:00
|
|
|
System.out.printf("Solution #%d: time = %.02f s%n", solutionCount, wallTime());
|
2018-09-20 11:30:19 +02:00
|
|
|
System.out.printf(" objective value = %f%n", objectiveValue());
|
2018-08-28 11:19:49 +02:00
|
|
|
for (IntVar v : variableArray) {
|
|
|
|
|
System.out.printf(" %s = %d%n", v.getName(), value(v));
|
|
|
|
|
}
|
|
|
|
|
solutionCount++;
|
|
|
|
|
}
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-11-10 23:56:52 +01:00
|
|
|
public int getSolutionCount() {
|
|
|
|
|
return solutionCount;
|
|
|
|
|
}
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
private int solutionCount;
|
|
|
|
|
private final IntVar[] variableArray;
|
|
|
|
|
}
|
2018-11-15 15:20:50 -08:00
|
|
|
// [END print_solution]
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-08-03 16:42:45 -07:00
|
|
|
public static void main(String[] args) throws Exception {
|
2020-09-11 03:07:18 +02:00
|
|
|
Loader.loadNativeLibraries();
|
2018-08-28 11:19:49 +02:00
|
|
|
// Create the model.
|
2018-11-15 15:20:50 -08:00
|
|
|
// [START model]
|
2018-08-02 15:12:57 -07:00
|
|
|
CpModel model = new CpModel();
|
2018-11-15 15:20:50 -08:00
|
|
|
// [END model]
|
|
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
// Create the variables.
|
2018-11-15 15:20:50 -08:00
|
|
|
// [START variables]
|
2018-08-03 16:42:45 -07:00
|
|
|
int numVals = 3;
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-08-03 16:42:45 -07:00
|
|
|
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]
|
|
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
// Create the constraint.
|
2018-11-15 15:20:50 -08:00
|
|
|
// [START constraints]
|
2018-08-02 15:12:57 -07:00
|
|
|
model.addDifferent(x, y);
|
2018-11-15 15:20:50 -08:00
|
|
|
// [END constraints]
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
// Maximize a linear combination of variables.
|
2018-11-15 15:20:50 -08:00
|
|
|
// [START objective]
|
2022-01-03 18:57:09 +01:00
|
|
|
model.maximize(LinearExpr.weightedSum(new IntVar[] {x, y, z}, new long[] {1, 2, 3}));
|
2018-11-15 15:20:50 -08:00
|
|
|
// [END objective]
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
// Create a solver and solve the model.
|
2018-11-15 15:20:50 -08:00
|
|
|
// [START solve]
|
2018-08-02 15:12:57 -07:00
|
|
|
CpSolver solver = new CpSolver();
|
|
|
|
|
VarArraySolutionPrinterWithObjective cb =
|
|
|
|
|
new VarArraySolutionPrinterWithObjective(new IntVar[] {x, y, z});
|
2021-05-03 12:11:39 +02:00
|
|
|
solver.solve(model, cb);
|
2018-11-15 15:20:50 -08:00
|
|
|
// [END solve]
|
2018-08-02 15:12:57 -07:00
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
System.out.println(cb.getSolutionCount() + " solutions found.");
|
2018-08-02 15:12:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-11-15 15:20:50 -08:00
|
|
|
// [END program]
|