OR-Tools  9.0
cp_model_loader.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_LOADER_H_
15 #define OR_TOOLS_SAT_CP_MODEL_LOADER_H_
16 
17 #include <cstdint>
18 #include <functional>
19 #include <vector>
20 
21 #include "absl/container/flat_hash_set.h"
22 #include "ortools/base/int_type.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/base/map_util.h"
29 #include "ortools/sat/integer.h"
30 #include "ortools/sat/intervals.h"
31 #include "ortools/sat/model.h"
32 #include "ortools/sat/sat_base.h"
33 
34 namespace operations_research {
35 namespace sat {
36 
37 // For an optimization problem, this contains the internal integer objective
38 // to minimize and information on how to display it correctly in the logs.
40  double scaling_factor = 1.0;
41  double offset = 0.0;
42  IntegerVariable objective_var = kNoIntegerVariable;
43 
44  // The objective linear expression that should be equal to objective_var.
45  // If not all proto variable have an IntegerVariable view, then some vars
46  // will be set to kNoIntegerVariable. In practice, when this is used, we make
47  // sure there is a view though.
48  std::vector<IntegerVariable> vars;
49  std::vector<IntegerValue> coeffs;
50 
51  // List of variable that when set to their lower bound should help getting a
52  // better objective. This is used by some search heuristic to preferably
53  // assign any of the variable here to their lower bound first.
54  absl::flat_hash_set<IntegerVariable> objective_impacting_variables;
55 
56  double ScaleIntegerObjective(IntegerValue value) const {
57  return (ToDouble(value) + offset) * scaling_factor;
58  }
59 };
60 
61 // Holds the mapping between CpModel proto indices and the sat::model ones.
62 //
63 // This also holds some information used when loading a CpModel proto.
65  public:
66  // Extracts all the used variables in the CpModelProto and creates a
67  // sat::Model representation for them. More precisely
68  // - All Boolean variables will be mapped.
69  // - All Interval variables will be mapped.
70  // - All non-Boolean variable will have a corresponding IntegerVariable, and
71  // depending on the view_all_booleans_as_integers, some or all of the
72  // BooleanVariable will also have an IntegerVariable corresponding to its
73  // "integer view".
74  //
75  // Note(user): We could create IntegerVariable on the fly as they are needed,
76  // but that loose the original variable order which might be useful in
77  // heuristics later.
78  void CreateVariables(const CpModelProto& model_proto,
79  bool view_all_booleans_as_integers, Model* m);
80 
81  // Automatically detect optional variables.
82  void DetectOptionalVariables(const CpModelProto& model_proto, Model* m);
83 
84  // Experimental. Loads the symmetry form the proto symmetry field, as long as
85  // they only involve Booleans.
86  //
87  // TODO(user): We currently only have the code for Booleans, it is why we
88  // currently ignore symmetries involving integer variables.
89  void LoadBooleanSymmetries(const CpModelProto& model_proto, Model* m);
90 
91  // Extract the encodings (IntegerVariable <-> Booleans) present in the model.
92  // This effectively load some linear constraints of size 1 that will be marked
93  // as already loaded.
94  void ExtractEncoding(const CpModelProto& model_proto, Model* m);
95 
96  // Process all affine relations of the form a*X + b*Y == cte. For each
97  // literals associated to (X >= bound) or (X == value) associate it to its
98  // corresponding relation on Y. Also do the other side.
99  //
100  // TODO(user): In an ideal world, all affine relations like this should be
101  // removed in the presolve.
103  const CpModelProto& model_proto, Model* m);
104 
105  // Returns true if the given CpModelProto variable reference refers to a
106  // Boolean variable. Such variable will always have an associated Literal(),
107  // but not always an associated Integer().
108  bool IsBoolean(int ref) const {
109  DCHECK_LT(PositiveRef(ref), booleans_.size());
110  return booleans_[PositiveRef(ref)] != kNoBooleanVariable;
111  }
112 
113  bool IsInteger(int ref) const {
114  DCHECK_LT(PositiveRef(ref), integers_.size());
115  return integers_[PositiveRef(ref)] != kNoIntegerVariable;
116  }
117 
118  sat::Literal Literal(int ref) const {
119  DCHECK(IsBoolean(ref));
120  return sat::Literal(booleans_[PositiveRef(ref)], RefIsPositive(ref));
121  }
122 
123  IntegerVariable Integer(int ref) const {
124  DCHECK(IsInteger(ref));
125  const IntegerVariable var = integers_[PositiveRef(ref)];
126  return RefIsPositive(ref) ? var : NegationOf(var);
127  }
128 
129  // TODO(user): We could "easily" create an intermediate variable for more
130  // complex linear expression. We could also identify duplicate expressions to
131  // not create two identical integer variable.
132  AffineExpression LoadAffineView(const LinearExpressionProto& exp) const {
133  CHECK_LE(exp.vars().size(), 1);
134  if (exp.vars().empty()) {
135  return AffineExpression(IntegerValue(exp.offset()));
136  }
137  return AffineExpression(Integer(exp.vars(0)), IntegerValue(exp.coeffs(0)),
138  IntegerValue(exp.offset()));
139  }
140 
141  IntervalVariable Interval(int i) const {
142  CHECK_GE(i, 0);
143  CHECK_LT(i, intervals_.size());
144  CHECK_NE(intervals_[i], kNoIntervalVariable);
145  return intervals_[i];
146  }
147 
148  template <typename List>
149  std::vector<IntegerVariable> Integers(const List& list) const {
150  std::vector<IntegerVariable> result;
151  for (const auto i : list) result.push_back(Integer(i));
152  return result;
153  }
154 
155  template <typename ProtoIndices>
156  std::vector<sat::Literal> Literals(const ProtoIndices& indices) const {
157  std::vector<sat::Literal> result;
158  for (const int i : indices) result.push_back(CpModelMapping::Literal(i));
159  return result;
160  }
161 
162  template <typename ProtoIndices>
163  std::vector<IntervalVariable> Intervals(const ProtoIndices& indices) const {
164  std::vector<IntervalVariable> result;
165  for (const int i : indices) result.push_back(Interval(i));
166  return result;
167  }
168 
169  // Depending on the option, we will load constraints in stages. This is used
170  // to detect constraints that are already loaded. For instance the interval
171  // constraints and the linear constraint of size 1 (encodings) are usually
172  // loaded first.
173  bool ConstraintIsAlreadyLoaded(const ConstraintProto* ct) const {
174  return already_loaded_ct_.contains(ct);
175  }
176 
177  // Returns true if the given constraint is a "half-encoding" constraint. That
178  // is, if it is of the form (b => size 1 linear) but there is no (<=) side in
179  // the model. Such constraint are detected while we extract integer encoding
180  // and are cached here so that we can deal properly with them during the
181  // linear relaxation.
182  bool IsHalfEncodingConstraint(const ConstraintProto* ct) const {
183  return is_half_encoding_ct_.contains(ct);
184  }
185 
186  // Note that both these functions returns positive reference or -1.
187  int GetProtoVariableFromBooleanVariable(BooleanVariable var) const {
188  if (var.value() >= reverse_boolean_map_.size()) return -1;
189  return reverse_boolean_map_[var];
190  }
191  int GetProtoVariableFromIntegerVariable(IntegerVariable var) const {
192  if (var.value() >= reverse_integer_map_.size()) return -1;
193  return reverse_integer_map_[var];
194  }
195 
196  const std::vector<IntegerVariable>& GetVariableMapping() const {
197  return integers_;
198  }
199 
200  // For logging only, these are not super efficient.
201  int NumIntegerVariables() const {
202  int result = 0;
203  for (const IntegerVariable var : integers_) {
204  if (var != kNoIntegerVariable) result++;
205  }
206  return result;
207  }
208  int NumBooleanVariables() const {
209  int result = 0;
210  for (const BooleanVariable var : booleans_) {
211  if (var != kNoBooleanVariable) result++;
212  }
213  return result;
214  }
215 
216  // Returns a heuristic set of values that could be created for the given
217  // variable when the constraints will be loaded.
218  // Note that the pointer is not stable across calls.
219  // It returns nullptr if the set is empty.
220  const absl::flat_hash_set<int64_t>& PotentialEncodedValues(int var) {
221  const auto& it = variables_to_encoded_values_.find(var);
222  if (it != variables_to_encoded_values_.end()) {
223  return it->second;
224  }
225  return empty_set_;
226  }
227 
228  // Returns the number of variables in the loaded proto.
229  int NumProtoVariables() const { return integers_.size(); }
230 
231  private:
232  // Note that only the variables used by at least one constraint will be
233  // created, the other will have a kNo[Integer,Interval,Boolean]VariableValue.
234  std::vector<IntegerVariable> integers_;
235  std::vector<IntervalVariable> intervals_;
236  std::vector<BooleanVariable> booleans_;
237 
238  // Recover from a IntervalVariable/BooleanVariable its associated CpModelProto
239  // index. The value of -1 is used to indicate that there is no correspondence
240  // (i.e. this variable is only used internally).
241  absl::StrongVector<BooleanVariable, int> reverse_boolean_map_;
242  absl::StrongVector<IntegerVariable, int> reverse_integer_map_;
243 
244  // Set of constraints to ignore because they were already dealt with by
245  // ExtractEncoding().
246  absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
247  absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
248 
249  absl::flat_hash_map<int, absl::flat_hash_set<int64_t>>
250  variables_to_encoded_values_;
251  const absl::flat_hash_set<int64_t> empty_set_;
252 };
253 
254 // Inspects the model and use some heuristic to decide which variable, if any,
255 // should be fully encoded. Note that some constraints like the element or table
256 // constraints require some of their variables to be fully encoded.
257 //
258 // TODO(user): This function exists so that we fully encode first all the
259 // variable that needs to be fully encoded so that at loading time we can adapt
260 // the algorithm used. Howeve it needs to duplicate the logic that decide what
261 // needs to be fully encoded. Try to come up with a more robust design.
262 void MaybeFullyEncodeMoreVariables(const CpModelProto& model_proto, Model* m);
263 
264 // Calls one of the functions below.
265 // Returns false if we do not know how to load the given constraints.
266 bool LoadConstraint(const ConstraintProto& ct, Model* m);
267 
268 void LoadBoolOrConstraint(const ConstraintProto& ct, Model* m);
269 void LoadBoolAndConstraint(const ConstraintProto& ct, Model* m);
270 void LoadAtMostOneConstraint(const ConstraintProto& ct, Model* m);
271 void LoadExactlyOneConstraint(const ConstraintProto& ct, Model* m);
272 void LoadBoolXorConstraint(const ConstraintProto& ct, Model* m);
273 void LoadLinearConstraint(const ConstraintProto& ct, Model* m);
274 void LoadAllDiffConstraint(const ConstraintProto& ct, Model* m);
275 void LoadIntProdConstraint(const ConstraintProto& ct, Model* m);
276 void LoadIntDivConstraint(const ConstraintProto& ct, Model* m);
277 void LoadIntMinConstraint(const ConstraintProto& ct, Model* m);
278 void LoadLinMaxConstraint(const ConstraintProto& ct, Model* m);
279 void LoadIntMaxConstraint(const ConstraintProto& ct, Model* m);
280 void LoadNoOverlapConstraint(const ConstraintProto& ct, Model* m);
281 void LoadNoOverlap2dConstraint(const ConstraintProto& ct, Model* m);
282 void LoadCumulativeConstraint(const ConstraintProto& ct, Model* m);
283 void LoadReservoirConstraint(const ConstraintProto& ct, Model* m);
284 void LoadElementConstraintBounds(const ConstraintProto& ct, Model* m);
285 void LoadElementConstraintAC(const ConstraintProto& ct, Model* m);
286 void LoadElementConstraint(const ConstraintProto& ct, Model* m);
287 void LoadTableConstraint(const ConstraintProto& ct, Model* m);
288 void LoadAutomatonConstraint(const ConstraintProto& ct, Model* m);
289 void LoadCircuitConstraint(const ConstraintProto& ct, Model* m);
290 void LoadRoutesConstraint(const ConstraintProto& ct, Model* m);
291 void LoadCircuitCoveringConstraint(const ConstraintProto& ct, Model* m);
292 void LoadInverseConstraint(const ConstraintProto& ct, Model* m);
293 
294 LinearExpression GetExprFromProto(const LinearExpressionProto& expr_proto,
295  const CpModelMapping& mapping);
296 
297 } // namespace sat
298 } // namespace operations_research
299 
300 #endif // OR_TOOLS_SAT_CP_MODEL_LOADER_H_
#define CHECK_LT(val1, val2)
Definition: base/logging.h:708
#define CHECK_GE(val1, val2)
Definition: base/logging.h:709
#define CHECK_NE(val1, val2)
Definition: base/logging.h:706
#define DCHECK_LT(val1, val2)
Definition: base/logging.h:896
#define DCHECK(condition)
Definition: base/logging.h:892
#define CHECK_LE(val1, val2)
Definition: base/logging.h:707
size_type size() const
IntervalVariable Interval(int i) const
std::vector< sat::Literal > Literals(const ProtoIndices &indices) const
int GetProtoVariableFromIntegerVariable(IntegerVariable var) const
AffineExpression LoadAffineView(const LinearExpressionProto &exp) const
std::vector< IntegerVariable > Integers(const List &list) const
const std::vector< IntegerVariable > & GetVariableMapping() const
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
void LoadBooleanSymmetries(const CpModelProto &model_proto, Model *m)
const absl::flat_hash_set< int64_t > & PotentialEncodedValues(int var)
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const
IntegerVariable Integer(int ref) const
sat::Literal Literal(int ref) const
void DetectOptionalVariables(const CpModelProto &model_proto, Model *m)
void ExtractEncoding(const CpModelProto &model_proto, Model *m)
void PropagateEncodingFromEquivalenceRelations(const CpModelProto &model_proto, Model *m)
void CreateVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
CpModelProto const * model_proto
const Constraint * ct
int64_t value
IntVar * var
Definition: expr_array.cc:1874
void LoadTableConstraint(const ConstraintProto &ct, Model *m)
void LoadCircuitCoveringConstraint(const ConstraintProto &ct, Model *m)
void LoadExactlyOneConstraint(const ConstraintProto &ct, Model *m)
void LoadIntProdConstraint(const ConstraintProto &ct, Model *m)
bool LoadConstraint(const ConstraintProto &ct, Model *m)
void LoadBoolOrConstraint(const ConstraintProto &ct, Model *m)
bool RefIsPositive(int ref)
void MaybeFullyEncodeMoreVariables(const CpModelProto &model_proto, Model *m)
void LoadCumulativeConstraint(const ConstraintProto &ct, Model *m)
void LoadRoutesConstraint(const ConstraintProto &ct, Model *m)
void LoadReservoirConstraint(const ConstraintProto &ct, Model *m)
void LoadBoolAndConstraint(const ConstraintProto &ct, Model *m)
void LoadLinMaxConstraint(const ConstraintProto &ct, Model *m)
void LoadBoolXorConstraint(const ConstraintProto &ct, Model *m)
LinearExpression GetExprFromProto(const LinearExpressionProto &expr_proto, const CpModelMapping &mapping)
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
void LoadIntDivConstraint(const ConstraintProto &ct, Model *m)
void LoadLinearConstraint(const ConstraintProto &ct, Model *m)
void LoadAtMostOneConstraint(const ConstraintProto &ct, Model *m)
void LoadCircuitConstraint(const ConstraintProto &ct, Model *m)
void LoadIntMaxConstraint(const ConstraintProto &ct, Model *m)
void LoadNoOverlapConstraint(const ConstraintProto &ct, Model *m)
void LoadAllDiffConstraint(const ConstraintProto &ct, Model *m)
void LoadElementConstraint(const ConstraintProto &ct, Model *m)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
Definition: integer.cc:29
void LoadAutomatonConstraint(const ConstraintProto &ct, Model *m)
void LoadNoOverlap2dConstraint(const ConstraintProto &ct, Model *m)
void LoadIntMinConstraint(const ConstraintProto &ct, Model *m)
void LoadInverseConstraint(const ConstraintProto &ct, Model *m)
const BooleanVariable kNoBooleanVariable(-1)
void LoadElementConstraintAC(const ConstraintProto &ct, Model *m)
double ToDouble(IntegerValue value)
Definition: integer.h:70
void LoadElementConstraintBounds(const ConstraintProto &ct, Model *m)
Collection of objects used to extend the Constraint Solver library.
double ScaleIntegerObjective(IntegerValue value) const
absl::flat_hash_set< IntegerVariable > objective_impacting_variables