Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_model_mapping.h
Go to the documentation of this file.
1// Copyright 2010-2025 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 ORTOOLS_SAT_CP_MODEL_MAPPING_H_
15#define ORTOOLS_SAT_CP_MODEL_MAPPING_H_
16
17#include <cstdint>
18#include <utility>
19#include <vector>
20
21#include "absl/container/flat_hash_set.h"
22#include "absl/log/check.h"
29#include "ortools/sat/model.h"
32
33namespace operations_research {
34namespace 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;
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 double ScaleObjective(double value) const {
60 return (value + offset) * scaling_factor;
61 }
62};
63
64// Holds the mapping between CpModel proto indices and the sat::model ones.
65//
66// This also holds some information used when loading a CpModel proto.
68 public:
69 // Returns true if the given CpModelProto variable reference refers to a
70 // Boolean variable. Such variable will always have an associated Literal(),
71 // but not always an associated Integer().
72 bool IsBoolean(int ref) const {
73 DCHECK_LT(PositiveRef(ref), booleans_.size());
74 return booleans_[PositiveRef(ref)] != kNoBooleanVariable;
75 }
76
77 bool IsInteger(int ref) const {
78 DCHECK_LT(PositiveRef(ref), integers_.size());
79 return integers_[PositiveRef(ref)] != kNoIntegerVariable;
80 }
81
82 sat::Literal Literal(int ref) const {
83 DCHECK(IsBoolean(ref));
84 return sat::Literal(booleans_[PositiveRef(ref)], RefIsPositive(ref));
85 }
86
87 IntegerVariable Integer(int ref) const {
88 DCHECK(IsInteger(ref));
89 const IntegerVariable var = integers_[PositiveRef(ref)];
90 return RefIsPositive(ref) ? var : NegationOf(var);
91 }
92
93 // TODO(user): We could "easily" create an intermediate variable for more
94 // complex linear expression. We could also identify duplicate expressions to
95 // not create two identical integer variable.
97 CHECK_LE(exp.vars().size(), 1);
98 if (exp.vars().empty()) {
99 return AffineExpression(IntegerValue(exp.offset()));
100 }
101 return AffineExpression(Integer(exp.vars(0)), IntegerValue(exp.coeffs(0)),
102 IntegerValue(exp.offset()));
103 }
104
105 IntervalVariable Interval(int i) const {
106 CHECK_GE(i, 0);
107 CHECK_LT(i, intervals_.size());
108 CHECK_NE(intervals_[i], kNoIntervalVariable);
109 return intervals_[i];
110 }
111
112 template <typename List>
113 std::vector<IntegerVariable> Integers(const List& list) const {
114 std::vector<IntegerVariable> result;
115 result.reserve(list.size());
116 for (const auto i : list) result.push_back(Integer(i));
117 return result;
118 }
119
120 template <typename ProtoIndices>
121 std::vector<sat::Literal> Literals(const ProtoIndices& indices) const {
122 std::vector<sat::Literal> result;
123 result.reserve(indices.size());
124 for (const int i : indices) result.push_back(CpModelMapping::Literal(i));
125 return result;
126 }
127
128 template <typename List>
129 std::vector<AffineExpression> Affines(const List& list) const {
130 std::vector<AffineExpression> result;
131 result.reserve(list.size());
132 for (const auto& i : list) result.push_back(Affine(i));
133 return result;
134 }
135
136 template <typename ProtoIndices>
137 std::vector<IntervalVariable> Intervals(const ProtoIndices& indices) const {
138 std::vector<IntervalVariable> result;
139 result.reserve(indices.size());
140 for (const int i : indices) result.push_back(Interval(i));
141 return result;
142 }
143
144 // Depending on the option, we will load constraints in stages. This is used
145 // to detect constraints that are already loaded. For instance the interval
146 // constraints and the linear constraint of size 1 (encodings) are usually
147 // loaded first.
149 return already_loaded_ct_.contains(ct);
150 }
151
152 // Returns true if the given constraint is a "half-encoding" constraint. That
153 // is, if it is of the form (b => size 1 linear) but there is no (<=) side in
154 // the model. Such constraint are detected while we extract integer encoding
155 // and are cached here so that we can deal properly with them during the
156 // linear relaxation.
158 return is_half_encoding_ct_.contains(ct);
159 }
160
161 // Note that both these functions returns positive reference or -1.
162 int GetProtoVariableFromBooleanVariable(BooleanVariable var) const {
163 if (var.value() >= reverse_boolean_map_.size()) return -1;
164 return reverse_boolean_map_[var];
165 }
166 int GetProtoVariableFromIntegerVariable(IntegerVariable var) const {
167 DCHECK(VariableIsPositive(var));
168 const PositiveOnlyIndex index = GetPositiveOnlyIndex(var);
169 if (index >= reverse_integer_map_.end_index()) return -1;
170 return reverse_integer_map_[index];
171 }
172
173 // This one should only be used when we have a mapping.
175 const int proto_var = GetProtoVariableFromBooleanVariable(lit.Variable());
176 DCHECK_NE(proto_var, -1);
177 return lit.IsPositive() ? proto_var : NegatedRef(proto_var);
178 }
179
180 const std::vector<IntegerVariable>& GetVariableMapping() const {
181 return integers_;
182 }
183
185 const LinearExpressionProto& expr_proto) const {
186 LinearExpression expr;
187 expr.vars = Integers(expr_proto.vars());
188 for (int j = 0; j < expr_proto.coeffs_size(); ++j) {
189 expr.coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
190 }
191 expr.offset = IntegerValue(expr_proto.offset());
192 return CanonicalizeExpr(expr);
193 }
194
195 // Returns the min/max activity of the linear constraint under the current
196 // integer_trail bounds.
197 std::pair<int64_t, int64_t> ComputeMinMaxActivity(
198 const LinearConstraintProto& proto, IntegerTrail* integer_trail) {
199 int64_t sum_min = 0;
200 int64_t sum_max = 0;
201
202 for (int i = 0; i < proto.vars_size(); ++i) {
203 const int64_t coeff = proto.coeffs(i);
204 const IntegerVariable var = this->Integer(proto.vars(i));
205 const int64_t lb = integer_trail->LowerBound(var).value();
206 const int64_t ub = integer_trail->UpperBound(var).value();
207 if (coeff >= 0) {
208 sum_min += coeff * lb;
209 sum_max += coeff * ub;
210 } else {
211 sum_min += coeff * ub;
212 sum_max += coeff * lb;
213 }
214 }
215 return {sum_min, sum_max};
216 }
217
218 // For logging only, these are not super efficient.
220 int result = 0;
221 for (const IntegerVariable var : integers_) {
222 if (var != kNoIntegerVariable) result++;
223 }
224 return result;
225 }
227 int result = 0;
228 for (const BooleanVariable var : booleans_) {
229 if (var != kNoBooleanVariable) result++;
230 }
231 return result;
232 }
233
234 // Returns the number of variables in the loaded proto.
235 int NumProtoVariables() const { return integers_.size(); }
236
237 private:
238 friend void LoadVariables(const CpModelProto& model_proto,
239 bool view_all_booleans_as_integers, Model* m);
240 friend void ExtractEncoding(const CpModelProto& model_proto, Model* m);
241
242 // Note that only the variables used by at least one constraint will be
243 // created, the other will have a kNo[Integer,Interval,Boolean]VariableValue.
244 std::vector<IntegerVariable> integers_;
245 std::vector<IntervalVariable> intervals_;
246 std::vector<BooleanVariable> booleans_;
247
248 // Recover from a IntervalVariable/BooleanVariable its associated CpModelProto
249 // index. The value of -1 is used to indicate that there is no correspondence
250 // (i.e. this variable is only used internally).
253
254 // Set of constraints to ignore because they were already dealt with by
255 // ExtractEncoding().
256 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
257 absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
258};
259
260} // namespace sat
261} // namespace operations_research
262
263#endif // ORTOOLS_SAT_CP_MODEL_MAPPING_H_
friend void LoadVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
std::vector< IntegerVariable > Integers(const List &list) const
AffineExpression Affine(const LinearExpressionProto &exp) const
std::pair< int64_t, int64_t > ComputeMinMaxActivity(const LinearConstraintProto &proto, IntegerTrail *integer_trail)
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const
int GetProtoLiteralFromLiteral(sat::Literal lit) const
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
int GetProtoVariableFromIntegerVariable(IntegerVariable var) const
LinearExpression GetExprFromProto(const LinearExpressionProto &expr_proto) const
friend void ExtractEncoding(const CpModelProto &model_proto, Model *m)
std::vector< sat::Literal > Literals(const ProtoIndices &indices) const
std::vector< AffineExpression > Affines(const List &list) const
IntervalVariable Interval(int i) const
IntegerVariable Integer(int ref) const
const std::vector< IntegerVariable > & GetVariableMapping() const
IntegerValue LowerBound(IntegerVariable i) const
Definition integer.h:1537
IntegerValue UpperBound(IntegerVariable i) const
Definition integer.h:1541
BooleanVariable Variable() const
Definition sat_base.h:88
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
Definition integer.cc:52
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
PositiveOnlyIndex GetPositiveOnlyIndex(IntegerVariable var)
const BooleanVariable kNoBooleanVariable(-1)
bool VariableIsPositive(IntegerVariable i)
double ToDouble(IntegerValue value)
OR-Tools root namespace.
absl::flat_hash_set< IntegerVariable > objective_impacting_variables
double ScaleIntegerObjective(IntegerValue value) const