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_;
247 const IntegerVariable num_;
248 const IntegerVariable denom_;
249 const IntegerVariable div_;
266 const IntegerVariable a_;
267 const IntegerValue b_;
268 const IntegerVariable c_;
285 const IntegerVariable x_;
286 const IntegerVariable s_;
297template <
typename VectorInt>
299 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
303 if (vars.size() == 1) {
343 const int num_vars = vars.size();
344 if (num_vars > 100) {
345 std::vector<IntegerVariable> bucket_sum_vars;
347 std::vector<IntegerVariable> local_vars;
348 std::vector<IntegerValue> local_coeffs;
351 const int num_buckets =
static_cast<int>(std::round(std::sqrt(num_vars)));
352 for (
int b = 0;
b < num_buckets; ++
b) {
354 local_coeffs.clear();
355 int64_t bucket_lb = 0;
356 int64_t bucket_ub = 0;
357 const int limit = num_vars * (
b + 1);
358 for (; i * num_buckets < limit; ++i) {
359 local_vars.push_back(vars[i]);
361 const int64_t term1 =
363 const int64_t term2 =
365 bucket_lb +=
std::min(term1, term2);
366 bucket_ub +=
std::max(term1, term2);
369 const IntegerVariable bucket_sum =
371 bucket_sum_vars.push_back(bucket_sum);
372 local_vars.push_back(bucket_sum);
373 local_coeffs.push_back(IntegerValue(-1));
375 {}, local_vars, local_coeffs, IntegerValue(0),
model);
377 model->TakeOwnership(constraint);
382 local_coeffs.clear();
383 for (
const IntegerVariable
var : bucket_sum_vars) {
384 local_vars.push_back(
var);
385 local_coeffs.push_back(IntegerValue(1));
390 model->TakeOwnership(constraint);
399 model->TakeOwnership(constraint);
404template <
typename VectorInt>
406 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
410 for (int64_t& ref : negated_coeffs) ref = -ref;
415template <
typename VectorInt>
417 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
426template <
typename VectorInt>
428 const std::vector<Literal>& enforcement_literals,
429 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
433 if (vars.size() == 1) {
437 enforcement_literals,
443 enforcement_literals,
455 enforcement_literals);
464 enforcement_literals);
469 IntegerValue expression_min(0);
471 for (
int i = 0; i < vars.size(); ++i) {
474 ? integer_trail->LowerBound(vars[i])
475 : integer_trail->UpperBound(vars[i]));
481 IntegerValue non_cached_min;
482 for (
int i = 0; i < vars.size(); ++i) {
484 const IntegerValue lb = integer_trail->LowerBound(vars[i]);
489 const IntegerValue ub = integer_trail->UpperBound(vars[i]);
495 if (non_cached_min > expression_min) {
496 std::vector<Literal> clause;
497 for (
const Literal l : enforcement_literals) {
498 clause.push_back(l.Negated());
504 enforcement_literals, vars,
508 model->TakeOwnership(constraint);
514template <
typename VectorInt>
516 const std::vector<Literal>& enforcement_literals,
517 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
521 for (int64_t& ref : negated_coeffs) ref = -ref;
527template <
typename VectorInt>
529 Literal is_le,
const std::vector<IntegerVariable>& vars,
540template <
typename VectorInt>
542 Literal is_ge,
const std::vector<IntegerVariable>& vars,
554 if (cst.
vars.empty()) {
555 if (cst.
lb <= 0 && cst.
ub >= 0)
return;
561 std::vector<int64_t> converted_coeffs;
562 for (
const IntegerValue v : cst.
coeffs) converted_coeffs.push_back(v.value());
574 const absl::Span<const Literal> enforcement_literals,
576 if (enforcement_literals.empty()) {
579 if (cst.
vars.empty()) {
580 if (cst.
lb <= 0 && cst.
ub >= 0)
return;
585 std::vector<Literal> converted_literals(enforcement_literals.begin(),
586 enforcement_literals.end());
587 std::vector<int64_t> converted_coeffs;
588 for (
const IntegerValue v : cst.
coeffs) converted_coeffs.push_back(v.value());
592 converted_literals, cst.
vars, converted_coeffs, cst.
ub.value()));
596 converted_literals, cst.
vars, converted_coeffs, cst.
lb.value()));
602template <
typename VectorInt>
604 Literal is_eq,
const std::vector<IntegerVariable>& vars,
619template <
typename VectorInt>
621 const std::vector<IntegerVariable>& vars,
const VectorInt&
coefficients,
641template <
typename VectorInt>
643 const VectorInt&
coefficients,
const std::vector<IntegerVariable>& vars) {
645 std::vector<IntegerVariable> new_vars = vars;
652 for (
int i = 0; i < new_vars.size(); ++i) {
663 new_vars.push_back(sum);
665 new_coeffs.push_back(-1);
674 IntegerVariable min_var,
const std::vector<IntegerVariable>& vars) {
676 for (
const IntegerVariable&
var : vars) {
683 model->TakeOwnership(constraint);
692 const std::vector<LinearExpression>& exprs) {
696 IntegerVariable min_var;
697 if (min_expr.
vars.size() == 1 &&
698 std::abs(min_expr.
coeffs[0].value()) == 1 && min_expr.
offset == 0) {
699 if (min_expr.
coeffs[0].value() == 1) {
700 min_var = min_expr.
vars[0];
711 std::vector<IntegerVariable> min_sum_vars = min_expr.
vars;
712 std::vector<int64_t> min_sum_coeffs;
713 for (IntegerValue coeff : min_expr.
coeffs) {
714 min_sum_coeffs.push_back(coeff.value());
716 min_sum_vars.push_back(min_var);
717 min_sum_coeffs.push_back(-1);
720 -min_expr.
offset.value()));
724 std::vector<IntegerVariable> vars = expr.vars;
725 std::vector<int64_t> coeffs;
726 for (IntegerValue coeff : expr.coeffs) {
727 coeffs.push_back(coeff.value());
729 vars.push_back(min_var);
730 coeffs.push_back(-1);
736 model->TakeOwnership(constraint);
743 IntegerVariable max_var,
const std::vector<IntegerVariable>& vars) {
745 std::vector<IntegerVariable> negated_vars;
746 for (
const IntegerVariable&
var : vars) {
754 model->TakeOwnership(constraint);
760std::function<void(Model*)>
IsOneOf(IntegerVariable
var,
761 const std::vector<Literal>& selectors,
762 const std::vector<IntegerValue>& values);
812 IntegerVariable denom,
813 IntegerVariable div) {
819 model->TakeOwnership(constraint);
834 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 *)> WeightedSumGreaterOrEqual(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t lower_bound)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
IntegerValue LinExprLowerBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
std::function< void(Model *)> WeightedSumLowerOrEqual(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t upper_bound)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
std::function< void(Model *)> ClauseConstraint(absl::Span< const Literal > literals)
std::function< int64_t(const Model &)> LowerBound(IntegerVariable v)
std::function< void(Model *)> IsOneOf(IntegerVariable var, const std::vector< Literal > &selectors, const std::vector< IntegerValue > &values)
std::function< BooleanVariable(Model *)> NewBooleanVariable()
std::function< void(Model *)> LowerOrEqual(IntegerVariable v, int64_t ub)
std::function< void(Model *)> DivisionConstraint(IntegerVariable num, IntegerVariable denom, IntegerVariable div)
void LoadConditionalLinearConstraint(const absl::Span< const Literal > enforcement_literals, const LinearConstraint &cst, Model *model)
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 *)> WeightedSumGreaterOrEqualReif(Literal is_ge, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t lower_bound)
std::function< void(Model *)> Sum2LowerOrEqual(IntegerVariable a, IntegerVariable b, int64_t ub)
std::function< void(Model *)> ConditionalWeightedSumLowerOrEqual(const std::vector< Literal > &enforcement_literals, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t upper_bound)
std::function< void(Model *)> WeightedSumNotEqual(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t value)
std::function< void(Model *)> IsEqualToMaxOf(IntegerVariable max_var, const std::vector< IntegerVariable > &vars)
std::function< void(Model *)> Implication(const std::vector< Literal > &enforcement_literals, IntegerLiteral i)
std::function< void(Model *)> WeightedSumLowerOrEqualReif(Literal is_le, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t upper_bound)
std::function< void(Model *)> ProductConstraint(IntegerVariable a, IntegerVariable b, IntegerVariable p)
void RegisterAndTransferOwnership(Model *model, T *ct)
void LoadLinearConstraint(const ConstraintProto &ct, Model *m)
std::function< void(Model *)> FixedDivisionConstraint(IntegerVariable a, IntegerValue b, IntegerVariable c)
std::function< int64_t(const Model &)> UpperBound(IntegerVariable v)
std::function< void(Model *)> ReifiedBoolAnd(const std::vector< Literal > &literals, Literal r)
std::function< void(Model *)> FixedWeightedSumReif(Literal is_eq, const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t value)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
std::function< void(Model *)> IsEqualToMinOf(IntegerVariable min_var, const std::vector< IntegerVariable > &vars)
std::function< IntegerVariable(Model *)> NewIntegerVariable(int64_t lb, int64_t ub)
std::function< void(Model *)> GreaterOrEqual(IntegerVariable v, int64_t lb)
std::function< void(Model *)> ConditionalSum2LowerOrEqual(IntegerVariable a, IntegerVariable b, int64_t ub, const std::vector< Literal > &enforcement_literals)
std::function< void(Model *)> Sum3LowerOrEqual(IntegerVariable a, IntegerVariable b, IntegerVariable c, int64_t ub)
IntegerValue LinExprUpperBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
std::function< IntegerVariable(Model *)> NewWeightedSum(const VectorInt &coefficients, const std::vector< IntegerVariable > &vars)
std::function< void(Model *)> ConditionalSum3LowerOrEqual(IntegerVariable a, IntegerVariable b, IntegerVariable c, int64_t ub, const std::vector< Literal > &enforcement_literals)
std::function< void(Model *)> FixedWeightedSum(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t value)
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