14#ifndef OR_TOOLS_SAT_LINEAR_CONSTRAINT_H_
15#define OR_TOOLS_SAT_LINEAR_CONSTRAINT_H_
24#include "absl/base/attributes.h"
25#include "absl/strings/str_cat.h"
26#include "absl/types/span.h"
58 std::unique_ptr<IntegerVariable[]>
vars;
59 std::unique_ptr<IntegerValue[]>
coeffs;
68 IntegerVariable* tmp_vars =
new IntegerVariable[
size];
69 IntegerValue* tmp_coeffs =
new IntegerValue[
size];
72 memcpy(tmp_vars,
vars.get(),
sizeof(IntegerVariable) * to_copy);
73 memcpy(tmp_coeffs,
coeffs.get(),
sizeof(IntegerValue) * to_copy);
83 absl::StrAppend(&result,
lb.value(),
" <= ");
86 absl::StrAppend(&result,
i > 0 ?
" " :
"",
90 absl::StrAppend(&result,
" <= ",
ub.value());
96 if (this->num_terms != other.
num_terms)
return false;
97 if (this->num_terms == 0)
return true;
98 if (memcmp(this->vars.get(), other.
vars.get(),
99 sizeof(IntegerVariable) * this->num_terms)) {
102 if (memcmp(this->coeffs.get(), other.
coeffs.get(),
103 sizeof(IntegerValue) * this->num_terms)) {
110 if (this->lb != other.
lb)
return false;
111 if (this->ub != other.
ub)
return false;
125 os <<
ct.DebugString();
132 std::vector<IntegerVariable>
vars;
201 : encoder_(encoder), lb_(0), ub_(0) {}
206 : encoder_(encoder), lb_(lb), ub_(ub) {}
215 : encoder_(nullptr), lb_(lb), ub_(ub) {}
219 void AddTerm(IntegerVariable
var, IntegerValue coeff);
230 absl::Span<const LiteralValueValue> product);
235 Literal lit, IntegerValue coeff = IntegerValue(1));
251 bool* is_quadratic =
nullptr);
255 offset_ = IntegerValue(0);
286 IntegerValue offset_ = IntegerValue(0);
290 std::vector<std::pair<IntegerVariable, IntegerValue>> terms_;
296 const LinearConstraint& constraint,
306 const LinearConstraint& constraint);
317 const LinearConstraint& constraint2);
338 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
340 output->
vars.clear();
345 std::sort(terms->begin(), terms->end());
347 IntegerValue current_coeff(0);
348 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
349 if (previous_var == entry.first) {
350 current_coeff += entry.second;
351 }
else if (previous_var ==
NegationOf(entry.first)) {
352 current_coeff -= entry.second;
354 if (current_coeff != 0) {
355 output->
vars.push_back(previous_var);
356 output->
coeffs.push_back(current_coeff);
358 previous_var = entry.first;
359 current_coeff = entry.second;
362 if (current_coeff != 0) {
363 output->
vars.push_back(previous_var);
364 output->
coeffs.push_back(current_coeff);
369 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
374 output->
resize(terms->size());
375 std::sort(terms->begin(), terms->end());
377 IntegerValue current_coeff(0);
378 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
379 if (previous_var == entry.first) {
380 current_coeff += entry.second;
381 }
else if (previous_var ==
NegationOf(entry.first)) {
382 current_coeff -= entry.second;
384 if (current_coeff != 0) {
385 output->
vars[new_size] = previous_var;
386 output->
coeffs[new_size] = current_coeff;
389 previous_var = entry.first;
390 current_coeff = entry.second;
393 if (current_coeff != 0) {
394 output->
vars[new_size] = previous_var;
395 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)
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::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
Returns the vector of the negated variables.
std::string IntegerTermDebugString(IntegerVariable var, IntegerValue coeff)
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)
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.
double ScalarProduct(const LinearConstraint &ct1, const LinearConstraint &ct2)
In SWIG mode, we don't want anything besides these top-level includes.
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