2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2018-07-16 18:40:14 -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]
|
2021-10-18 14:24:28 +02:00
|
|
|
// [START import]
|
2022-02-15 18:00:11 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2025-05-16 14:34:40 +02:00
|
|
|
#include "absl/base/log_severity.h"
|
|
|
|
|
#include "absl/log/globals.h"
|
|
|
|
|
#include "ortools/base/init_google.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/base/logging.h"
|
2018-11-05 16:24:47 +01:00
|
|
|
#include "ortools/sat/cp_model.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/sat/cp_model.pb.h"
|
|
|
|
|
#include "ortools/sat/cp_model_solver.h"
|
|
|
|
|
#include "ortools/util/sorted_interval_list.h"
|
2021-10-18 14:24:28 +02:00
|
|
|
// [END import]
|
2018-07-16 18:40:14 -07:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
namespace sat {
|
|
|
|
|
|
2018-11-15 14:32:20 -08:00
|
|
|
void SimpleSatProgram() {
|
2018-11-16 05:02:48 -08:00
|
|
|
// [START model]
|
2018-11-05 16:24:47 +01:00
|
|
|
CpModelBuilder cp_model;
|
2018-11-16 05:02:48 -08:00
|
|
|
// [END model]
|
2018-07-16 18:40:14 -07:00
|
|
|
|
2018-11-16 05:02:48 -08:00
|
|
|
// [START variables]
|
|
|
|
|
const Domain domain(0, 2);
|
|
|
|
|
const IntVar x = cp_model.NewIntVar(domain).WithName("x");
|
|
|
|
|
const IntVar y = cp_model.NewIntVar(domain).WithName("y");
|
|
|
|
|
const IntVar z = cp_model.NewIntVar(domain).WithName("z");
|
|
|
|
|
// [END variables]
|
|
|
|
|
|
|
|
|
|
// [START constraints]
|
|
|
|
|
cp_model.AddNotEqual(x, y);
|
|
|
|
|
// [END constraints]
|
|
|
|
|
|
|
|
|
|
// Solving part.
|
|
|
|
|
// [START solve]
|
2019-04-05 14:58:33 +02:00
|
|
|
const CpSolverResponse response = Solve(cp_model.Build());
|
2018-11-16 05:02:48 -08:00
|
|
|
// [END solve]
|
|
|
|
|
|
2021-10-18 14:24:28 +02:00
|
|
|
// [START print_solution]
|
2021-10-20 13:27:12 +02:00
|
|
|
if (response.status() == CpSolverStatus::OPTIMAL ||
|
|
|
|
|
response.status() == CpSolverStatus::FEASIBLE) {
|
2018-11-16 05:02:48 -08:00
|
|
|
// Get the value of x in the solution.
|
|
|
|
|
LOG(INFO) << "x = " << SolutionIntegerValue(response, x);
|
|
|
|
|
LOG(INFO) << "y = " << SolutionIntegerValue(response, y);
|
|
|
|
|
LOG(INFO) << "z = " << SolutionIntegerValue(response, z);
|
2021-10-18 14:24:28 +02:00
|
|
|
} else {
|
|
|
|
|
LOG(INFO) << "No solution found.";
|
2018-11-16 05:02:48 -08:00
|
|
|
}
|
2021-10-18 14:24:28 +02:00
|
|
|
// [END print_solution]
|
2018-07-16 18:40:14 -07:00
|
|
|
}
|
2018-11-16 05:02:48 -08:00
|
|
|
|
2018-07-16 18:40:14 -07:00
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|
|
|
|
|
|
2025-05-16 14:34:40 +02:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
InitGoogle(argv[0], &argc, &argv, true);
|
|
|
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
2018-11-15 14:32:20 -08:00
|
|
|
operations_research::sat::SimpleSatProgram();
|
2018-07-16 18:40:14 -07:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
2018-11-16 05:02:48 -08:00
|
|
|
// [END program]
|