OR-Tools  9.2
presolve_util.cc
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
15
16#include <cstdint>
17
20
21namespace operations_research {
22namespace sat {
23
24void DomainDeductions::AddDeduction(int literal_ref, int var, Domain domain) {
25 CHECK_GE(var, 0);
26 const Index index = IndexFromLiteral(literal_ref);
27 if (index >= something_changed_.size()) {
28 something_changed_.Resize(index + 1);
29 enforcement_to_vars_.resize(index.value() + 1);
30 }
31 if (var >= tmp_num_occurrences_.size()) {
32 tmp_num_occurrences_.resize(var + 1, 0);
33 }
34 const auto insert = deductions_.insert({{index, var}, domain});
35 if (insert.second) {
36 // New element.
37 something_changed_.Set(index);
38 enforcement_to_vars_[index].push_back(var);
39 } else {
40 // Existing element.
41 const Domain& old_domain = insert.first->second;
42 if (!old_domain.IsIncludedIn(domain)) {
43 insert.first->second = domain.IntersectionWith(old_domain);
44 something_changed_.Set(index);
45 }
46 }
47}
48
49std::vector<std::pair<int, Domain>> DomainDeductions::ProcessClause(
50 absl::Span<const int> clause) {
51 std::vector<std::pair<int, Domain>> result;
52
53 // We only need to process this clause if something changed since last time.
54 bool abort = true;
55 for (const int ref : clause) {
56 const Index index = IndexFromLiteral(ref);
57 if (index >= something_changed_.size()) return result;
58 if (something_changed_[index]) {
59 abort = false;
60 }
61 }
62 if (abort) return result;
63
64 // Count for each variable, how many times it appears in the deductions lists.
65 std::vector<int> to_process;
66 std::vector<int> to_clean;
67 for (const int ref : clause) {
68 const Index index = IndexFromLiteral(ref);
69 for (const int var : enforcement_to_vars_[index]) {
70 if (tmp_num_occurrences_[var] == 0) {
71 to_clean.push_back(var);
72 }
73 tmp_num_occurrences_[var]++;
74 if (tmp_num_occurrences_[var] == clause.size()) {
75 to_process.push_back(var);
76 }
77 }
78 }
79
80 // Clear the counts.
81 for (const int var : to_clean) {
82 tmp_num_occurrences_[var] = 0;
83 }
84
85 // Compute the domain unions.
86 std::vector<Domain> domains(to_process.size());
87 for (const int ref : clause) {
88 const Index index = IndexFromLiteral(ref);
89 for (int i = 0; i < to_process.size(); ++i) {
90 domains[i] = domains[i].UnionWith(
91 gtl::FindOrDieNoPrint(deductions_, {index, to_process[i]}));
92 }
93 }
94
95 for (int i = 0; i < to_process.size(); ++i) {
96 result.push_back({to_process[i], std::move(domains[i])});
97 }
98 return result;
99}
100
101namespace {
102// Helper method for variable substitution. Returns the coefficient of 'var' in
103// 'proto' and copies other terms in 'terms'.
104template <typename ProtoWithVarsAndCoeffs>
105int64_t GetVarCoeffAndCopyOtherTerms(
106 const int var, const ProtoWithVarsAndCoeffs& proto,
107 std::vector<std::pair<int, int64_t>>* terms) {
108 int64_t var_coeff = 0;
109 const int size = proto.vars().size();
110 for (int i = 0; i < size; ++i) {
111 int ref = proto.vars(i);
112 int64_t coeff = proto.coeffs(i);
113 if (!RefIsPositive(ref)) {
114 ref = NegatedRef(ref);
115 coeff = -coeff;
116 }
117
118 if (ref == var) {
119 // If var appear multiple time, we add its coefficient.
120 var_coeff += coeff;
121 continue;
122 } else {
123 terms->push_back({ref, coeff});
124 }
125 }
126 return var_coeff;
127}
128
129// Helper method for variable substituion. Sorts and merges the terms in 'terms'
130// and adds them to 'proto'.
131template <typename ProtoWithVarsAndCoeffs>
132void SortAndMergeTerms(std::vector<std::pair<int, int64_t>>* terms,
133 ProtoWithVarsAndCoeffs* proto) {
134 proto->clear_vars();
135 proto->clear_coeffs();
136 std::sort(terms->begin(), terms->end());
137 int current_var = 0;
138 int64_t current_coeff = 0;
139 for (const auto entry : *terms) {
140 CHECK(RefIsPositive(entry.first));
141 if (entry.first == current_var) {
142 current_coeff += entry.second;
143 } else {
144 if (current_coeff != 0) {
145 proto->add_vars(current_var);
146 proto->add_coeffs(current_coeff);
147 }
148 current_var = entry.first;
149 current_coeff = entry.second;
150 }
151 }
152 if (current_coeff != 0) {
153 proto->add_vars(current_var);
154 proto->add_coeffs(current_coeff);
155 }
156}
157
158// Adds all the terms from the var definition constraint with given var
159// coefficient.
160void AddTermsFromVarDefinition(const int var, const int64_t var_coeff,
161 const ConstraintProto& definition,
162 std::vector<std::pair<int, int64_t>>* terms) {
163 const int definition_size = definition.linear().vars().size();
164 for (int i = 0; i < definition_size; ++i) {
165 int ref = definition.linear().vars(i);
166 int64_t coeff = definition.linear().coeffs(i);
167 if (!RefIsPositive(ref)) {
168 ref = NegatedRef(ref);
169 coeff = -coeff;
170 }
171
172 if (ref == var) {
173 continue;
174 } else {
175 terms->push_back({ref, -coeff * var_coeff});
176 }
177 }
178}
179} // namespace
180
181bool SubstituteVariable(int var, int64_t var_coeff_in_definition,
182 const ConstraintProto& definition,
185 CHECK_EQ(std::abs(var_coeff_in_definition), 1);
186
187 // Copy all the terms (except the one referring to var).
188 std::vector<std::pair<int, int64_t>> terms;
189 int64_t var_coeff = GetVarCoeffAndCopyOtherTerms(var, ct->linear(), &terms);
190 if (var_coeff == 0) return false;
191
192 if (var_coeff_in_definition < 0) var_coeff *= -1;
193
194 AddTermsFromVarDefinition(var, var_coeff, definition, &terms);
195
196 // The substitution is correct only if we don't loose information here.
197 // But for a constant definition rhs that is always the case.
198 bool exact = false;
199 Domain offset = ReadDomainFromProto(definition.linear());
200 offset = offset.MultiplicationBy(-var_coeff, &exact);
201 CHECK(exact);
202
203 const Domain rhs = ReadDomainFromProto(ct->linear());
204 FillDomainInProto(rhs.AdditionWith(offset), ct->mutable_linear());
205
206 SortAndMergeTerms(&terms, ct->mutable_linear());
207 return true;
208}
209
210} // namespace sat
211} // namespace operations_research
#define CHECK(condition)
Definition: base/logging.h:492
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:699
#define CHECK_GE(val1, val2)
Definition: base/logging.h:703
We call domain any subset of Int64 = [kint64min, kint64max].
bool IsIncludedIn(const Domain &domain) const
Returns true iff D is included in the given domain.
Domain AdditionWith(const Domain &domain) const
Returns {x ∈ Int64, ∃ a ∈ D, ∃ b ∈ domain, x = a + b}.
Domain MultiplicationBy(int64_t coeff, bool *exact=nullptr) const
Returns {x ∈ Int64, ∃ e ∈ D, x = e * coeff}.
Domain IntersectionWith(const Domain &domain) const
Returns the intersection of D and domain.
const ::operations_research::sat::LinearConstraintProto & linear() const
std::vector< std::pair< int, Domain > > ProcessClause(absl::Span< const int > clause)
void AddDeduction(int literal_ref, int var, Domain domain)
CpModelProto proto
const Constraint * ct
IntVar * var
Definition: expr_array.cc:1874
const Collection::value_type::second_type & FindOrDieNoPrint(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition: map_util.h:216
bool RefIsPositive(int ref)
void FillDomainInProto(const Domain &domain, ProtoWithDomain *proto)
Domain ReadDomainFromProto(const ProtoWithDomain &proto)
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.
int index
Definition: pack.cc:509