26 if (coeff == 0)
return;
30 terms_.push_back({
var, coeff});
38 if (coeff == 0)
return;
43 terms_.push_back({expr.
var, coeff * expr.
coeff});
59 for (
int i = 0; i < expr.
vars.size(); ++i) {
62 terms_.push_back({expr.
vars[i], expr.
coeffs[i] * coeff});
74 if (left.
IsFixed(integer_trail)) {
76 }
else if (right.
IsFixed(integer_trail)) {
79 const IntegerValue left_min = left.
Min(integer_trail);
80 const IntegerValue right_min = right.
Min(integer_trail);
94 Literal lit, IntegerValue coeff) {
96 bool has_opposite_view =
102 if (has_direct_view && has_opposite_view) {
105 has_opposite_view =
false;
107 has_direct_view =
false;
110 if (has_direct_view) {
114 if (has_opposite_view) {
124 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
126 constraint->
vars.clear();
127 constraint->
coeffs.clear();
131 std::sort(terms->begin(), terms->end());
133 IntegerValue current_coeff(0);
134 for (
const std::pair<IntegerVariable, IntegerValue> entry : *terms) {
135 if (previous_var == entry.first) {
136 current_coeff += entry.second;
137 }
else if (previous_var ==
NegationOf(entry.first)) {
138 current_coeff -= entry.second;
140 if (current_coeff != 0) {
141 constraint->
vars.push_back(previous_var);
142 constraint->
coeffs.push_back(current_coeff);
144 previous_var = entry.first;
145 current_coeff = entry.second;
148 if (current_coeff != 0) {
149 constraint->
vars.push_back(previous_var);
150 constraint->
coeffs.push_back(current_coeff);
166 for (
int i = 0; i < constraint.
vars.size(); ++i) {
167 const IntegerVariable
var = constraint.
vars[i];
168 const IntegerValue coeff = constraint.
coeffs[i];
169 activity += coeff.value() * values[
var];
176 for (
const IntegerValue coeff : constraint.
coeffs) {
179 return std::sqrt(sum);
183 IntegerValue result(0);
184 for (
const IntegerValue coeff : constraint.
coeffs) {
192 DCHECK(std::is_sorted(constraint1.
vars.begin(), constraint1.
vars.end()));
193 DCHECK(std::is_sorted(constraint2.
vars.begin(), constraint2.
vars.end()));
194 double scalar_product = 0.0;
197 while (index_1 < constraint1.
vars.size() &&
198 index_2 < constraint2.
vars.size()) {
199 if (constraint1.
vars[index_1] == constraint2.
vars[index_2]) {
204 }
else if (constraint1.
vars[index_1] > constraint2.
vars[index_2]) {
210 return scalar_product;
216 IntegerValue ComputeGcd(
const std::vector<IntegerValue>& values) {
217 if (values.empty())
return IntegerValue(1);
219 for (
const IntegerValue
value : values) {
223 if (gcd < 0)
return IntegerValue(1);
224 return IntegerValue(gcd);
230 if (constraint->
coeffs.empty())
return;
231 const IntegerValue gcd = ComputeGcd(constraint->
coeffs);
232 if (gcd == 1)
return;
240 for (IntegerValue& coeff : constraint->
coeffs) coeff /= gcd;
245 const int size = constraint->
vars.size();
246 for (
int i = 0; i < size; ++i) {
247 if (constraint->
coeffs[i] == 0)
continue;
248 constraint->
vars[new_size] = constraint->
vars[i];
252 constraint->
vars.resize(new_size);
253 constraint->
coeffs.resize(new_size);
257 const int size = constraint->
vars.size();
258 for (
int i = 0; i < size; ++i) {
259 const IntegerValue coeff = constraint->
coeffs[i];
261 constraint->
coeffs[i] = -coeff;
268 const int size = constraint->
vars.size();
269 for (
int i = 0; i < size; ++i) {
270 const IntegerVariable
var = constraint->
vars[i];
281 for (
int i = 0; i <
vars.size(); ++i) {
289 for (
int i = 0; i <
vars.size(); ++i) {
290 absl::StrAppend(&result, i > 0 ?
" " :
"",
294 absl::StrAppend(&result,
" + ",
offset.value());
306 std::vector<std::pair<IntegerVariable, IntegerValue>> terms;
308 const int size =
ct->vars.size();
309 for (
int i = 0; i < size; ++i) {
311 terms.push_back({
ct->vars[i],
ct->coeffs[i]});
316 std::sort(terms.begin(), terms.end());
320 for (
const auto& term : terms) {
321 ct->vars.push_back(term.first);
322 ct->coeffs.push_back(term.second);
327 absl::flat_hash_set<IntegerVariable> seen_variables;
328 const int size =
ct.vars.size();
329 for (
int i = 0; i < size; ++i) {
331 if (!seen_variables.insert(
ct.vars[i]).second)
return false;
333 if (!seen_variables.insert(
NegationOf(
ct.vars[i])).second)
return false;
342 for (
int i = 0; i < expr.
vars.size(); ++i) {
347 canonical_expr.
vars.push_back(expr.
vars[i]);
351 return canonical_expr;
357 for (
int i = 0; i < expr.
vars.size(); ++i) {
367 for (
int i = 0; i < expr.
vars.size(); ++i) {
378 int64_t positive_sum(0);
379 int64_t negative_sum(0);
380 for (
int i = 0; i < constraint.
vars.size(); ++i) {
381 const IntegerVariable
var = constraint.
vars[i];
382 const IntegerValue coeff = constraint.
coeffs[i];
386 int64_t min_prod =
CapProd(coeff.value(), lb.value());
387 int64_t max_prod =
CapProd(coeff.value(), ub.value());
388 if (min_prod > max_prod)
std::swap(min_prod, max_prod);
390 positive_sum =
CapAdd(positive_sum,
std::max(int64_t{0}, max_prod));
391 negative_sum =
CapAdd(negative_sum,
std::min(int64_t{0}, min_prod));
395 if (positive_sum >= limit)
return false;
396 if (negative_sum <= -limit)
return false;
397 if (
CapSub(positive_sum, negative_sum) >= limit)
return false;
413 for (
int i = 0; i < expr.
vars.size(); ++i) {
415 result.
vars.push_back(expr.
vars[i]);
427 for (
int i = 0; i < expr.
vars.size(); ++i) {
434 return IntegerValue(0);
440 for (
int i = 0; i < expr.
vars.size(); ++i) {
445 return IntegerValue(0);
int64_t CapSub(int64_t x, int64_t y)
IntegerValue GetCoefficient(const IntegerVariable var, const LinearExpression &expr)
double ComputeL2Norm(const LinearConstraint &constraint)
IntegerValue LinExprLowerBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
std::vector< IntegerValue > coeffs
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
IntegerValue LowerBound(IntegerVariable i) const
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
void CleanTermsAndFillConstraint(std::vector< std::pair< IntegerVariable, IntegerValue >> *terms, LinearConstraint *constraint)
void AddLinearExpression(const LinearExpression &expr)
std::string IntegerTermDebugString(IntegerVariable var, IntegerValue coeff)
LinearExpression PositiveVarExpr(const LinearExpression &expr)
void AddTerm(IntegerVariable var, IntegerValue coeff)
int64_t CapProd(int64_t x, int64_t y)
ABSL_MUST_USE_RESULT bool AddLiteralTerm(Literal lit, IntegerValue coeff)
void MakeAllCoefficientsPositive(LinearConstraint *constraint)
double LpValue(const absl::StrongVector< IntegerVariable, double > &lp_values) const
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
std::vector< IntegerValue > coeffs
const IntegerVariable GetLiteralView(Literal lit) const
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
double ComputeActivity(const LinearConstraint &constraint, const absl::StrongVector< IntegerVariable, double > &values)
std::vector< IntegerVariable > vars
std::vector< IntegerVariable > vars
double ToDouble(IntegerValue value)
void DivideByGCD(LinearConstraint *constraint)
int64_t CapAdd(int64_t x, int64_t y)
IntegerValue ComputeInfinityNorm(const LinearConstraint &constraint)
IntegerValue LevelZeroUpperBound(IntegerVariable var) const
static int64_t GCD64(int64_t x, int64_t y)
std::string DebugString() const
bool IsFixed(IntegerTrail *integer_trail) const
bool VariableIsPositive(IntegerVariable i)
#define DCHECK_GE(val1, val2)
bool ValidateLinearConstraintForOverflow(const LinearConstraint &constraint, const IntegerTrail &integer_trail)
double ScalarProduct(const LinearConstraint &constraint1, const LinearConstraint &constraint2)
IntegerValue LinExprUpperBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
void CanonicalizeConstraint(LinearConstraint *ct)
#define DCHECK(condition)
IntegerValue FloorRatio(IntegerValue dividend, IntegerValue positive_divisor)
void AddConstant(IntegerValue value)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
IntegerValue UpperBound(IntegerVariable i) const
Collection of objects used to extend the Constraint Solver library.
const IntegerVariable kNoIntegerVariable(-1)
IntType IntTypeAbs(IntType t)
void MakeAllVariablesPositive(LinearConstraint *constraint)
IntegerValue Min(IntegerTrail *integer_trail) const
IntegerValue LevelZeroLowerBound(IntegerVariable var) const
void RemoveZeroTerms(LinearConstraint *constraint)
void AddQuadraticLowerBound(AffineExpression left, AffineExpression right, IntegerTrail *integer_trail)
IntegerValue GetCoefficientOfPositiveVar(const IntegerVariable var, const LinearExpression &expr)
bool NoDuplicateVariable(const LinearConstraint &ct)