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/meta/type_traits.h"
27#include "ortools/sat/cp_model.pb.h"
50 std::vector<IntegerVariable>
vars;
97 if (exp.vars().empty()) {
101 IntegerValue(exp.offset()));
108 return intervals_[i];
111 template <
typename List>
112 std::vector<IntegerVariable>
Integers(
const List& list)
const {
113 std::vector<IntegerVariable> result;
114 for (
const auto i : list) result.push_back(
Integer(i));
118 template <
typename ProtoIndices>
119 std::vector<sat::Literal>
Literals(
const ProtoIndices& indices)
const {
120 std::vector<sat::Literal> result;
125 template <
typename List>
126 std::vector<AffineExpression>
Affines(
const List& list)
const {
127 std::vector<AffineExpression> result;
128 for (
const auto& i : list) result.push_back(
Affine(i));
132 template <
typename ProtoIndices>
133 std::vector<IntervalVariable>
Intervals(
const ProtoIndices& indices)
const {
134 std::vector<IntervalVariable> result;
135 for (
const int i : indices) result.push_back(
Interval(i));
144 return already_loaded_ct_.contains(
ct);
153 return is_half_encoding_ct_.contains(
ct);
158 if (
var.value() >= reverse_boolean_map_.
size())
return -1;
159 return reverse_boolean_map_[
var];
162 if (
var.value() >= reverse_integer_map_.
size())
return -1;
163 return reverse_integer_map_[
var];
171 const LinearExpressionProto& expr_proto)
const {
174 for (
int j = 0; j < expr_proto.coeffs_size(); ++j) {
175 expr.
coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
177 expr.
offset = IntegerValue(expr_proto.offset());
184 for (
const IntegerVariable
var : integers_) {
191 for (
const BooleanVariable
var : booleans_) {
202 const auto& it = variables_to_encoded_values_.find(
var);
203 if (it != variables_to_encoded_values_.end()) {
214 bool view_all_booleans_as_integers,
Model* m);
219 std::vector<IntegerVariable> integers_;
220 std::vector<IntervalVariable> intervals_;
221 std::vector<BooleanVariable> booleans_;
231 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
232 absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
234 absl::flat_hash_map<int, absl::flat_hash_set<int64_t>>
235 variables_to_encoded_values_;
236 const absl::flat_hash_set<int64_t> empty_set_;
#define CHECK_LT(val1, val2)
#define CHECK_GE(val1, val2)
#define CHECK_NE(val1, val2)
#define DCHECK_LT(val1, val2)
#define DCHECK(condition)
#define CHECK_LE(val1, val2)
IntervalVariable Interval(int i) const
int GetProtoVariableFromIntegerVariable(IntegerVariable var) const
friend void LoadVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
const absl::flat_hash_set< int64_t > & PotentialEncodedValues(int var)
bool IsBoolean(int ref) const
int NumBooleanVariables() const
bool IsInteger(int ref) const
std::vector< IntegerVariable > Integers(const List &list) const
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
int NumIntegerVariables() const
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
AffineExpression Affine(const LinearExpressionProto &exp) const
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const
IntegerVariable Integer(int ref) const
std::vector< AffineExpression > Affines(const List &list) const
sat::Literal Literal(int ref) const
LinearExpression GetExprFromProto(const LinearExpressionProto &expr_proto) const
const std::vector< IntegerVariable > & GetVariableMapping() const
friend void ExtractEncoding(const CpModelProto &model_proto, Model *m)
int NumProtoVariables() const
std::vector< sat::Literal > Literals(const ProtoIndices &indices) const
Class that owns everything related to a particular optimization model.
CpModelProto const * model_proto
bool RefIsPositive(int ref)
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
const BooleanVariable kNoBooleanVariable(-1)
double ToDouble(IntegerValue value)
Collection of objects used to extend the Constraint Solver library.
std::vector< IntegerValue > coeffs
std::vector< IntegerVariable > vars
std::vector< IntegerValue > coeffs
std::vector< IntegerVariable > vars
IntegerVariable objective_var
double ScaleIntegerObjective(IntegerValue value) const
absl::flat_hash_set< IntegerVariable > objective_impacting_variables