14#ifndef ORTOOLS_SAT_CP_MODEL_MAPPING_H_
15#define ORTOOLS_SAT_CP_MODEL_MAPPING_H_
21#include "absl/container/flat_hash_set.h"
22#include "absl/log/check.h"
47 std::vector<IntegerVariable>
vars;
89 const IntegerVariable var = integers_[
PositiveRef(ref)];
97 CHECK_LE(exp.
vars().size(), 1);
98 if (exp.
vars().empty()) {
102 IntegerValue(exp.
offset()));
107 CHECK_LT(
i, intervals_.size());
109 return intervals_[
i];
112 template <
typename List>
113 std::vector<IntegerVariable>
Integers(
const List& list)
const {
114 std::vector<IntegerVariable> result;
115 result.reserve(list.size());
116 for (
const auto i : list) result.push_back(
Integer(
i));
120 template <
typename ProtoIndices>
121 std::vector<sat::Literal>
Literals(
const ProtoIndices& indices)
const {
122 std::vector<sat::Literal> result;
123 result.reserve(indices.size());
128 template <
typename List>
129 std::vector<AffineExpression>
Affines(
const List& list)
const {
130 std::vector<AffineExpression> result;
131 result.reserve(list.size());
132 for (
const auto&
i : list) result.push_back(
Affine(
i));
136 template <
typename ProtoIndices>
137 std::vector<IntervalVariable>
Intervals(
const ProtoIndices& indices)
const {
138 std::vector<IntervalVariable> result;
139 result.reserve(indices.size());
140 for (
const int i : indices) result.push_back(
Interval(
i));
149 return already_loaded_ct_.contains(ct);
158 return is_half_encoding_ct_.contains(ct);
163 if (var.value() >= reverse_boolean_map_.size())
return -1;
164 return reverse_boolean_map_[var];
169 if (index >= reverse_integer_map_.end_index())
return -1;
170 return reverse_integer_map_[index];
176 DCHECK_NE(proto_var, -1);
188 for (
int j = 0; j < expr_proto.
coeffs_size(); ++j) {
189 expr.
coeffs.push_back(IntegerValue(expr_proto.
coeffs(j)));
203 const int64_t coeff = proto.
coeffs(
i);
204 const IntegerVariable var = this->
Integer(proto.
vars(
i));
205 const int64_t lb = integer_trail->
LowerBound(var).value();
206 const int64_t ub = integer_trail->
UpperBound(var).value();
208 sum_min += coeff * lb;
209 sum_max += coeff * ub;
211 sum_min += coeff * ub;
212 sum_max += coeff * lb;
215 return {sum_min, sum_max};
221 for (
const IntegerVariable var : integers_) {
228 for (
const BooleanVariable var : booleans_) {
239 bool view_all_booleans_as_integers,
Model* m);
244 std::vector<IntegerVariable> integers_;
245 std::vector<IntervalVariable> intervals_;
246 std::vector<BooleanVariable> booleans_;
256 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
257 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
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
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
int NumProtoVariables() const
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
IntegerValue UpperBound(IntegerVariable i) const
::int32_t vars(int index) const
::int64_t coeffs(int index) const
::int64_t coeffs(int index) const
::int32_t vars(int index) const
BooleanVariable Variable() const
bool RefIsPositive(int ref)
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
PositiveOnlyIndex GetPositiveOnlyIndex(IntegerVariable var)
const BooleanVariable kNoBooleanVariable(-1)
bool VariableIsPositive(IntegerVariable i)
double ToDouble(IntegerValue value)
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