This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp_model_expand.cc
395 lines (359 loc) · 15 KB
/
cp_model_expand.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// 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_expand.h"
#include <map>
#include "absl/container/flat_hash_map.h"
#include "ortools/base/hash.h"
#include "ortools/base/map_util.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_presolve.h"
#include "ortools/sat/cp_model_utils.h"
#include "ortools/util/saturated_arithmetic.h"
namespace operations_research {
namespace sat {
namespace {
void ExpandReservoir(ConstraintProto* ct, PresolveContext* context) {
// TODO(user): Support sharing constraints in the model across constraints.
absl::flat_hash_map<std::pair<int, int>, int> precedence_cache;
const ReservoirConstraintProto& reservoir = ct->reservoir();
const int num_variables = reservoir.times_size();
auto is_optional = [&context, &reservoir](int index) {
if (reservoir.actives_size() == 0) return false;
const int literal = reservoir.actives(index);
const int ref = PositiveRef(literal);
const IntegerVariableProto& var_proto =
context->working_model->variables(ref);
return var_proto.domain_size() != 2 ||
var_proto.domain(0) != var_proto.domain(1);
};
const int true_literal = context->GetOrCreateConstantVar(1);
auto active = [&reservoir, true_literal](int index) {
if (reservoir.actives_size() == 0) return true_literal;
return reservoir.actives(index);
};
// x_lesseq_y <=> (x <= y && l_x is true && l_y is true).
const auto add_reified_precedence = [&context, true_literal](
int x_lesseq_y, int x, int y, int l_x,
int l_y) {
// x_lesseq_y => (x <= y) && l_x is true && l_y is true.
ConstraintProto* const lesseq = context->working_model->add_constraints();
lesseq->add_enforcement_literal(x_lesseq_y);
lesseq->mutable_linear()->add_vars(x);
lesseq->mutable_linear()->add_vars(y);
lesseq->mutable_linear()->add_coeffs(-1);
lesseq->mutable_linear()->add_coeffs(1);
lesseq->mutable_linear()->add_domain(0);
lesseq->mutable_linear()->add_domain(kint64max);
if (l_x != true_literal) {
context->AddImplication(x_lesseq_y, l_x);
}
if (l_y != true_literal) {
context->AddImplication(x_lesseq_y, l_y);
}
// Not(x_lesseq_y) && l_x && l_y => (x > y)
ConstraintProto* const greater = context->working_model->add_constraints();
greater->mutable_linear()->add_vars(x);
greater->mutable_linear()->add_vars(y);
greater->mutable_linear()->add_coeffs(-1);
greater->mutable_linear()->add_coeffs(1);
greater->mutable_linear()->add_domain(kint64min);
greater->mutable_linear()->add_domain(-1);
// Manages enforcement literal.
if (l_x == true_literal && l_y == true_literal) {
greater->add_enforcement_literal(NegatedRef(x_lesseq_y));
} else {
// conjunction <=> l_x && l_y && not(x_lesseq_y).
const int conjunction = context->NewBoolVar();
context->AddImplication(conjunction, NegatedRef(x_lesseq_y));
BoolArgumentProto* const bool_or =
context->working_model->add_constraints()->mutable_bool_or();
bool_or->add_literals(conjunction);
bool_or->add_literals(x_lesseq_y);
if (l_x != true_literal) {
context->AddImplication(conjunction, l_x);
bool_or->add_literals(NegatedRef(l_x));
}
if (l_y != true_literal) {
context->AddImplication(conjunction, l_y);
bool_or->add_literals(NegatedRef(l_y));
}
greater->add_enforcement_literal(conjunction);
}
};
int num_positives = 0;
int num_negatives = 0;
for (const int64 demand : reservoir.demands()) {
if (demand > 0) {
num_positives++;
} else if (demand < 0) {
num_negatives++;
}
}
if (num_positives > 0 && num_negatives > 0) {
// Creates Boolean variables equivalent to (start[i] <= start[j]) i != j
for (int i = 0; i < num_variables - 1; ++i) {
const int time_i = reservoir.times(i);
for (int j = i + 1; j < num_variables; ++j) {
const int time_j = reservoir.times(j);
const std::pair<int, int> p = std::make_pair(time_i, time_j);
const std::pair<int, int> rev_p = std::make_pair(time_j, time_i);
if (gtl::ContainsKey(precedence_cache, p)) continue;
const int i_lesseq_j = context->NewBoolVar();
precedence_cache[p] = i_lesseq_j;
const int j_lesseq_i = context->NewBoolVar();
precedence_cache[rev_p] = j_lesseq_i;
add_reified_precedence(i_lesseq_j, time_i, time_j, active(i),
active(j));
add_reified_precedence(j_lesseq_i, time_j, time_i, active(j),
active(i));
// Consistency. This is redundant but should improves performance.
auto* const bool_or =
context->working_model->add_constraints()->mutable_bool_or();
bool_or->add_literals(i_lesseq_j);
bool_or->add_literals(j_lesseq_i);
if (is_optional(i)) {
bool_or->add_literals(NegatedRef(reservoir.actives(i)));
}
if (is_optional(j)) {
bool_or->add_literals(NegatedRef(reservoir.actives(j)));
}
}
}
// Constrains the running level to be consistent at all times.
// For this we only add a constraint at the time a given demand
// take place. We also have a constraint for time zero if needed
// (added below).
for (int i = 0; i < num_variables; ++i) {
const int time_i = reservoir.times(i);
// Accumulates demands of all predecessors.
ConstraintProto* const level = context->working_model->add_constraints();
for (int j = 0; j < num_variables; ++j) {
if (i == j) continue;
const int time_j = reservoir.times(j);
level->mutable_linear()->add_vars(gtl::FindOrDieNoPrint(
precedence_cache, std::make_pair(time_j, time_i)));
level->mutable_linear()->add_coeffs(reservoir.demands(j));
}
// Accounts for own demand.
const int64 demand_i = reservoir.demands(i);
level->mutable_linear()->add_domain(
CapSub(reservoir.min_level(), demand_i));
level->mutable_linear()->add_domain(
CapSub(reservoir.max_level(), demand_i));
if (is_optional(i)) {
level->add_enforcement_literal(reservoir.actives(i));
}
}
} else {
// If all demands have the same sign, we do not care about the order, just
// the sum.
int64 fixed_demand = 0;
auto* const sum =
context->working_model->add_constraints()->mutable_linear();
for (int i = 0; i < num_variables; ++i) {
const int64 demand = reservoir.demands(i);
if (demand == 0) continue;
if (is_optional(i)) {
sum->add_vars(reservoir.actives(i));
sum->add_coeffs(demand);
} else {
fixed_demand += demand;
}
}
sum->add_domain(CapSub(reservoir.min_level(), fixed_demand));
sum->add_domain(CapSub(reservoir.max_level(), fixed_demand));
}
// Constrains the reservoir level to be consistent at time 0.
// We need to do it only if 0 is not in [min_level..max_level].
// Otherwise, the regular propagation will already check it.
if (reservoir.min_level() > 0 || reservoir.max_level() < 0) {
auto* const initial_ct =
context->working_model->add_constraints()->mutable_linear();
for (int i = 0; i < num_variables; ++i) {
const int time_i = reservoir.times(i);
const int lesseq_0 = context->NewBoolVar();
// lesseq_0 <=> (x <= 0 && lit is true).
context->AddImplyInDomain(lesseq_0, time_i, Domain(kint64min, 0));
if (active(i) == true_literal) {
context->AddImplyInDomain(NegatedRef(lesseq_0), time_i,
Domain(1, kint64max));
} else {
// conjunction <=> lit && not(lesseq_0).
const int conjunction = context->NewBoolVar();
context->AddImplication(conjunction, active(i));
context->AddImplication(conjunction, NegatedRef(lesseq_0));
BoolArgumentProto* const bool_or =
context->working_model->add_constraints()->mutable_bool_or();
bool_or->add_literals(NegatedRef(active(i)));
bool_or->add_literals(lesseq_0);
bool_or->add_literals(conjunction);
context->AddImplyInDomain(conjunction, time_i, Domain(1, kint64max));
}
initial_ct->add_vars(lesseq_0);
initial_ct->add_coeffs(reservoir.demands(i));
}
initial_ct->add_domain(reservoir.min_level());
initial_ct->add_domain(reservoir.max_level());
}
ct->Clear();
context->UpdateRuleStats("reservoir: expanded");
}
void ExpandIntMod(ConstraintProto* ct, PresolveContext* context) {
const IntegerArgumentProto& int_mod = ct->int_mod();
const IntegerVariableProto& var_proto =
context->working_model->variables(int_mod.vars(0));
const IntegerVariableProto& mod_proto =
context->working_model->variables(int_mod.vars(1));
const int target_var = int_mod.target();
const int64 mod_lb = mod_proto.domain(0);
CHECK_GE(mod_lb, 1);
const int64 mod_ub = mod_proto.domain(mod_proto.domain_size() - 1);
const int64 var_lb = var_proto.domain(0);
const int64 var_ub = var_proto.domain(var_proto.domain_size() - 1);
// Compute domains of var / mod_proto.
const int div_var =
context->NewIntVar(Domain(var_lb / mod_ub, var_ub / mod_lb));
auto add_enforcement_literal_if_needed = [&]() {
if (ct->enforcement_literal_size() == 0) return;
const int literal = ct->enforcement_literal(0);
ConstraintProto* const last = context->working_model->mutable_constraints(
context->working_model->constraints_size() - 1);
last->add_enforcement_literal(literal);
};
// div = var / mod.
IntegerArgumentProto* const div_proto =
context->working_model->add_constraints()->mutable_int_div();
div_proto->set_target(div_var);
div_proto->add_vars(int_mod.vars(0));
div_proto->add_vars(int_mod.vars(1));
add_enforcement_literal_if_needed();
// Checks if mod is constant.
if (mod_lb == mod_ub) {
// var - div_var * mod = target.
LinearConstraintProto* const lin =
context->working_model->add_constraints()->mutable_linear();
lin->add_vars(int_mod.vars(0));
lin->add_coeffs(1);
lin->add_vars(div_var);
lin->add_coeffs(-mod_lb);
lin->add_vars(target_var);
lin->add_coeffs(-1);
lin->add_domain(0);
lin->add_domain(0);
add_enforcement_literal_if_needed();
} else {
// Create prod_var = div_var * mod.
const int mod_var = int_mod.vars(1);
const int prod_var = context->NewIntVar(
Domain(var_lb * mod_lb / mod_ub, var_ub * mod_ub / mod_lb));
IntegerArgumentProto* const int_prod =
context->working_model->add_constraints()->mutable_int_prod();
int_prod->set_target(prod_var);
int_prod->add_vars(div_var);
int_prod->add_vars(mod_var);
add_enforcement_literal_if_needed();
// var - prod_var = target.
LinearConstraintProto* const lin =
context->working_model->add_constraints()->mutable_linear();
lin->add_vars(int_mod.vars(0));
lin->add_coeffs(1);
lin->add_vars(prod_var);
lin->add_coeffs(-1);
lin->add_vars(target_var);
lin->add_coeffs(-1);
lin->add_domain(0);
lin->add_domain(0);
add_enforcement_literal_if_needed();
}
ct->Clear();
context->UpdateRuleStats("int_mod: expanded");
}
void ExpandIntProdWithBoolean(int bool_ref, int int_ref, int product_ref,
PresolveContext* context) {
ConstraintProto* const one = context->working_model->add_constraints();
one->add_enforcement_literal(bool_ref);
one->mutable_linear()->add_vars(int_ref);
one->mutable_linear()->add_coeffs(1);
one->mutable_linear()->add_vars(product_ref);
one->mutable_linear()->add_coeffs(-1);
one->mutable_linear()->add_domain(0);
one->mutable_linear()->add_domain(0);
ConstraintProto* const zero = context->working_model->add_constraints();
zero->add_enforcement_literal(NegatedRef(bool_ref));
zero->mutable_linear()->add_vars(product_ref);
zero->mutable_linear()->add_coeffs(1);
zero->mutable_linear()->add_domain(0);
zero->mutable_linear()->add_domain(0);
}
void ExpandIntProd(ConstraintProto* ct, PresolveContext* context) {
const IntegerArgumentProto& int_prod = ct->int_prod();
if (int_prod.vars_size() != 2) return;
const int a = int_prod.vars(0);
const int b = int_prod.vars(1);
const IntegerVariableProto& a_proto =
context->working_model->variables(PositiveRef(a));
const IntegerVariableProto& b_proto =
context->working_model->variables(PositiveRef(b));
const int p = int_prod.target();
const bool a_is_boolean = RefIsPositive(a) && a_proto.domain_size() == 2 &&
a_proto.domain(0) == 0 && a_proto.domain(1) == 1;
const bool b_is_boolean = RefIsPositive(b) && b_proto.domain_size() == 2 &&
b_proto.domain(0) == 0 && b_proto.domain(1) == 1;
// We expand if exactly one of {a, b} is Boolean. If both are Boolean, it
// will be presolved into a better version.
if (a_is_boolean && !b_is_boolean) {
ExpandIntProdWithBoolean(a, b, p, context);
ct->Clear();
context->UpdateRuleStats("int_prod: expanded product with Boolean var");
} else if (b_is_boolean && !a_is_boolean) {
ExpandIntProdWithBoolean(b, a, p, context);
ct->Clear();
context->UpdateRuleStats("int_prod: expanded product with Boolean var");
}
}
} // namespace
void ExpandCpModel(CpModelProto* working_model, PresolveOptions options) {
PresolveContext context;
context.working_model = working_model;
const int num_constraints = context.working_model->constraints_size();
for (int i = 0; i < num_constraints; ++i) {
ConstraintProto* const ct = context.working_model->mutable_constraints(i);
switch (ct->constraint_case()) {
case ConstraintProto::ConstraintCase::kReservoir:
ExpandReservoir(ct, &context);
break;
case ConstraintProto::ConstraintCase::kIntMod:
ExpandIntMod(ct, &context);
break;
case ConstraintProto::ConstraintCase::kIntProd:
ExpandIntProd(ct, &context);
break;
default:
break;
}
}
if (options.log_info) {
std::map<std::string, int> sorted_rules(context.stats_by_rule_name.begin(),
context.stats_by_rule_name.end());
for (const auto& entry : sorted_rules) {
if (entry.second == 1) {
LOG(INFO) << "- rule '" << entry.first << "' was applied 1 time.";
} else {
LOG(INFO) << "- rule '" << entry.first << "' was applied "
<< entry.second << " times.";
}
}
}
}
} // namespace sat
} // namespace operations_research