forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_model_expand.cc
1593 lines (1416 loc) · 59 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010-2021 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 <algorithm>
#include <cstdint>
#include <limits>
#include <map>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "ortools/base/hash.h"
#include "ortools/base/map_util.h"
#include "ortools/base/stl_util.h"
#include "ortools/port/proto_utils.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_utils.h"
#include "ortools/sat/presolve_context.h"
#include "ortools/sat/util.h"
#include "ortools/util/saturated_arithmetic.h"
#include "ortools/util/sorted_interval_list.h"
namespace operations_research {
namespace sat {
namespace {
void ExpandReservoir(ConstraintProto* ct, PresolveContext* context) {
if (ct->reservoir().min_level() > ct->reservoir().max_level()) {
VLOG(1) << "Empty level domain in reservoir constraint.";
return (void)context->NotifyThatModelIsUnsat();
}
const ReservoirConstraintProto& reservoir = ct->reservoir();
const int num_events = reservoir.time_exprs_size();
const int true_literal = context->GetOrCreateConstantVar(1);
const auto is_active_literal = [&reservoir, true_literal](int index) {
if (reservoir.active_literals_size() == 0) return true_literal;
return reservoir.active_literals(index);
};
int num_positives = 0;
int num_negatives = 0;
for (const int64_t demand : reservoir.level_changes()) {
if (demand > 0) {
num_positives++;
} else if (demand < 0) {
num_negatives++;
}
}
absl::flat_hash_map<std::pair<int, int>, int> precedence_cache;
if (num_positives > 0 && num_negatives > 0) {
// Creates Boolean variables equivalent to (start[i] <= start[j]) i != j
for (int i = 0; i < num_events - 1; ++i) {
const int active_i = is_active_literal(i);
if (context->LiteralIsFalse(active_i)) continue;
const LinearExpressionProto& time_i = reservoir.time_exprs(i);
for (int j = i + 1; j < num_events; ++j) {
const int active_j = is_active_literal(j);
if (context->LiteralIsFalse(active_j)) continue;
const LinearExpressionProto& time_j = reservoir.time_exprs(j);
const int i_lesseq_j = context->GetOrCreateReifiedPrecedenceLiteral(
time_i, time_j, active_i, active_j);
context->working_model->mutable_variables(i_lesseq_j)
->set_name(absl::StrCat(i, " before ", j));
precedence_cache[{i, j}] = i_lesseq_j;
const int j_lesseq_i = context->GetOrCreateReifiedPrecedenceLiteral(
time_j, time_i, active_j, active_i);
context->working_model->mutable_variables(j_lesseq_i)
->set_name(absl::StrCat(j, " before ", i));
precedence_cache[{j, i}] = j_lesseq_i;
}
}
// Constrains the running level to be consistent at all time_exprs.
// 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_events; ++i) {
const int active_i = is_active_literal(i);
if (context->LiteralIsFalse(active_i)) continue;
// Accumulates level_changes of all predecessors.
ConstraintProto* const level = context->working_model->add_constraints();
level->add_enforcement_literal(active_i);
// Add contributions from previous events.
int64_t offset = 0;
for (int j = 0; j < num_events; ++j) {
if (i == j) continue;
const int active_j = is_active_literal(j);
if (context->LiteralIsFalse(active_j)) continue;
const auto prec_it = precedence_cache.find({j, i});
CHECK(prec_it != precedence_cache.end());
const int prec_lit = prec_it->second;
const int64_t demand = reservoir.level_changes(j);
if (RefIsPositive(prec_lit)) {
level->mutable_linear()->add_vars(prec_lit);
level->mutable_linear()->add_coeffs(demand);
} else {
level->mutable_linear()->add_vars(prec_lit);
level->mutable_linear()->add_coeffs(-demand);
offset -= demand;
}
}
// Accounts for own demand in the domain of the sum.
const int64_t demand_i = reservoir.level_changes(i);
level->mutable_linear()->add_domain(
CapAdd(CapSub(reservoir.min_level(), demand_i), offset));
level->mutable_linear()->add_domain(
CapAdd(CapSub(reservoir.max_level(), demand_i), offset));
}
} else {
// If all level_changes have the same sign, we do not care about the order,
// just the sum.
auto* const sum =
context->working_model->add_constraints()->mutable_linear();
for (int i = 0; i < num_events; ++i) {
sum->add_vars(is_active_literal(i));
sum->add_coeffs(reservoir.level_changes(i));
}
sum->add_domain(reservoir.min_level());
sum->add_domain(reservoir.max_level());
}
ct->Clear();
context->UpdateRuleStats("reservoir: expanded");
}
void ExpandIntMod(ConstraintProto* ct, PresolveContext* context) {
const LinearArgumentProto& int_mod = ct->int_mod();
const LinearExpressionProto& mod_expr = int_mod.exprs(1);
if (context->IsFixed(mod_expr)) return;
const LinearExpressionProto& expr = int_mod.exprs(0);
const LinearExpressionProto& target_expr = int_mod.target();
// We reduce the domain of target_expr to avoid later overflow.
if (!context->IntersectDomainWith(
target_expr, context->DomainSuperSetOf(expr).PositiveModuloBySuperset(
context->DomainSuperSetOf(mod_expr)))) {
return;
}
// Create a new constraint with the same enforcement as ct.
auto new_enforced_constraint = [&]() {
ConstraintProto* new_ct = context->working_model->add_constraints();
*new_ct->mutable_enforcement_literal() = ct->enforcement_literal();
return new_ct;
};
// div_expr = expr / mod_expr.
const int div_var = context->NewIntVar(
context->DomainSuperSetOf(expr).PositiveDivisionBySuperset(
context->DomainSuperSetOf(mod_expr)));
LinearExpressionProto div_expr;
div_expr.add_vars(div_var);
div_expr.add_coeffs(1);
LinearArgumentProto* const div_proto =
new_enforced_constraint()->mutable_int_div();
*div_proto->mutable_target() = div_expr;
*div_proto->add_exprs() = expr;
*div_proto->add_exprs() = mod_expr;
// Create prod_expr = div_expr * mod_expr.
const Domain prod_domain =
context->DomainOf(div_var)
.ContinuousMultiplicationBy(context->DomainSuperSetOf(mod_expr))
.IntersectionWith(context->DomainSuperSetOf(expr).AdditionWith(
context->DomainSuperSetOf(target_expr).Negation()));
const int prod_var = context->NewIntVar(prod_domain);
LinearExpressionProto prod_expr;
prod_expr.add_vars(prod_var);
prod_expr.add_coeffs(1);
LinearArgumentProto* const int_prod =
new_enforced_constraint()->mutable_int_prod();
*int_prod->mutable_target() = prod_expr;
*int_prod->add_exprs() = div_expr;
*int_prod->add_exprs() = mod_expr;
// expr - prod_expr = target_expr.
LinearConstraintProto* const lin =
new_enforced_constraint()->mutable_linear();
lin->add_domain(0);
lin->add_domain(0);
AddLinearExpressionToLinearConstraint(expr, 1, lin);
AddLinearExpressionToLinearConstraint(prod_expr, -1, lin);
AddLinearExpressionToLinearConstraint(target_expr, -1, lin);
ct->Clear();
context->UpdateRuleStats("int_mod: expanded");
}
// TODO(user): Move this into the presolve instead?
void ExpandIntProdWithBoolean(int bool_ref,
const LinearExpressionProto& int_expr,
const LinearExpressionProto& product_expr,
PresolveContext* context) {
ConstraintProto* const one = context->working_model->add_constraints();
one->add_enforcement_literal(bool_ref);
one->mutable_linear()->add_domain(0);
one->mutable_linear()->add_domain(0);
AddLinearExpressionToLinearConstraint(int_expr, 1, one->mutable_linear());
AddLinearExpressionToLinearConstraint(product_expr, -1,
one->mutable_linear());
ConstraintProto* const zero = context->working_model->add_constraints();
zero->add_enforcement_literal(NegatedRef(bool_ref));
zero->mutable_linear()->add_domain(0);
zero->mutable_linear()->add_domain(0);
AddLinearExpressionToLinearConstraint(product_expr, 1,
zero->mutable_linear());
}
void ExpandIntProd(ConstraintProto* ct, PresolveContext* context) {
const LinearArgumentProto& int_prod = ct->int_prod();
if (int_prod.exprs_size() != 2) return;
const LinearExpressionProto& a = int_prod.exprs(0);
const LinearExpressionProto& b = int_prod.exprs(1);
const LinearExpressionProto& p = int_prod.target();
int literal;
const bool a_is_literal = context->ExpressionIsALiteral(a, &literal);
const bool b_is_literal = context->ExpressionIsALiteral(b, &literal);
// We expand if exactly one of {a, b} is a literal. If both are literals, it
// will be presolved into a better version.
if (a_is_literal && !b_is_literal) {
ExpandIntProdWithBoolean(literal, b, p, context);
ct->Clear();
context->UpdateRuleStats("int_prod: expanded product with Boolean var");
} else if (b_is_literal) {
ExpandIntProdWithBoolean(literal, a, p, context);
ct->Clear();
context->UpdateRuleStats("int_prod: expanded product with Boolean var");
}
}
void ExpandInverse(ConstraintProto* ct, PresolveContext* context) {
const auto& f_direct = ct->inverse().f_direct();
const auto& f_inverse = ct->inverse().f_inverse();
const int n = f_direct.size();
CHECK_EQ(n, f_inverse.size());
// Make sure the domains are included in [0, n - 1).
// Note that if a variable and its negation appear, the domains will be set to
// zero here.
//
// TODO(user): Add support for UNSAT at expansion. This should create empty
// domain if UNSAT, so it should still work correctly.
absl::flat_hash_set<int> used_variables;
for (const int ref : f_direct) {
used_variables.insert(PositiveRef(ref));
if (!context->IntersectDomainWith(ref, Domain(0, n - 1))) {
VLOG(1) << "Empty domain for a variable in ExpandInverse()";
return;
}
}
for (const int ref : f_inverse) {
used_variables.insert(PositiveRef(ref));
if (!context->IntersectDomainWith(ref, Domain(0, n - 1))) {
VLOG(1) << "Empty domain for a variable in ExpandInverse()";
return;
}
}
// If we have duplicate variables, we make sure the domain are reduced
// as the loop below might not detect incompatibilities.
if (used_variables.size() != 2 * n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
// Note that if we don't have the same sign, both domain are at zero.
if (PositiveRef(f_direct[i]) != PositiveRef(f_inverse[j])) continue;
// We can't have i or j as value if i != j.
if (i == j) continue;
if (!context->IntersectDomainWith(
f_direct[i], Domain::FromValues({i, j}).Complement())) {
return;
}
}
}
}
// Reduce the domains of each variable by checking that the inverse value
// exists.
std::vector<int64_t> possible_values;
// Propagate from one vector to its counterpart.
const auto filter_inverse_domain =
[context, n, &possible_values](const auto& direct, const auto& inverse) {
// Propagate from the inverse vector to the direct vector.
for (int i = 0; i < n; ++i) {
possible_values.clear();
const Domain domain = context->DomainOf(direct[i]);
bool removed_value = false;
for (const int64_t j : domain.Values()) {
if (context->DomainOf(inverse[j]).Contains(i)) {
possible_values.push_back(j);
} else {
removed_value = true;
}
}
if (removed_value) {
if (!context->IntersectDomainWith(
direct[i], Domain::FromValues(possible_values))) {
VLOG(1) << "Empty domain for a variable in ExpandInverse()";
return false;
}
}
}
return true;
};
// Note that this should reach the fixed point in one pass.
// However, if we have duplicate variable, I am not sure.
if (!filter_inverse_domain(f_direct, f_inverse)) return;
if (!filter_inverse_domain(f_inverse, f_direct)) return;
// Expand the inverse constraint by associating literal to var == value
// and sharing them between the direct and inverse variables.
//
// Note that this is only correct because the domain are tight now.
for (int i = 0; i < n; ++i) {
const int f_i = f_direct[i];
for (const int64_t j : context->DomainOf(f_i).Values()) {
// We have f[i] == j <=> r[j] == i;
const int r_j = f_inverse[j];
int r_j_i;
if (context->HasVarValueEncoding(r_j, i, &r_j_i)) {
context->InsertVarValueEncoding(r_j_i, f_i, j);
} else {
const int f_i_j = context->GetOrCreateVarValueEncoding(f_i, j);
context->InsertVarValueEncoding(f_i_j, r_j, i);
}
}
}
ct->Clear();
context->UpdateRuleStats("inverse: expanded");
}
// A[V] == V means for all i, V == i => A_i == i
void ExpandElementWithTargetEqualIndex(ConstraintProto* ct,
PresolveContext* context) {
const ElementConstraintProto& element = ct->element();
DCHECK_EQ(element.index(), element.target());
const int index_ref = element.index();
std::vector<int64_t> valid_indices;
for (const int64_t v : context->DomainOf(index_ref).Values()) {
if (!context->DomainContains(element.vars(v), v)) continue;
valid_indices.push_back(v);
}
if (valid_indices.size() < context->DomainOf(index_ref).Size()) {
if (!context->IntersectDomainWith(index_ref,
Domain::FromValues(valid_indices))) {
VLOG(1) << "No compatible variable domains in "
"ExpandElementWithTargetEqualIndex()";
return;
}
context->UpdateRuleStats("element: reduced index domain");
}
for (const int64_t v : context->DomainOf(index_ref).Values()) {
const int var = element.vars(v);
if (context->MinOf(var) == v && context->MaxOf(var) == v) continue;
context->AddImplyInDomain(
context->GetOrCreateVarValueEncoding(index_ref, v), var, Domain(v));
}
context->UpdateRuleStats(
"element: expanded with special case target = index");
ct->Clear();
}
// Special case if the array of the element is filled with constant values.
void ExpandConstantArrayElement(ConstraintProto* ct, PresolveContext* context) {
const ElementConstraintProto& element = ct->element();
const int index_ref = element.index();
const int target_ref = element.target();
// Index and target domain have been reduced before calling this function.
const Domain index_domain = context->DomainOf(index_ref);
const Domain target_domain = context->DomainOf(target_ref);
// This BoolOrs implements the deduction that if all index literals pointing
// to the same value in the constant array are false, then this value is no
// no longer valid for the target variable. They are created only for values
// that have multiples literals supporting them.
// Order is not important.
absl::flat_hash_map<int64_t, BoolArgumentProto*> supports;
{
absl::flat_hash_map<int64_t, int> constant_var_values_usage;
for (const int64_t v : index_domain.Values()) {
DCHECK(context->IsFixed(element.vars(v)));
const int64_t value = context->MinOf(element.vars(v));
if (++constant_var_values_usage[value] == 2) {
// First time we cross > 1.
BoolArgumentProto* const support =
context->working_model->add_constraints()->mutable_bool_or();
const int target_literal =
context->GetOrCreateVarValueEncoding(target_ref, value);
support->add_literals(NegatedRef(target_literal));
supports[value] = support;
}
}
}
{
// While this is not stricly needed since all value in the index will be
// covered, it allows to easily detect this fact in the presolve.
auto* exactly_one =
context->working_model->add_constraints()->mutable_exactly_one();
for (const int64_t v : index_domain.Values()) {
const int index_literal =
context->GetOrCreateVarValueEncoding(index_ref, v);
exactly_one->add_literals(index_literal);
const int64_t value = context->MinOf(element.vars(v));
const auto& it = supports.find(value);
if (it != supports.end()) {
// The encoding literal for 'value' of the target_ref has been
// created before.
const int target_literal =
context->GetOrCreateVarValueEncoding(target_ref, value);
context->AddImplication(index_literal, target_literal);
it->second->add_literals(index_literal);
} else {
// Try to reuse the literal of the index.
context->InsertVarValueEncoding(index_literal, target_ref, value);
}
}
}
context->UpdateRuleStats("element: expanded value element");
ct->Clear();
}
// General element when the array contains non fixed variables.
void ExpandVariableElement(ConstraintProto* ct, PresolveContext* context) {
const ElementConstraintProto& element = ct->element();
const int index_ref = element.index();
const int target_ref = element.target();
const Domain index_domain = context->DomainOf(index_ref);
BoolArgumentProto* bool_or =
context->working_model->add_constraints()->mutable_bool_or();
for (const int64_t v : index_domain.Values()) {
const int var = element.vars(v);
const Domain var_domain = context->DomainOf(var);
const int index_lit = context->GetOrCreateVarValueEncoding(index_ref, v);
bool_or->add_literals(index_lit);
if (var_domain.IsFixed()) {
context->AddImplyInDomain(index_lit, target_ref, var_domain);
} else {
ConstraintProto* const ct = context->working_model->add_constraints();
ct->add_enforcement_literal(index_lit);
ct->mutable_linear()->add_vars(var);
ct->mutable_linear()->add_coeffs(1);
ct->mutable_linear()->add_vars(target_ref);
ct->mutable_linear()->add_coeffs(-1);
ct->mutable_linear()->add_domain(0);
ct->mutable_linear()->add_domain(0);
}
}
context->UpdateRuleStats("element: expanded");
ct->Clear();
}
void ExpandElement(ConstraintProto* ct, PresolveContext* context) {
const ElementConstraintProto& element = ct->element();
const int index_ref = element.index();
const int target_ref = element.target();
const int size = element.vars_size();
// Reduce the domain of the index to be compatible with the array of
// variables. Note that the element constraint is 0 based.
if (!context->IntersectDomainWith(index_ref, Domain(0, size - 1))) {
VLOG(1) << "Empty domain for the index variable in ExpandElement()";
return;
}
// Special case when index = target.
if (index_ref == target_ref) {
ExpandElementWithTargetEqualIndex(ct, context);
return;
}
// Reduces the domain of the index and the target.
bool all_constants = true;
std::vector<int64_t> valid_indices;
const Domain index_domain = context->DomainOf(index_ref);
const Domain target_domain = context->DomainOf(target_ref);
Domain reached_domain;
for (const int64_t v : index_domain.Values()) {
const Domain var_domain = context->DomainOf(element.vars(v));
if (var_domain.IntersectionWith(target_domain).IsEmpty()) continue;
valid_indices.push_back(v);
reached_domain = reached_domain.UnionWith(var_domain);
if (var_domain.Min() != var_domain.Max()) {
all_constants = false;
}
}
if (valid_indices.size() < index_domain.Size()) {
if (!context->IntersectDomainWith(index_ref,
Domain::FromValues(valid_indices))) {
VLOG(1) << "No compatible variable domains in ExpandElement()";
return;
}
context->UpdateRuleStats("element: reduced index domain");
}
// We know the target_domain is not empty as this would have triggered the
// above check.
bool target_domain_changed = false;
if (!context->IntersectDomainWith(target_ref, reached_domain,
&target_domain_changed)) {
return;
}
if (target_domain_changed) {
context->UpdateRuleStats("element: reduced target domain");
}
if (all_constants) {
ExpandConstantArrayElement(ct, context);
return;
}
ExpandVariableElement(ct, context);
}
// Adds clauses so that literals[i] true <=> encoding[values[i]] true.
// This also implicitly use the fact that exactly one alternative is true.
void LinkLiteralsAndValues(const std::vector<int>& literals,
const std::vector<int64_t>& values,
const absl::flat_hash_map<int64_t, int>& encoding,
PresolveContext* context) {
CHECK_EQ(literals.size(), values.size());
// We use a map to make this method deterministic.
//
// TODO(user): Make sure this does not appear in the profile. We could use
// the same code as in ProcessOneVariable() otherwise.
std::map<int, std::vector<int>> encoding_lit_to_support;
// If a value is false (i.e not possible), then the tuple with this
// value is false too (i.e not possible). Conversely, if the tuple is
// selected, the value must be selected.
for (int i = 0; i < values.size(); ++i) {
encoding_lit_to_support[encoding.at(values[i])].push_back(literals[i]);
}
// If all tuples supporting a value are false, then this value must be
// false.
for (const auto& [encoding_lit, support] : encoding_lit_to_support) {
CHECK(!support.empty());
if (support.size() == 1) {
context->StoreBooleanEqualityRelation(encoding_lit, support[0]);
} else {
BoolArgumentProto* bool_or =
context->working_model->add_constraints()->mutable_bool_or();
bool_or->add_literals(NegatedRef(encoding_lit));
for (const int lit : support) {
bool_or->add_literals(lit);
context->AddImplication(lit, encoding_lit);
}
}
}
}
// Add the constraint literal => one_of(encoding[v]), for v in reachable_values.
// Note that all possible values are the ones appearing in encoding.
void AddImplyInReachableValues(int literal,
std::vector<int64_t>& reachable_values,
const absl::flat_hash_map<int64_t, int> encoding,
PresolveContext* context) {
gtl::STLSortAndRemoveDuplicates(&reachable_values);
if (reachable_values.size() == encoding.size()) return; // No constraint.
if (reachable_values.size() <= encoding.size() / 2) {
// Bool or encoding.
ConstraintProto* ct = context->working_model->add_constraints();
ct->add_enforcement_literal(literal);
BoolArgumentProto* bool_or = ct->mutable_bool_or();
for (const int64_t v : reachable_values) {
bool_or->add_literals(encoding.at(v));
}
} else {
// Bool and encoding.
absl::flat_hash_set<int64_t> set(reachable_values.begin(),
reachable_values.end());
ConstraintProto* ct = context->working_model->add_constraints();
ct->add_enforcement_literal(literal);
BoolArgumentProto* bool_and = ct->mutable_bool_and();
for (const auto [value, literal] : encoding) {
if (!set.contains(value)) {
bool_and->add_literals(NegatedRef(literal));
}
}
}
}
void ExpandAutomaton(ConstraintProto* ct, PresolveContext* context) {
AutomatonConstraintProto& proto = *ct->mutable_automaton();
if (proto.vars_size() == 0) {
const int64_t initial_state = proto.starting_state();
for (const int64_t final_state : proto.final_states()) {
if (initial_state == final_state) {
context->UpdateRuleStats("automaton: empty and trivially feasible");
ct->Clear();
return;
}
}
return (void)context->NotifyThatModelIsUnsat(
"automaton: empty with an initial state not in the final states.");
} else if (proto.transition_label_size() == 0) {
return (void)context->NotifyThatModelIsUnsat(
"automaton: non-empty with no transition.");
}
const int n = proto.vars_size();
const std::vector<int> vars = {proto.vars().begin(), proto.vars().end()};
// Compute the set of reachable state at each time point.
const absl::flat_hash_set<int64_t> final_states(
{proto.final_states().begin(), proto.final_states().end()});
std::vector<absl::flat_hash_set<int64_t>> reachable_states(n + 1);
reachable_states[0].insert(proto.starting_state());
// Forward pass.
for (int time = 0; time < n; ++time) {
for (int t = 0; t < proto.transition_tail_size(); ++t) {
const int64_t tail = proto.transition_tail(t);
const int64_t label = proto.transition_label(t);
const int64_t head = proto.transition_head(t);
if (!reachable_states[time].contains(tail)) continue;
if (!context->DomainContains(vars[time], label)) continue;
if (time == n - 1 && !final_states.contains(head)) continue;
reachable_states[time + 1].insert(head);
}
}
// Backward pass.
for (int time = n - 1; time >= 0; --time) {
absl::flat_hash_set<int64_t> new_set;
for (int t = 0; t < proto.transition_tail_size(); ++t) {
const int64_t tail = proto.transition_tail(t);
const int64_t label = proto.transition_label(t);
const int64_t head = proto.transition_head(t);
if (!reachable_states[time].contains(tail)) continue;
if (!context->DomainContains(vars[time], label)) continue;
if (!reachable_states[time + 1].contains(head)) continue;
new_set.insert(tail);
}
reachable_states[time].swap(new_set);
}
// We will model at each time step the current automaton state using Boolean
// variables. We will have n+1 time step. At time zero, we start in the
// initial state, and at time n we should be in one of the final states. We
// don't need to create Booleans at at time when there is just one possible
// state (like at time zero).
absl::flat_hash_map<int64_t, int> encoding;
absl::flat_hash_map<int64_t, int> in_encoding;
absl::flat_hash_map<int64_t, int> out_encoding;
bool removed_values = false;
for (int time = 0; time < n; ++time) {
// All these vector have the same size. We will use them to enforce a
// local table constraint representing one step of the automaton at the
// given time.
std::vector<int64_t> in_states;
std::vector<int64_t> labels;
std::vector<int64_t> out_states;
for (int i = 0; i < proto.transition_label_size(); ++i) {
const int64_t tail = proto.transition_tail(i);
const int64_t label = proto.transition_label(i);
const int64_t head = proto.transition_head(i);
if (!reachable_states[time].contains(tail)) continue;
if (!reachable_states[time + 1].contains(head)) continue;
if (!context->DomainContains(vars[time], label)) continue;
// TODO(user): if this transition correspond to just one in-state or
// one-out state or one variable value, we could reuse the corresponding
// Boolean variable instead of creating a new one!
in_states.push_back(tail);
labels.push_back(label);
// On the last step we don't need to distinguish the output states, so
// we use zero.
out_states.push_back(time + 1 == n ? 0 : head);
}
// Deal with single tuple.
const int num_tuples = in_states.size();
if (num_tuples == 1) {
if (!context->IntersectDomainWith(vars[time], Domain(labels.front()))) {
VLOG(1) << "Infeasible automaton.";
return;
}
in_encoding.clear();
continue;
}
// Fully encode vars[time].
{
std::vector<int64_t> transitions = labels;
gtl::STLSortAndRemoveDuplicates(&transitions);
encoding.clear();
if (!context->IntersectDomainWith(
vars[time], Domain::FromValues(transitions), &removed_values)) {
VLOG(1) << "Infeasible automaton.";
return;
}
// Fully encode the variable.
// We can leave the encoding empty for fixed vars.
if (!context->IsFixed(vars[time])) {
for (const int64_t v : context->DomainOf(vars[time]).Values()) {
encoding[v] = context->GetOrCreateVarValueEncoding(vars[time], v);
}
}
}
// Count how many time each value appear.
// We use this to reuse literals if possible.
absl::flat_hash_map<int64_t, int> in_count;
absl::flat_hash_map<int64_t, int> transition_count;
absl::flat_hash_map<int64_t, int> out_count;
for (int i = 0; i < num_tuples; ++i) {
in_count[in_states[i]]++;
transition_count[labels[i]]++;
out_count[out_states[i]]++;
}
// For each possible out states, create one Boolean variable.
//
// TODO(user): Add exactly one?
{
std::vector<int64_t> states = out_states;
gtl::STLSortAndRemoveDuplicates(&states);
out_encoding.clear();
if (states.size() == 2) {
const int var = context->NewBoolVar();
out_encoding[states[0]] = var;
out_encoding[states[1]] = NegatedRef(var);
} else if (states.size() > 2) {
struct UniqueDetector {
void Set(int64_t v) {
if (!is_unique) return;
if (is_set) {
if (v != value) is_unique = false;
} else {
is_set = true;
value = v;
}
}
bool is_set = false;
bool is_unique = true;
int64_t value = 0;
};
// Optimization to detect if we have an in state that is only matched to
// a single out state. Same with transition.
absl::flat_hash_map<int64_t, UniqueDetector> out_to_in;
absl::flat_hash_map<int64_t, UniqueDetector> out_to_transition;
for (int i = 0; i < num_tuples; ++i) {
out_to_in[out_states[i]].Set(in_states[i]);
out_to_transition[out_states[i]].Set(labels[i]);
}
for (const int64_t state : states) {
// If we have a relation in_state <=> out_state, then we can reuse
// the in Boolean and do not need to create a new one.
if (!in_encoding.empty() && out_to_in[state].is_unique) {
const int64_t unique_in = out_to_in[state].value;
if (in_count[unique_in] == out_count[state]) {
out_encoding[state] = in_encoding[unique_in];
continue;
}
}
// Same if we have an unique transition value that correspond only to
// this state.
if (!encoding.empty() && out_to_transition[state].is_unique) {
const int64_t unique_transition = out_to_transition[state].value;
if (transition_count[unique_transition] == out_count[state]) {
out_encoding[state] = encoding[unique_transition];
continue;
}
}
out_encoding[state] = context->NewBoolVar();
}
}
}
// Simple encoding. This is enough to properly enforce the constraint, but
// it propagate less. It creates a lot less Booleans though. Note that we
// use implicit "exactly one" on the encoding and do not add any extra
// exacly one if the simple encoding is used.
//
// We currently decide which encoding to use depending on the number of new
// literals needed by the "heavy" encoding compared to the number of states
// and labels. When the automaton is small, using the full encoding is
// better, see for instance on rotating-workforce_Example789 were the simple
// encoding make the problem hard to solve but the full encoding allow the
// solver to solve it in a couple of seconds!
//
// Note that both encoding create about the same number of constraints.
const int num_involved_variables =
in_encoding.size() + encoding.size() + out_encoding.size();
const bool use_light_encoding = (num_tuples > num_involved_variables);
if (use_light_encoding && !in_encoding.empty() && !encoding.empty() &&
!out_encoding.empty()) {
// Part 1: If a in_state is selected, restrict the set of possible labels.
// We also restrict the set of possible out states, but this is not needed
// for correctness.
absl::flat_hash_map<int64_t, std::vector<int64_t>> in_to_label;
absl::flat_hash_map<int64_t, std::vector<int64_t>> in_to_out;
for (int i = 0; i < num_tuples; ++i) {
in_to_label[in_states[i]].push_back(labels[i]);
in_to_out[in_states[i]].push_back(out_states[i]);
}
for (const auto [in_value, in_literal] : in_encoding) {
AddImplyInReachableValues(in_literal, in_to_label[in_value], encoding,
context);
AddImplyInReachableValues(in_literal, in_to_out[in_value], out_encoding,
context);
}
// Part2, add all 3-clauses: (in_state, label) => out_state.
for (int i = 0; i < num_tuples; ++i) {
auto* bool_or =
context->working_model->add_constraints()->mutable_bool_or();
bool_or->add_literals(NegatedRef(in_encoding.at(in_states[i])));
bool_or->add_literals(NegatedRef(encoding.at(labels[i])));
bool_or->add_literals(out_encoding.at(out_states[i]));
}
in_encoding.swap(out_encoding);
out_encoding.clear();
continue;
}
// Create the tuple literals.
//
// TODO(user): Call and use the same heuristics as the table constraint to
// expand this small table with 3 columns (i.e. compress, negate, etc...).
std::vector<int> tuple_literals;
if (num_tuples == 2) {
const int bool_var = context->NewBoolVar();
tuple_literals.push_back(bool_var);
tuple_literals.push_back(NegatedRef(bool_var));
} else {
// Note that we do not need the ExactlyOneConstraint(tuple_literals)
// because it is already implicitly encoded since we have exactly one
// transition value. But adding one seems to help.
BoolArgumentProto* exactly_one =
context->working_model->add_constraints()->mutable_exactly_one();
for (int i = 0; i < num_tuples; ++i) {
int tuple_literal;
if (in_count[in_states[i]] == 1 && !in_encoding.empty()) {
tuple_literal = in_encoding[in_states[i]];
} else if (transition_count[labels[i]] == 1 && !encoding.empty()) {
tuple_literal = encoding[labels[i]];
} else if (out_count[out_states[i]] == 1 && !out_encoding.empty()) {
tuple_literal = out_encoding[out_states[i]];
} else {
tuple_literal = context->NewBoolVar();
}
tuple_literals.push_back(tuple_literal);
exactly_one->add_literals(tuple_literal);
}
}
if (!in_encoding.empty()) {
LinkLiteralsAndValues(tuple_literals, in_states, in_encoding, context);
}
if (!encoding.empty()) {
LinkLiteralsAndValues(tuple_literals, labels, encoding, context);
}
if (!out_encoding.empty()) {
LinkLiteralsAndValues(tuple_literals, out_states, out_encoding, context);
}
in_encoding.swap(out_encoding);
out_encoding.clear();
}
if (removed_values) {
context->UpdateRuleStats("automaton: reduced variable domains");
}
context->UpdateRuleStats("automaton: expanded");
ct->Clear();
}
void ExpandNegativeTable(ConstraintProto* ct, PresolveContext* context) {
TableConstraintProto& table = *ct->mutable_table();
const int num_vars = table.vars_size();
const int num_original_tuples = table.values_size() / num_vars;
std::vector<std::vector<int64_t>> tuples(num_original_tuples);
int count = 0;
for (int i = 0; i < num_original_tuples; ++i) {
for (int j = 0; j < num_vars; ++j) {
tuples[i].push_back(table.values(count++));
}
}
if (tuples.empty()) { // Early exit.
context->UpdateRuleStats("table: empty negated constraint");
ct->Clear();
return;
}
// Compress tuples.
const int64_t any_value = std::numeric_limits<int64_t>::min();
std::vector<int64_t> domain_sizes;
for (int i = 0; i < num_vars; ++i) {
domain_sizes.push_back(context->DomainOf(table.vars(i)).Size());
}
CompressTuples(domain_sizes, any_value, &tuples);
// For each tuple, forbid the variables values to be this tuple.
std::vector<int> clause;
for (const std::vector<int64_t>& tuple : tuples) {
clause.clear();
for (int i = 0; i < num_vars; ++i) {
const int64_t value = tuple[i];
if (value == any_value) continue;
const int literal =
context->GetOrCreateVarValueEncoding(table.vars(i), value);
clause.push_back(NegatedRef(literal));
}
// Note: if the clause is empty, then the model is infeasible.
BoolArgumentProto* bool_or =
context->working_model->add_constraints()->mutable_bool_or();
for (const int lit : clause) {
bool_or->add_literals(lit);
}
}
context->UpdateRuleStats("table: expanded negated constraint");
ct->Clear();
}
// Add the implications and clauses to link one variable of a table to the
// literals controlling if the tuples are possible or not. The parallel vectors
// (tuple_literals, values) contains all valid projected tuples.
//
// The special value "any_value" is used to indicate literal that will support
// any value.
void ProcessOneVariable(const std::vector<int>& tuple_literals,
const std::vector<int64_t>& values, int variable,
int64_t any_value, PresolveContext* context) {
VLOG(2) << "Process var(" << variable << ") with domain "
<< context->DomainOf(variable) << " and " << values.size()
<< " tuples.";
CHECK_EQ(tuple_literals.size(), values.size());
// Collect pairs of value-literal.
std::vector<int> tuples_with_any;
std::vector<std::pair<int64_t, int>> pairs;
for (int i = 0; i < values.size(); ++i) {
const int64_t value = values[i];
if (value == any_value) {
tuples_with_any.push_back(tuple_literals[i]);