14#ifndef OR_TOOLS_SAT_LINEAR_CONSTRAINT_H_
15#define OR_TOOLS_SAT_LINEAR_CONSTRAINT_H_
26#include "absl/base/attributes.h"
27#include "absl/log/check.h"
28#include "absl/strings/str_cat.h"
29#include "absl/types/span.h"
63 std::unique_ptr<IntegerVariable[]>
vars;
64 std::unique_ptr<IntegerValue[]>
coeffs;
73 IntegerVariable* tmp_vars =
new IntegerVariable[size];
74 IntegerValue* tmp_coeffs =
new IntegerValue[size];
75 const int to_copy = std::min(size,
num_terms);
77 memcpy(tmp_vars,
vars.get(),
sizeof(IntegerVariable) * to_copy);
78 memcpy(tmp_coeffs,
coeffs.get(),
sizeof(IntegerValue) * to_copy);
88 absl::StrAppend(&result,
lb.value(),
" <= ");
91 absl::StrAppend(&result,
i > 0 ?
" " :
"",
95 absl::StrAppend(&result,
" <= ",
ub.value());
101 if (this->num_terms != other.
num_terms)
return false;
102 if (this->num_terms == 0)
return true;
103 if (memcmp(this->vars.get(), other.
vars.get(),
104 sizeof(IntegerVariable) * this->num_terms)) {
107 if (memcmp(this->coeffs.get(), other.
coeffs.get(),
108 sizeof(IntegerValue) * this->num_terms)) {
115 if (this->lb != other.
lb)
return false;
116 if (this->ub != other.
ub)
return false;
137 std::vector<IntegerVariable>
vars;
206 : encoder_(encoder), lb_(0), ub_(0) {}
211 : encoder_(encoder), lb_(lb), ub_(ub) {}
220 : encoder_(nullptr), lb_(lb), ub_(ub) {}
224 void AddTerm(IntegerVariable var, IntegerValue coeff);
235 absl::Span<const LiteralValueValue> product);
240 Literal lit, IntegerValue coeff = IntegerValue(1));
256 bool* is_quadratic =
nullptr);
260 offset_ = IntegerValue(0);
296 IntegerValue offset_ = IntegerValue(0);
300 std::vector<std::pair<IntegerVariable, IntegerValue>> terms_;
306 const LinearConstraint& constraint,
316 const LinearConstraint& constraint);
327 const LinearConstraint& constraint2);
348 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
350 output->
vars.clear();
355 std::sort(terms->begin(), terms->end());
357 IntegerValue current_coeff(0);
358 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
359 if (previous_var == entry.first) {
360 current_coeff += entry.second;
361 }
else if (previous_var ==
NegationOf(entry.first)) {
362 current_coeff -= entry.second;
364 if (current_coeff != 0) {
365 output->
vars.push_back(previous_var);
366 output->
coeffs.push_back(current_coeff);
368 previous_var = entry.first;
369 current_coeff = entry.second;
372 if (current_coeff != 0) {
373 output->
vars.push_back(previous_var);
374 output->
coeffs.push_back(current_coeff);
379 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
384 output->
resize(terms->size());
385 std::sort(terms->begin(), terms->end());
387 IntegerValue current_coeff(0);
388 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
389 if (previous_var == entry.first) {
390 current_coeff += entry.second;
391 }
else if (previous_var ==
NegationOf(entry.first)) {
392 current_coeff -= entry.second;
394 if (current_coeff != 0) {
395 output->
vars[new_size] = previous_var;
396 output->
coeffs[new_size] = current_coeff;
399 previous_var = entry.first;
400 current_coeff = entry.second;
403 if (current_coeff != 0) {
404 output->
vars[new_size] = previous_var;
405 output->
coeffs[new_size] = current_coeff;
412 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
417 output->
resize(terms->size());
418 std::sort(terms->begin(), terms->end());
420 int64_t current_coeff = 0;
421 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
423 if (previous_var == entry.first) {
428 if (current_coeff != 0) {
429 output->
vars[new_size] = previous_var;
430 output->
coeffs[new_size] = current_coeff;
433 previous_var = entry.first;
434 current_coeff = entry.second.value();
437 if (current_coeff != 0) {
438 output->
vars[new_size] = previous_var;
439 output->
coeffs[new_size] = current_coeff;
void AddQuadraticLowerBound(AffineExpression left, AffineExpression right, IntegerTrail *integer_trail, bool *is_quadratic=nullptr)
LinearConstraintBuilder()
LinearExpression BuildExpression()
LinearConstraintBuilder(IntegerEncoder *encoder)
ABSL_MUST_USE_RESULT bool AddLiteralTerm(Literal lit, IntegerValue coeff=IntegerValue(1))
void ResetBounds(IntegerValue lb, IntegerValue ub)
Reset the bounds passed at construction time.
LinearConstraintBuilder(IntegerEncoder *encoder, IntegerValue lb, IntegerValue ub)
void AddTerm(IntegerVariable var, IntegerValue coeff)
void AddLinearExpression(const LinearExpression &expr)
void AddConstant(IntegerValue value)
Adds the corresponding term to the current linear expression.
LinearConstraintBuilder(const Model *model)
void Clear()
Clears all added terms and constants. Keeps the original bounds.
LinearConstraintBuilder(const Model *model, IntegerValue lb, IntegerValue ub)
LinearConstraint BuildConstraint(IntegerValue lb, IntegerValue ub)
LinearConstraintBuilder(IntegerValue lb, IntegerValue ub)
bool BuildIntoConstraintAndCheckOverflow(IntegerValue lb, IntegerValue ub, LinearConstraint *ct)
ABSL_MUST_USE_RESULT bool AddDecomposedProduct(absl::Span< const LiteralValueValue > product)
double ComputeActivity(const LinearConstraint &constraint, const util_intops::StrongVector< IntegerVariable, double > &values)
void DivideByGCD(LinearConstraint *constraint)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
double ComputeL2Norm(const LinearConstraint &ct)
Returns sqrt(sum square(coeff)).
void RemoveZeroTerms(LinearConstraint *constraint)
Removes the entries with a coefficient of zero.
IntegerValue ComputeInfinityNorm(const LinearConstraint &ct)
Returns the maximum absolute value of the coefficients.
std::string IntegerTermDebugString(IntegerVariable var, IntegerValue coeff)
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
Returns the vector of the negated variables.
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
const IntegerVariable kNoIntegerVariable(-1)
bool PossibleOverflow(const IntegerTrail &integer_trail, const LinearConstraint &constraint)
void CleanTermsAndFillConstraint(std::vector< std::pair< IntegerVariable, IntegerValue > > *terms, LinearExpression *output)
void MakeAllCoefficientsPositive(LinearConstraint *constraint)
Makes all coefficients positive by transforming a variable to its negation.
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
bool MergePositiveVariableTermsAndCheckForOverflow(std::vector< std::pair< IntegerVariable, IntegerValue > > *terms, LinearConstraint *output)
LinearExpression PositiveVarExpr(const LinearExpression &expr)
Returns the same expression with positive variables.
IntegerValue GetCoefficient(const IntegerVariable var, const LinearExpression &expr)
void MakeAllVariablesPositive(LinearConstraint *constraint)
Makes all variables "positive" by transforming a variable to its negation.
IntegerValue GetCoefficientOfPositiveVar(const IntegerVariable var, const LinearExpression &expr)
bool ValidateLinearConstraintForOverflow(const LinearConstraint &constraint, const IntegerTrail &integer_trail)
bool NoDuplicateVariable(const LinearConstraint &ct)
Returns false if duplicate variables are found in ct.
bool VariableIsPositive(IntegerVariable i)
double ScalarProduct(const LinearConstraint &ct1, const LinearConstraint &ct2)
In SWIG mode, we don't want anything besides these top-level includes.
bool AddIntoOverflow(int64_t x, int64_t *y)
LinearConstraint()=default
std::unique_ptr< IntegerValue[]> coeffs
std::unique_ptr< IntegerVariable[]> vars
absl::Span< const IntegerValue > CoeffsAsSpan() const
std::string DebugString() const
absl::Span< const IntegerVariable > VarsAsSpan() const
bool IsEqualIgnoringBounds(const LinearConstraint &other) const
bool operator==(const LinearConstraint &other) const
LinearConstraint(IntegerValue _lb, IntegerValue _ub)
IntegerValue LevelZeroMin(IntegerTrail *integer_trail) const
IntegerValue Min(const IntegerTrail &integer_trail) const
double LpValue(const util_intops::StrongVector< IntegerVariable, double > &lp_values) const
Return[s] the evaluation of the linear expression.
std::string DebugString() const
std::vector< IntegerVariable > vars
std::vector< IntegerValue > coeffs
IntegerValue Max(const IntegerTrail &integer_trail) const