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