SIGN IN SIGN UP
google / or-tools UNCLAIMED

Google's Operations Research tools:

0 0 1 C++
// 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.
// [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]
import com.google.ortools.Loader;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
2021-10-18 14:24:28 +02:00
// [END import]
2018-08-01 15:29:10 -07:00
/** Minimal CP-SAT example to showcase calling the solver. */
2021-11-03 12:58:52 +01:00
public final class SimpleSatProgram {
public static void main(String[] args) throws Exception {
Loader.loadNativeLibraries();
// Create the model.
// [START model]
2018-08-01 15:29:10 -07:00
CpModel model = new CpModel();
// [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) {
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.");
}
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
}
// [END program]