OR-Tools  9.0
presolve_util.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_PRESOLVE_UTIL_H_
15 #define OR_TOOLS_SAT_PRESOLVE_UTIL_H_
16 
17 #include <algorithm>
18 #include <cstdint>
19 #include <vector>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "ortools/base/int_type.h"
24 #include "ortools/base/logging.h"
28 #include "ortools/util/bitset.h"
30 
31 namespace operations_research {
32 namespace sat {
33 
34 // If for each literal of a clause, we can infer a domain on an integer
35 // variable, then we know that this variable domain is included in the union of
36 // such infered domains.
37 //
38 // This allows to propagate "element" like constraints encoded as enforced
39 // linear relations, and other more general reasoning.
40 //
41 // TODO(user): Also use these "deductions" in the solver directly. This is done
42 // in good MIP solvers, and we should exploit them more.
43 //
44 // TODO(user): Also propagate implicit clauses (lit, not(lit)). Maybe merge
45 // that with probing code? it might be costly to store all deduction done by
46 // probing though, but I think this is what MIP solver do.
48  public:
49  // Adds the fact that enforcement => var \in domain.
50  //
51  // Important: No need to store any deductions where the domain is a superset
52  // of the current variable domain.
53  void AddDeduction(int literal_ref, int var, Domain domain);
54 
55  // Returns list of (var, domain) that were deduced because:
56  // 1/ We have a domain deduction for var and all literal from the clause
57  // 2/ So we can take the union of all the deduced domains.
58  //
59  // TODO(user): We could probably be even more efficient. We could also
60  // compute exactly what clauses need to be "waked up" as new deductions are
61  // added.
62  std::vector<std::pair<int, Domain>> ProcessClause(
63  absl::Span<const int> clause);
64 
65  // Optimization. Any following ProcessClause() will be fast if no more
66  // deduction touching that clause are added.
68  something_changed_.ClearAndResize(something_changed_.size());
69  }
70 
71  // Returns the total number of "deductions" stored by this class.
72  int NumDeductions() const { return deductions_.size(); }
73 
74  private:
75  DEFINE_INT_TYPE(Index, int);
76  Index IndexFromLiteral(int ref) {
77  return Index(ref >= 0 ? 2 * ref : -2 * ref - 1);
78  }
79 
80  std::vector<int> tmp_num_occurrences_;
81 
82  SparseBitset<Index> something_changed_;
83  absl::StrongVector<Index, std::vector<int>> enforcement_to_vars_;
84  absl::flat_hash_map<std::pair<Index, int>, Domain> deductions_;
85 };
86 
87 // Replaces the variable var in ct using the definition constraint.
88 // Currently the coefficient in the definition must be 1 or -1.
89 void SubstituteVariable(int var, int64_t var_coeff_in_definition,
90  const ConstraintProto& definition, ConstraintProto* ct);
91 
92 } // namespace sat
93 } // namespace operations_research
94 
95 #endif // OR_TOOLS_SAT_PRESOLVE_UTIL_H_
We call domain any subset of Int64 = [kint64min, kint64max].
IntegerType size() const
Definition: bitset.h:770
void ClearAndResize(IntegerType size)
Definition: bitset.h:779
std::vector< std::pair< int, Domain > > ProcessClause(absl::Span< const int > clause)
void AddDeduction(int literal_ref, int var, Domain domain)
const Constraint * ct
IntVar * var
Definition: expr_array.cc:1874
void SubstituteVariable(int var, int64_t var_coeff_in_definition, const ConstraintProto &definition, ConstraintProto *ct)
Collection of objects used to extend the Constraint Solver library.