14 #ifndef OR_TOOLS_SAT_INTEGER_EXPR_H_
15 #define OR_TOOLS_SAT_INTEGER_EXPR_H_
57 IntegerSumLE(
const std::vector<Literal>& enforcement_literals,
58 const std::vector<IntegerVariable>& vars,
59 const std::vector<IntegerValue>& coeffs,
79 void FillIntegerReason();
81 const std::vector<Literal> enforcement_literals_;
82 const IntegerValue upper_bound_;
90 bool is_registered_ =
false;
91 IntegerValue rev_lb_fixed_vars_;
94 int rev_num_fixed_vars_;
99 std::vector<IntegerVariable> vars_;
100 std::vector<IntegerValue> coeffs_;
101 std::vector<IntegerValue> max_variations_;
103 std::vector<Literal> literal_reason_;
106 std::vector<IntegerLiteral> integer_reason_;
107 std::vector<IntegerValue> reason_coeffs_;
123 const std::vector<IntegerVariable>& vars,
124 const std::vector<IntegerValue>& coeffs,
Model*
model);
129 const IntegerVariable target_;
130 const std::vector<IntegerVariable> vars_;
131 const std::vector<IntegerValue> coeffs_;
133 IntegerValue gcd_ = IntegerValue(1);
172 const std::vector<IntegerVariable> vars_;
173 const IntegerVariable min_var_;
176 std::vector<IntegerLiteral> integer_reason_;
198 bool PropagateLinearUpperBound(const std::vector<IntegerVariable>& vars,
199 const std::vector<IntegerValue>& coeffs,
203 const IntegerVariable min_var_;
204 std::vector<IntegerValue> expr_lbs_;
208 int rev_unique_candidate_ = 0;
225 const IntegerVariable a_;
226 const IntegerVariable b_;
227 const IntegerVariable p_;
249 const IntegerVariable a_;
250 const IntegerVariable b_;
251 const IntegerVariable c_;
268 const IntegerVariable a_;
269 const IntegerValue b_;
270 const IntegerVariable c_;
287 const IntegerVariable x_;
288 const IntegerVariable s_;
299 template <
typename VectorInt>
301 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
305 if (vars.size() == 1) {
345 const int num_vars = vars.size();
346 if (num_vars > 100) {
347 std::vector<IntegerVariable> bucket_sum_vars;
349 std::vector<IntegerVariable> local_vars;
350 std::vector<IntegerValue> local_coeffs;
353 const int num_buckets =
static_cast<int>(std::round(std::sqrt(num_vars)));
354 for (
int b = 0;
b < num_buckets; ++
b) {
356 local_coeffs.clear();
357 int64_t bucket_lb = 0;
358 int64_t bucket_ub = 0;
359 const int limit = num_vars * (
b + 1);
360 for (; i * num_buckets < limit; ++i) {
361 local_vars.push_back(vars[i]);
363 const int64_t term1 =
365 const int64_t term2 =
367 bucket_lb +=
std::min(term1, term2);
368 bucket_ub +=
std::max(term1, term2);
371 const IntegerVariable bucket_sum =
373 bucket_sum_vars.push_back(bucket_sum);
374 local_vars.push_back(bucket_sum);
375 local_coeffs.push_back(IntegerValue(-1));
377 {}, local_vars, local_coeffs, IntegerValue(0),
model);
379 model->TakeOwnership(constraint);
384 local_coeffs.clear();
385 for (
const IntegerVariable
var : bucket_sum_vars) {
386 local_vars.push_back(
var);
387 local_coeffs.push_back(IntegerValue(1));
392 model->TakeOwnership(constraint);
401 model->TakeOwnership(constraint);
406 template <
typename VectorInt>
408 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
412 for (int64_t& ref : negated_coeffs) ref = -ref;
417 template <
typename VectorInt>
419 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
428 template <
typename VectorInt>
430 const std::vector<Literal>& enforcement_literals,
431 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
435 if (vars.size() == 1) {
439 enforcement_literals,
445 enforcement_literals,
457 enforcement_literals);
466 enforcement_literals);
471 IntegerValue expression_min(0);
473 for (
int i = 0; i < vars.size(); ++i) {
476 ? integer_trail->LowerBound(vars[i])
477 : integer_trail->UpperBound(vars[i]));
483 IntegerValue non_cached_min;
484 for (
int i = 0; i < vars.size(); ++i) {
486 const IntegerValue lb = integer_trail->LowerBound(vars[i]);
491 const IntegerValue ub = integer_trail->UpperBound(vars[i]);
497 if (non_cached_min > expression_min) {
498 std::vector<Literal> clause;
499 for (
const Literal l : enforcement_literals) {
500 clause.push_back(l.Negated());
506 enforcement_literals, vars,
510 model->TakeOwnership(constraint);
516 template <
typename VectorInt>
518 const std::vector<Literal>& enforcement_literals,
519 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
523 for (int64_t& ref : negated_coeffs) ref = -ref;
529 template <
typename VectorInt>
531 Literal is_le,
const std::vector<IntegerVariable>& vars,
542 template <
typename VectorInt>
544 Literal is_ge,
const std::vector<IntegerVariable>& vars,
556 if (cst.
vars.empty()) {
557 if (cst.
lb <= 0 && cst.
ub >= 0)
return;
563 std::vector<int64_t> converted_coeffs;
564 for (
const IntegerValue v : cst.
coeffs) converted_coeffs.push_back(v.value());
576 const absl::Span<const Literal> enforcement_literals,
578 if (enforcement_literals.empty()) {
581 if (cst.
vars.empty()) {
582 if (cst.
lb <= 0 && cst.
ub >= 0)
return;
587 std::vector<Literal> converted_literals(enforcement_literals.begin(),
588 enforcement_literals.end());
589 std::vector<int64_t> converted_coeffs;
590 for (
const IntegerValue v : cst.
coeffs) converted_coeffs.push_back(v.value());
594 converted_literals, cst.
vars, converted_coeffs, cst.
ub.value()));
598 converted_literals, cst.
vars, converted_coeffs, cst.
lb.value()));
604 template <
typename VectorInt>
606 Literal is_eq,
const std::vector<IntegerVariable>& vars,
621 template <
typename VectorInt>
623 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
643 template <
typename VectorInt>
645 const VectorInt&
coefficients,
const std::vector<IntegerVariable>& vars) {
647 std::vector<IntegerVariable> new_vars = vars;
654 for (
int i = 0; i < new_vars.size(); ++i) {
665 new_vars.push_back(sum);
667 new_coeffs.push_back(-1);
676 IntegerVariable min_var,
const std::vector<IntegerVariable>& vars) {
678 for (
const IntegerVariable&
var : vars) {
685 model->TakeOwnership(constraint);
694 const std::vector<LinearExpression>& exprs) {
698 IntegerVariable min_var;
699 if (min_expr.
vars.size() == 1 &&
700 std::abs(min_expr.
coeffs[0].value()) == 1) {
701 if (min_expr.
coeffs[0].value() == 1) {
702 min_var = min_expr.
vars[0];
713 std::vector<IntegerVariable> min_sum_vars = min_expr.
vars;
714 std::vector<int64_t> min_sum_coeffs;
715 for (IntegerValue coeff : min_expr.
coeffs) {
716 min_sum_coeffs.push_back(coeff.value());
718 min_sum_vars.push_back(min_var);
719 min_sum_coeffs.push_back(-1);
722 -min_expr.
offset.value()));
726 std::vector<IntegerVariable> vars = expr.vars;
727 std::vector<int64_t> coeffs;
728 for (IntegerValue coeff : expr.coeffs) {
729 coeffs.push_back(coeff.value());
731 vars.push_back(min_var);
732 coeffs.push_back(-1);
738 model->TakeOwnership(constraint);
745 IntegerVariable max_var,
const std::vector<IntegerVariable>& vars) {
747 std::vector<IntegerVariable> negated_vars;
748 for (
const IntegerVariable&
var : vars) {
756 model->TakeOwnership(constraint);
762 std::function<void(Model*)>
IsOneOf(IntegerVariable
var,
763 const std::vector<Literal>& selectors,
764 const std::vector<IntegerValue>& values);
821 model->TakeOwnership(constraint);
836 model->TakeOwnership(constraint);
#define CHECK_GE(val1, val2)
#define CHECK_NE(val1, val2)
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
void RegisterWith(GenericLiteralWatcher *watcher)
bool PropagateAtLevelZero()
IntegerSumLE(const std::vector< Literal > &enforcement_literals, const std::vector< IntegerVariable > &vars, const std::vector< IntegerValue > &coeffs, IntegerValue upper_bound, Model *model)
IntegerVariable AddIntegerVariable(IntegerValue lower_bound, IntegerValue upper_bound)
LevelZeroEquality(IntegerVariable target, const std::vector< IntegerVariable > &vars, const std::vector< IntegerValue > &coeffs, Model *model)
LinMinPropagator & operator=(const LinMinPropagator &)=delete
LinMinPropagator(const std::vector< LinearExpression > &exprs, IntegerVariable min_var, Model *model)
LinMinPropagator(const LinMinPropagator &)=delete
void RegisterWith(GenericLiteralWatcher *watcher)
void RegisterWith(GenericLiteralWatcher *watcher)
MinPropagator(const std::vector< IntegerVariable > &vars, IntegerVariable min_var, IntegerTrail *integer_trail)
Class that owns everything related to a particular optimization model.
absl::Span< const double > coefficients
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
IntegerValue FloorRatio(IntegerValue dividend, IntegerValue positive_divisor)
std::function< void(Model *)> GreaterOrEqual(IntegerVariable v, int64_t lb)
std::function< int64_t(const Model &)> UpperBound(IntegerVariable v)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
std::function< void(Model *)> IsEqualToMaxOf(IntegerVariable max_var, const std::vector< IntegerVariable > &vars)
IntegerValue LinExprLowerBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
std::function< void(Model *)> ClauseConstraint(absl::Span< const Literal > literals)
std::function< void(Model *)> DivisionConstraint(IntegerVariable a, IntegerVariable b, IntegerVariable c)
std::function< void(Model *)> ConditionalWeightedSumLowerOrEqual(const std::vector< Literal > &enforcement_literals, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t upper_bound)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
std::function< void(Model *)> FixedWeightedSumReif(Literal is_eq, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t value)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
std::function< BooleanVariable(Model *)> NewBooleanVariable()
std::function< void(Model *)> IsOneOf(IntegerVariable var, const std::vector< Literal > &selectors, const std::vector< IntegerValue > &values)
void LoadConditionalLinearConstraint(const absl::Span< const Literal > enforcement_literals, const LinearConstraint &cst, Model *model)
std::function< void(Model *)> ConditionalSum2LowerOrEqual(IntegerVariable a, IntegerVariable b, int64_t ub, const std::vector< Literal > &enforcement_literals)
std::function< void(Model *)> WeightedSumNotEqual(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t value)
std::function< void(Model *)> ConditionalWeightedSumGreaterOrEqual(const std::vector< Literal > &enforcement_literals, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t lower_bound)
std::function< void(Model *)> Sum2LowerOrEqual(IntegerVariable a, IntegerVariable b, int64_t ub)
void RegisterAndTransferOwnership(Model *model, T *ct)
std::function< IntegerVariable(Model *)> NewWeightedSum(const VectorInt &coefficients, const std::vector< IntegerVariable > &vars)
void LoadLinearConstraint(const ConstraintProto &ct, Model *m)
std::function< void(Model *)> Implication(const std::vector< Literal > &enforcement_literals, IntegerLiteral i)
std::function< void(Model *)> FixedDivisionConstraint(IntegerVariable a, IntegerValue b, IntegerVariable c)
std::function< void(Model *)> Sum3LowerOrEqual(IntegerVariable a, IntegerVariable b, IntegerVariable c, int64_t ub)
std::function< void(Model *)> WeightedSumLowerOrEqual(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t upper_bound)
std::function< void(Model *)> FixedWeightedSum(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t value)
std::function< void(Model *)> LowerOrEqual(IntegerVariable v, int64_t ub)
std::function< IntegerVariable(Model *)> NewIntegerVariable(int64_t lb, int64_t ub)
std::function< void(Model *)> ReifiedBoolAnd(const std::vector< Literal > &literals, Literal r)
std::function< void(Model *)> WeightedSumLowerOrEqualReif(Literal is_le, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t upper_bound)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
std::function< void(Model *)> IsEqualToMinOf(IntegerVariable min_var, const std::vector< IntegerVariable > &vars)
std::function< void(Model *)> WeightedSumGreaterOrEqualReif(Literal is_ge, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t lower_bound)
IntegerValue LinExprUpperBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
std::function< int64_t(const Model &)> LowerBound(IntegerVariable v)
std::function< void(Model *)> ProductConstraint(IntegerVariable a, IntegerVariable b, IntegerVariable p)
std::function< void(Model *)> WeightedSumGreaterOrEqual(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t lower_bound)
std::function< void(Model *)> ConditionalSum3LowerOrEqual(IntegerVariable a, IntegerVariable b, IntegerVariable c, int64_t ub, const std::vector< Literal > &enforcement_literals)
Collection of objects used to extend the Constraint Solver library.
static IntegerLiteral LowerOrEqual(IntegerVariable i, IntegerValue bound)
static IntegerLiteral GreaterOrEqual(IntegerVariable i, IntegerValue bound)
std::vector< IntegerValue > coeffs
std::vector< IntegerVariable > vars
std::vector< IntegerValue > coeffs
std::vector< IntegerVariable > vars