examples/contrib: add issue4269

This commit is contained in:
Mizux Seiha
2025-03-04 21:07:42 +01:00
parent d97048d4eb
commit adf0973a35
2 changed files with 55 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
cc_binary(
name = "issue4269",
srcs = [
"issue4269.cc",
],
deps = [
"//ortools/base",
"//ortools/base:path",
"//ortools/sat:cp_model",
"//ortools/sat:cp_model_solver",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
],
)

View File

@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
#include "ortools/sat/cp_model.h"
#include "ortools/sat/cp_model_checker.h"
using namespace operations_research;
using namespace operations_research::sat;
int main() {
std::vector<int> w = {3, 4, 5, 5};
std::vector<int> c = {9, 9};
sat::CpModelBuilder builder;
std::vector<std::vector<BoolVar>> assignment(w.size());
for (size_t i = 0; i < w.size(); i++) {
assignment[i].reserve(c.size());
for (size_t j = 0; j < c.size(); j++) {
assignment[i].emplace_back(builder.NewBoolVar());
}
builder.AddExactlyOne(assignment[i]);
}
for (size_t j = 0; j < c.size(); j++) {
LinearExpr expr;
for (size_t i = 0; i < w.size(); i++) {
expr += LinearExpr::Term(assignment[i][j], w[i]);
}
builder.AddLinearConstraint(expr, Domain(0, c[j]));
}
sat::Model model;
sat::SatParameters parameters;
parameters.set_num_search_workers(4);
parameters.set_max_time_in_seconds(10.0);
model.Add(sat::NewSatParameters(parameters));
auto response = sat::SolveCpModel(builder.Build(), &model);
if (response.status() == sat::CpSolverStatus::OPTIMAL || response.status() == sat::CpSolverStatus::FEASIBLE) {
std::cout << "all ok";
}
}