2025-01-10 11:33:35 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2018-08-01 15:29:10 -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-16 05:02:48 -08:00
|
|
|
// [START program]
|
2020-05-26 09:30:42 +02:00
|
|
|
package com.google.ortools.sat.samples;
|
2021-10-18 16:04:53 +02:00
|
|
|
// [START import]
|
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;
|
2018-11-16 05:02:48 -08:00
|
|
|
import com.google.ortools.sat.CpSolver;
|
|
|
|
|
import com.google.ortools.sat.CpSolverStatus;
|
2018-08-03 16:42:45 -07:00
|
|
|
import com.google.ortools.sat.IntVar;
|
2021-10-18 14:24:28 +02:00
|
|
|
// [END import]
|
2018-08-01 15:29:10 -07:00
|
|
|
|
2018-11-16 05:02:48 -08:00
|
|
|
/** Minimal CP-SAT example to showcase calling the solver. */
|
2021-11-03 12:58:52 +01:00
|
|
|
public final class SimpleSatProgram {
|
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-11-16 05:02:48 -08:00
|
|
|
// Create the model.
|
|
|
|
|
// [START model]
|
2018-08-01 15:29:10 -07:00
|
|
|
CpModel model = new CpModel();
|
2018-11-16 05:02:48 -08:00
|
|
|
// [END model]
|
|
|
|
|
|
|
|
|
|
// Create the variables.
|
|
|
|
|
// [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");
|
|
|
|
|
// [END variables]
|
|
|
|
|
|
|
|
|
|
// Create the constraints.
|
|
|
|
|
// [START constraints]
|
|
|
|
|
model.addDifferent(x, y);
|
|
|
|
|
// [END constraints]
|
|
|
|
|
|
|
|
|
|
// Create a solver and solve the model.
|
|
|
|
|
// [START solve]
|
|
|
|
|
CpSolver solver = new CpSolver();
|
|
|
|
|
CpSolverStatus status = solver.solve(model);
|
|
|
|
|
// [END solve]
|
|
|
|
|
|
2021-10-18 14:24:28 +02:00
|
|
|
// [START print_solution]
|
|
|
|
|
if (status == CpSolverStatus.OPTIMAL || status == CpSolverStatus.FEASIBLE) {
|
2018-11-16 05:02:48 -08:00
|
|
|
System.out.println("x = " + solver.value(x));
|
|
|
|
|
System.out.println("y = " + solver.value(y));
|
|
|
|
|
System.out.println("z = " + solver.value(z));
|
2021-10-18 14:24:28 +02:00
|
|
|
} else {
|
|
|
|
|
System.out.println("No solution found.");
|
2018-11-16 05:02:48 -08:00
|
|
|
}
|
2021-10-18 14:24:28 +02:00
|
|
|
// [END print_solution]
|
2018-08-01 15:29:10 -07:00
|
|
|
}
|
2021-11-03 12:58:52 +01:00
|
|
|
|
|
|
|
|
private SimpleSatProgram() {}
|
2018-08-01 15:29:10 -07:00
|
|
|
}
|
2018-11-16 05:02:48 -08:00
|
|
|
// [END program]
|