14 #ifndef OR_TOOLS_SAT_LINEAR_CONSTRAINT_H_
15 #define OR_TOOLS_SAT_LINEAR_CONSTRAINT_H_
23 #include "absl/base/attributes.h"
24 #include "absl/strings/str_cat.h"
44 std::vector<IntegerVariable>
vars;
56 lb =
ub = IntegerValue(0);
68 absl::StrAppend(&result,
lb.value(),
" <= ");
70 for (
int i = 0; i <
vars.size(); ++i) {
71 absl::StrAppend(&result, i > 0 ?
" " :
"",
75 absl::StrAppend(&result,
" <= ",
ub.value());
81 if (this->lb != other.
lb)
return false;
82 if (this->ub != other.
ub)
return false;
83 if (this->vars != other.
vars)
return false;
84 if (this->coeffs != other.
coeffs)
return false;
90 os <<
ct.DebugString();
97 std::vector<IntegerVariable>
vars;
99 IntegerValue
offset = IntegerValue(0);
178 void AddTerm(IntegerVariable
var, IntegerValue coeff);
189 const std::vector<LiteralValueValue>& product);
194 Literal lit, IntegerValue coeff = IntegerValue(1));
210 bool* is_quadratic =
nullptr);
214 offset_ = IntegerValue(0);
245 IntegerValue offset_ = IntegerValue(0);
249 std::vector<std::pair<IntegerVariable, IntegerValue>> terms_;
255 const LinearConstraint& constraint,
267 const LinearConstraint& constraint2);
295 template <
class ClassWithVarsAndCoeffs>
297 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
298 ClassWithVarsAndCoeffs* output) {
299 output->vars.clear();
300 output->coeffs.clear();
304 std::sort(terms->begin(), terms->end());
306 IntegerValue current_coeff(0);
307 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
308 if (previous_var == entry.first) {
309 current_coeff += entry.second;
310 }
else if (previous_var ==
NegationOf(entry.first)) {
311 current_coeff -= entry.second;
313 if (current_coeff != 0) {
314 output->vars.push_back(previous_var);
315 output->coeffs.push_back(current_coeff);
317 previous_var = entry.first;
318 current_coeff = entry.second;
321 if (current_coeff != 0) {
322 output->vars.push_back(previous_var);
323 output->coeffs.push_back(current_coeff);
ABSL_MUST_USE_RESULT bool AddLiteralTerm(Literal lit, IntegerValue coeff=IntegerValue(1))
ABSL_MUST_USE_RESULT bool AddDecomposedProduct(const std::vector< LiteralValueValue > &product)
void AddConstant(IntegerValue value)
LinearConstraintBuilder(const Model *model)
void AddLinearExpression(const LinearExpression &expr)
LinearConstraint BuildConstraint(IntegerValue lb, IntegerValue ub)
LinearConstraintBuilder()
LinearExpression BuildExpression()
void ResetBounds(IntegerValue lb, IntegerValue ub)
LinearConstraintBuilder(const Model *model, IntegerValue lb, IntegerValue ub)
void AddTerm(IntegerVariable var, IntegerValue coeff)
void AddQuadraticLowerBound(AffineExpression left, AffineExpression right, IntegerTrail *integer_trail, bool *is_quadratic=nullptr)
Class that owns everything related to a particular optimization model.
bool ValidateLinearConstraintForOverflow(const LinearConstraint &constraint, const IntegerTrail &integer_trail)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
std::string IntegerTermDebugString(IntegerVariable var, IntegerValue coeff)
void RemoveZeroTerms(LinearConstraint *constraint)
LinearExpression PositiveVarExpr(const LinearExpression &expr)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
double ScalarProduct(const LinearConstraint &constraint1, const LinearConstraint &constraint2)
const IntegerVariable kNoIntegerVariable(-1)
void MakeAllCoefficientsPositive(LinearConstraint *constraint)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
void CanonicalizeConstraint(LinearConstraint *ct)
bool NoDuplicateVariable(const LinearConstraint &ct)
double ComputeL2Norm(const LinearConstraint &constraint)
IntegerValue GetCoefficient(const IntegerVariable var, const LinearExpression &expr)
void MakeAllVariablesPositive(LinearConstraint *constraint)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
IntegerValue GetCoefficientOfPositiveVar(const IntegerVariable var, const LinearExpression &expr)
IntegerValue ComputeInfinityNorm(const LinearConstraint &constraint)
void CleanTermsAndFillConstraint(std::vector< std::pair< IntegerVariable, IntegerValue >> *terms, ClassWithVarsAndCoeffs *output)
void DivideByGCD(LinearConstraint *constraint)
double ComputeActivity(const LinearConstraint &constraint, const absl::StrongVector< IntegerVariable, double > &values)
Collection of objects used to extend the Constraint Solver library.
std::vector< IntegerValue > coeffs
bool operator==(const LinearConstraint other) const
std::vector< IntegerVariable > vars
std::string DebugString() const
LinearConstraint(IntegerValue _lb, IntegerValue _ub)
void AddTerm(IntegerVariable var, IntegerValue coeff)
IntegerValue LevelZeroMin(IntegerTrail *integer_trail) const
std::vector< IntegerValue > coeffs
IntegerValue Max(const IntegerTrail &integer_trail) const
std::vector< IntegerVariable > vars
std::string DebugString() const
double LpValue(const absl::StrongVector< IntegerVariable, double > &lp_values) const
IntegerValue Min(const IntegerTrail &integer_trail) const