OR-Tools  9.1
cp_model_mapping.h
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
15 #define OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
16 
17 #include <cstdint>
18 #include <functional>
19 #include <vector>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/container/flat_hash_set.h"
23 #include "ortools/base/int_type.h"
25 #include "ortools/base/logging.h"
29 #include "ortools/sat/integer.h"
30 #include "ortools/sat/intervals.h"
32 #include "ortools/sat/model.h"
33 #include "ortools/sat/sat_base.h"
34 
35 namespace operations_research {
36 namespace sat {
37 
38 // For an optimization problem, this contains the internal integer objective
39 // to minimize and information on how to display it correctly in the logs.
41  double scaling_factor = 1.0;
42  double offset = 0.0;
43  IntegerVariable objective_var = kNoIntegerVariable;
44 
45  // The objective linear expression that should be equal to objective_var.
46  // If not all proto variable have an IntegerVariable view, then some vars
47  // will be set to kNoIntegerVariable. In practice, when this is used, we make
48  // sure there is a view though.
49  std::vector<IntegerVariable> vars;
50  std::vector<IntegerValue> coeffs;
51 
52  // List of variable that when set to their lower bound should help getting a
53  // better objective. This is used by some search heuristic to preferably
54  // assign any of the variable here to their lower bound first.
55  absl::flat_hash_set<IntegerVariable> objective_impacting_variables;
56 
57  double ScaleIntegerObjective(IntegerValue value) const {
58  return (ToDouble(value) + offset) * scaling_factor;
59  }
60 };
61 
62 // Holds the mapping between CpModel proto indices and the sat::model ones.
63 //
64 // This also holds some information used when loading a CpModel proto.
66  public:
67  // Returns true if the given CpModelProto variable reference refers to a
68  // Boolean variable. Such variable will always have an associated Literal(),
69  // but not always an associated Integer().
70  bool IsBoolean(int ref) const {
71  DCHECK_LT(PositiveRef(ref), booleans_.size());
72  return booleans_[PositiveRef(ref)] != kNoBooleanVariable;
73  }
74 
75  bool IsInteger(int ref) const {
76  DCHECK_LT(PositiveRef(ref), integers_.size());
77  return integers_[PositiveRef(ref)] != kNoIntegerVariable;
78  }
79 
80  sat::Literal Literal(int ref) const {
81  DCHECK(IsBoolean(ref));
82  return sat::Literal(booleans_[PositiveRef(ref)], RefIsPositive(ref));
83  }
84 
85  IntegerVariable Integer(int ref) const {
86  DCHECK(IsInteger(ref));
87  const IntegerVariable var = integers_[PositiveRef(ref)];
88  return RefIsPositive(ref) ? var : NegationOf(var);
89  }
90 
91  // TODO(user): We could "easily" create an intermediate variable for more
92  // complex linear expression. We could also identify duplicate expressions to
93  // not create two identical integer variable.
95  CHECK_LE(exp.vars().size(), 1);
96  if (exp.vars().empty()) {
97  return AffineExpression(IntegerValue(exp.offset()));
98  }
99  return AffineExpression(Integer(exp.vars(0)), IntegerValue(exp.coeffs(0)),
100  IntegerValue(exp.offset()));
101  }
102 
103  IntervalVariable Interval(int i) const {
104  CHECK_GE(i, 0);
105  CHECK_LT(i, intervals_.size());
106  CHECK_NE(intervals_[i], kNoIntervalVariable);
107  return intervals_[i];
108  }
109 
110  template <typename List>
111  std::vector<IntegerVariable> Integers(const List& list) const {
112  std::vector<IntegerVariable> result;
113  for (const auto i : list) result.push_back(Integer(i));
114  return result;
115  }
116 
117  template <typename ProtoIndices>
118  std::vector<sat::Literal> Literals(const ProtoIndices& indices) const {
119  std::vector<sat::Literal> result;
120  for (const int i : indices) result.push_back(CpModelMapping::Literal(i));
121  return result;
122  }
123 
124  template <typename ProtoIndices>
125  std::vector<IntervalVariable> Intervals(const ProtoIndices& indices) const {
126  std::vector<IntervalVariable> result;
127  for (const int i : indices) result.push_back(Interval(i));
128  return result;
129  }
130 
131  // Depending on the option, we will load constraints in stages. This is used
132  // to detect constraints that are already loaded. For instance the interval
133  // constraints and the linear constraint of size 1 (encodings) are usually
134  // loaded first.
136  return already_loaded_ct_.contains(ct);
137  }
138 
139  // Returns true if the given constraint is a "half-encoding" constraint. That
140  // is, if it is of the form (b => size 1 linear) but there is no (<=) side in
141  // the model. Such constraint are detected while we extract integer encoding
142  // and are cached here so that we can deal properly with them during the
143  // linear relaxation.
145  return is_half_encoding_ct_.contains(ct);
146  }
147 
148  // Note that both these functions returns positive reference or -1.
149  int GetProtoVariableFromBooleanVariable(BooleanVariable var) const {
150  if (var.value() >= reverse_boolean_map_.size()) return -1;
151  return reverse_boolean_map_[var];
152  }
153  int GetProtoVariableFromIntegerVariable(IntegerVariable var) const {
154  if (var.value() >= reverse_integer_map_.size()) return -1;
155  return reverse_integer_map_[var];
156  }
157 
158  const std::vector<IntegerVariable>& GetVariableMapping() const {
159  return integers_;
160  }
161 
163  const LinearExpressionProto& expr_proto) const {
164  LinearExpression expr;
165  expr.vars = Integers(expr_proto.vars());
166  for (int j = 0; j < expr_proto.coeffs_size(); ++j) {
167  expr.coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
168  }
169  expr.offset = IntegerValue(expr_proto.offset());
170  return CanonicalizeExpr(expr);
171  }
172 
173  // For logging only, these are not super efficient.
174  int NumIntegerVariables() const {
175  int result = 0;
176  for (const IntegerVariable var : integers_) {
177  if (var != kNoIntegerVariable) result++;
178  }
179  return result;
180  }
181  int NumBooleanVariables() const {
182  int result = 0;
183  for (const BooleanVariable var : booleans_) {
184  if (var != kNoBooleanVariable) result++;
185  }
186  return result;
187  }
188 
189  // Returns a heuristic set of values that could be created for the given
190  // variable when the constraints will be loaded.
191  // Note that the pointer is not stable across calls.
192  // It returns nullptr if the set is empty.
193  const absl::flat_hash_set<int64_t>& PotentialEncodedValues(int var) {
194  const auto& it = variables_to_encoded_values_.find(var);
195  if (it != variables_to_encoded_values_.end()) {
196  return it->second;
197  }
198  return empty_set_;
199  }
200 
201  // Returns the number of variables in the loaded proto.
202  int NumProtoVariables() const { return integers_.size(); }
203 
204  private:
205  friend void LoadVariables(const CpModelProto& model_proto,
206  bool view_all_booleans_as_integers, Model* m);
207  friend void ExtractEncoding(const CpModelProto& model_proto, Model* m);
208 
209  // Note that only the variables used by at least one constraint will be
210  // created, the other will have a kNo[Integer,Interval,Boolean]VariableValue.
211  std::vector<IntegerVariable> integers_;
212  std::vector<IntervalVariable> intervals_;
213  std::vector<BooleanVariable> booleans_;
214 
215  // Recover from a IntervalVariable/BooleanVariable its associated CpModelProto
216  // index. The value of -1 is used to indicate that there is no correspondence
217  // (i.e. this variable is only used internally).
218  absl::StrongVector<BooleanVariable, int> reverse_boolean_map_;
219  absl::StrongVector<IntegerVariable, int> reverse_integer_map_;
220 
221  // Set of constraints to ignore because they were already dealt with by
222  // ExtractEncoding().
223  absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
224  absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
225 
226  absl::flat_hash_map<int, absl::flat_hash_set<int64_t>>
227  variables_to_encoded_values_;
228  const absl::flat_hash_set<int64_t> empty_set_;
229 };
230 
231 } // namespace sat
232 } // namespace operations_research
233 
234 #endif // OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
::PROTOBUF_NAMESPACE_ID::int64 offset() const
Definition: cp_model.pb.h:7038
std::vector< sat::Literal > Literals(const ProtoIndices &indices) const
AffineExpression LoadAffineView(const LinearExpressionProto &exp) const
#define CHECK_GE(val1, val2)
Definition: base/logging.h:702
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
absl::flat_hash_set< IntegerVariable > objective_impacting_variables
LinearExpression GetExprFromProto(const LinearExpressionProto &expr_proto) const
sat::Literal Literal(int ref) const
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
#define CHECK_LT(val1, val2)
Definition: base/logging.h:701
double ToDouble(IntegerValue value)
Definition: integer.h:70
double ScaleIntegerObjective(IntegerValue value) const
int GetProtoVariableFromIntegerVariable(IntegerVariable var) const
const BooleanVariable kNoBooleanVariable(-1)
::PROTOBUF_NAMESPACE_ID::int64 coeffs(int index) const
Definition: cp_model.pb.h:6997
#define CHECK_LE(val1, val2)
Definition: base/logging.h:700
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
IntervalVariable Interval(int i) const
size_type size() const
CpModelProto const * model_proto
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
Definition: integer.cc:29
#define DCHECK(condition)
Definition: base/logging.h:885
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const
friend void LoadVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
Collection of objects used to extend the Constraint Solver library.
const IntegerVariable kNoIntegerVariable(-1)
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
::PROTOBUF_NAMESPACE_ID::int32 vars(int index) const
Definition: cp_model.pb.h:6950
bool RefIsPositive(int ref)
IntVar * var
Definition: expr_array.cc:1874
const absl::flat_hash_set< int64_t > & PotentialEncodedValues(int var)
std::vector< IntegerVariable > Integers(const List &list) const
IntegerVariable Integer(int ref) const
int64_t value
friend void ExtractEncoding(const CpModelProto &model_proto, Model *m)
#define CHECK_NE(val1, val2)
Definition: base/logging.h:699
const Constraint * ct
#define DCHECK_LT(val1, val2)
Definition: base/logging.h:889
const std::vector< IntegerVariable > & GetVariableMapping() const
const IntervalVariable kNoIntervalVariable(-1)