Google OR-Tools v9.11
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-2024 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 "absl/log/check.h"
24#include "absl/meta/type_traits.h"
27#include "ortools/base/types.h"
28#include "ortools/sat/cp_model.pb.h"
30#include "ortools/sat/integer.h"
33#include "ortools/sat/model.h"
36
37namespace operations_research {
38namespace sat {
39
40// For an optimization problem, this contains the internal integer objective
41// to minimize and information on how to display it correctly in the logs.
43 double scaling_factor = 1.0;
44 double offset = 0.0;
46
47 // The objective linear expression that should be equal to objective_var.
48 // If not all proto variable have an IntegerVariable view, then some vars
49 // will be set to kNoIntegerVariable. In practice, when this is used, we make
50 // sure there is a view though.
51 std::vector<IntegerVariable> vars;
52 std::vector<IntegerValue> coeffs;
53
54 // List of variable that when set to their lower bound should help getting a
55 // better objective. This is used by some search heuristic to preferably
56 // assign any of the variable here to their lower bound first.
57 absl::flat_hash_set<IntegerVariable> objective_impacting_variables;
58
59 double ScaleIntegerObjective(IntegerValue value) const {
60 return (ToDouble(value) + offset) * scaling_factor;
61 }
62
63 double ScaleObjective(double value) const {
64 return (value + offset) * scaling_factor;
65 }
66};
67
68// Holds the mapping between CpModel proto indices and the sat::model ones.
69//
70// This also holds some information used when loading a CpModel proto.
72 public:
73 // Returns true if the given CpModelProto variable reference refers to a
74 // Boolean variable. Such variable will always have an associated Literal(),
75 // but not always an associated Integer().
76 bool IsBoolean(int ref) const {
77 DCHECK_LT(PositiveRef(ref), booleans_.size());
78 return booleans_[PositiveRef(ref)] != kNoBooleanVariable;
79 }
80
81 bool IsInteger(int ref) const {
82 DCHECK_LT(PositiveRef(ref), integers_.size());
83 return integers_[PositiveRef(ref)] != kNoIntegerVariable;
84 }
85
86 sat::Literal Literal(int ref) const {
87 DCHECK(IsBoolean(ref));
88 return sat::Literal(booleans_[PositiveRef(ref)], RefIsPositive(ref));
89 }
90
91 IntegerVariable Integer(int ref) const {
92 DCHECK(IsInteger(ref));
93 const IntegerVariable var = integers_[PositiveRef(ref)];
94 return RefIsPositive(ref) ? var : NegationOf(var);
95 }
96
97 // TODO(user): We could "easily" create an intermediate variable for more
98 // complex linear expression. We could also identify duplicate expressions to
99 // not create two identical integer variable.
100 AffineExpression Affine(const LinearExpressionProto& exp) const {
101 CHECK_LE(exp.vars().size(), 1);
102 if (exp.vars().empty()) {
103 return AffineExpression(IntegerValue(exp.offset()));
104 }
105 return AffineExpression(Integer(exp.vars(0)), IntegerValue(exp.coeffs(0)),
106 IntegerValue(exp.offset()));
107 }
108
109 IntervalVariable Interval(int i) const {
110 CHECK_GE(i, 0);
111 CHECK_LT(i, intervals_.size());
112 CHECK_NE(intervals_[i], kNoIntervalVariable);
113 return intervals_[i];
114 }
115
116 template <typename List>
117 std::vector<IntegerVariable> Integers(const List& list) const {
118 std::vector<IntegerVariable> result;
119 result.reserve(list.size());
120 for (const auto i : list) result.push_back(Integer(i));
121 return result;
122 }
123
124 template <typename ProtoIndices>
125 std::vector<sat::Literal> Literals(const ProtoIndices& indices) const {
126 std::vector<sat::Literal> result;
127 result.reserve(indices.size());
128 for (const int i : indices) result.push_back(CpModelMapping::Literal(i));
129 return result;
130 }
131
132 template <typename List>
133 std::vector<AffineExpression> Affines(const List& list) const {
134 std::vector<AffineExpression> result;
135 result.reserve(list.size());
136 for (const auto& i : list) result.push_back(Affine(i));
137 return result;
138 }
139
140 template <typename ProtoIndices>
141 std::vector<IntervalVariable> Intervals(const ProtoIndices& indices) const {
142 std::vector<IntervalVariable> result;
143 result.reserve(indices.size());
144 for (const int i : indices) result.push_back(Interval(i));
145 return result;
146 }
147
148 // Depending on the option, we will load constraints in stages. This is used
149 // to detect constraints that are already loaded. For instance the interval
150 // constraints and the linear constraint of size 1 (encodings) are usually
151 // loaded first.
152 bool ConstraintIsAlreadyLoaded(const ConstraintProto* ct) const {
153 return already_loaded_ct_.contains(ct);
154 }
155
156 // Returns true if the given constraint is a "half-encoding" constraint. That
157 // is, if it is of the form (b => size 1 linear) but there is no (<=) side in
158 // the model. Such constraint are detected while we extract integer encoding
159 // and are cached here so that we can deal properly with them during the
160 // linear relaxation.
161 bool IsHalfEncodingConstraint(const ConstraintProto* ct) const {
162 return is_half_encoding_ct_.contains(ct);
163 }
164
165 // Note that both these functions returns positive reference or -1.
166 int GetProtoVariableFromBooleanVariable(BooleanVariable var) const {
167 if (var.value() >= reverse_boolean_map_.size()) return -1;
168 return reverse_boolean_map_[var];
169 }
170 int GetProtoVariableFromIntegerVariable(IntegerVariable var) const {
171 if (var.value() >= reverse_integer_map_.size()) return -1;
172 return reverse_integer_map_[var];
173 }
174
175 const std::vector<IntegerVariable>& GetVariableMapping() const {
176 return integers_;
177 }
178
180 const LinearExpressionProto& expr_proto) const {
181 LinearExpression expr;
182 expr.vars = Integers(expr_proto.vars());
183 for (int j = 0; j < expr_proto.coeffs_size(); ++j) {
184 expr.coeffs.push_back(IntegerValue(expr_proto.coeffs(j)));
185 }
186 expr.offset = IntegerValue(expr_proto.offset());
187 return CanonicalizeExpr(expr);
188 }
189
190 // For logging only, these are not super efficient.
192 int result = 0;
193 for (const IntegerVariable var : integers_) {
194 if (var != kNoIntegerVariable) result++;
195 }
196 return result;
197 }
199 int result = 0;
200 for (const BooleanVariable var : booleans_) {
201 if (var != kNoBooleanVariable) result++;
202 }
203 return result;
204 }
205
206 // Returns the number of variables in the loaded proto.
207 int NumProtoVariables() const { return integers_.size(); }
208
209 private:
210 friend void LoadVariables(const CpModelProto& model_proto,
211 bool view_all_booleans_as_integers, Model* m);
212 friend void ExtractEncoding(const CpModelProto& model_proto, Model* m);
213
214 // Note that only the variables used by at least one constraint will be
215 // created, the other will have a kNo[Integer,Interval,Boolean]VariableValue.
216 std::vector<IntegerVariable> integers_;
217 std::vector<IntervalVariable> intervals_;
218 std::vector<BooleanVariable> booleans_;
219
220 // Recover from a IntervalVariable/BooleanVariable its associated CpModelProto
221 // index. The value of -1 is used to indicate that there is no correspondence
222 // (i.e. this variable is only used internally).
225
226 // Set of constraints to ignore because they were already dealt with by
227 // ExtractEncoding().
228 absl::flat_hash_set<const ConstraintProto*> already_loaded_ct_;
229 absl::flat_hash_set<const ConstraintProto*> is_half_encoding_ct_;
230};
231
232} // namespace sat
233} // namespace operations_research
234
235#endif // OR_TOOLS_SAT_CP_MODEL_MAPPING_H_
friend void LoadVariables(const CpModelProto &model_proto, bool view_all_booleans_as_integers, Model *m)
int NumIntegerVariables() const
For logging only, these are not super efficient.
std::vector< IntegerVariable > Integers(const List &list) const
AffineExpression Affine(const LinearExpressionProto &exp) const
int GetProtoVariableFromBooleanVariable(BooleanVariable var) const
bool ConstraintIsAlreadyLoaded(const ConstraintProto *ct) const
bool IsHalfEncodingConstraint(const ConstraintProto *ct) const
std::vector< IntervalVariable > Intervals(const ProtoIndices &indices) const
int NumProtoVariables() const
Returns the number of variables in the loaded proto.
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
const Constraint * ct
int64_t value
IntVar * var
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
Returns the vector of the negated variables.
Definition integer.cc:51
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
LinearExpression CanonicalizeExpr(const LinearExpression &expr)
const BooleanVariable kNoBooleanVariable(-1)
double ToDouble(IntegerValue value)
Definition integer.h:73
In SWIG mode, we don't want anything besides these top-level includes.
absl::flat_hash_set< IntegerVariable > objective_impacting_variables
double ScaleIntegerObjective(IntegerValue value) const