29 terms_.push_back({
var, coeff});
41 terms_.push_back({expr.
var, coeff * expr.
coeff});
56 Literal lit, IntegerValue coeff) {
58 bool has_opposite_view =
64 if (has_direct_view && has_opposite_view) {
67 has_opposite_view =
false;
69 has_direct_view =
false;
72 if (has_direct_view) {
76 if (has_opposite_view) {
86 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
88 constraint->
vars.clear();
89 constraint->
coeffs.clear();
93 std::sort(terms->begin(), terms->end());
95 IntegerValue current_coeff(0);
96 for (
const std::pair<IntegerVariable, IntegerValue> entry : *terms) {
97 if (previous_var == entry.first) {
98 current_coeff += entry.second;
99 }
else if (previous_var ==
NegationOf(entry.first)) {
100 current_coeff -= entry.second;
102 if (current_coeff != 0) {
103 constraint->
vars.push_back(previous_var);
104 constraint->
coeffs.push_back(current_coeff);
106 previous_var = entry.first;
107 current_coeff = entry.second;
110 if (current_coeff != 0) {
111 constraint->
vars.push_back(previous_var);
112 constraint->
coeffs.push_back(current_coeff);
128 for (
int i = 0; i < constraint.
vars.size(); ++i) {
129 const IntegerVariable
var = constraint.
vars[i];
130 const IntegerValue coeff = constraint.
coeffs[i];
131 activity += coeff.value() * values[
var];
138 for (
const IntegerValue coeff : constraint.
coeffs) {
141 return std::sqrt(sum);
145 IntegerValue result(0);
146 for (
const IntegerValue coeff : constraint.
coeffs) {
154 DCHECK(std::is_sorted(constraint1.
vars.begin(), constraint1.
vars.end()));
155 DCHECK(std::is_sorted(constraint2.
vars.begin(), constraint2.
vars.end()));
156 double scalar_product = 0.0;
159 while (index_1 < constraint1.
vars.size() &&
160 index_2 < constraint2.
vars.size()) {
161 if (constraint1.
vars[index_1] == constraint2.
vars[index_2]) {
166 }
else if (constraint1.
vars[index_1] > constraint2.
vars[index_2]) {
172 return scalar_product;
178 IntegerValue ComputeGcd(
const std::vector<IntegerValue>& values) {
179 if (values.empty())
return IntegerValue(1);
181 for (
const IntegerValue
value : values) {
185 if (gcd < 0)
return IntegerValue(1);
186 return IntegerValue(gcd);
192 if (constraint->
coeffs.empty())
return;
193 const IntegerValue gcd = ComputeGcd(constraint->
coeffs);
194 if (gcd == 1)
return;
202 for (IntegerValue& coeff : constraint->
coeffs) coeff /= gcd;
207 const int size = constraint->
vars.size();
208 for (
int i = 0; i < size; ++i) {
209 if (constraint->
coeffs[i] == 0)
continue;
210 constraint->
vars[new_size] = constraint->
vars[i];
214 constraint->
vars.resize(new_size);
215 constraint->
coeffs.resize(new_size);
219 const int size = constraint->
vars.size();
220 for (
int i = 0; i < size; ++i) {
221 const IntegerValue coeff = constraint->
coeffs[i];
223 constraint->
coeffs[i] = -coeff;
230 const int size = constraint->
vars.size();
231 for (
int i = 0; i < size; ++i) {
232 const IntegerVariable
var = constraint->
vars[i];
247 std::vector<std::pair<IntegerVariable, IntegerValue>> terms;
249 const int size =
ct->vars.size();
250 for (
int i = 0; i < size; ++i) {
252 terms.push_back({
ct->vars[i],
ct->coeffs[i]});
257 std::sort(terms.begin(), terms.end());
261 for (
const auto& term : terms) {
262 ct->vars.push_back(term.first);
263 ct->coeffs.push_back(term.second);
268 absl::flat_hash_set<IntegerVariable> seen_variables;
269 const int size =
ct.vars.size();
270 for (
int i = 0; i < size; ++i) {
272 if (!seen_variables.insert(
ct.vars[i]).second)
return false;
274 if (!seen_variables.insert(
NegationOf(
ct.vars[i])).second)
return false;
283 for (
int i = 0; i < expr.
vars.size(); ++i) {
288 canonical_expr.
vars.push_back(expr.
vars[i]);
292 return canonical_expr;
298 for (
int i = 0; i < expr.
vars.size(); ++i) {
308 for (
int i = 0; i < expr.
vars.size(); ++i) {
326 for (
int i = 0; i < expr.
vars.size(); ++i) {
328 result.
vars.push_back(expr.
vars[i]);
340 for (
int i = 0; i < expr.
vars.size(); ++i) {
347 return IntegerValue(0);
353 for (
int i = 0; i < expr.
vars.size(); ++i) {
358 return IntegerValue(0);
#define DCHECK_GE(val1, val2)
#define DCHECK(condition)
static int64_t GCD64(int64_t x, int64_t y)
const IntegerVariable GetLiteralView(Literal lit) const
IntegerValue UpperBound(IntegerVariable i) const
IntegerValue LowerBound(IntegerVariable i) const
ABSL_MUST_USE_RESULT bool AddLiteralTerm(Literal lit, IntegerValue coeff)
void AddConstant(IntegerValue value)
void AddTerm(IntegerVariable var, IntegerValue coeff)
IntegerValue FloorRatio(IntegerValue dividend, IntegerValue positive_divisor)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
IntegerValue LinExprLowerBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
IntType IntTypeAbs(IntType t)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
void RemoveZeroTerms(LinearConstraint *constraint)
LinearExpression PositiveVarExpr(const LinearExpression &expr)
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, LinearConstraint *constraint)
IntegerValue LinExprUpperBound(const LinearExpression &expr, const IntegerTrail &integer_trail)
bool VariableIsPositive(IntegerVariable i)
void DivideByGCD(LinearConstraint *constraint)
double ComputeActivity(const LinearConstraint &constraint, const absl::StrongVector< IntegerVariable, double > &values)
double ToDouble(IntegerValue value)
Collection of objects used to extend the Constraint Solver library.
std::vector< IntegerValue > coeffs
std::vector< IntegerVariable > vars
std::vector< IntegerValue > coeffs
std::vector< IntegerVariable > vars