14#ifndef OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
15#define OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
21#include "absl/container/flat_hash_set.h"
22#include "absl/log/check.h"
48 std::vector<IntegerVariable>
vars;
90 const IntegerVariable var = integers_[
PositiveRef(ref)];
98 CHECK_LE(exp.
vars().size(), 1);
99 if (exp.
vars().empty()) {
103 IntegerValue(exp.
offset()));
108 CHECK_LT(
i, intervals_.size());
110 return intervals_[
i];
113 template <
typename List>
114 std::vector<IntegerVariable>
Integers(
const List& list)
const {
115 std::vector<IntegerVariable> result;
116 result.reserve(list.size());
117 for (
const auto i : list) result.push_back(
Integer(
i));
121 template <
typename ProtoIndices>
122 std::vector<sat::Literal>
Literals(
const ProtoIndices& indices)
const {
123 std::vector<sat::Literal> result;
124 result.reserve(indices.size());
129 template <
typename List>
130 std::vector<AffineExpression>
Affines(
const List& list)
const {
131 std::vector<AffineExpression> result;
132 result.reserve(list.size());
133 for (
const auto&
i : list) result.push_back(
Affine(
i));
137 template <
typename ProtoIndices>
138 std::vector<IntervalVariable>
Intervals(
const ProtoIndices& indices)
const {
139 std::vector<IntervalVariable> result;
140 result.reserve(indices.size());
141 for (
const int i : indices) result.push_back(
Interval(
i));
150 return already_loaded_ct_.contains(ct);
159 return is_half_encoding_ct_.contains(ct);
164 if (var.value() >= reverse_boolean_map_.size())
return -1;
165 return reverse_boolean_map_[var];
168 if (var.value() >= reverse_integer_map_.size())
return -1;
169 return reverse_integer_map_[var];
175 DCHECK_NE(proto_var, -1);
187 for (
int j = 0; j < expr_proto.
coeffs_size(); ++j) {
188 expr.
coeffs.push_back(IntegerValue(expr_proto.
coeffs(j)));
202 const int64_t coeff = proto.
coeffs(
i);
203 const IntegerVariable var = this->
Integer(proto.
vars(
i));
204 const int64_t lb = integer_trail->
LowerBound(var).value();
205 const int64_t ub = integer_trail->
UpperBound(var).value();
207 sum_min += coeff * lb;
208 sum_max += coeff * ub;
210 sum_min += coeff * ub;
211 sum_max += coeff * lb;
214 return {sum_min, sum_max};
220 for (
const IntegerVariable var : integers_) {
227 for (
const BooleanVariable var : booleans_) {
238 bool view_all_booleans_as_integers,
Model* m);
243 std::vector<IntegerVariable> integers_;
244 std::vector<IntervalVariable> intervals_;
245 std::vector<BooleanVariable> booleans_;
255 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
256 absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
friend void LoadVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
int NumIntegerVariables() const
For logging only, these are not super efficient.
sat::Literal Literal(int ref) const
std::vector< IntegerVariable > Integers(const List &list) const
AffineExpression Affine(const LinearExpressionProto &exp) const
std::pair< int64_t, int64_t > ComputeMinMaxActivity(const LinearConstraintProto &proto, IntegerTrail *integer_trail)
bool IsBoolean(int ref) const
bool IsInteger(int ref) const
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const
int GetProtoLiteralFromLiteral(sat::Literal lit) const
This one should only be used when we have a mapping.
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
int NumProtoVariables() const
Returns the number of variables in the loaded proto.
int GetProtoVariableFromIntegerVariable(IntegerVariable var) const
LinearExpression GetExprFromProto(const LinearExpressionProto &expr_proto) const
friend void ExtractEncoding(const CpModelProto &model_proto, Model *m)
std::vector< sat::Literal > Literals(const ProtoIndices &indices) const
std::vector< AffineExpression > Affines(const List &list) const
IntervalVariable Interval(int i) const
int NumBooleanVariables() const
IntegerVariable Integer(int ref) const
const std::vector< IntegerVariable > & GetVariableMapping() const
IntegerValue LowerBound(IntegerVariable i) const
Returns the current lower/upper bound of the given integer variable.
IntegerValue UpperBound(IntegerVariable i) const
::int32_t vars(int index) const
int vars_size() const
repeated int32 vars = 1;
::int64_t coeffs(int index) const
::int64_t coeffs(int index) const
::int32_t vars(int index) const
int coeffs_size() const
repeated int64 coeffs = 2;
BooleanVariable Variable() const
bool RefIsPositive(int ref)
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
Returns the vector of the negated variables.
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
const BooleanVariable kNoBooleanVariable(-1)
int NegatedRef(int ref)
Small utility functions to deal with negative variable/literal references.
double ToDouble(IntegerValue value)
In SWIG mode, we don't want anything besides these top-level includes.
std::vector< IntegerVariable > vars
std::vector< IntegerValue > coeffs
IntegerVariable objective_var
absl::flat_hash_set< IntegerVariable > objective_impacting_variables
double ScaleObjective(double value) const
std::vector< IntegerVariable > vars
double ScaleIntegerObjective(IntegerValue value) const
std::vector< IntegerValue > coeffs