OR-Tools  9.2
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"
29#include "ortools/sat/integer.h"
32#include "ortools/sat/model.h"
34
35namespace operations_research {
36namespace 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;
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 List>
125 std::vector<AffineExpression> Affines(const List& list) const {
126 std::vector<AffineExpression> result;
127 for (const auto& i : list) result.push_back(Affine(i));
128 return result;
129 }
130
131 template <typename ProtoIndices>
132 std::vector<IntervalVariable> Intervals(const ProtoIndices& indices) const {
133 std::vector<IntervalVariable> result;
134 for (const int i : indices) result.push_back(Interval(i));
135 return result;
136 }
137
138 // Depending on the option, we will load constraints in stages. This is used
139 // to detect constraints that are already loaded. For instance the interval
140 // constraints and the linear constraint of size 1 (encodings) are usually
141 // loaded first.
143 return already_loaded_ct_.contains(ct);
144 }
145
146 // Returns true if the given constraint is a "half-encoding" constraint. That
147 // is, if it is of the form (b => size 1 linear) but there is no (<=) side in
148 // the model. Such constraint are detected while we extract integer encoding
149 // and are cached here so that we can deal properly with them during the
150 // linear relaxation.
152 return is_half_encoding_ct_.contains(ct);
153 }
154
155 // Note that both these functions returns positive reference or -1.
156 int GetProtoVariableFromBooleanVariable(BooleanVariable var) const {
157 if (var.value() >= reverse_boolean_map_.size()) return -1;
158 return reverse_boolean_map_[var];
159 }
160 int GetProtoVariableFromIntegerVariable(IntegerVariable var) const {
161 if (var.value() >= reverse_integer_map_.size()) return -1;
162 return reverse_integer_map_[var];
163 }
164
165 const std::vector<IntegerVariable>& GetVariableMapping() const {
166 return integers_;
167 }
168
170 const LinearExpressionProto& expr_proto) const {
171 LinearExpression expr;
172 expr.vars = Integers(expr_proto.vars());
173 for (int j = 0; j < expr_proto.coeffs_size(); ++j) {
174 expr.coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
175 }
176 expr.offset = IntegerValue(expr_proto.offset());
177 return CanonicalizeExpr(expr);
178 }
179
180 // For logging only, these are not super efficient.
182 int result = 0;
183 for (const IntegerVariable var : integers_) {
184 if (var != kNoIntegerVariable) result++;
185 }
186 return result;
187 }
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_t>& 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 // Returns the number of variables in the loaded proto.
209 int NumProtoVariables() const { return integers_.size(); }
210
211 private:
212 friend void LoadVariables(const CpModelProto& model_proto,
213 bool view_all_booleans_as_integers, Model* m);
214 friend void ExtractEncoding(const CpModelProto& model_proto, Model* m);
215
216 // Note that only the variables used by at least one constraint will be
217 // created, the other will have a kNo[Integer,Interval,Boolean]VariableValue.
218 std::vector<IntegerVariable> integers_;
219 std::vector<IntervalVariable> intervals_;
220 std::vector<BooleanVariable> booleans_;
221
222 // Recover from a IntervalVariable/BooleanVariable its associated CpModelProto
223 // index. The value of -1 is used to indicate that there is no correspondence
224 // (i.e. this variable is only used internally).
225 absl::StrongVector<BooleanVariable, int> reverse_boolean_map_;
226 absl::StrongVector<IntegerVariable, int> reverse_integer_map_;
227
228 // Set of constraints to ignore because they were already dealt with by
229 // ExtractEncoding().
230 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
231 absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
232
233 absl::flat_hash_map<int, absl::flat_hash_set<int64_t>>
234 variables_to_encoded_values_;
235 const absl::flat_hash_set<int64_t> empty_set_;
236};
237
238} // namespace sat
239} // namespace operations_research
240
241#endif // OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
#define CHECK_LT(val1, val2)
Definition: base/logging.h:702
#define CHECK_GE(val1, val2)
Definition: base/logging.h:703
#define CHECK_NE(val1, val2)
Definition: base/logging.h:700
#define DCHECK_LT(val1, val2)
Definition: base/logging.h:890
#define DCHECK(condition)
Definition: base/logging.h:886
#define CHECK_LE(val1, val2)
Definition: base/logging.h:701
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)
std::vector< IntegerVariable > Integers(const List &list) const
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) 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)
std::vector< sat::Literal > Literals(const ProtoIndices &indices) const
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
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)
Definition: integer.cc:30
const BooleanVariable kNoBooleanVariable(-1)
double ToDouble(IntegerValue value)
Definition: integer.h:71
Collection of objects used to extend the Constraint Solver library.
double ScaleIntegerObjective(IntegerValue value) const
absl::flat_hash_set< IntegerVariable > objective_impacting_variables