14#ifndef OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
15#define OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
19#include "absl/container/flat_hash_set.h"
20#include "absl/log/check.h"
23#include "ortools/sat/cp_model.pb.h"
46 std::vector<IntegerVariable>
vars;
88 const IntegerVariable var = integers_[
PositiveRef(ref)];
96 CHECK_LE(exp.vars().size(), 1);
97 if (exp.vars().empty()) {
101 IntegerValue(exp.offset()));
106 CHECK_LT(
i, intervals_.size());
108 return intervals_[
i];
111 template <
typename List>
112 std::vector<IntegerVariable>
Integers(
const List& list)
const {
113 std::vector<IntegerVariable> result;
114 result.reserve(list.size());
115 for (
const auto i : list) result.push_back(
Integer(
i));
119 template <
typename ProtoIndices>
120 std::vector<sat::Literal>
Literals(
const ProtoIndices& indices)
const {
121 std::vector<sat::Literal> result;
122 result.reserve(indices.size());
127 template <
typename List>
128 std::vector<AffineExpression>
Affines(
const List& list)
const {
129 std::vector<AffineExpression> result;
130 result.reserve(list.size());
131 for (
const auto&
i : list) result.push_back(
Affine(
i));
135 template <
typename ProtoIndices>
136 std::vector<IntervalVariable>
Intervals(
const ProtoIndices& indices)
const {
137 std::vector<IntervalVariable> result;
138 result.reserve(indices.size());
139 for (
const int i : indices) result.push_back(
Interval(
i));
148 return already_loaded_ct_.contains(ct);
157 return is_half_encoding_ct_.contains(ct);
162 if (var.value() >= reverse_boolean_map_.size())
return -1;
163 return reverse_boolean_map_[var];
166 if (var.value() >= reverse_integer_map_.size())
return -1;
167 return reverse_integer_map_[var];
173 DCHECK_NE(proto_var, -1);
182 const LinearExpressionProto& expr_proto)
const {
185 for (
int j = 0; j < expr_proto.coeffs_size(); ++j) {
186 expr.
coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
188 expr.
offset = IntegerValue(expr_proto.offset());
195 for (
const IntegerVariable var : integers_) {
202 for (
const BooleanVariable var : booleans_) {
213 bool view_all_booleans_as_integers,
Model* m);
218 std::vector<IntegerVariable> integers_;
219 std::vector<IntervalVariable> intervals_;
220 std::vector<BooleanVariable> booleans_;
230 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
231 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
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
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