14 #ifndef OR_TOOLS_SAT_INTEGER_EXPR_H_
15 #define OR_TOOLS_SAT_INTEGER_EXPR_H_
56 IntegerSumLE(
const std::vector<Literal>& enforcement_literals,
57 const std::vector<IntegerVariable>& vars,
58 const std::vector<IntegerValue>& coeffs,
78 void FillIntegerReason();
80 const std::vector<Literal> enforcement_literals_;
81 const IntegerValue upper_bound_;
89 bool is_registered_ =
false;
90 IntegerValue rev_lb_fixed_vars_;
93 int rev_num_fixed_vars_;
98 std::vector<IntegerVariable> vars_;
99 std::vector<IntegerValue> coeffs_;
100 std::vector<IntegerValue> max_variations_;
102 std::vector<Literal> literal_reason_;
105 std::vector<IntegerLiteral> integer_reason_;
106 std::vector<IntegerValue> reason_coeffs_;
122 const std::vector<IntegerVariable>& vars,
123 const std::vector<IntegerValue>& coeffs,
Model*
model);
128 const IntegerVariable target_;
129 const std::vector<IntegerVariable> vars_;
130 const std::vector<IntegerValue> coeffs_;
132 IntegerValue gcd_ = IntegerValue(1);
171 const std::vector<IntegerVariable> vars_;
172 const IntegerVariable min_var_;
175 std::vector<IntegerLiteral> integer_reason_;
197 bool PropagateLinearUpperBound(const std::vector<IntegerVariable>& vars,
198 const std::vector<IntegerValue>& coeffs,
199 IntegerValue upper_bound);
202 const IntegerVariable min_var_;
203 std::vector<IntegerValue> expr_lbs_;
207 int rev_unique_candidate_ = 0;
224 const IntegerVariable a_;
225 const IntegerVariable b_;
226 const IntegerVariable p_;
248 const IntegerVariable a_;
249 const IntegerVariable b_;
250 const IntegerVariable c_;
267 const IntegerVariable a_;
268 const IntegerValue b_;
269 const IntegerVariable c_;
286 const IntegerVariable x_;
287 const IntegerVariable s_;
298 template <
typename VectorInt>
300 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
304 if (vars.size() == 1) {
344 const int num_vars = vars.size();
345 if (num_vars > 100) {
346 std::vector<IntegerVariable> bucket_sum_vars;
348 std::vector<IntegerVariable> local_vars;
349 std::vector<IntegerValue> local_coeffs;
352 const int num_buckets =
static_cast<int>(std::round(std::sqrt(num_vars)));
353 for (
int b = 0;
b < num_buckets; ++
b) {
355 local_coeffs.clear();
358 const int limit = num_vars * (
b + 1);
359 for (; i * num_buckets < limit; ++i) {
360 local_vars.push_back(vars[i]);
364 bucket_lb +=
std::min(term1, term2);
365 bucket_ub +=
std::max(term1, term2);
368 const IntegerVariable bucket_sum =
370 bucket_sum_vars.push_back(bucket_sum);
371 local_vars.push_back(bucket_sum);
372 local_coeffs.push_back(IntegerValue(-1));
374 {}, local_vars, local_coeffs, IntegerValue(0),
model);
376 model->TakeOwnership(constraint);
381 local_coeffs.clear();
382 for (
const IntegerVariable
var : bucket_sum_vars) {
383 local_vars.push_back(
var);
384 local_coeffs.push_back(IntegerValue(1));
387 {}, local_vars, local_coeffs, IntegerValue(upper_bound),
model);
389 model->TakeOwnership(constraint);
396 IntegerValue(upper_bound),
model);
398 model->TakeOwnership(constraint);
403 template <
typename VectorInt>
405 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
409 for (
int64& ref : negated_coeffs) ref = -ref;
414 template <
typename VectorInt>
416 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
425 template <
typename VectorInt>
427 const std::vector<Literal>& enforcement_literals,
428 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
432 if (vars.size() == 1) {
436 enforcement_literals,
438 vars[0],
FloorRatio(IntegerValue(upper_bound),
442 enforcement_literals,
444 vars[0],
CeilRatio(IntegerValue(-upper_bound),
454 enforcement_literals);
463 enforcement_literals);
468 IntegerValue expression_min(0);
470 for (
int i = 0; i < vars.size(); ++i) {
473 ? integer_trail->LowerBound(vars[i])
474 : integer_trail->UpperBound(vars[i]));
476 if (expression_min == upper_bound) {
477 for (
int i = 0; i < vars.size(); ++i) {
482 vars[i], integer_trail->LowerBound(vars[i]))));
487 vars[i], integer_trail->UpperBound(vars[i]))));
492 enforcement_literals, vars,
494 IntegerValue(upper_bound),
model);
496 model->TakeOwnership(constraint);
502 template <
typename VectorInt>
504 const std::vector<Literal>& enforcement_literals,
505 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
509 for (
int64& ref : negated_coeffs) ref = -ref;
511 negated_coeffs, -lower_bound);
515 template <
typename VectorInt>
517 Literal is_le,
const std::vector<IntegerVariable>& vars,
528 template <
typename VectorInt>
530 Literal is_ge,
const std::vector<IntegerVariable>& vars,
542 if (cst.
vars.empty()) {
543 if (cst.
lb <= 0 && cst.
ub >= 0)
return;
549 std::vector<int64> converted_coeffs;
550 for (
const IntegerValue v : cst.
coeffs) converted_coeffs.push_back(v.value());
562 const absl::Span<const Literal> enforcement_literals,
564 if (enforcement_literals.empty()) {
567 if (cst.
vars.empty()) {
568 if (cst.
lb <= 0 && cst.
ub >= 0)
return;
573 std::vector<Literal> converted_literals(enforcement_literals.begin(),
574 enforcement_literals.end());
575 std::vector<int64> converted_coeffs;
576 for (
const IntegerValue v : cst.
coeffs) converted_coeffs.push_back(v.value());
580 converted_literals, cst.
vars, converted_coeffs, cst.
ub.value()));
584 converted_literals, cst.
vars, converted_coeffs, cst.
lb.value()));
590 template <
typename VectorInt>
592 Literal is_eq,
const std::vector<IntegerVariable>& vars,
607 template <
typename VectorInt>
609 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
629 template <
typename VectorInt>
631 const VectorInt&
coefficients,
const std::vector<IntegerVariable>& vars) {
633 std::vector<IntegerVariable> new_vars = vars;
640 for (
int i = 0; i < new_vars.size(); ++i) {
651 new_vars.push_back(sum);
653 new_coeffs.push_back(-1);
662 IntegerVariable min_var,
const std::vector<IntegerVariable>& vars) {
664 for (
const IntegerVariable&
var : vars) {
671 model->TakeOwnership(constraint);
680 const std::vector<LinearExpression>& exprs) {
684 IntegerVariable min_var;
685 if (min_expr.
vars.size() == 1 &&
686 std::abs(min_expr.
coeffs[0].value()) == 1) {
687 if (min_expr.
coeffs[0].value() == 1) {
688 min_var = min_expr.
vars[0];
699 std::vector<IntegerVariable> min_sum_vars = min_expr.
vars;
700 std::vector<int64> min_sum_coeffs;
701 for (IntegerValue coeff : min_expr.
coeffs) {
702 min_sum_coeffs.push_back(coeff.value());
704 min_sum_vars.push_back(min_var);
705 min_sum_coeffs.push_back(-1);
708 -min_expr.
offset.value()));
712 std::vector<IntegerVariable> vars = expr.vars;
713 std::vector<int64> coeffs;
714 for (IntegerValue coeff : expr.coeffs) {
715 coeffs.push_back(coeff.value());
717 vars.push_back(min_var);
718 coeffs.push_back(-1);
724 model->TakeOwnership(constraint);
731 IntegerVariable max_var,
const std::vector<IntegerVariable>& vars) {
733 std::vector<IntegerVariable> negated_vars;
734 for (
const IntegerVariable&
var : vars) {
742 model->TakeOwnership(constraint);
748 std::function<void(Model*)>
IsOneOf(IntegerVariable
var,
749 const std::vector<Literal>& selectors,
750 const std::vector<IntegerValue>& values);
807 model->TakeOwnership(constraint);
822 model->TakeOwnership(constraint);
829 #endif // OR_TOOLS_SAT_INTEGER_EXPR_H_