Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
presolve_context.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_PRESOLVE_CONTEXT_H_
15#define ORTOOLS_SAT_PRESOLVE_CONTEXT_H_
16
17#include <cstdint>
18#include <optional>
19#include <string>
20#include <tuple>
21#include <utility>
22#include <vector>
23
24#include "absl/base/attributes.h"
25#include "absl/container/flat_hash_map.h"
26#include "absl/container/flat_hash_set.h"
27#include "absl/flags/declare.h"
28#include "absl/log/check.h"
29#include "absl/strings/str_cat.h"
30#include "absl/strings/string_view.h"
31#include "absl/types/span.h"
36#include "ortools/sat/model.h"
40#include "ortools/sat/util.h"
42#include "ortools/util/bitset.h"
47
48#ifndef SWIG
49OR_DLL ABSL_DECLARE_FLAG(bool, cp_model_debug_postsolve);
50#endif
51
52namespace operations_research {
53namespace sat {
54
55// We use some special constraint index in our variable <-> constraint graph.
56constexpr int kObjectiveConstraint = -1;
57constexpr int kAffineRelationConstraint = -2;
58constexpr int kAssumptionsConstraint = -3;
59
60class PresolveContext;
61
62// When storing a reference to a literal, it is important not to forget when
63// reading it back to take its representative. Otherwise, we might introduce
64// literal that have already been removed, which will break invariants in a
65// bunch of places.
67 public:
68 SavedLiteral() = default;
69 explicit SavedLiteral(int ref) : ref_(ref) {}
70 int Get(PresolveContext* context) const;
71
72 private:
73 int ref_ = 0;
74};
75
76// Same as SavedLiteral for variable.
77//
78// TODO(user): get rid of this, we don't have the notion of equivalent variable
79// anymore, but the more general affine relation one. We just need to support
80// general affine for the linear1 involving an absolute value.
82 public:
83 SavedVariable() = default;
84 explicit SavedVariable(int ref) : ref_(ref) {}
85 int Get() const;
86
87 private:
88 int ref_ = 0;
89};
90
91// If a floating point objective is present, scale it using the current domains
92// and transform it to an integer_objective.
93ABSL_MUST_USE_RESULT bool ScaleFloatingPointObjective(
94 const SatParameters& params, SolverLogger* logger, CpModelProto* proto);
95
96// Wrap the CpModelProto we are presolving with extra data structure like the
97// in-memory domain of each variables and the constraint variable graph.
99 public:
100 PresolveContext(Model* model, CpModelProto* cp_model, CpModelProto* mapping)
101 : working_model(cp_model),
102 mapping_model(mapping),
103 logger_(model->GetOrCreate<SolverLogger>()),
104 params_(*model->GetOrCreate<SatParameters>()),
105 time_limit_(model->GetOrCreate<TimeLimit>()),
106 random_(model->GetOrCreate<ModelRandomGenerator>()) {}
107
108 // Helpers to adds new variables to the presolved model.
109
110 // Creates a new integer variable with the given domain.
111 // WARNING: this does not set any hint value for the new variable.
112 int NewIntVar(const Domain& domain);
113
114 // Creates a new Boolean variable.
115 // WARNING: this does not set any hint value for the new variable.
116 int NewBoolVar(absl::string_view source);
117
118 // Creates a new integer variable with the given domain and definition.
119 // By default this also creates the linking constraint new_var = definition.
120 // Its hint value is set to the value of the definition. Returns -1 if we
121 // couldn't create the definition due to overflow.
123 const Domain& domain,
124 absl::Span<const std::pair<int, int64_t>> definition,
125 bool append_constraint_to_mapping_model = false);
126
127 // Creates a new bool var.
128 // Its hint value is set to the value of the given clause.
129 int NewBoolVarWithClause(absl::Span<const int> clause);
130
131 // Creates a new bool var.
132 // Its hint value is set to the value of the given conjunction.
133 int NewBoolVarWithConjunction(absl::Span<const int> conjunction);
134
135 // Some expansion code use constant literal to be simpler to write. This will
136 // create a NewBoolVar() the first time, but later call will just returns it.
137 int GetTrueLiteral();
138 int GetFalseLiteral();
139
140 // Shortcuts to create enforced constraints.
142 absl::Span<const int> enforcement_literals);
144
145 // a => b.
146 void AddImplication(int a, int b);
147
148 // b => (x ∈ domain).
149 void AddImplyInDomain(int b, int x, const Domain& domain);
150
151 // b => (expr ∈ domain).
152 void AddImplyInDomain(int b, const LinearExpressionProto& expr,
153 const Domain& domain);
154
155 // Helpers to query the current domain of a variable.
156 bool DomainIsEmpty(int ref) const;
157 bool IsFixed(int ref) const;
158 bool CanBeUsedAsLiteral(int ref) const;
159 bool LiteralIsTrue(int lit) const;
160 bool LiteralIsFalse(int lit) const;
161 int64_t MinOf(int ref) const;
162 int64_t MaxOf(int ref) const;
163 int64_t FixedValue(int ref) const;
164
165 // Check if the domain contains the given value. Note that this is stronger
166 // than DomainOf(ref).Contains(value) since here we will use the affine
167 // representative if any.
168 bool VarCanTakeValue(int var, int64_t value) const;
169
170 const Domain& DomainOf(int var) const;
171 int64_t DomainSize(int ref) const;
172 absl::Span<const Domain> AllDomains() const { return domains_; }
173
174 // Helper to query the state of an interval.
175 bool IntervalIsConstant(int ct_ref) const;
176 int64_t StartMin(int ct_ref) const;
177 int64_t StartMax(int ct_ref) const;
178 int64_t SizeMin(int ct_ref) const;
179 int64_t SizeMax(int ct_ref) const;
180 int64_t EndMin(int ct_ref) const;
181 int64_t EndMax(int ct_ref) const;
182 std::string IntervalDebugString(int ct_ref) const;
183
184 // Helpers to query the current domain of a linear expression.
185 // This doesn't check for integer overflow, but our linear expression
186 // should be such that this cannot happen (tested at validation).
187 int64_t MinOf(const LinearExpressionProto& expr) const;
188 int64_t MaxOf(const LinearExpressionProto& expr) const;
189 bool IsFixed(const LinearExpressionProto& expr) const;
190 int64_t FixedValue(const LinearExpressionProto& expr) const;
191
192 // This is faster than testing IsFixed() + FixedValue().
193 std::optional<int64_t> FixedValueOrNullopt(
194 const LinearExpressionProto& expr) const;
195
196 // Accepts any proto with two parallel vector .vars() and .coeffs(), like
197 // LinearConstraintProto or ObjectiveProto or LinearExpressionProto but beware
198 // that this ignore any offset.
199 template <typename ProtoWithVarsAndCoeffs>
200 std::pair<int64_t, int64_t> ComputeMinMaxActivity(
201 const ProtoWithVarsAndCoeffs& proto) const {
202 int64_t min_activity = 0;
203 int64_t max_activity = 0;
204 const int num_vars = proto.vars().size();
205 for (int i = 0; i < num_vars; ++i) {
206 const int var = proto.vars(i);
207 const int64_t coeff = proto.coeffs(i);
208 if (coeff > 0) {
209 min_activity += coeff * MinOf(var);
210 max_activity += coeff * MaxOf(var);
211 } else {
212 min_activity += coeff * MaxOf(var);
213 max_activity += coeff * MinOf(var);
214 }
215 }
216 return {min_activity, max_activity};
217 }
218
219 // Utility function.
220 void CappedUpdateMinMaxActivity(int var, int64_t coeff, int64_t* min_activity,
221 int64_t* max_activity) {
222 if (coeff > 0) {
223 *min_activity = CapAdd(*min_activity, CapProd(coeff, MinOf(var)));
224 *max_activity = CapAdd(*max_activity, CapProd(coeff, MaxOf(var)));
225 } else {
226 *min_activity = CapAdd(*min_activity, CapProd(coeff, MaxOf(var)));
227 *max_activity = CapAdd(*max_activity, CapProd(coeff, MinOf(var)));
228 }
229 }
230
231 // Canonicalization of linear constraint. This might also be needed when
232 // creating new constraint to make sure there are no duplicate variables.
233 // Returns true if the set of variables in the expression changed.
234 //
235 // This uses affine relation and regroup duplicate/fixed terms.
237 bool* is_impossible = nullptr);
238 bool CanonicalizeLinearExpression(absl::Span<const int> enforcements,
240
241 // This methods only works for affine expressions (checked).
242 bool DomainContains(const LinearExpressionProto& expr, int64_t value) const;
243
244 // Return a super-set of the domain of the linear expression.
246
247 // Returns true iff the expr is of the form a * literal + b.
248 // The other function can be used to get the literal that achieve MaxOf().
249 bool ExpressionIsAffineBoolean(const LinearExpressionProto& expr) const;
250 int LiteralForExpressionMax(const LinearExpressionProto& expr) const;
251
252 // Returns true iff the expr is of the form 1 * var + 0.
254
255 // Returns true iff the expr is a literal (x or not(x)).
257 int* literal = nullptr) const;
258
259 // This function takes a positive variable reference.
260 bool DomainOfVarIsIncludedIn(int var, const Domain& domain) {
261 return domains_[var].IsIncludedIn(domain);
262 }
263
264 // Returns true if this ref only appear in one constraint.
265 bool VariableIsUnique(int ref) const;
266 bool VariableIsUniqueAndRemovable(int ref) const;
267
268 // Returns true if this ref no longer appears in the model.
269 bool VariableIsNotUsedAnymore(int ref) const;
270
271 // Functions to make sure that once we remove a variable, we no longer reuse
272 // it.
273 void MarkVariableAsRemoved(int ref);
274 bool VariableWasRemoved(int ref) const;
275
276 // Same as VariableIsUniqueAndRemovable() except that in this case the
277 // variable also appear in the objective in addition to a single constraint.
278 bool VariableWithCostIsUnique(int ref) const;
279 bool VariableWithCostIsUniqueAndRemovable(int ref) const;
280
281 // Returns true if an integer variable is only appearing in the rhs of
282 // constraints of the form lit => var in domain. When this is the case, then
283 // we can usually remove this variable and replace these constraints with
284 // the proper constraints on the enforcement literals.
286
287 // Similar to VariableIsOnlyUsedInEncodingAndMaybeInObjective() for the case
288 // where we have one extra constraint instead of the objective. Sometimes it
289 // is possible to transfer the linear1 domain restrictions to another
290 // variable. for instance if the other constraint is of the form Y = abs(X) or
291 // Y = X^2, then a domain restriction on Y can be transferred to X. We can
292 // then move the extra constraint to the mapping model and remove one
293 // variable. This happens on the flatzinc celar problems for instance.
295
296 // Returns false if the new domain is empty. Sets 'domain_modified' (if
297 // provided) to true iff the domain is modified otherwise does not change it.
298 ABSL_MUST_USE_RESULT bool IntersectDomainWith(
299 int ref, const Domain& domain, bool* domain_modified = nullptr);
300
301 // Returns false if the 'lit' doesn't have the desired value in the domain.
302 ABSL_MUST_USE_RESULT bool SetLiteralToFalse(int lit);
303 ABSL_MUST_USE_RESULT bool SetLiteralToTrue(int lit);
304
305 // Same as IntersectDomainWith() but take a linear expression as input.
306 // If this expression if of size > 1, this does nothing for now, so it will
307 // only propagates for constant and affine expression.
308 ABSL_MUST_USE_RESULT bool IntersectDomainWith(
309 const LinearExpressionProto& expr, const Domain& domain,
310 bool* domain_modified = nullptr);
311
312 ABSL_MUST_USE_RESULT bool IntersectionOfAffineExprsIsNotEmpty(
314
315 // This function always return false. It is just a way to make a little bit
316 // more sure that we abort right away when infeasibility is detected.
317 ABSL_MUST_USE_RESULT bool NotifyThatModelIsUnsat(
318 absl::string_view message = "") {
319 // TODO(user): Report any explanation for the client in a nicer way?
320 SOLVER_LOG(logger_, "INFEASIBLE: '", message, "'");
321 is_unsat_ = true;
322 return false;
323 }
324 bool ModelIsUnsat() const { return is_unsat_; }
325
326 // Stores a description of a rule that was just applied to have a summary of
327 // what the presolve did at the end.
328 void UpdateRuleStats(std::string_view name, int num_times = 1);
329
330 // Updates the constraints <-> variables graph. This needs to be called each
331 // time a constraint is modified.
333
334 // At the beginning of the presolve, we delay the costly creation of this
335 // "graph" until we at least ran some basic presolve. This is because during
336 // a LNS neighborhood, many constraints will be reduced significantly by
337 // this "simple" presolve.
339
340 // Calls UpdateConstraintVariableUsage() on all newly created constraints.
342
343 // Returns true if our current constraints <-> variables graph is ok.
344 // This is meant to be used in DEBUG mode only.
346
347 // Loop over all variable and return true if one of them is only used in
348 // affine relation and is not a representative. This is in O(num_vars) and
349 // only meant to be used in DCHECKs.
350 bool HasUnusedAffineVariable() const;
351
352 // A "canonical domain" always have a MinOf() equal to zero.
353 // If needed we introduce a new variable with such canonical domain and
354 // add the relation X = Y + offset.
355 //
356 // This is useful in some corner case to avoid overflow.
357 //
358 // TODO(user): When we can always get rid of affine relation, it might be good
359 // to do a final pass to canonicalize all domains in a model after presolve.
360 void CanonicalizeVariable(int ref);
361
362 // Given the relation (X * coeff % mod = rhs % mod), this creates a new
363 // variable so that X = mod * Y + cte.
364 //
365 // This requires mod != 0 and coeff != 0.
366 //
367 // Note that the new variable will have a canonical domain (i.e. min == 0).
368 // We also do not create anything if this fixes the given variable or the
369 // relation simplifies. Returns false if the model is infeasible.
370 bool CanonicalizeAffineVariable(int ref, int64_t coeff, int64_t mod,
371 int64_t rhs);
372
373 // Adds the relation (var_x = coeff * var_y + offset) to the repository.
374 // Returns false if we detect infeasability because of this.
375 //
376 // Once the relation is added, it doesn't need to be enforced by a constraint
377 // in the model proto, since we will propagate such relation directly and add
378 // them to the proto at the end of the presolve.
379 //
380 // Note that this should always add a relation, even though it might need to
381 // create a new representative for both var_x and var_y in some cases. Like if
382 // x = 3z and y = 5t are already added, if we add x = 2y, we have 3z = 10t and
383 // can only resolve this by creating a new variable r such that z = 10r and t
384 // = 3r.
385 //
386 // All involved variables will be marked to appear in the special
387 // kAffineRelationConstraint. This will allow to identify when a variable is
388 // no longer needed (only appear there and is not a representative).
389 bool StoreAffineRelation(int var_x, int var_y, int64_t coeff, int64_t offset,
390 bool debug_no_recursion = false);
391
392 // Adds the fact that ref_a == ref_b using StoreAffineRelation() above.
393 // Returns false if this makes the problem infeasible.
394 bool StoreBooleanEqualityRelation(int ref_a, int ref_b);
395
396 // Returns the representative of a literal.
397 int GetLiteralRepresentative(int ref) const;
398
399 // Check if an integer variable is an affine representative.
400 bool VariableIsAffineRepresentative(int var) const;
401
402 // Used for statistics.
403 int NumAffineRelations() const { return affine_relations_.NumRelations(); }
404
405 // Returns the representative of ref under the affine relations.
407
408 // To facilitate debugging.
409 std::string RefDebugString(int ref) const;
410 std::string AffineRelationDebugString(int ref) const;
411
412 // Makes sure the domain of ref and of its representative (ref = coeff * rep +
413 // offset) are in sync. Returns false on unsat.
414 bool PropagateAffineRelation(int var);
415 bool PropagateAffineRelation(int var, int rep, int64_t coeff, int64_t offset);
416
417 // Creates the internal structure for any new variables in working_model.
419
420 // This is a bit hacky. Clear some fields. See call site.
421 //
422 // TODO(user): The ModelCopier should probably not depend on the full context
423 // it only need to read/write domains and call UpdateRuleStats(), so we might
424 // want to split that part out so that we can just initialize the full context
425 // later. Alternatively, we could just move more complex part of the context
426 // out, like the graph, the encoding, the affine representative, and so on to
427 // individual and easier to manage classes.
428 void ResetAfterCopy();
429
430 // Clears the "rules" statistics.
431 void ClearStats();
432
433 // Inserts the given literal to encode var == value.
434 // If an encoding already exists, it adds the two implications between
435 // the previous encoding and the new encoding.
436 //
437 // Important: This does not update the constraint<->variable graph, so
438 // ConstraintVariableGraphIsUpToDate() will be false until
439 // UpdateNewConstraintsVariableUsage() is called.
440 //
441 // Returns false if the model become UNSAT.
442 //
443 // TODO(user): This function is not always correct if
444 // !context->DomainOf(var).contains(value), we could make it correct but it
445 // might be a bit expansive to do so. For now we just have a DCHECK().
446 bool InsertVarValueEncoding(int literal, int var, int64_t value);
447
448 // Gets the associated literal if it is already created. Otherwise
449 // create it, add the corresponding constraints and returns it.
450 //
451 // Important: This does not update the constraint<->variable graph, so
452 // ConstraintVariableGraphIsUpToDate() will be false until
453 // UpdateNewConstraintsVariableUsage() is called.
454 int GetOrCreateVarValueEncoding(int ref, int64_t value);
455
456 // Gets the associated literal if it is already created. Otherwise
457 // create it, add the corresponding constraints and returns it.
458 //
459 // Important: This does not update the constraint<->variable graph, so
460 // ConstraintVariableGraphIsUpToDate() will be false until
461 // UpdateNewConstraintsVariableUsage() is called.
463 int64_t value);
464
465 // If not already done, adds a Boolean to represent any integer variables that
466 // take only two values. Make sure all the relevant affine and encoding
467 // relations are updated.
468 //
469 // Note that this might create a new Boolean variable.
470 void CanonicalizeDomainOfSizeTwo(int var);
471
472 // Returns true if a literal attached to ref == value exists.
473 // It assigns the corresponding to `literal` if non null.
474 // This function will check that the value is in the domain of ref.
475 bool HasVarValueEncoding(int ref, int64_t value, int* literal = nullptr);
476
477 // Returns true if a literal attached to expr == value exists.
478 // It assigns the corresponding to `literal`. This methods checks that the
479 // expression as exactly one variable.
480 //
481 // This methods checks that the value is in the domain of the expression.
482 bool HasAffineValueEncoding(const LinearExpressionProto& expr, int64_t value,
483 int* literal = nullptr);
484
485 // Returns true if we have literal <=> var = value for all values of var.
486 //
487 // TODO(user): If the domain was shrunk, we can have a false positive.
488 // Still it means that the number of values removed is greater than the number
489 // of values not encoded.
490 bool IsFullyEncoded(int ref) const;
491
492 // This methods only works for affine expressions (checked).
493 // It returns true iff the expression is constant or its one variable is full
494 // encoded.
495 bool IsFullyEncoded(const LinearExpressionProto& expr) const;
496
497 // TODO(user): If the domain was shrunk, we can have a false positive.
498 // Still it means that the number of values removed is greater than the number
499 // of values not encoded.
500 bool IsMostlyFullyEncoded(int ref) const;
501
502 // Returns the number of values encoded for the given reference.
503 int64_t GetValueEncodingSize(int ref) const;
504
505 // Stores the fact that literal implies var == value.
506 // It returns true if that information is new.
507 bool StoreLiteralImpliesVarEqValue(int literal, int var, int64_t value);
508
509 // Stores the fact that literal implies var != value.
510 // It returns true if that information is new.
511 bool StoreLiteralImpliesVarNeValue(int literal, int var, int64_t value);
512
513 // Objective handling functions. We load it at the beginning so that during
514 // presolve we can work on the more efficient hash_map representation.
515 //
516 // Note that ReadObjectiveFromProto() makes sure that var_to_constraints of
517 // all the variable that appear in the objective contains -1. This is later
518 // enforced by all the functions modifying the objective.
519 //
520 // Note(user): Because we process affine relation only on
521 // CanonicalizeObjective(), it is possible that when processing a
522 // canonicalized linear constraint, we don't detect that a variable in affine
523 // relation is in the objective. For now this is fine, because when this is
524 // the case, we also have an affine linear constraint, so we can't really do
525 // anything with that variable since it appear in at least two constraints.
527 bool AddToObjectiveOffset(int64_t delta);
528 ABSL_MUST_USE_RESULT bool CanonicalizeOneObjectiveVariable(int var);
529 ABSL_MUST_USE_RESULT bool CanonicalizeObjective(bool simplify_domain = true);
530 void WriteObjectiveToProto() const;
531
532 // When the objective is singleton, we can always restrict the domain of var
533 // so that the current objective domain is non-constraining. Returns false
534 // on UNSAT.
536
537 // Some function need the domain to be up to date in the proto.
538 // This make sures our in-memory domain are written back to the proto.
539 void WriteVariableDomainsToProto() const;
540
541 // Checks if the given exactly_one is included in the objective, and simplify
542 // the objective by adding a constant value to all the exactly one terms.
543 //
544 // Returns true if a simplification was done.
545 bool ExploitExactlyOneInObjective(absl::Span<const int> exactly_one);
546
547 // We can always add a multiple of sum X - 1 == 0 to the objective.
548 // However, depending on which multiple we choose, this might break our
549 // overflow preconditions on the objective. So we return false and do nothing
550 // if this happens.
551 bool ShiftCostInExactlyOne(absl::Span<const int> exactly_one, int64_t shift);
552
553 // Allows to manipulate the objective coefficients.
554 void RemoveVariableFromObjective(int ref);
555 void AddToObjective(int var, int64_t value);
556 void AddLiteralToObjective(int ref, int64_t value);
557
558 // Given a variable defined by the given inequality that also appear in the
559 // objective, remove it from the objective by transferring its cost to other
560 // variables in the equality.
561 //
562 // Returns false, if the substitution cannot be done. This is the case if the
563 // model become UNSAT or if doing it will result in an objective that do not
564 // satisfy our overflow preconditions. Note that this can only happen if the
565 // substituted variable is not implied free (i.e. if its domain is smaller
566 // than the implied domain from the equality).
567 ABSL_MUST_USE_RESULT bool SubstituteVariableInObjective(
568 int var_in_equality, int64_t coeff_in_equality,
569 const ConstraintProto& equality);
570
571 // Objective getters.
572 const Domain& ObjectiveDomain() const { return objective_domain_; }
573 const absl::flat_hash_map<int, int64_t>& ObjectiveMap() const {
574 return objective_map_;
575 }
576 int64_t ObjectiveCoeff(int var) const {
577 DCHECK_GE(var, 0);
578 const auto it = objective_map_.find(var);
579 return it == objective_map_.end() ? 0 : it->second;
580 }
581
582 // Returns false if the variables in the objective with a positive (resp.
583 // negative) coefficient can freely decrease (resp. increase) within their
584 // domain (if we ignore the other constraints). Otherwise, returns true.
586 return objective_domain_is_constraining_;
587 }
588
589 // If var is an unused variable in an affine relation and is not a
590 // representative, we can remove it from the model. Note that this requires
591 // the variable usage graph to be up to date.
593
594 // Advanced usage. This should be called when a variable can be removed from
595 // the problem, so we don't count it as part of an affine relation anymore.
598
599 // Variable <-> constraint graph.
600 // The vector list is sorted and contains unique elements.
601 //
602 // Important: To properly handle the objective, var_to_constraints[objective]
603 // contains kObjectiveConstraint (i.e. -1) so that if the objective appear in
604 // only one constraint, the constraint cannot be simplified.
605 absl::Span<const int> ConstraintToVars(int c) const {
607 return constraint_to_vars_[c];
608 }
609 const absl::flat_hash_set<int>& VarToConstraints(int var) const {
611 return var_to_constraints_[var];
612 }
613 int IntervalUsage(int c) const {
615 if (c >= interval_usage_.size()) return 0;
616 return interval_usage_[c];
617 }
618
619 // Note this function does not update the constraint graph. It assumes this is
620 // done elsewhere.
621 bool MarkConstraintAsFalse(ConstraintProto* ct, std::string_view reason);
622
623 // Checks if a constraint contains an enforcement literal set to false,
624 // or if it has been cleared.
625 bool ConstraintIsInactive(int ct_index) const;
626
627 // Checks if a constraint contains an enforcement literal not fixed, and
628 // no enforcement literals set to false.
629 bool ConstraintIsOptional(int ct_ref) const;
630
631 // Make sure we never delete an "assumption" literal by using a special
632 // constraint for that.
634 for (const int ref : working_model->assumptions()) {
635 var_to_constraints_[PositiveRef(ref)].insert(kAssumptionsConstraint);
636 }
637 }
638
639 // The "expansion" phase should be done once and allow to transform complex
640 // constraints into basic ones (see cp_model_expand.h). Some presolve rules
641 // need to know if the expansion was ran before being applied.
642 bool ModelIsExpanded() const { return model_is_expanded_; }
643 void NotifyThatModelIsExpanded() { model_is_expanded_ = true; }
644
645 // The following helper adds the following constraint:
646 // result <=> (time_i <= time_j && active_i is true && active_j is true)
647 // and returns the (cached) literal result.
648 //
649 // Note that this cache should just be used temporarily and then cleared
650 // with ClearPrecedenceCache() because there is no mechanism to update the
651 // cached literals when literal equivalence are detected.
653 const LinearExpressionProto& time_j,
654 int active_i, int active_j);
655
656 std::tuple<int, int64_t, int, int64_t, int64_t, int, int>
658 const LinearExpressionProto& time_j, int active_i,
659 int active_j);
660
661 // Clear the precedence cache.
663
664 // Logs stats to the logger.
665 void LogInfo();
666
667 // This should be called only once after InitializeNewDomains() to load
668 // the hint, in order to maintain it as best as possible during presolve.
669 // Hint values outside the domain of their variable are adjusted to the
670 // nearest value in this domain. Missing hint values are completed when
671 // possible (e.g. for the model proto's fixed variables).
672 void LoadSolutionHint();
673
674 SolutionCrush& solution_crush() { return solution_crush_; }
675 // This is slow O(problem_size) but can be used to debug presolve, either by
676 // pinpointing the transition from feasible to infeasible or the other way
677 // around if for some reason the presolve drop constraint that it shouldn't.
679
680 SolverLogger* logger() const { return logger_; }
681 const SatParameters& params() const { return params_; }
682 TimeLimit* time_limit() { return time_limit_; }
683 ModelRandomGenerator* random() { return random_; }
684
687
688 // Number of "rules" applied. This should be equal to the sum of all numbers
689 // in stats_by_rule_name. This is used to decide if we should do one more pass
690 // of the presolve or not. Note that depending on the presolve transformation,
691 // a rule can correspond to a tiny change or a big change. Because of that,
692 // this isn't a perfect proxy for the efficacy of the presolve.
694
695 // Temporary storage.
696 std::vector<int> tmp_literals;
697 std::vector<Domain> tmp_term_domains;
698 std::vector<Domain> tmp_left_domains;
699 absl::flat_hash_set<int> tmp_literal_set;
700
701 // Each time a domain is modified this is set to true.
703
704 // Each time the constraint <-> variable graph is updated, we update this.
705 // A variable is added here iff its usage decreased and is now one or two.
707
708 // Advanced presolve. See this class comment.
710
711 // Adds a new constraint to the mapping proto. The version with the base
712 // constraint will copy that constraint to the new constraint.
713 //
714 // If the flag --cp_model_debug_postsolve is set, we will use the caller
715 // file/line number to add debug info in the constraint name() field.
716 ConstraintProto* NewMappingConstraint(absl::string_view file, int line);
718 absl::string_view file, int line);
719
720 private:
721 void MaybeResizeIntervalData();
722
723 void EraseFromVarToConstraint(int var, int c);
724
725 // Helper to add an affine relation x = c.y + o to the given repository.
726 bool AddRelation(int x, int y, int64_t c, int64_t o, AffineRelation* repo);
727
728 void AddVariableUsage(int c);
729 void UpdateLinear1Usage(const ConstraintProto& ct, int c);
730
731 // Makes sure we only insert encoding about the current representative.
732 //
733 // Returns false if ref cannot take the given value (it might not have been
734 // propagated yet).
735 bool CanonicalizeEncoding(int* ref, int64_t* value) const;
736
737 // Inserts an half reified var value encoding (literal => var ==/!= value).
738 // It returns true if the new state is different from the old state.
739 // Not that if imply_eq is false, the literal will be stored in its negated
740 // form.
741 //
742 // Thus, if you detect literal <=> var == value, then two calls must be made:
743 // InsertHalfVarValueEncoding(literal, var, value, true);
744 // InsertHalfVarValueEncoding(NegatedRef(literal), var, value, false);
745 bool InsertHalfVarValueEncoding(int literal, int var, int64_t value,
746 bool imply_eq);
747
748 // Insert fully reified var-value encoding.
749 // Returns false if this make the problem infeasible.
750 bool InsertVarValueEncodingInternal(int literal, int var, int64_t value,
751 bool add_constraints);
752
753 SolverLogger* logger_;
754 const SatParameters& params_;
755 TimeLimit* time_limit_;
756 ModelRandomGenerator* random_;
757
758 // Initially false, and set to true on the first inconsistency.
759 bool is_unsat_ = false;
760
761 // The current domain of each variables.
762 std::vector<Domain> domains_;
763
764 SolutionCrush solution_crush_;
765
766 // Internal representation of the objective. During presolve, we first load
767 // the objective in this format in order to have more efficient substitution
768 // on large problems (also because the objective is often dense). At the end
769 // we convert it back to its proto form.
770 mutable bool objective_proto_is_up_to_date_ = false;
771 absl::flat_hash_map<int, int64_t> objective_map_;
772 int64_t objective_overflow_detection_;
773 mutable std::vector<std::pair<int, int64_t>> tmp_entries_;
774 bool objective_domain_is_constraining_ = false;
775 Domain objective_domain_;
776 double objective_offset_;
777 double objective_scaling_factor_;
778 int64_t objective_integer_before_offset_;
779 int64_t objective_integer_after_offset_;
780 int64_t objective_integer_scaling_factor_;
781
782 // Constraints <-> Variables graph.
783 std::vector<std::vector<int>> constraint_to_vars_;
784 std::vector<absl::flat_hash_set<int>> var_to_constraints_;
785
786 // Number of constraints of the form [lit =>] var in domain.
787 std::vector<int> constraint_to_linear1_var_;
788 std::vector<int> var_to_num_linear1_;
789
790 // We maintain how many time each interval is used.
791 std::vector<std::vector<int>> constraint_to_intervals_;
792 std::vector<int> interval_usage_;
793
794 // Used by GetTrueLiteral()/GetFalseLiteral().
795 bool true_literal_is_defined_ = false;
796 int true_literal_;
797
798 // Contains variables with some encoded value: encoding_[i][v] points
799 // to the literal attached to the value v of the variable i.
800 absl::flat_hash_map<int, absl::flat_hash_map<int64_t, SavedLiteral>>
801 encoding_;
802
803 // Contains the currently collected half value encodings:
804 // (literal, var, value), i.e.: literal => var ==/!= value
805 // The state is accumulated (adding x => var == value then !x => var != value)
806 // will deduce that x equivalent to var == value.
807 absl::flat_hash_map<std::tuple<int, int>, int64_t> eq_half_encoding_;
808 absl::flat_hash_map<std::tuple<int, int>, int64_t> neq_half_encoding_;
809
810 // This regroups all the affine relations between variables. Note that the
811 // constraints used to detect such relations will be removed from the model at
812 // detection time. But we mark all the variables in affine relations as part
813 // of the kAffineRelationConstraint.
814 AffineRelation affine_relations_;
815
816 // Used by SetVariableAsRemoved() and VariableWasRemoved().
817 absl::flat_hash_set<int> removed_variables_;
818
819 // Cache for the reified precedence literals created during the expansion of
820 // the reservoir constraint. This cache is only valid during the expansion
821 // phase, and is cleared afterwards.
822 absl::flat_hash_map<std::tuple<int, int64_t, int, int64_t, int64_t, int, int>,
823 int>
824 reified_precedences_cache_;
825
826 // Just used to display statistics on the presolve rules that were used.
827 absl::flat_hash_map<std::string, int> stats_by_rule_name_;
828
829 // Used by CanonicalizeLinearExpressionInternal().
830 std::vector<std::pair<int, int64_t>> tmp_terms_;
831
832 bool model_is_expanded_ = false;
833};
834
835// Utility function to load the current problem into a in-memory representation
836// that will be used for probing. Returns false if UNSAT.
837bool LoadModelForProbing(PresolveContext* context, Model* local_model);
838
839bool LoadModelForPresolve(const CpModelProto& model_proto, SatParameters params,
840 PresolveContext* context, Model* local_model,
841 absl::string_view name_for_logging);
842
844 const PresolveContext* context,
845 std::vector<int>* variable_mapping,
846 CpModelProto* mini_model);
847} // namespace sat
848} // namespace operations_research
849
850#endif // ORTOOLS_SAT_PRESOLVE_CONTEXT_H_
#define OR_DLL
Definition base_export.h:27
bool HasAffineValueEncoding(const LinearExpressionProto &expr, int64_t value, int *literal=nullptr)
ABSL_MUST_USE_RESULT bool CanonicalizeObjective(bool simplify_domain=true)
bool ExpressionIsSingleVariable(const LinearExpressionProto &expr) const
bool CanonicalizeAffineVariable(int ref, int64_t coeff, int64_t mod, int64_t rhs)
ABSL_MUST_USE_RESULT bool IntersectDomainWith(int ref, const Domain &domain, bool *domain_modified=nullptr)
bool ExpressionIsALiteral(const LinearExpressionProto &expr, int *literal=nullptr) const
bool StoreLiteralImpliesVarEqValue(int literal, int var, int64_t value)
bool DomainContains(const LinearExpressionProto &expr, int64_t value) const
bool StoreAffineRelation(int var_x, int var_y, int64_t coeff, int64_t offset, bool debug_no_recursion=false)
ABSL_MUST_USE_RESULT bool SetLiteralToTrue(int lit)
int NewIntVarWithDefinition(const Domain &domain, absl::Span< const std::pair< int, int64_t > > definition, bool append_constraint_to_mapping_model=false)
ABSL_MUST_USE_RESULT bool IntersectionOfAffineExprsIsNotEmpty(const LinearExpressionProto &a, const LinearExpressionProto &b)
ABSL_MUST_USE_RESULT bool SubstituteVariableInObjective(int var_in_equality, int64_t coeff_in_equality, const ConstraintProto &equality)
bool ShiftCostInExactlyOne(absl::Span< const int > exactly_one, int64_t shift)
ABSL_MUST_USE_RESULT bool SetLiteralToFalse(int lit)
absl::Span< const int > ConstraintToVars(int c) const
ABSL_MUST_USE_RESULT bool NotifyThatModelIsUnsat(absl::string_view message="")
int GetOrCreateVarValueEncoding(int ref, int64_t value)
ABSL_MUST_USE_RESULT bool CanonicalizeOneObjectiveVariable(int var)
std::string IntervalDebugString(int ct_ref) const
bool ExpressionIsAffineBoolean(const LinearExpressionProto &expr) const
bool VariableIsOnlyUsedInLinear1AndOneExtraConstraint(int var) const
bool StoreBooleanEqualityRelation(int ref_a, int ref_b)
void AddImplyInDomain(int b, int x, const Domain &domain)
bool VariableIsOnlyUsedInEncodingAndMaybeInObjective(int var) const
std::tuple< int, int64_t, int, int64_t, int64_t, int, int > GetReifiedPrecedenceKey(const LinearExpressionProto &time_i, const LinearExpressionProto &time_j, int active_i, int active_j)
void CappedUpdateMinMaxActivity(int var, int64_t coeff, int64_t *min_activity, int64_t *max_activity)
bool CanonicalizeLinearConstraint(ConstraintProto *ct, bool *is_impossible=nullptr)
std::pair< int64_t, int64_t > ComputeMinMaxActivity(const ProtoWithVarsAndCoeffs &proto) const
int NewBoolVar(absl::string_view source)
int NewBoolVarWithClause(absl::Span< const int > clause)
bool HasVarValueEncoding(int ref, int64_t value, int *literal=nullptr)
bool MarkConstraintAsFalse(ConstraintProto *ct, std::string_view reason)
std::optional< int64_t > FixedValueOrNullopt(const LinearExpressionProto &expr) const
absl::Span< const Domain > AllDomains() const
Domain DomainSuperSetOf(const LinearExpressionProto &expr) const
ConstraintProto * NewMappingConstraint(absl::string_view file, int line)
int GetOrCreateAffineValueEncoding(const LinearExpressionProto &expr, int64_t value)
int LiteralForExpressionMax(const LinearExpressionProto &expr) const
bool CanonicalizeLinearExpression(absl::Span< const int > enforcements, LinearExpressionProto *expr)
bool DomainOfVarIsIncludedIn(int var, const Domain &domain)
AffineRelation::Relation GetAffineRelation(int ref) const
ConstraintProto * AddEnforcedConstraint(absl::Span< const int > enforcement_literals)
void AddToObjective(int var, int64_t value)
const absl::flat_hash_set< int > & VarToConstraints(int var) const
void AddLiteralToObjective(int ref, int64_t value)
bool InsertVarValueEncoding(int literal, int var, int64_t value)
PresolveContext(Model *model, CpModelProto *cp_model, CpModelProto *mapping)
const absl::flat_hash_map< int, int64_t > & ObjectiveMap() const
std::string AffineRelationDebugString(int ref) const
bool VarCanTakeValue(int var, int64_t value) const
bool ExploitExactlyOneInObjective(absl::Span< const int > exactly_one)
bool StoreLiteralImpliesVarNeValue(int literal, int var, int64_t value)
void UpdateRuleStats(std::string_view name, int num_times=1)
int NewBoolVarWithConjunction(absl::Span< const int > conjunction)
int GetOrCreateReifiedPrecedenceLiteral(const LinearExpressionProto &time_i, const LinearExpressionProto &time_j, int active_i, int active_j)
int Get(PresolveContext *context) const
Definition file.cc:327
bool LoadModelForProbing(PresolveContext *context, Model *local_model)
bool ScaleFloatingPointObjective(const SatParameters &params, SolverLogger *logger, CpModelProto *proto)
constexpr int kAffineRelationConstraint
constexpr int kAssumptionsConstraint
constexpr int kObjectiveConstraint
void CreateValidModelWithSingleConstraint(const ConstraintProto &ct, const PresolveContext *context, std::vector< int > *variable_mapping, CpModelProto *mini_model)
bool LoadModelForPresolve(const CpModelProto &model_proto, SatParameters params, PresolveContext *context, Model *local_model, absl::string_view name_for_logging)
OR-Tools root namespace.
int64_t CapAdd(int64_t x, int64_t y)
int64_t CapProd(int64_t x, int64_t y)
OR_DLL ABSL_DECLARE_FLAG(bool, cp_model_debug_postsolve)
#define SOLVER_LOG(logger,...)
Definition logging.h:114