14#ifndef ORTOOLS_SAT_LINEAR_CONSTRAINT_H_
15#define ORTOOLS_SAT_LINEAR_CONSTRAINT_H_
26#include "absl/base/attributes.h"
27#include "absl/log/check.h"
28#include "absl/strings/str_cat.h"
29#include "absl/types/span.h"
63 std::unique_ptr<IntegerVariable[]>
vars;
64 std::unique_ptr<IntegerValue[]>
coeffs;
79 IntegerVariable* tmp_vars =
new IntegerVariable[size];
80 IntegerValue* tmp_coeffs =
new IntegerValue[size];
81 const int to_copy = std::min(size,
num_terms);
83 memcpy(tmp_vars,
vars.get(),
sizeof(IntegerVariable) * to_copy);
84 memcpy(tmp_coeffs,
coeffs.get(),
sizeof(IntegerValue) * to_copy);
94 absl::StrAppend(&result,
lb.value(),
" <= ");
97 absl::StrAppend(&result,
i > 0 ?
" " :
"",
101 absl::StrAppend(&result,
" <= ",
ub.value());
107 if (this->num_terms != other.
num_terms)
return false;
108 if (this->num_terms == 0)
return true;
109 if (memcmp(this->vars.get(), other.
vars.get(),
110 sizeof(IntegerVariable) * this->num_terms)) {
113 if (memcmp(this->coeffs.get(), other.
coeffs.get(),
114 sizeof(IntegerValue) * this->num_terms)) {
128 std::memcpy(
vars.get(), other.
vars.get(), n *
sizeof(IntegerVariable));
129 std::memcpy(
coeffs.get(), other.
coeffs.get(), n *
sizeof(IntegerValue));
133 if (this->lb != other.
lb)
return false;
134 if (this->ub != other.
ub)
return false;
155 std::vector<IntegerVariable>
vars;
224 : encoder_(encoder), lb_(0), ub_(0) {}
229 : encoder_(encoder), lb_(lb), ub_(ub) {}
238 : encoder_(nullptr), lb_(lb), ub_(ub) {}
242 void AddTerm(IntegerVariable var, IntegerValue coeff);
253 absl::Span<const LiteralValueValue> product);
258 Literal lit, IntegerValue coeff = IntegerValue(1));
274 bool* is_quadratic =
nullptr);
278 offset_ = IntegerValue(0);
316 IntegerValue offset_ = IntegerValue(0);
320 std::vector<std::pair<IntegerVariable, IntegerValue>> terms_;
326 const LinearConstraint& constraint,
336 const LinearConstraint& constraint);
347 const LinearConstraint& constraint2);
362 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
364 output->
vars.clear();
369 std::sort(terms->begin(), terms->end());
371 IntegerValue current_coeff(0);
372 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
373 if (previous_var == entry.first) {
374 current_coeff += entry.second;
375 }
else if (previous_var ==
NegationOf(entry.first)) {
376 current_coeff -= entry.second;
378 if (current_coeff != 0) {
379 output->
vars.push_back(previous_var);
380 output->
coeffs.push_back(current_coeff);
382 previous_var = entry.first;
383 current_coeff = entry.second;
386 if (current_coeff != 0) {
387 output->
vars.push_back(previous_var);
388 output->
coeffs.push_back(current_coeff);
393 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
398 output->
resize(terms->size());
399 std::sort(terms->begin(), terms->end());
401 IntegerValue current_coeff(0);
402 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
403 if (previous_var == entry.first) {
404 current_coeff += entry.second;
405 }
else if (previous_var ==
NegationOf(entry.first)) {
406 current_coeff -= entry.second;
408 if (current_coeff != 0) {
409 output->
vars[new_size] = previous_var;
410 output->
coeffs[new_size] = current_coeff;
413 previous_var = entry.first;
414 current_coeff = entry.second;
417 if (current_coeff != 0) {
418 output->
vars[new_size] = previous_var;
419 output->
coeffs[new_size] = current_coeff;
426 std::vector<std::pair<IntegerVariable, IntegerValue>>* terms,
431 output->
resize(terms->size());
432 std::sort(terms->begin(), terms->end());
434 int64_t current_coeff = 0;
435 for (
const std::pair<IntegerVariable, IntegerValue>& entry : *terms) {
437 if (previous_var == entry.first) {
442 if (current_coeff != 0) {
443 output->
vars[new_size] = previous_var;
444 output->
coeffs[new_size] = current_coeff;
447 previous_var = entry.first;
448 current_coeff = entry.second.value();
451 if (current_coeff != 0) {
452 output->
vars[new_size] = previous_var;
453 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)
LinearConstraintBuilder(IntegerEncoder *encoder, IntegerValue lb, IntegerValue ub)
void AddTerm(IntegerVariable var, IntegerValue coeff)
void AddLinearExpression(const LinearExpression &expr)
void AddConstant(IntegerValue value)
LinearConstraintBuilder(const Model *model)
LinearConstraintBuilder(const Model *model, IntegerValue lb, IntegerValue ub)
LinearConstraint BuildConstraint(IntegerValue lb, IntegerValue ub)
LinearConstraintBuilder(IntegerValue lb, IntegerValue ub)
bool BuildIntoConstraintAndCheckOverflow(IntegerValue lb, IntegerValue ub, LinearConstraint *ct)
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)
std::string IntegerTermDebugString(IntegerVariable var, IntegerValue coeff)
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
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)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
bool MergePositiveVariableTermsAndCheckForOverflow(std::vector< std::pair< IntegerVariable, IntegerValue > > *terms, LinearConstraint *output)
IntegerValue ComputeInfinityNorm(const LinearConstraint &ct)
LinearExpression PositiveVarExpr(const LinearExpression &expr)
double ComputeL2Norm(const LinearConstraint &ct)
IntegerValue GetCoefficient(const IntegerVariable var, const LinearExpression &expr)
void MakeAllVariablesPositive(LinearConstraint *constraint)
IntegerValue GetCoefficientOfPositiveVar(const IntegerVariable var, const LinearExpression &expr)
bool ValidateLinearConstraintForOverflow(const LinearConstraint &constraint, const IntegerTrail &integer_trail)
bool NoDuplicateVariable(const LinearConstraint &ct)
bool VariableIsPositive(IntegerVariable i)
double ScalarProduct(const LinearConstraint &ct1, const LinearConstraint &ct2)
bool AddIntoOverflow(int64_t x, int64_t *y)
LinearConstraint()=default
std::unique_ptr< IntegerValue[]> coeffs
std::unique_ptr< IntegerVariable[]> vars
void CopyFrom(const LinearConstraint &other)
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)
double NormalizedViolation(const util_intops::StrongVector< IntegerVariable, double > &lp_values) const
IntegerValue LevelZeroMin(IntegerTrail *integer_trail) const
IntegerValue Min(const IntegerTrail &integer_trail) const
double LpValue(const util_intops::StrongVector< IntegerVariable, double > &lp_values) const
std::string DebugString() const
std::vector< IntegerVariable > vars
std::vector< IntegerValue > coeffs
IntegerValue Max(const IntegerTrail &integer_trail) const