forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_model_postsolve.cc
282 lines (258 loc) · 10.4 KB
/
cp_model_postsolve.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/sat/cp_model_postsolve.h"
#include "ortools/sat/cp_model_utils.h"
namespace operations_research {
namespace sat {
// This postsolve is "special". If the clause is not satisfied, we fix the
// first literal in the clause to true (even if it was fixed to false). This
// allows to handle more complex presolve operations used by the SAT presolver.
//
// Also, any "free" Boolean should be fixed to some value for the subsequent
// postsolve steps.
void PostsolveClause(const ConstraintProto& ct, std::vector<Domain>* domains) {
const int size = ct.bool_or().literals_size();
CHECK_NE(size, 0);
bool satisfied = false;
for (int i = 0; i < size; ++i) {
const int ref = ct.bool_or().literals(i);
const int var = PositiveRef(ref);
if ((*domains)[var].IsFixed()) {
if ((*domains)[var].FixedValue() == (RefIsPositive(ref) ? 1 : 0)) {
satisfied = true;
}
} else {
// We still need to assign free variable. Any value should work.
(*domains)[PositiveRef(ref)] = Domain(0);
}
}
if (satisfied) return;
// Change the value of the first variable (which was chosen at presolve).
const int first_ref = ct.bool_or().literals(0);
(*domains)[PositiveRef(first_ref)] = Domain(RefIsPositive(first_ref) ? 1 : 0);
}
// Here we simply assign all non-fixed variable to a feasible value. Which
// should always exists by construction.
void PostsolveLinear(const ConstraintProto& ct,
const std::vector<bool>& prefer_lower_value,
std::vector<Domain>* domains) {
int64 fixed_activity = 0;
const int size = ct.linear().vars().size();
std::vector<int> free_vars;
std::vector<int64> free_coeffs;
for (int i = 0; i < size; ++i) {
const int var = ct.linear().vars(i);
const int64 coeff = ct.linear().coeffs(i);
CHECK_LT(var, domains->size());
if (coeff == 0) continue;
if ((*domains)[var].IsFixed()) {
fixed_activity += (*domains)[var].FixedValue() * coeff;
} else {
free_vars.push_back(var);
free_coeffs.push_back(coeff);
}
}
if (free_vars.empty()) return;
Domain rhs =
ReadDomainFromProto(ct.linear()).AdditionWith(Domain(-fixed_activity));
// Fast track for the most common case.
if (free_vars.size() == 1) {
const int var = free_vars[0];
const Domain domain = rhs.InverseMultiplicationBy(free_coeffs[0])
.IntersectionWith((*domains)[var]);
const int64 value = prefer_lower_value[var] ? domain.Min() : domain.Max();
(*domains)[var] = Domain(value);
return;
}
// The postsolve code is a bit involved if there is more than one free
// variable, we have to postsolve them one by one.
std::vector<Domain> to_add;
to_add.push_back(Domain(0));
for (int i = 0; i + 1 < free_vars.size(); ++i) {
bool exact = false;
Domain term =
(*domains)[free_vars[i]].MultiplicationBy(-free_coeffs[i], &exact);
CHECK(exact);
to_add.push_back(term.AdditionWith(to_add.back()));
}
for (int i = free_vars.size() - 1; i >= 0; --i) {
// Choose a value for free_vars[i] that fall into rhs + to_add[i].
// This will crash if the intersection is empty, but it shouldn't be.
const int var = free_vars[i];
const int64 coeff = free_coeffs[i];
const Domain domain = rhs.AdditionWith(to_add[i])
.InverseMultiplicationBy(coeff)
.IntersectionWith((*domains)[var]);
CHECK(!domain.IsEmpty()) << ct.ShortDebugString();
const int64 value = prefer_lower_value[var] ? domain.Min() : domain.Max();
(*domains)[var] = Domain(value);
rhs = rhs.AdditionWith(Domain(-coeff * value));
// Only needed in debug.
fixed_activity += coeff * value;
}
DCHECK(ReadDomainFromProto(ct.linear()).Contains(fixed_activity));
}
// We assign any non fixed lhs variables to their minimum value. Then we assign
// the target to the max. This should always be feasible.
void PostsolveIntMax(const ConstraintProto& ct, std::vector<Domain>* domains) {
int64 m = kint64min;
for (const int ref : ct.int_max().vars()) {
const int var = PositiveRef(ref);
if (!(*domains)[var].IsFixed()) {
// Assign to minimum value.
const int64 value =
RefIsPositive(ref) ? (*domains)[var].Min() : (*domains)[var].Max();
(*domains)[var] = Domain(value);
}
const int64 value = (*domains)[var].FixedValue();
m = std::max(m, RefIsPositive(ref) ? value : -value);
}
const int target_ref = ct.int_max().target();
const int target_var = PositiveRef(target_ref);
if (RefIsPositive(target_ref)) {
(*domains)[target_var] = (*domains)[target_var].IntersectionWith(Domain(m));
} else {
(*domains)[target_var] =
(*domains)[target_var].IntersectionWith(Domain(-m));
}
CHECK(!(*domains)[target_var].IsEmpty());
}
// We only support two cases in the presolve currently.
void PostsolveElement(const ConstraintProto& ct, std::vector<Domain>* domains) {
const int index_ref = ct.element().index();
const int index_var = PositiveRef(index_ref);
const int target_ref = ct.element().target();
const int target_var = PositiveRef(target_ref);
// Deal with fixed index (and constant vars).
if ((*domains)[PositiveRef(index_ref)].IsFixed()) {
const int64 index_value = (*domains)[index_var].FixedValue();
const int selected_ref = ct.element().vars(
RefIsPositive(index_ref) ? index_value : -index_value);
const int selected_var = PositiveRef(selected_ref);
const int64 selected_value = (*domains)[selected_var].FixedValue();
(*domains)[target_var] = (*domains)[target_var].IntersectionWith(
Domain(RefIsPositive(target_ref) == RefIsPositive(selected_ref)
? selected_value
: -selected_value));
return;
}
// Deal with fixed target (and constant vars).
const int64 target_value = (*domains)[target_var].FixedValue();
int selected_index_value = -1;
for (int i = 0; i < ct.element().vars().size(); ++i) {
const int ref = ct.element().vars(i);
const int var = PositiveRef(ref);
const int64 value = (*domains)[var].FixedValue();
if (RefIsPositive(target_ref) == RefIsPositive(ref)) {
if (value == target_value) {
selected_index_value = i;
break;
}
} else {
if (value == -target_value) {
selected_index_value = i;
break;
}
}
}
CHECK_NE(selected_index_value, -1);
(*domains)[index_var] = (*domains)[index_var].IntersectionWith(Domain(
RefIsPositive(index_var) ? selected_index_value : -selected_index_value));
}
void PostsolveResponse(const int64 num_variables_in_original_model,
const CpModelProto& mapping_proto,
const std::vector<int>& postsolve_mapping,
CpSolverResponse* response) {
// Abort if no solution or something is wrong.
if (response->status() != CpSolverStatus::FEASIBLE &&
response->status() != CpSolverStatus::OPTIMAL) {
return;
}
if (response->solution_size() != postsolve_mapping.size()) return;
// Read the initial variable domains, either from the fixed solution of the
// presolved problems or from the mapping model.
std::vector<Domain> domains(mapping_proto.variables_size());
for (int i = 0; i < postsolve_mapping.size(); ++i) {
CHECK_LE(postsolve_mapping[i], domains.size());
domains[postsolve_mapping[i]] = Domain(response->solution(i));
}
for (int i = 0; i < domains.size(); ++i) {
if (domains[i].IsEmpty()) {
domains[i] = ReadDomainFromProto(mapping_proto.variables(i));
}
CHECK(!domains[i].IsEmpty());
}
// Some free variable should be fixed towards their good objective direction.
//
// TODO(user): currently the objective is not part of the mapping_proto, so
// this shouldn't matter for our current presolve reduction.
CHECK(!mapping_proto.has_objective());
std::vector<bool> prefer_lower_value(domains.size(), true);
if (mapping_proto.has_objective()) {
const int size = mapping_proto.objective().vars().size();
for (int i = 0; i < size; ++i) {
int var = mapping_proto.objective().vars(i);
int64 coeff = mapping_proto.objective().coeffs(i);
if (!RefIsPositive(var)) {
var = PositiveRef(var);
coeff = -coeff;
}
prefer_lower_value[i] = (coeff >= 0);
}
}
// Process the constraints in reverse order.
const int num_constraints = mapping_proto.constraints_size();
for (int i = num_constraints - 1; i >= 0; i--) {
const ConstraintProto& ct = mapping_proto.constraints(i);
// We should only encounter assigned enforcement literal.
bool enforced = true;
for (const int ref : ct.enforcement_literal()) {
if (domains[PositiveRef(ref)].FixedValue() ==
(RefIsPositive(ref) ? 0 : 1)) {
enforced = false;
break;
}
}
if (!enforced) continue;
switch (ct.constraint_case()) {
case ConstraintProto::kBoolOr:
PostsolveClause(ct, &domains);
break;
case ConstraintProto::kLinear:
PostsolveLinear(ct, prefer_lower_value, &domains);
break;
case ConstraintProto::kIntMax:
PostsolveIntMax(ct, &domains);
break;
case ConstraintProto::kElement:
PostsolveElement(ct, &domains);
break;
default:
// This should never happen as we control what kind of constraint we
// add to the mapping_proto;
LOG(FATAL) << "Unsupported constraint: " << ct.ShortDebugString();
}
}
// Fill the response. Maybe fix some still unfixed variable.
response->mutable_solution()->Clear();
CHECK_LE(num_variables_in_original_model, domains.size());
for (int i = 0; i < num_variables_in_original_model; ++i) {
if (prefer_lower_value[i]) {
response->add_solution(domains[i].Min());
} else {
response->add_solution(domains[i].Max());
}
}
}
} // namespace sat
} // namespace operations_research