14#ifndef OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
15#define OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
21#include "absl/container/flat_hash_map.h"
22#include "absl/container/flat_hash_set.h"
23#include "absl/log/check.h"
24#include "absl/meta/type_traits.h"
28#include "ortools/sat/cp_model.pb.h"
51 std::vector<IntegerVariable>
vars;
101 CHECK_LE(exp.vars().size(), 1);
102 if (exp.vars().empty()) {
106 IntegerValue(exp.offset()));
111 CHECK_LT(
i, intervals_.size());
113 return intervals_[
i];
116 template <
typename List>
117 std::vector<IntegerVariable>
Integers(
const List& list)
const {
118 std::vector<IntegerVariable> result;
119 result.reserve(list.size());
120 for (
const auto i : list) result.push_back(
Integer(
i));
124 template <
typename ProtoIndices>
125 std::vector<sat::Literal>
Literals(
const ProtoIndices& indices)
const {
126 std::vector<sat::Literal> result;
127 result.reserve(indices.size());
132 template <
typename List>
133 std::vector<AffineExpression>
Affines(
const List& list)
const {
134 std::vector<AffineExpression> result;
135 result.reserve(list.size());
136 for (
const auto&
i : list) result.push_back(
Affine(
i));
140 template <
typename ProtoIndices>
141 std::vector<IntervalVariable>
Intervals(
const ProtoIndices& indices)
const {
142 std::vector<IntervalVariable> result;
143 result.reserve(indices.size());
144 for (
const int i : indices) result.push_back(
Interval(
i));
153 return already_loaded_ct_.contains(
ct);
162 return is_half_encoding_ct_.contains(
ct);
167 if (
var.value() >= reverse_boolean_map_.size())
return -1;
168 return reverse_boolean_map_[
var];
171 if (
var.value() >= reverse_integer_map_.size())
return -1;
172 return reverse_integer_map_[
var];
180 const LinearExpressionProto& expr_proto)
const {
183 for (
int j = 0; j < expr_proto.coeffs_size(); ++j) {
184 expr.
coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
186 expr.
offset = IntegerValue(expr_proto.offset());
193 for (
const IntegerVariable
var : integers_) {
200 for (
const BooleanVariable
var : booleans_) {
211 bool view_all_booleans_as_integers,
Model* m);
216 std::vector<IntegerVariable> integers_;
217 std::vector<IntervalVariable> intervals_;
218 std::vector<BooleanVariable> booleans_;
228 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
229 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
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
bool RefIsPositive(int ref)
std::vector< IntegerVariable > NegationOf(const std::vector< 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)
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