Files
ortools-clone/examples/cpp/constraint_programming_cp.cc

69 lines
2.4 KiB
C++
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
// 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.
// Constraint programming example that shows how to use the API.
2025-02-25 16:03:40 +01:00
#include <cstdint>
#include <cstdlib>
#include <vector>
2025-02-25 16:03:40 +01:00
#include "absl/base/log_severity.h"
#include "absl/log/globals.h"
2025-03-04 21:10:09 +01:00
#include "absl/log/log.h"
2023-01-31 20:46:43 +01:00
#include "ortools/base/init_google.h"
#include "ortools/constraint_solver/constraint_solver.h"
namespace operations_research {
2018-11-22 13:15:13 +01:00
void RunConstraintProgrammingExample() {
// Instantiate the solver.
Solver solver("ConstraintProgrammingExample");
2025-02-25 16:03:40 +01:00
const int64_t kNumVals = 3;
2018-11-22 13:15:13 +01:00
// Define decision variables.
2025-02-25 16:03:40 +01:00
IntVar* const x = solver.MakeIntVar(0, kNumVals - 1, "x");
IntVar* const y = solver.MakeIntVar(0, kNumVals - 1, "y");
IntVar* const z = solver.MakeIntVar(0, kNumVals - 1, "z");
2018-11-22 13:15:13 +01:00
// Define constraints.
2020-10-29 14:25:39 +01:00
std::vector<IntVar*> xyvars = {x, y};
2018-11-22 13:15:13 +01:00
solver.AddConstraint(solver.MakeAllDifferent(xyvars));
LOG(INFO) << "Number of constraints: " << solver.constraints();
// Create decision builder to search for solutions.
2020-10-29 14:25:39 +01:00
std::vector<IntVar*> allvars = {x, y, z};
DecisionBuilder* const db = solver.MakePhase(
2018-11-22 13:15:13 +01:00
allvars, Solver::CHOOSE_FIRST_UNBOUND, Solver::ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
2024-12-29 19:21:42 +01:00
LOG(INFO) << "Solution"
<< ": x = " << x->Value() << "; y = " << y->Value()
2018-11-22 13:15:13 +01:00
<< "; z = " << z->Value();
}
2018-11-22 13:15:13 +01:00
solver.EndSearch();
LOG(INFO) << "Number of solutions: " << solver.solutions();
LOG(INFO) << "";
LOG(INFO) << "Advanced usage:";
LOG(INFO) << "Problem solved in " << solver.wall_time() << "ms";
LOG(INFO) << "Memory usage: " << Solver::MemoryUsage() << " bytes";
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research
2020-10-29 14:25:39 +01:00
int main(int argc, char** argv) {
2025-02-25 16:03:40 +01:00
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
InitGoogle(argv[0], &argc, &argv, true);
operations_research::RunConstraintProgrammingExample();
2018-11-07 09:52:37 +01:00
return EXIT_SUCCESS;
}