OR-Tools  8.0
cp_model_loader.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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 <functional>
18 #include <vector>
19 
20 #include "absl/container/flat_hash_set.h"
21 #include "ortools/base/int_type.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/base/map_util.h"
28 #include "ortools/sat/integer.h"
29 #include "ortools/sat/intervals.h"
30 #include "ortools/sat/model.h"
31 #include "ortools/sat/sat_base.h"
32 
33 namespace operations_research {
34 namespace sat {
35 
36 // For an optimization problem, this contains the internal integer objective
37 // to minimize and information on how to display it correctly in the logs.
39  double scaling_factor = 1.0;
40  double offset = 0.0;
41  IntegerVariable objective_var = kNoIntegerVariable;
42 
43  // The objective linear expression that should be equal to objective_var.
44  // If not all proto variable have an IntegerVariable view, then some vars
45  // will be set to kNoIntegerVariable. In practice, when this is used, we make
46  // sure there is a view though.
47  std::vector<IntegerVariable> vars;
48  std::vector<IntegerValue> coeffs;
49 
50  // List of variable that when set to their lower bound should help getting a
51  // better objective. This is used by some search heuristic to preferably
52  // assign any of the variable here to their lower bound first.
53  absl::flat_hash_set<IntegerVariable> objective_impacting_variables;
54 
55  double ScaleIntegerObjective(IntegerValue value) const {
56  return (ToDouble(value) + offset) * scaling_factor;
57  }
58 };
59 
60 // Holds the mapping between CpModel proto indices and the sat::model ones.
61 //
62 // This also holds some information used when loading a CpModel proto.
64  public:
65  // Extracts all the used variables in the CpModelProto and creates a
66  // sat::Model representation for them. More precisely
67  // - All Boolean variables will be mapped.
68  // - All Interval variables will be mapped.
69  // - All non-Boolean variable will have a corresponding IntegerVariable, and
70  // depending on the view_all_booleans_as_integers, some or all of the
71  // BooleanVariable will also have an IntegerVariable corresponding to its
72  // "integer view".
73  //
74  // Note(user): We could create IntegerVariable on the fly as they are needed,
75  // but that loose the original variable order which might be useful in
76  // heuristics later.
77  void CreateVariables(const CpModelProto& model_proto,
78  bool view_all_booleans_as_integers, Model* m);
79 
80  // Automatically detect optional variables.
81  void DetectOptionalVariables(const CpModelProto& model_proto, Model* m);
82 
83  // Extract the encodings (IntegerVariable <-> Booleans) present in the model.
84  // This effectively load some linear constraints of size 1 that will be marked
85  // as already loaded.
86  void ExtractEncoding(const CpModelProto& model_proto, Model* m);
87 
88  // Process all affine relations of the form a*X + b*Y == cte. For each
89  // literals associated to (X >= bound) or (X == value) associate it to its
90  // corresponding relation on Y. Also do the other side.
91  //
92  // TODO(user): In an ideal world, all affine relations like this should be
93  // removed in the presolve.
95  const CpModelProto& model_proto, Model* m);
96 
97  // Returns true if the given CpModelProto variable reference refers to a
98  // Boolean varaible. Such variable will always have an associated Literal(),
99  // but not always an associated Integer().
100  bool IsBoolean(int ref) const {
101  DCHECK_LT(PositiveRef(ref), booleans_.size());
102  return booleans_[PositiveRef(ref)] != kNoBooleanVariable;
103  }
104 
105  bool IsInteger(int ref) const {
106  DCHECK_LT(PositiveRef(ref), integers_.size());
107  return integers_[PositiveRef(ref)] != kNoIntegerVariable;
108  }
109 
110  sat::Literal Literal(int ref) const {
111  DCHECK(IsBoolean(ref));
112  return sat::Literal(booleans_[PositiveRef(ref)], RefIsPositive(ref));
113  }
114 
115  IntegerVariable Integer(int ref) const {
116  DCHECK(IsInteger(ref));
117  const IntegerVariable var = integers_[PositiveRef(ref)];
118  return RefIsPositive(ref) ? var : NegationOf(var);
119  }
120 
121  IntervalVariable Interval(int i) const {
122  CHECK_GE(i, 0);
123  CHECK_LT(i, intervals_.size());
124  CHECK_NE(intervals_[i], kNoIntervalVariable);
125  return intervals_[i];
126  }
127 
128  template <typename List>
129  std::vector<IntegerVariable> Integers(const List& list) const {
130  std::vector<IntegerVariable> result;
131  for (const auto i : list) result.push_back(Integer(i));
132  return result;
133  }
134 
135  template <typename ProtoIndices>
136  std::vector<sat::Literal> Literals(const ProtoIndices& indices) const {
137  std::vector<sat::Literal> result;
138  for (const int i : indices) result.push_back(CpModelMapping::Literal(i));
139  return result;
140  }
141 
142  template <typename ProtoIndices>
143  std::vector<IntervalVariable> Intervals(const ProtoIndices& indices) const {
144  std::vector<IntervalVariable> result;
145  for (const int i : indices) result.push_back(Interval(i));
146  return result;
147  }
148 
149  // Depending on the option, we will load constraints in stages. This is used
150  // to detect constraints that are already loaded. For instance the interval
151  // constraints and the linear constraint of size 1 (encodings) are usually
152  // loaded first.
153  bool ConstraintIsAlreadyLoaded(const ConstraintProto* ct) const {
154  return already_loaded_ct_.contains(ct);
155  }
156 
157  // Returns true if the given constraint is a "half-encoding" constraint. That
158  // is, if it is of the form (b => size 1 linear) but there is no (<=) side in
159  // the model. Such constraint are detected while we extract integer encoding
160  // and are cached here so that we can deal properly with them during the
161  // linear relaxation.
162  bool IsHalfEncodingConstraint(const ConstraintProto* ct) const {
163  return is_half_encoding_ct_.contains(ct);
164  }
165 
166  // Note that both these functions returns positive reference or -1.
167  int GetProtoVariableFromBooleanVariable(BooleanVariable var) const {
168  if (var.value() >= reverse_boolean_map_.size()) return -1;
169  return reverse_boolean_map_[var];
170  }
171  int GetProtoVariableFromIntegerVariable(IntegerVariable var) const {
172  if (var.value() >= reverse_integer_map_.size()) return -1;
173  return reverse_integer_map_[var];
174  }
175 
176  const std::vector<IntegerVariable>& GetVariableMapping() const {
177  return integers_;
178  }
179 
180  // For logging only, these are not super efficient.
181  int NumIntegerVariables() const {
182  int result = 0;
183  for (const IntegerVariable var : integers_) {
184  if (var != kNoIntegerVariable) result++;
185  }
186  return result;
187  }
188  int NumBooleanVariables() const {
189  int result = 0;
190  for (const BooleanVariable var : booleans_) {
191  if (var != kNoBooleanVariable) result++;
192  }
193  return result;
194  }
195 
196  // Returns a heuristic set of values that could be created for the given
197  // variable when the constraints will be loaded.
198  // Note that the pointer is not stable across calls.
199  // It returns nullptr if the set is empty.
200  const absl::flat_hash_set<int64>& PotentialEncodedValues(int var) {
201  const auto& it = variables_to_encoded_values_.find(var);
202  if (it != variables_to_encoded_values_.end()) {
203  return it->second;
204  }
205  return empty_set_;
206  }
207 
208  private:
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  gtl::ITIVector<BooleanVariable, int> reverse_boolean_map_;
219  gtl::ITIVector<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>>
227  variables_to_encoded_values_;
228  const absl::flat_hash_set<int64> empty_set_;
229 };
230 
231 // Inspects the model and use some heuristic to decide which variable, if any,
232 // should be fully encoded. Note that some constraints like the element or table
233 // constraints require some of their variables to be fully encoded.
234 //
235 // TODO(user): This function exists so that we fully encode first all the
236 // variable that needs to be fully encoded so that at loading time we can adapt
237 // the algorithm used. Howeve it needs to duplicate the logic that decide what
238 // needs to be fully encoded. Try to come up with a more robust design.
239 void MaybeFullyEncodeMoreVariables(const CpModelProto& model_proto, Model* m);
240 
241 // Calls one of the functions below.
242 // Returns false if we do not know how to load the given constraints.
243 bool LoadConstraint(const ConstraintProto& ct, Model* m);
244 
245 void LoadBoolOrConstraint(const ConstraintProto& ct, Model* m);
246 void LoadBoolAndConstraint(const ConstraintProto& ct, Model* m);
247 void LoadAtMostOneConstraint(const ConstraintProto& ct, Model* m);
248 void LoadBoolXorConstraint(const ConstraintProto& ct, Model* m);
249 void LoadLinearConstraint(const ConstraintProto& ct, Model* m);
250 void LoadAllDiffConstraint(const ConstraintProto& ct, Model* m);
251 void LoadIntProdConstraint(const ConstraintProto& ct, Model* m);
252 void LoadIntDivConstraint(const ConstraintProto& ct, Model* m);
253 void LoadIntMinConstraint(const ConstraintProto& ct, Model* m);
254 void LoadLinMaxConstraint(const ConstraintProto& ct, Model* m);
255 void LoadIntMaxConstraint(const ConstraintProto& ct, Model* m);
256 void LoadNoOverlapConstraint(const ConstraintProto& ct, Model* m);
257 void LoadNoOverlap2dConstraint(const ConstraintProto& ct, Model* m);
258 void LoadCumulativeConstraint(const ConstraintProto& ct, Model* m);
259 void LoadElementConstraintBounds(const ConstraintProto& ct, Model* m);
260 void LoadElementConstraintAC(const ConstraintProto& ct, Model* m);
261 void LoadElementConstraint(const ConstraintProto& ct, Model* m);
262 void LoadTableConstraint(const ConstraintProto& ct, Model* m);
263 void LoadAutomatonConstraint(const ConstraintProto& ct, Model* m);
264 void LoadCircuitConstraint(const ConstraintProto& ct, Model* m);
265 void LoadRoutesConstraint(const ConstraintProto& ct, Model* m);
266 void LoadCircuitCoveringConstraint(const ConstraintProto& ct, Model* m);
267 void LoadInverseConstraint(const ConstraintProto& ct, Model* m);
268 
269 LinearExpression GetExprFromProto(const LinearExpressionProto& expr_proto,
270  const CpModelMapping& mapping);
271 
272 } // namespace sat
273 } // namespace operations_research
274 
275 #endif // OR_TOOLS_SAT_CP_MODEL_LOADER_H_
var
IntVar * var
Definition: expr_array.cc:1858
operations_research::sat::CpModelMapping::GetProtoVariableFromBooleanVariable
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const
Definition: cp_model_loader.h:167
operations_research::sat::CpModelMapping::NumIntegerVariables
int NumIntegerVariables() const
Definition: cp_model_loader.h:181
integral_types.h
map_util.h
operations_research::sat::LoadIntProdConstraint
void LoadIntProdConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1229
cp_model.pb.h
operations_research::sat::kNoIntegerVariable
const IntegerVariable kNoIntegerVariable(-1)
operations_research::sat::LoadElementConstraint
void LoadElementConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1561
operations_research::sat::CpModelMapping::Literal
sat::Literal Literal(int ref) const
Definition: cp_model_loader.h:110
operations_research::sat::CpModelMapping::CreateVariables
void CreateVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
Definition: cp_model_loader.cc:113
operations_research::sat::LoadRoutesConstraint
void LoadRoutesConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1723
operations_research::sat::LoadElementConstraintBounds
void LoadElementConstraintBounds(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1387
operations_research::sat::LoadCircuitCoveringConstraint
void LoadCircuitCoveringConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1736
operations_research::sat::ObjectiveDefinition::objective_impacting_variables
absl::flat_hash_set< IntegerVariable > objective_impacting_variables
Definition: cp_model_loader.h:53
logging.h
operations_research::sat::CpModelMapping::IsInteger
bool IsInteger(int ref) const
Definition: cp_model_loader.h:105
model_proto
CpModelProto const * model_proto
Definition: cp_model_solver.cc:2023
value
int64 value
Definition: demon_profiler.cc:43
operations_research::sat::LinearExpression
Definition: linear_constraint.h:165
model.h
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::sat::NegationOf
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
Definition: integer.cc:42
operations_research::sat::CpModelMapping::ConstraintIsAlreadyLoaded
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
Definition: cp_model_loader.h:153
operations_research::sat::CpModelMapping::Literals
std::vector< sat::Literal > Literals(const ProtoIndices &indices) const
Definition: cp_model_loader.h:136
gtl::ITIVector::size
size_type size() const
Definition: int_type_indexed_vector.h:146
operations_research::sat::LoadIntDivConstraint
void LoadIntDivConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1238
operations_research::sat::LoadCircuitConstraint
void LoadCircuitConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1711
operations_research::sat::Model
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
operations_research::sat::CpModelMapping::Interval
IntervalVariable Interval(int i) const
Definition: cp_model_loader.h:121
sat_base.h
operations_research::sat::LoadBoolAndConstraint
void LoadBoolAndConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:955
operations_research::sat::ObjectiveDefinition
Definition: cp_model_loader.h:38
operations_research::sat::LoadLinMaxConstraint
void LoadLinMaxConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1274
operations_research::sat::LoadAtMostOneConstraint
void LoadAtMostOneConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:969
operations_research::sat::ObjectiveDefinition::scaling_factor
double scaling_factor
Definition: cp_model_loader.h:39
operations_research::sat::Literal
Definition: sat_base.h:64
operations_research::sat::PositiveRef
int PositiveRef(int ref)
Definition: cp_model_utils.h:33
operations_research::sat::LoadBoolOrConstraint
void LoadBoolOrConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:946
operations_research::sat::CpModelMapping::Integers
std::vector< IntegerVariable > Integers(const List &list) const
Definition: cp_model_loader.h:129
operations_research::sat::LoadConstraint
bool LoadConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1749
operations_research::sat::ObjectiveDefinition::ScaleIntegerObjective
double ScaleIntegerObjective(IntegerValue value) const
Definition: cp_model_loader.h:55
operations_research::sat::CpModelMapping::Intervals
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
Definition: cp_model_loader.h:143
operations_research::sat::ObjectiveDefinition::coeffs
std::vector< IntegerValue > coeffs
Definition: cp_model_loader.h:48
operations_research::sat::LoadAllDiffConstraint
void LoadAllDiffConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1202
operations_research::sat::CpModelMapping::GetProtoVariableFromIntegerVariable
int GetProtoVariableFromIntegerVariable(IntegerVariable var) const
Definition: cp_model_loader.h:171
int_type.h
intervals.h
operations_research::sat::CpModelMapping::DetectOptionalVariables
void DetectOptionalVariables(const CpModelProto &model_proto, Model *m)
Definition: cp_model_loader.cc:589
operations_research::sat::CpModelMapping::GetVariableMapping
const std::vector< IntegerVariable > & GetVariableMapping() const
Definition: cp_model_loader.h:176
int_type_indexed_vector.h
operations_research::sat::LoadLinearConstraint
void LoadLinearConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1055
ct
const Constraint * ct
Definition: demon_profiler.cc:42
operations_research::sat::CpModelMapping::IsHalfEncodingConstraint
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
Definition: cp_model_loader.h:162
operations_research::sat::kNoBooleanVariable
const BooleanVariable kNoBooleanVariable(-1)
operations_research::sat::CpModelMapping::PropagateEncodingFromEquivalenceRelations
void PropagateEncodingFromEquivalenceRelations(const CpModelProto &model_proto, Model *m)
Definition: cp_model_loader.cc:506
operations_research::sat::ObjectiveDefinition::objective_var
IntegerVariable objective_var
Definition: cp_model_loader.h:41
operations_research::sat::CpModelMapping::Integer
IntegerVariable Integer(int ref) const
Definition: cp_model_loader.h:115
operations_research::sat::LoadIntMinConstraint
void LoadIntMinConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1255
operations_research::sat::LoadNoOverlap2dConstraint
void LoadNoOverlap2dConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1301
operations_research::sat::LoadNoOverlapConstraint
void LoadNoOverlapConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1296
operations_research::sat::ToDouble
double ToDouble(IntegerValue value)
Definition: integer.h:69
operations_research::sat::LoadBoolXorConstraint
void LoadBoolXorConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:975
operations_research::sat::RefIsPositive
bool RefIsPositive(int ref)
Definition: cp_model_utils.h:34
operations_research::sat::CpModelMapping
Definition: cp_model_loader.h:63
operations_research::sat::CpModelMapping::NumBooleanVariables
int NumBooleanVariables() const
Definition: cp_model_loader.h:188
operations_research::sat::LoadElementConstraintAC
void LoadElementConstraintAC(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1439
operations_research::sat::MaybeFullyEncodeMoreVariables
void MaybeFullyEncodeMoreVariables(const CpModelProto &model_proto, Model *m)
Definition: cp_model_loader.cc:937
operations_research::sat::LoadTableConstraint
void LoadTableConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1638
operations_research::sat::CpModelMapping::ExtractEncoding
void ExtractEncoding(const CpModelProto &model_proto, Model *m)
Definition: cp_model_loader.cc:259
operations_research::sat::LoadCumulativeConstraint
void LoadCumulativeConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1313
operations_research::sat::CpModelMapping::PotentialEncodedValues
const absl::flat_hash_set< int64 > & PotentialEncodedValues(int var)
Definition: cp_model_loader.h:200
gtl::ITIVector< BooleanVariable, int >
operations_research::sat::GetExprFromProto
LinearExpression GetExprFromProto(const LinearExpressionProto &expr_proto, const CpModelMapping &mapping)
Definition: cp_model_loader.cc:1263
operations_research::sat::LoadAutomatonConstraint
void LoadAutomatonConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1659
operations_research::sat::LoadInverseConstraint
void LoadInverseConstraint(const ConstraintProto &ct, Model *m)
operations_research::sat::LoadIntMaxConstraint
void LoadIntMaxConstraint(const ConstraintProto &ct, Model *m)
Definition: cp_model_loader.cc:1288
integer.h
operations_research::sat::ObjectiveDefinition::vars
std::vector< IntegerVariable > vars
Definition: cp_model_loader.h:47
operations_research::sat::CpModelMapping::IsBoolean
bool IsBoolean(int ref) const
Definition: cp_model_loader.h:100
operations_research::sat::kNoIntervalVariable
const IntervalVariable kNoIntervalVariable(-1)
cp_model_utils.h
operations_research::sat::ObjectiveDefinition::offset
double offset
Definition: cp_model_loader.h:40