forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_cst.cc
805 lines (713 loc) · 26.1 KB
/
range_cst.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
// 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.
//
// Range constraints
#include <stddef.h>
#include <string>
#include "absl/strings/str_format.h"
#include "ortools/base/logging.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/constraint_solver/constraint_solveri.h"
namespace operations_research {
//-----------------------------------------------------------------------------
// RangeEquality
namespace {
class RangeEquality : public Constraint {
public:
RangeEquality(Solver* const s, IntExpr* const l, IntExpr* const r)
: Constraint(s), left_(l), right_(r) {}
~RangeEquality() override {}
void Post() override {
Demon* const d = solver()->MakeConstraintInitialPropagateCallback(this);
left_->WhenRange(d);
right_->WhenRange(d);
}
void InitialPropagate() override {
left_->SetRange(right_->Min(), right_->Max());
right_->SetRange(left_->Min(), left_->Max());
}
std::string DebugString() const override {
return left_->DebugString() + " == " + right_->DebugString();
}
IntVar* Var() override { return solver()->MakeIsEqualVar(left_, right_); }
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kEquality, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->EndVisitConstraint(ModelVisitor::kEquality, this);
}
private:
IntExpr* const left_;
IntExpr* const right_;
};
//-----------------------------------------------------------------------------
// RangeLessOrEqual
class RangeLessOrEqual : public Constraint {
public:
RangeLessOrEqual(Solver* const s, IntExpr* const l, IntExpr* const r);
~RangeLessOrEqual() override {}
void Post() override;
void InitialPropagate() override;
std::string DebugString() const override;
IntVar* Var() override {
return solver()->MakeIsLessOrEqualVar(left_, right_);
}
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kLessOrEqual, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->EndVisitConstraint(ModelVisitor::kLessOrEqual, this);
}
private:
IntExpr* const left_;
IntExpr* const right_;
Demon* demon_;
};
RangeLessOrEqual::RangeLessOrEqual(Solver* const s, IntExpr* const l,
IntExpr* const r)
: Constraint(s), left_(l), right_(r), demon_(nullptr) {}
void RangeLessOrEqual::Post() {
demon_ = solver()->MakeConstraintInitialPropagateCallback(this);
left_->WhenRange(demon_);
right_->WhenRange(demon_);
}
void RangeLessOrEqual::InitialPropagate() {
left_->SetMax(right_->Max());
right_->SetMin(left_->Min());
if (left_->Max() <= right_->Min()) {
demon_->inhibit(solver());
}
}
std::string RangeLessOrEqual::DebugString() const {
return left_->DebugString() + " <= " + right_->DebugString();
}
//-----------------------------------------------------------------------------
// RangeLess
class RangeLess : public Constraint {
public:
RangeLess(Solver* const s, IntExpr* const l, IntExpr* const r);
~RangeLess() override {}
void Post() override;
void InitialPropagate() override;
std::string DebugString() const override;
IntVar* Var() override { return solver()->MakeIsLessVar(left_, right_); }
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kLess, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->EndVisitConstraint(ModelVisitor::kLess, this);
}
private:
IntExpr* const left_;
IntExpr* const right_;
Demon* demon_;
};
RangeLess::RangeLess(Solver* const s, IntExpr* const l, IntExpr* const r)
: Constraint(s), left_(l), right_(r), demon_(nullptr) {}
void RangeLess::Post() {
demon_ = solver()->MakeConstraintInitialPropagateCallback(this);
left_->WhenRange(demon_);
right_->WhenRange(demon_);
}
void RangeLess::InitialPropagate() {
left_->SetMax(right_->Max() - 1);
right_->SetMin(left_->Min() + 1);
if (left_->Max() < right_->Min()) {
demon_->inhibit(solver());
}
}
std::string RangeLess::DebugString() const {
return left_->DebugString() + " < " + right_->DebugString();
}
//-----------------------------------------------------------------------------
// DiffVar
class DiffVar : public Constraint {
public:
DiffVar(Solver* const s, IntVar* const l, IntVar* const r);
~DiffVar() override {}
void Post() override;
void InitialPropagate() override;
std::string DebugString() const override;
IntVar* Var() override { return solver()->MakeIsDifferentVar(left_, right_); }
void LeftBound();
void RightBound();
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kNonEqual, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->EndVisitConstraint(ModelVisitor::kNonEqual, this);
}
private:
IntVar* const left_;
IntVar* const right_;
};
DiffVar::DiffVar(Solver* const s, IntVar* const l, IntVar* const r)
: Constraint(s), left_(l), right_(r) {}
void DiffVar::Post() {
Demon* const left_demon =
MakeConstraintDemon0(solver(), this, &DiffVar::LeftBound, "LeftBound");
Demon* const right_demon =
MakeConstraintDemon0(solver(), this, &DiffVar::RightBound, "RightBound");
left_->WhenBound(left_demon);
right_->WhenBound(right_demon);
// TODO(user) : improve me, separated demons, actually to test
}
void DiffVar::LeftBound() {
if (right_->Size() < 0xFFFFFF) {
right_->RemoveValue(left_->Min()); // we use min instead of value
} else {
solver()->AddConstraint(solver()->MakeNonEquality(right_, left_->Min()));
}
}
void DiffVar::RightBound() {
if (left_->Size() < 0xFFFFFF) {
left_->RemoveValue(right_->Min()); // see above
} else {
solver()->AddConstraint(solver()->MakeNonEquality(left_, right_->Min()));
}
}
void DiffVar::InitialPropagate() {
if (left_->Bound()) {
LeftBound();
}
if (right_->Bound()) {
RightBound();
}
}
std::string DiffVar::DebugString() const {
return left_->DebugString() + " != " + right_->DebugString();
}
// --------------------- Reified API -------------------
// A reified API transforms an constraint into a status variables.
// For example x == y is transformed into IsEqual(x, y, b) where
// b is a boolean variable which is true if and only if x is equal to b.
// IsEqualCt
class IsEqualCt : public CastConstraint {
public:
IsEqualCt(Solver* const s, IntExpr* const l, IntExpr* const r,
IntVar* const b)
: CastConstraint(s, b), left_(l), right_(r), range_demon_(nullptr) {}
~IsEqualCt() override {}
void Post() override {
range_demon_ = solver()->MakeConstraintInitialPropagateCallback(this);
left_->WhenRange(range_demon_);
right_->WhenRange(range_demon_);
Demon* const target_demon = MakeConstraintDemon0(
solver(), this, &IsEqualCt::PropagateTarget, "PropagateTarget");
target_var_->WhenBound(target_demon);
}
void InitialPropagate() override {
if (target_var_->Bound()) {
PropagateTarget();
return;
}
if (left_->Min() > right_->Max() || left_->Max() < right_->Min()) {
target_var_->SetValue(0);
range_demon_->inhibit(solver());
} else if (left_->Bound()) {
if (right_->Bound()) {
target_var_->SetValue(left_->Min() == right_->Min());
} else if (right_->IsVar() && !right_->Var()->Contains(left_->Min())) {
range_demon_->inhibit(solver());
target_var_->SetValue(0);
}
} else if (right_->Bound() && left_->IsVar() &&
!left_->Var()->Contains(right_->Min())) {
range_demon_->inhibit(solver());
target_var_->SetValue(0);
}
}
void PropagateTarget() {
if (target_var_->Min() == 0) {
if (left_->Bound()) {
range_demon_->inhibit(solver());
if (right_->IsVar()) {
right_->Var()->RemoveValue(left_->Min());
} else {
solver()->AddConstraint(
solver()->MakeNonEquality(right_, left_->Min()));
}
} else if (right_->Bound()) {
range_demon_->inhibit(solver());
if (left_->IsVar()) {
left_->Var()->RemoveValue(right_->Min());
} else {
solver()->AddConstraint(
solver()->MakeNonEquality(left_, right_->Min()));
}
}
} else { // Var is true.
left_->SetRange(right_->Min(), right_->Max());
right_->SetRange(left_->Min(), left_->Max());
}
}
std::string DebugString() const override {
return absl::StrFormat("IsEqualCt(%s, %s, %s)", left_->DebugString(),
right_->DebugString(), target_var_->DebugString());
}
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kIsEqual, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kTargetArgument,
target_var_);
visitor->EndVisitConstraint(ModelVisitor::kIsEqual, this);
}
private:
IntExpr* const left_;
IntExpr* const right_;
Demon* range_demon_;
};
// IsDifferentCt
class IsDifferentCt : public CastConstraint {
public:
IsDifferentCt(Solver* const s, IntExpr* const l, IntExpr* const r,
IntVar* const b)
: CastConstraint(s, b), left_(l), right_(r), range_demon_(nullptr) {}
~IsDifferentCt() override {}
void Post() override {
range_demon_ = solver()->MakeConstraintInitialPropagateCallback(this);
left_->WhenRange(range_demon_);
right_->WhenRange(range_demon_);
Demon* const target_demon = MakeConstraintDemon0(
solver(), this, &IsDifferentCt::PropagateTarget, "PropagateTarget");
target_var_->WhenBound(target_demon);
}
void InitialPropagate() override {
if (target_var_->Bound()) {
PropagateTarget();
return;
}
if (left_->Min() > right_->Max() || left_->Max() < right_->Min()) {
target_var_->SetValue(1);
range_demon_->inhibit(solver());
} else if (left_->Bound()) {
if (right_->Bound()) {
target_var_->SetValue(left_->Min() != right_->Min());
} else if (right_->IsVar() && !right_->Var()->Contains(left_->Min())) {
range_demon_->inhibit(solver());
target_var_->SetValue(1);
}
} else if (right_->Bound() && left_->IsVar() &&
!left_->Var()->Contains(right_->Min())) {
range_demon_->inhibit(solver());
target_var_->SetValue(1);
}
}
void PropagateTarget() {
if (target_var_->Min() == 0) {
left_->SetRange(right_->Min(), right_->Max());
right_->SetRange(left_->Min(), left_->Max());
} else { // Var is true.
if (left_->Bound()) {
range_demon_->inhibit(solver());
solver()->AddConstraint(
solver()->MakeNonEquality(right_, left_->Min()));
} else if (right_->Bound()) {
range_demon_->inhibit(solver());
solver()->AddConstraint(
solver()->MakeNonEquality(left_, right_->Min()));
}
}
}
std::string DebugString() const override {
return absl::StrFormat("IsDifferentCt(%s, %s, %s)", left_->DebugString(),
right_->DebugString(), target_var_->DebugString());
}
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kIsDifferent, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kTargetArgument,
target_var_);
visitor->EndVisitConstraint(ModelVisitor::kIsDifferent, this);
}
private:
IntExpr* const left_;
IntExpr* const right_;
Demon* range_demon_;
};
class IsLessOrEqualCt : public CastConstraint {
public:
IsLessOrEqualCt(Solver* const s, IntExpr* const l, IntExpr* const r,
IntVar* const b)
: CastConstraint(s, b), left_(l), right_(r), demon_(nullptr) {}
~IsLessOrEqualCt() override {}
void Post() override {
demon_ = solver()->MakeConstraintInitialPropagateCallback(this);
left_->WhenRange(demon_);
right_->WhenRange(demon_);
target_var_->WhenBound(demon_);
}
void InitialPropagate() override {
if (target_var_->Bound()) {
if (target_var_->Min() == 0) {
right_->SetMax(left_->Max() - 1);
left_->SetMin(right_->Min() + 1);
} else { // Var is true.
right_->SetMin(left_->Min());
left_->SetMax(right_->Max());
}
} else if (right_->Min() >= left_->Max()) {
demon_->inhibit(solver());
target_var_->SetValue(1);
} else if (right_->Max() < left_->Min()) {
demon_->inhibit(solver());
target_var_->SetValue(0);
}
}
std::string DebugString() const override {
return absl::StrFormat("IsLessOrEqualCt(%s, %s, %s)", left_->DebugString(),
right_->DebugString(), target_var_->DebugString());
}
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kIsLessOrEqual, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kTargetArgument,
target_var_);
visitor->EndVisitConstraint(ModelVisitor::kIsLessOrEqual, this);
}
private:
IntExpr* const left_;
IntExpr* const right_;
Demon* demon_;
};
class IsLessCt : public CastConstraint {
public:
IsLessCt(Solver* const s, IntExpr* const l, IntExpr* const r, IntVar* const b)
: CastConstraint(s, b), left_(l), right_(r), demon_(nullptr) {}
~IsLessCt() override {}
void Post() override {
demon_ = solver()->MakeConstraintInitialPropagateCallback(this);
left_->WhenRange(demon_);
right_->WhenRange(demon_);
target_var_->WhenBound(demon_);
}
void InitialPropagate() override {
if (target_var_->Bound()) {
if (target_var_->Min() == 0) {
right_->SetMax(left_->Max());
left_->SetMin(right_->Min());
} else { // Var is true.
right_->SetMin(left_->Min() + 1);
left_->SetMax(right_->Max() - 1);
}
} else if (right_->Min() > left_->Max()) {
demon_->inhibit(solver());
target_var_->SetValue(1);
} else if (right_->Max() <= left_->Min()) {
demon_->inhibit(solver());
target_var_->SetValue(0);
}
}
std::string DebugString() const override {
return absl::StrFormat("IsLessCt(%s, %s, %s)", left_->DebugString(),
right_->DebugString(), target_var_->DebugString());
}
void Accept(ModelVisitor* const visitor) const override {
visitor->BeginVisitConstraint(ModelVisitor::kIsLess, this);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kLeftArgument, left_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kRightArgument,
right_);
visitor->VisitIntegerExpressionArgument(ModelVisitor::kTargetArgument,
target_var_);
visitor->EndVisitConstraint(ModelVisitor::kIsLess, this);
}
private:
IntExpr* const left_;
IntExpr* const right_;
Demon* demon_;
};
} // namespace
Constraint* Solver::MakeEquality(IntExpr* const l, IntExpr* const r) {
CHECK(l != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK(r != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK_EQ(this, l->solver());
CHECK_EQ(this, r->solver());
if (l->Bound()) {
return MakeEquality(r, l->Min());
} else if (r->Bound()) {
return MakeEquality(l, r->Min());
} else {
return RevAlloc(new RangeEquality(this, l, r));
}
}
Constraint* Solver::MakeLessOrEqual(IntExpr* const l, IntExpr* const r) {
CHECK(l != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK(r != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK_EQ(this, l->solver());
CHECK_EQ(this, r->solver());
if (l == r) {
return MakeTrueConstraint();
} else if (l->Bound()) {
return MakeGreaterOrEqual(r, l->Min());
} else if (r->Bound()) {
return MakeLessOrEqual(l, r->Min());
} else {
return RevAlloc(new RangeLessOrEqual(this, l, r));
}
}
Constraint* Solver::MakeGreaterOrEqual(IntExpr* const l, IntExpr* const r) {
return MakeLessOrEqual(r, l);
}
Constraint* Solver::MakeLess(IntExpr* const l, IntExpr* const r) {
CHECK(l != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK(r != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK_EQ(this, l->solver());
CHECK_EQ(this, r->solver());
if (l->Bound()) {
return MakeGreater(r, l->Min());
} else if (r->Bound()) {
return MakeLess(l, r->Min());
} else {
return RevAlloc(new RangeLess(this, l, r));
}
}
Constraint* Solver::MakeGreater(IntExpr* const l, IntExpr* const r) {
return MakeLess(r, l);
}
Constraint* Solver::MakeNonEquality(IntExpr* const l, IntExpr* const r) {
CHECK(l != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK(r != nullptr) << "left expression nullptr, maybe a bad cast";
CHECK_EQ(this, l->solver());
CHECK_EQ(this, r->solver());
if (l->Bound()) {
return MakeNonEquality(r, l->Min());
} else if (r->Bound()) {
return MakeNonEquality(l, r->Min());
}
return RevAlloc(new DiffVar(this, l->Var(), r->Var()));
}
IntVar* Solver::MakeIsEqualVar(IntExpr* const v1, IntExpr* const v2) {
CHECK_EQ(this, v1->solver());
CHECK_EQ(this, v2->solver());
if (v1->Bound()) {
return MakeIsEqualCstVar(v2, v1->Min());
} else if (v2->Bound()) {
return MakeIsEqualCstVar(v1, v2->Min());
}
IntExpr* cache = model_cache_->FindExprExprExpression(
v1, v2, ModelCache::EXPR_EXPR_IS_EQUAL);
if (cache == nullptr) {
cache = model_cache_->FindExprExprExpression(
v2, v1, ModelCache::EXPR_EXPR_IS_EQUAL);
}
if (cache != nullptr) {
return cache->Var();
} else {
IntVar* boolvar = nullptr;
IntExpr* reverse_cache = model_cache_->FindExprExprExpression(
v1, v2, ModelCache::EXPR_EXPR_IS_NOT_EQUAL);
if (reverse_cache == nullptr) {
reverse_cache = model_cache_->FindExprExprExpression(
v2, v1, ModelCache::EXPR_EXPR_IS_NOT_EQUAL);
}
if (reverse_cache != nullptr) {
boolvar = MakeDifference(1, reverse_cache)->Var();
} else {
std::string name1 = v1->name();
if (name1.empty()) {
name1 = v1->DebugString();
}
std::string name2 = v2->name();
if (name2.empty()) {
name2 = v2->DebugString();
}
boolvar =
MakeBoolVar(absl::StrFormat("IsEqualVar(%s, %s)", name1, name2));
AddConstraint(MakeIsEqualCt(v1, v2, boolvar));
model_cache_->InsertExprExprExpression(boolvar, v1, v2,
ModelCache::EXPR_EXPR_IS_EQUAL);
}
return boolvar;
}
}
Constraint* Solver::MakeIsEqualCt(IntExpr* const v1, IntExpr* const v2,
IntVar* b) {
CHECK_EQ(this, v1->solver());
CHECK_EQ(this, v2->solver());
if (v1->Bound()) {
return MakeIsEqualCstCt(v2, v1->Min(), b);
} else if (v2->Bound()) {
return MakeIsEqualCstCt(v1, v2->Min(), b);
}
if (b->Bound()) {
if (b->Min() == 0) {
return MakeNonEquality(v1, v2);
} else {
return MakeEquality(v1, v2);
}
}
return RevAlloc(new IsEqualCt(this, v1, v2, b));
}
IntVar* Solver::MakeIsDifferentVar(IntExpr* const v1, IntExpr* const v2) {
CHECK_EQ(this, v1->solver());
CHECK_EQ(this, v2->solver());
if (v1->Bound()) {
return MakeIsDifferentCstVar(v2, v1->Min());
} else if (v2->Bound()) {
return MakeIsDifferentCstVar(v1, v2->Min());
}
IntExpr* cache = model_cache_->FindExprExprExpression(
v1, v2, ModelCache::EXPR_EXPR_IS_NOT_EQUAL);
if (cache == nullptr) {
cache = model_cache_->FindExprExprExpression(
v2, v1, ModelCache::EXPR_EXPR_IS_NOT_EQUAL);
}
if (cache != nullptr) {
return cache->Var();
} else {
IntVar* boolvar = nullptr;
IntExpr* reverse_cache = model_cache_->FindExprExprExpression(
v1, v2, ModelCache::EXPR_EXPR_IS_EQUAL);
if (reverse_cache == nullptr) {
reverse_cache = model_cache_->FindExprExprExpression(
v2, v1, ModelCache::EXPR_EXPR_IS_EQUAL);
}
if (reverse_cache != nullptr) {
boolvar = MakeDifference(1, reverse_cache)->Var();
} else {
std::string name1 = v1->name();
if (name1.empty()) {
name1 = v1->DebugString();
}
std::string name2 = v2->name();
if (name2.empty()) {
name2 = v2->DebugString();
}
boolvar =
MakeBoolVar(absl::StrFormat("IsDifferentVar(%s, %s)", name1, name2));
AddConstraint(MakeIsDifferentCt(v1, v2, boolvar));
}
model_cache_->InsertExprExprExpression(boolvar, v1, v2,
ModelCache::EXPR_EXPR_IS_NOT_EQUAL);
return boolvar;
}
}
Constraint* Solver::MakeIsDifferentCt(IntExpr* const v1, IntExpr* const v2,
IntVar* b) {
CHECK_EQ(this, v1->solver());
CHECK_EQ(this, v2->solver());
if (v1->Bound()) {
return MakeIsDifferentCstCt(v2, v1->Min(), b);
} else if (v2->Bound()) {
return MakeIsDifferentCstCt(v1, v2->Min(), b);
}
return RevAlloc(new IsDifferentCt(this, v1, v2, b));
}
IntVar* Solver::MakeIsLessOrEqualVar(IntExpr* const left,
IntExpr* const right) {
CHECK_EQ(this, left->solver());
CHECK_EQ(this, right->solver());
if (left->Bound()) {
return MakeIsGreaterOrEqualCstVar(right, left->Min());
} else if (right->Bound()) {
return MakeIsLessOrEqualCstVar(left, right->Min());
}
IntExpr* const cache = model_cache_->FindExprExprExpression(
left, right, ModelCache::EXPR_EXPR_IS_LESS_OR_EQUAL);
if (cache != nullptr) {
return cache->Var();
} else {
std::string name1 = left->name();
if (name1.empty()) {
name1 = left->DebugString();
}
std::string name2 = right->name();
if (name2.empty()) {
name2 = right->DebugString();
}
IntVar* const boolvar =
MakeBoolVar(absl::StrFormat("IsLessOrEqual(%s, %s)", name1, name2));
AddConstraint(RevAlloc(new IsLessOrEqualCt(this, left, right, boolvar)));
model_cache_->InsertExprExprExpression(
boolvar, left, right, ModelCache::EXPR_EXPR_IS_LESS_OR_EQUAL);
return boolvar;
}
}
Constraint* Solver::MakeIsLessOrEqualCt(IntExpr* const left,
IntExpr* const right, IntVar* const b) {
CHECK_EQ(this, left->solver());
CHECK_EQ(this, right->solver());
if (left->Bound()) {
return MakeIsGreaterOrEqualCstCt(right, left->Min(), b);
} else if (right->Bound()) {
return MakeIsLessOrEqualCstCt(left, right->Min(), b);
}
return RevAlloc(new IsLessOrEqualCt(this, left, right, b));
}
IntVar* Solver::MakeIsLessVar(IntExpr* const left, IntExpr* const right) {
CHECK_EQ(this, left->solver());
CHECK_EQ(this, right->solver());
if (left->Bound()) {
return MakeIsGreaterCstVar(right, left->Min());
} else if (right->Bound()) {
return MakeIsLessCstVar(left, right->Min());
}
IntExpr* const cache = model_cache_->FindExprExprExpression(
left, right, ModelCache::EXPR_EXPR_IS_LESS);
if (cache != nullptr) {
return cache->Var();
} else {
std::string name1 = left->name();
if (name1.empty()) {
name1 = left->DebugString();
}
std::string name2 = right->name();
if (name2.empty()) {
name2 = right->DebugString();
}
IntVar* const boolvar =
MakeBoolVar(absl::StrFormat("IsLessOrEqual(%s, %s)", name1, name2));
AddConstraint(RevAlloc(new IsLessCt(this, left, right, boolvar)));
model_cache_->InsertExprExprExpression(boolvar, left, right,
ModelCache::EXPR_EXPR_IS_LESS);
return boolvar;
}
}
Constraint* Solver::MakeIsLessCt(IntExpr* const left, IntExpr* const right,
IntVar* const b) {
CHECK_EQ(this, left->solver());
CHECK_EQ(this, right->solver());
if (left->Bound()) {
return MakeIsGreaterCstCt(right, left->Min(), b);
} else if (right->Bound()) {
return MakeIsLessCstCt(left, right->Min(), b);
}
return RevAlloc(new IsLessCt(this, left, right, b));
}
IntVar* Solver::MakeIsGreaterOrEqualVar(IntExpr* const left,
IntExpr* const right) {
return MakeIsLessOrEqualVar(right, left);
}
Constraint* Solver::MakeIsGreaterOrEqualCt(IntExpr* const left,
IntExpr* const right,
IntVar* const b) {
return MakeIsLessOrEqualCt(right, left, b);
}
IntVar* Solver::MakeIsGreaterVar(IntExpr* const left, IntExpr* const right) {
return MakeIsLessVar(right, left);
}
Constraint* Solver::MakeIsGreaterCt(IntExpr* const left, IntExpr* const right,
IntVar* const b) {
return MakeIsLessCt(right, left, b);
}
} // namespace operations_research