14#ifndef OR_TOOLS_SAT_INTEGER_BASE_H_
15#define OR_TOOLS_SAT_INTEGER_BASE_H_
27#include "absl/log/check.h"
28#include "absl/strings/str_cat.h"
29#include "absl/types/span.h"
57 std::numeric_limits<IntegerValue::ValueType>::max() - 1);
61 const double kInfinity = std::numeric_limits<double>::infinity();
64 return static_cast<double>(value.value());
67template <
class IntType>
69 return IntType(std::abs(t.value()));
73 IntegerValue positive_divisor) {
74 DCHECK_GT(positive_divisor, 0);
75 const IntegerValue result = dividend / positive_divisor;
76 const IntegerValue adjust =
77 static_cast<IntegerValue
>(result * positive_divisor < dividend);
78 return result + adjust;
82 IntegerValue positive_divisor) {
83 DCHECK_GT(positive_divisor, 0);
84 const IntegerValue result = dividend / positive_divisor;
85 const IntegerValue adjust =
86 static_cast<IntegerValue
>(result * positive_divisor > dividend);
87 return result - adjust;
92inline IntegerValue
CapProdI(IntegerValue a, IntegerValue
b) {
93 return IntegerValue(
CapProd(a.value(),
b.value()));
96inline IntegerValue
CapSubI(IntegerValue a, IntegerValue
b) {
97 return IntegerValue(
CapSub(a.value(),
b.value()));
100inline IntegerValue
CapAddI(IntegerValue a, IntegerValue
b) {
101 return IntegerValue(
CapAdd(a.value(),
b.value()));
119 IntegerValue positive_divisor) {
120 DCHECK_GT(positive_divisor, 0);
121 const IntegerValue m = dividend % positive_divisor;
122 return m < 0 ? m + positive_divisor : m;
125inline bool AddTo(IntegerValue a, IntegerValue* result) {
127 const IntegerValue add =
CapAddI(a, *result);
134inline bool AddProductTo(IntegerValue a, IntegerValue
b, IntegerValue* result) {
135 const IntegerValue prod =
CapProdI(a,
b);
137 const IntegerValue add =
CapAddI(prod, *result);
156 return IntegerVariable(
i.value() ^ 1);
160 return (
i.value() & 1) == 0;
164 return IntegerVariable(
i.value() & (~1));
170 return PositiveOnlyIndex(var.value() / 2);
174 IntegerValue coeff) {
176 return absl::StrCat(coeff.value(),
"*X", var.value() / 2);
180std::vector<IntegerVariable>
NegationOf(absl::Span<const IntegerVariable> vars);
225 ? absl::StrCat(
"I",
var.value() / 2,
">=",
bound.value())
226 : absl::StrCat(
"I",
var.value() / 2,
"<=", -
bound.value());
231 IntegerValue
bound = IntegerValue(0);
240 absl::Span<const IntegerLiteral> literals) {
249 os << literal.DebugString();
295 IntegerValue
ValueAt(IntegerValue var_value)
const {
311 return absl::StrCat(
"(",
coeff.value(),
" * X",
var.value(),
")");
313 return absl::StrCat(
"(",
coeff.value(),
" * X",
var.value(),
" + ",
323 IntegerValue
coeff = IntegerValue(0);
330 h = H::combine(std::move(h), e.
var);
331 h = H::combine(std::move(h), e.
coeff);
333 h = H::combine(std::move(h), e.
constant);
374 return (a.
value <
b.value) ||
385 IntegerValue
value = IntegerValue(0);
399 IntegerValue bound) {
405 IntegerValue bound) {
428 IntegerValue bound)
const {
bool AddSquareTo(IntegerValue a, IntegerValue *result)
Computes result += a * a, and return false iff there is an overflow.
constexpr double kInfinity
Infinity for type Fractional.
IntegerValue FloorRatio(IntegerValue dividend, IntegerValue positive_divisor)
bool AddProductTo(IntegerValue a, IntegerValue b, IntegerValue *result)
Computes result += a * b, and return false iff there is an overflow.
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
bool AddTo(IntegerValue a, IntegerValue *result)
IntType IntTypeAbs(IntType t)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
bool ProdOverflow(IntegerValue t, IntegerValue value)
const LiteralIndex kNoLiteralIndex(-1)
std::string IntegerTermDebugString(IntegerVariable var, IntegerValue coeff)
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
Returns the vector of the negated variables.
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
IntegerVariable PositiveVariable(IntegerVariable i)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
IntegerValue PositiveRemainder(IntegerValue dividend, IntegerValue positive_divisor)
IntegerValue CapAddI(IntegerValue a, IntegerValue b)
H AbslHashValue(H h, const IntVar &i)
– ABSL HASHING SUPPORT --------------------------------------------------—
PositiveOnlyIndex GetPositiveOnlyIndex(IntegerVariable var)
bool VariableIsPositive(IntegerVariable i)
IntegerValue CapSubI(IntegerValue a, IntegerValue b)
bool AtMinOrMaxInt64I(IntegerValue t)
IntegerValue CapProdI(IntegerValue a, IntegerValue b)
Overflows and saturated arithmetic.
double ToDouble(IntegerValue value)
In SWIG mode, we don't want anything besides these top-level includes.
bool AtMinOrMaxInt64(int64_t x)
Checks if x is equal to the min or the max value of an int64_t.
int64_t CapAdd(int64_t x, int64_t y)
int64_t CapSub(int64_t x, int64_t y)
int64_t CapProd(int64_t x, int64_t y)
#define DEFINE_STRONG_INT64_TYPE(integer_type_name)
#define DEFINE_STRONG_INDEX_TYPE(index_type_name)
IntegerLiteral GreaterOrEqual(IntegerValue bound) const
var * coeff + constant >= bound.
bool operator==(AffineExpression o) const
AffineExpression MultipliedBy(IntegerValue multiplier) const
AffineExpression Negated() const
AffineExpression(IntegerVariable v, IntegerValue c, IntegerValue cst)
AffineExpression()=default
Helper to construct an AffineExpression.
IntegerValue ValueAt(IntegerValue var_value) const
Returns the value of this affine expression given its variable value.
AffineExpression(IntegerVariable v, IntegerValue c)
double LpValue(const util_intops::StrongVector< IntegerVariable, double > &lp_values) const
Returns the affine expression value under a given LP solution.
AffineExpression(IntegerValue cst)
AffineExpression(IntegerVariable v)
std::string DebugString() const
IntegerLiteral LowerOrEqual(IntegerValue bound) const
var * coeff + constant <= bound.
std::vector< int64_t > proto_values
util_intops::StrongVector< IntegerVariable, bool > ivar_has_value
util_intops::StrongVector< IntegerVariable, IntegerValue > ivar_values
bool operator==(IntegerLiteral o) const
bool IsAlwaysFalse() const
static IntegerLiteral GreaterOrEqual(IntegerVariable i, IntegerValue bound)
static IntegerLiteral TrueLiteral()
IntegerLiteral Negated() const
The negation of x >= bound is x <= bound - 1.
std::string DebugString() const
static IntegerLiteral LowerOrEqual(IntegerVariable i, IntegerValue bound)
bool IsAlwaysTrue() const
static IntegerLiteral FalseLiteral()
IntegerLiteral(IntegerVariable v, IntegerValue b)
bool operator!=(IntegerLiteral o) const
IntegerLiteral()
Clients should prefer the static construction methods above.
bool operator()(const ValueLiteralPair &a, const ValueLiteralPair &b) const
bool operator()(const ValueLiteralPair &a, const ValueLiteralPair &b) const
std::string DebugString() const
bool operator==(const ValueLiteralPair &o) const