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);
201 offset_ = IntegerValue(0);
223 const IntegerValue lb_;
224 const IntegerValue ub_;
226 IntegerValue offset_ = IntegerValue(0);
230 std::vector<std::pair<IntegerVariable, IntegerValue>> terms_;
236 const LinearConstraint& constraint,
248 const LinearConstraint& constraint2);
276template <
class ClassWithVarsAndCoeffs>
278 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
279 ClassWithVarsAndCoeffs* output) {
280 output->vars.clear();
281 output->coeffs.clear();
285 std::sort(terms->begin(), terms->end());
287 IntegerValue current_coeff(0);
288 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
289 if (previous_var == entry.first) {
290 current_coeff += entry.second;
291 }
else if (previous_var ==
NegationOf(entry.first)) {
292 current_coeff -= entry.second;
294 if (current_coeff != 0) {
295 output->vars.push_back(previous_var);
296 output->coeffs.push_back(current_coeff);
298 previous_var = entry.first;
299 current_coeff = entry.second;
302 if (current_coeff != 0) {
303 output->vars.push_back(previous_var);
304 output->coeffs.push_back(current_coeff);
ABSL_MUST_USE_RESULT bool AddLiteralTerm(Literal lit, IntegerValue coeff)
void AddConstant(IntegerValue value)
LinearConstraintBuilder(const Model *model)
void AddLinearExpression(const LinearExpression &expr)
LinearConstraint BuildConstraint(IntegerValue lb, IntegerValue ub)
void AddQuadraticLowerBound(AffineExpression left, AffineExpression right, IntegerTrail *integer_trail)
LinearExpression BuildExpression()
LinearConstraintBuilder(const Model *model, IntegerValue lb, IntegerValue ub)
void AddTerm(IntegerVariable var, IntegerValue coeff)
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)
IntegerValue LinExprLowerBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
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 CleanTermsAndFillConstraint(std::vector< std::pair< IntegerVariable, IntegerValue > > *terms, ClassWithVarsAndCoeffs *output)
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)
IntegerValue LinExprUpperBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
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 Min(IntegerTrail *integer_trail) const
IntegerValue LevelZeroMin(IntegerTrail *integer_trail) const
std::vector< IntegerValue > coeffs
std::vector< IntegerVariable > vars
std::string DebugString() const
double LpValue(const absl::StrongVector< IntegerVariable, double > &lp_values) const