forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
model_cache.cc
851 lines (749 loc) · 27.3 KB
/
model_cache.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
// Copyright 2010-2024 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 <cstdint>
#include <string>
#include <vector>
#include "ortools/base/commandlineflags.h"
#include "ortools/base/logging.h"
#include "ortools/base/stl_util.h"
#include "ortools/base/types.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/constraint_solver/constraint_solveri.h"
ABSL_DECLARE_FLAG(int, cache_initial_size);
ABSL_FLAG(bool, cp_disable_cache, false, "Disable caching of model objects");
namespace operations_research {
// ----- ModelCache -----
ModelCache::ModelCache(Solver* const solver) : solver_(solver) {}
ModelCache::~ModelCache() {}
Solver* ModelCache::solver() const { return solver_; }
namespace {
// ----- Helpers -----
template <class T>
bool IsEqual(const T& a1, const T& a2) {
return a1 == a2;
}
template <class T>
bool IsEqual(const std::vector<T*>& a1, const std::vector<T*>& a2) {
if (a1.size() != a2.size()) {
return false;
}
for (int i = 0; i < a1.size(); ++i) {
if (a1[i] != a2[i]) {
return false;
}
}
return true;
}
template <class A1, class A2>
uint64_t Hash2(const A1& a1, const A2& a2) {
uint64_t a = Hash1(a1);
uint64_t b = uint64_t{0xe08c1d668b756f82}; // more of the golden ratio
uint64_t c = Hash1(a2);
mix(a, b, c);
return c;
}
template <class A1, class A2, class A3>
uint64_t Hash3(const A1& a1, const A2& a2, const A3& a3) {
uint64_t a = Hash1(a1);
uint64_t b = Hash1(a2);
uint64_t c = Hash1(a3);
mix(a, b, c);
return c;
}
template <class A1, class A2, class A3, class A4>
uint64_t Hash4(const A1& a1, const A2& a2, const A3& a3, const A4& a4) {
uint64_t a = Hash1(a1);
uint64_t b = Hash1(a2);
uint64_t c = Hash2(a3, a4);
mix(a, b, c);
return c;
}
template <class C>
void Double(C*** array_ptr, int* size_ptr) {
DCHECK(array_ptr != nullptr);
DCHECK(size_ptr != nullptr);
C** const old_cell_array = *array_ptr;
const int old_size = *size_ptr;
(*size_ptr) *= 2;
(*array_ptr) = new C*[(*size_ptr)];
memset(*array_ptr, 0, (*size_ptr) * sizeof(**array_ptr));
for (int i = 0; i < old_size; ++i) {
C* tmp = old_cell_array[i];
while (tmp != nullptr) {
C* const to_reinsert = tmp;
tmp = tmp->next();
const uint64_t position = to_reinsert->Hash() % (*size_ptr);
to_reinsert->set_next((*array_ptr)[position]);
(*array_ptr)[position] = to_reinsert;
}
}
delete[] (old_cell_array);
}
// ----- Cache objects built with 1 object -----
template <class C, class A1>
class Cache1 {
public:
Cache1()
: array_(new Cell*[absl::GetFlag(FLAGS_cache_initial_size)]),
size_(absl::GetFlag(FLAGS_cache_initial_size)),
num_items_(0) {
memset(array_, 0, sizeof(*array_) * size_);
}
~Cache1() {
for (int i = 0; i < size_; ++i) {
Cell* tmp = array_[i];
while (tmp != nullptr) {
Cell* const to_delete = tmp;
tmp = tmp->next();
delete to_delete;
}
}
delete[] array_;
}
void Clear() {
for (int i = 0; i < size_; ++i) {
Cell* tmp = array_[i];
while (tmp != nullptr) {
Cell* const to_delete = tmp;
tmp = tmp->next();
delete to_delete;
}
array_[i] = nullptr;
}
}
C* Find(const A1& a1) const {
uint64_t code = Hash1(a1) % size_;
Cell* tmp = array_[code];
while (tmp) {
C* const result = tmp->ReturnsIfEqual(a1);
if (result != nullptr) {
return result;
}
tmp = tmp->next();
}
return nullptr;
}
void UnsafeInsert(const A1& a1, C* const c) {
const int position = Hash1(a1) % size_;
Cell* const cell = new Cell(a1, c, array_[position]);
array_[position] = cell;
if (++num_items_ > 2 * size_) {
Double(&array_, &size_);
}
}
private:
class Cell {
public:
Cell(const A1& a1, C* const container, Cell* const next)
: a1_(a1), container_(container), next_(next) {}
C* ReturnsIfEqual(const A1& a1) const {
if (IsEqual(a1_, a1)) {
return container_;
}
return nullptr;
}
uint64_t Hash() const { return Hash1(a1_); }
void set_next(Cell* const next) { next_ = next; }
Cell* next() const { return next_; }
private:
const A1 a1_;
C* const container_;
Cell* next_;
};
Cell** array_;
int size_;
int num_items_;
};
// ----- Cache objects built with 2 objects -----
template <class C, class A1, class A2>
class Cache2 {
public:
Cache2()
: array_(new Cell*[absl::GetFlag(FLAGS_cache_initial_size)]),
size_(absl::GetFlag(FLAGS_cache_initial_size)),
num_items_(0) {
memset(array_, 0, sizeof(*array_) * size_);
}
~Cache2() {
for (int i = 0; i < size_; ++i) {
Cell* tmp = array_[i];
while (tmp != nullptr) {
Cell* const to_delete = tmp;
tmp = tmp->next();
delete to_delete;
}
}
delete[] array_;
}
void Clear() {
for (int i = 0; i < size_; ++i) {
Cell* tmp = array_[i];
while (tmp != nullptr) {
Cell* const to_delete = tmp;
tmp = tmp->next();
delete to_delete;
}
array_[i] = nullptr;
}
}
C* Find(const A1& a1, const A2& a2) const {
uint64_t code = Hash2(a1, a2) % size_;
Cell* tmp = array_[code];
while (tmp) {
C* const result = tmp->ReturnsIfEqual(a1, a2);
if (result != nullptr) {
return result;
}
tmp = tmp->next();
}
return nullptr;
}
void UnsafeInsert(const A1& a1, const A2& a2, C* const c) {
const int position = Hash2(a1, a2) % size_;
Cell* const cell = new Cell(a1, a2, c, array_[position]);
array_[position] = cell;
if (++num_items_ > 2 * size_) {
Double(&array_, &size_);
}
}
private:
class Cell {
public:
Cell(const A1& a1, const A2& a2, C* const container, Cell* const next)
: a1_(a1), a2_(a2), container_(container), next_(next) {}
C* ReturnsIfEqual(const A1& a1, const A2& a2) const {
if (IsEqual(a1_, a1) && IsEqual(a2_, a2)) {
return container_;
}
return nullptr;
}
uint64_t Hash() const { return Hash2(a1_, a2_); }
void set_next(Cell* const next) { next_ = next; }
Cell* next() const { return next_; }
private:
const A1 a1_;
const A2 a2_;
C* const container_;
Cell* next_;
};
Cell** array_;
int size_;
int num_items_;
};
// ----- Cache objects built with 2 objects -----
template <class C, class A1, class A2, class A3>
class Cache3 {
public:
Cache3()
: array_(new Cell*[absl::GetFlag(FLAGS_cache_initial_size)]),
size_(absl::GetFlag(FLAGS_cache_initial_size)),
num_items_(0) {
memset(array_, 0, sizeof(*array_) * size_);
}
~Cache3() {
for (int i = 0; i < size_; ++i) {
Cell* tmp = array_[i];
while (tmp != nullptr) {
Cell* const to_delete = tmp;
tmp = tmp->next();
delete to_delete;
}
}
delete[] array_;
}
void Clear() {
for (int i = 0; i < size_; ++i) {
Cell* tmp = array_[i];
while (tmp != nullptr) {
Cell* const to_delete = tmp;
tmp = tmp->next();
delete to_delete;
}
array_[i] = nullptr;
}
}
C* Find(const A1& a1, const A2& a2, const A3& a3) const {
uint64_t code = Hash3(a1, a2, a3) % size_;
Cell* tmp = array_[code];
while (tmp) {
C* const result = tmp->ReturnsIfEqual(a1, a2, a3);
if (result != nullptr) {
return result;
}
tmp = tmp->next();
}
return nullptr;
}
void UnsafeInsert(const A1& a1, const A2& a2, const A3& a3, C* const c) {
const int position = Hash3(a1, a2, a3) % size_;
Cell* const cell = new Cell(a1, a2, a3, c, array_[position]);
array_[position] = cell;
if (++num_items_ > 2 * size_) {
Double(&array_, &size_);
}
}
private:
class Cell {
public:
Cell(const A1& a1, const A2& a2, const A3& a3, C* const container,
Cell* const next)
: a1_(a1), a2_(a2), a3_(a3), container_(container), next_(next) {}
C* ReturnsIfEqual(const A1& a1, const A2& a2, const A3& a3) const {
if (IsEqual(a1_, a1) && IsEqual(a2_, a2) && IsEqual(a3_, a3)) {
return container_;
}
return nullptr;
}
uint64_t Hash() const { return Hash3(a1_, a2_, a3_); }
void set_next(Cell* const next) { next_ = next; }
Cell* next() const { return next_; }
private:
const A1 a1_;
const A2 a2_;
const A3 a3_;
C* const container_;
Cell* next_;
};
Cell** array_;
int size_;
int num_items_;
};
// ----- Model Cache -----
class NonReversibleCache : public ModelCache {
public:
typedef Cache1<IntExpr, IntExpr*> ExprIntExprCache;
typedef Cache1<IntExpr, std::vector<IntVar*> > VarArrayIntExprCache;
typedef Cache2<Constraint, IntVar*, int64_t> VarConstantConstraintCache;
typedef Cache2<Constraint, IntExpr*, IntExpr*> ExprExprConstraintCache;
typedef Cache2<IntExpr, IntVar*, int64_t> VarConstantIntExprCache;
typedef Cache2<IntExpr, IntExpr*, int64_t> ExprConstantIntExprCache;
typedef Cache2<IntExpr, IntExpr*, IntExpr*> ExprExprIntExprCache;
typedef Cache2<IntExpr, IntVar*, const std::vector<int64_t>&>
VarConstantArrayIntExprCache;
typedef Cache2<IntExpr, std::vector<IntVar*>, const std::vector<int64_t>&>
VarArrayConstantArrayIntExprCache;
typedef Cache2<IntExpr, std::vector<IntVar*>, int64_t>
VarArrayConstantIntExprCache;
typedef Cache3<IntExpr, IntVar*, int64_t, int64_t>
VarConstantConstantIntExprCache;
typedef Cache3<Constraint, IntVar*, int64_t, int64_t>
VarConstantConstantConstraintCache;
typedef Cache3<IntExpr, IntExpr*, IntExpr*, int64_t>
ExprExprConstantIntExprCache;
explicit NonReversibleCache(Solver* const solver)
: ModelCache(solver), void_constraints_(VOID_CONSTRAINT_MAX, nullptr) {
for (int i = 0; i < VAR_CONSTANT_CONSTRAINT_MAX; ++i) {
var_constant_constraints_.push_back(new VarConstantConstraintCache);
}
for (int i = 0; i < EXPR_EXPR_CONSTRAINT_MAX; ++i) {
expr_expr_constraints_.push_back(new ExprExprConstraintCache);
}
for (int i = 0; i < VAR_CONSTANT_CONSTANT_CONSTRAINT_MAX; ++i) {
var_constant_constant_constraints_.push_back(
new VarConstantConstantConstraintCache);
}
for (int i = 0; i < EXPR_EXPRESSION_MAX; ++i) {
expr_expressions_.push_back(new ExprIntExprCache);
}
for (int i = 0; i < EXPR_CONSTANT_EXPRESSION_MAX; ++i) {
expr_constant_expressions_.push_back(new ExprConstantIntExprCache);
}
for (int i = 0; i < EXPR_EXPR_EXPRESSION_MAX; ++i) {
expr_expr_expressions_.push_back(new ExprExprIntExprCache);
}
for (int i = 0; i < VAR_CONSTANT_CONSTANT_EXPRESSION_MAX; ++i) {
var_constant_constant_expressions_.push_back(
new VarConstantConstantIntExprCache);
}
for (int i = 0; i < VAR_CONSTANT_ARRAY_EXPRESSION_MAX; ++i) {
var_constant_array_expressions_.push_back(
new VarConstantArrayIntExprCache);
}
for (int i = 0; i < VAR_ARRAY_EXPRESSION_MAX; ++i) {
var_array_expressions_.push_back(new VarArrayIntExprCache);
}
for (int i = 0; i < VAR_ARRAY_CONSTANT_ARRAY_EXPRESSION_MAX; ++i) {
var_array_constant_array_expressions_.push_back(
new VarArrayConstantArrayIntExprCache);
}
for (int i = 0; i < VAR_ARRAY_CONSTANT_EXPRESSION_MAX; ++i) {
var_array_constant_expressions_.push_back(
new VarArrayConstantIntExprCache);
}
for (int i = 0; i < EXPR_EXPR_CONSTANT_EXPRESSION_MAX; ++i) {
expr_expr_constant_expressions_.push_back(
new ExprExprConstantIntExprCache);
}
}
~NonReversibleCache() override {
gtl::STLDeleteElements(&var_constant_constraints_);
gtl::STLDeleteElements(&expr_expr_constraints_);
gtl::STLDeleteElements(&var_constant_constant_constraints_);
gtl::STLDeleteElements(&expr_expressions_);
gtl::STLDeleteElements(&expr_constant_expressions_);
gtl::STLDeleteElements(&expr_expr_expressions_);
gtl::STLDeleteElements(&var_constant_constant_expressions_);
gtl::STLDeleteElements(&var_constant_array_expressions_);
gtl::STLDeleteElements(&var_array_expressions_);
gtl::STLDeleteElements(&var_array_constant_array_expressions_);
gtl::STLDeleteElements(&var_array_constant_expressions_);
gtl::STLDeleteElements(&expr_expr_constant_expressions_);
}
void Clear() override {
for (int i = 0; i < VAR_CONSTANT_CONSTRAINT_MAX; ++i) {
var_constant_constraints_[i]->Clear();
}
for (int i = 0; i < EXPR_EXPR_CONSTRAINT_MAX; ++i) {
expr_expr_constraints_[i]->Clear();
}
for (int i = 0; i < VAR_CONSTANT_CONSTANT_CONSTRAINT_MAX; ++i) {
var_constant_constant_constraints_[i]->Clear();
}
for (int i = 0; i < EXPR_EXPRESSION_MAX; ++i) {
expr_expressions_[i]->Clear();
}
for (int i = 0; i < EXPR_CONSTANT_EXPRESSION_MAX; ++i) {
expr_constant_expressions_[i]->Clear();
}
for (int i = 0; i < EXPR_EXPR_EXPRESSION_MAX; ++i) {
expr_expr_expressions_[i]->Clear();
}
for (int i = 0; i < VAR_CONSTANT_CONSTANT_EXPRESSION_MAX; ++i) {
var_constant_constant_expressions_[i]->Clear();
}
for (int i = 0; i < VAR_CONSTANT_ARRAY_EXPRESSION_MAX; ++i) {
var_constant_array_expressions_[i]->Clear();
}
for (int i = 0; i < VAR_ARRAY_EXPRESSION_MAX; ++i) {
var_array_expressions_[i]->Clear();
}
for (int i = 0; i < VAR_ARRAY_CONSTANT_ARRAY_EXPRESSION_MAX; ++i) {
var_array_constant_array_expressions_[i]->Clear();
}
for (int i = 0; i < VAR_ARRAY_CONSTANT_EXPRESSION_MAX; ++i) {
var_array_constant_expressions_[i]->Clear();
}
for (int i = 0; i < EXPR_EXPR_CONSTANT_EXPRESSION_MAX; ++i) {
expr_expr_constant_expressions_[i]->Clear();
}
}
// Void Constraint.-
Constraint* FindVoidConstraint(VoidConstraintType type) const override {
DCHECK_GE(type, 0);
DCHECK_LT(type, VOID_CONSTRAINT_MAX);
return void_constraints_[type];
}
void InsertVoidConstraint(Constraint* const ct,
VoidConstraintType type) override {
DCHECK_GE(type, 0);
DCHECK_LT(type, VOID_CONSTRAINT_MAX);
DCHECK(ct != nullptr);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache)) {
void_constraints_[type] = ct;
}
}
// VarConstantConstraint.
Constraint* FindVarConstantConstraint(
IntVar* const var, int64_t value,
VarConstantConstraintType type) const override {
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_CONSTRAINT_MAX);
return var_constant_constraints_[type]->Find(var, value);
}
void InsertVarConstantConstraint(Constraint* const ct, IntVar* const var,
int64_t value,
VarConstantConstraintType type) override {
DCHECK(ct != nullptr);
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_CONSTRAINT_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
var_constant_constraints_[type]->Find(var, value) == nullptr) {
var_constant_constraints_[type]->UnsafeInsert(var, value, ct);
}
}
// Var Constant Constant Constraint.
Constraint* FindVarConstantConstantConstraint(
IntVar* const var, int64_t value1, int64_t value2,
VarConstantConstantConstraintType type) const override {
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_CONSTANT_CONSTRAINT_MAX);
return var_constant_constant_constraints_[type]->Find(var, value1, value2);
}
void InsertVarConstantConstantConstraint(
Constraint* const ct, IntVar* const var, int64_t value1, int64_t value2,
VarConstantConstantConstraintType type) override {
DCHECK(ct != nullptr);
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_CONSTANT_CONSTRAINT_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
var_constant_constant_constraints_[type]->Find(var, value1, value2) ==
nullptr) {
var_constant_constant_constraints_[type]->UnsafeInsert(var, value1,
value2, ct);
}
}
// Var Var Constraint.
Constraint* FindExprExprConstraint(
IntExpr* const var1, IntExpr* const var2,
ExprExprConstraintType type) const override {
DCHECK(var1 != nullptr);
DCHECK(var2 != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPR_CONSTRAINT_MAX);
return expr_expr_constraints_[type]->Find(var1, var2);
}
void InsertExprExprConstraint(Constraint* const ct, IntExpr* const var1,
IntExpr* const var2,
ExprExprConstraintType type) override {
DCHECK(ct != nullptr);
DCHECK(var1 != nullptr);
DCHECK(var2 != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPR_CONSTRAINT_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
expr_expr_constraints_[type]->Find(var1, var2) == nullptr) {
expr_expr_constraints_[type]->UnsafeInsert(var1, var2, ct);
}
}
// Expr Expression.
IntExpr* FindExprExpression(IntExpr* const expr,
ExprExpressionType type) const override {
DCHECK(expr != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPRESSION_MAX);
return expr_expressions_[type]->Find(expr);
}
void InsertExprExpression(IntExpr* const expression, IntExpr* const expr,
ExprExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK(expr != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPRESSION_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
expr_expressions_[type]->Find(expr) == nullptr) {
expr_expressions_[type]->UnsafeInsert(expr, expression);
}
}
// Expr Constant Expressions.
IntExpr* FindExprConstantExpression(
IntExpr* const expr, int64_t value,
ExprConstantExpressionType type) const override {
DCHECK(expr != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_CONSTANT_EXPRESSION_MAX);
return expr_constant_expressions_[type]->Find(expr, value);
}
void InsertExprConstantExpression(IntExpr* const expression,
IntExpr* const expr, int64_t value,
ExprConstantExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK(expr != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_CONSTANT_EXPRESSION_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
expr_constant_expressions_[type]->Find(expr, value) == nullptr) {
expr_constant_expressions_[type]->UnsafeInsert(expr, value, expression);
}
}
// Expr Expr Expression.
IntExpr* FindExprExprExpression(IntExpr* const var1, IntExpr* const var2,
ExprExprExpressionType type) const override {
DCHECK(var1 != nullptr);
DCHECK(var2 != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPR_EXPRESSION_MAX);
return expr_expr_expressions_[type]->Find(var1, var2);
}
void InsertExprExprExpression(IntExpr* const expression, IntExpr* const var1,
IntExpr* const var2,
ExprExprExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK(var1 != nullptr);
DCHECK(var2 != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPR_EXPRESSION_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
expr_expr_expressions_[type]->Find(var1, var2) == nullptr) {
expr_expr_expressions_[type]->UnsafeInsert(var1, var2, expression);
}
}
// Expr Expr Constant Expression.
IntExpr* FindExprExprConstantExpression(
IntExpr* const var1, IntExpr* const var2, int64_t constant,
ExprExprConstantExpressionType type) const override {
DCHECK(var1 != nullptr);
DCHECK(var2 != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPR_CONSTANT_EXPRESSION_MAX);
return expr_expr_constant_expressions_[type]->Find(var1, var2, constant);
}
void InsertExprExprConstantExpression(
IntExpr* const expression, IntExpr* const var1, IntExpr* const var2,
int64_t constant, ExprExprConstantExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK(var1 != nullptr);
DCHECK(var2 != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, EXPR_EXPR_CONSTANT_EXPRESSION_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
expr_expr_constant_expressions_[type]->Find(var1, var2, constant) ==
nullptr) {
expr_expr_constant_expressions_[type]->UnsafeInsert(var1, var2, constant,
expression);
}
}
// Var Constant Constant Expression.
IntExpr* FindVarConstantConstantExpression(
IntVar* const var, int64_t value1, int64_t value2,
VarConstantConstantExpressionType type) const override {
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_CONSTANT_EXPRESSION_MAX);
return var_constant_constant_expressions_[type]->Find(var, value1, value2);
}
void InsertVarConstantConstantExpression(
IntExpr* const expression, IntVar* const var, int64_t value1,
int64_t value2, VarConstantConstantExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_CONSTANT_EXPRESSION_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
var_constant_constant_expressions_[type]->Find(var, value1, value2) ==
nullptr) {
var_constant_constant_expressions_[type]->UnsafeInsert(
var, value1, value2, expression);
}
}
// Var Constant Array Expression.
IntExpr* FindVarConstantArrayExpression(
IntVar* const var, const std::vector<int64_t>& values,
VarConstantArrayExpressionType type) const override {
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_ARRAY_EXPRESSION_MAX);
return var_constant_array_expressions_[type]->Find(var, values);
}
void InsertVarConstantArrayExpression(
IntExpr* const expression, IntVar* const var,
const std::vector<int64_t>& values,
VarConstantArrayExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK(var != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_CONSTANT_ARRAY_EXPRESSION_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
var_constant_array_expressions_[type]->Find(var, values) == nullptr) {
var_constant_array_expressions_[type]->UnsafeInsert(var, values,
expression);
}
}
// Var Array Expression.
IntExpr* FindVarArrayExpression(const std::vector<IntVar*>& vars,
VarArrayExpressionType type) const override {
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_ARRAY_EXPRESSION_MAX);
return var_array_expressions_[type]->Find(vars);
}
void InsertVarArrayExpression(IntExpr* const expression,
const std::vector<IntVar*>& vars,
VarArrayExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_ARRAY_EXPRESSION_MAX);
if (solver()->state() == Solver::OUTSIDE_SEARCH &&
!absl::GetFlag(FLAGS_cp_disable_cache) &&
var_array_expressions_[type]->Find(vars) == nullptr) {
var_array_expressions_[type]->UnsafeInsert(vars, expression);
}
}
// Var Array Constant Array Expressions.
IntExpr* FindVarArrayConstantArrayExpression(
const std::vector<IntVar*>& vars, const std::vector<int64_t>& values,
VarArrayConstantArrayExpressionType type) const override {
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_ARRAY_CONSTANT_ARRAY_EXPRESSION_MAX);
return var_array_constant_array_expressions_[type]->Find(vars, values);
}
void InsertVarArrayConstantArrayExpression(
IntExpr* const expression, const std::vector<IntVar*>& vars,
const std::vector<int64_t>& values,
VarArrayConstantArrayExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_ARRAY_CONSTANT_ARRAY_EXPRESSION_MAX);
if (solver()->state() != Solver::IN_SEARCH &&
var_array_constant_array_expressions_[type]->Find(vars, values) ==
nullptr) {
var_array_constant_array_expressions_[type]->UnsafeInsert(vars, values,
expression);
}
}
// Var Array Constant Expressions.
IntExpr* FindVarArrayConstantExpression(
const std::vector<IntVar*>& vars, int64_t value,
VarArrayConstantExpressionType type) const override {
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_ARRAY_CONSTANT_EXPRESSION_MAX);
return var_array_constant_expressions_[type]->Find(vars, value);
}
void InsertVarArrayConstantExpression(
IntExpr* const expression, const std::vector<IntVar*>& vars,
int64_t value, VarArrayConstantExpressionType type) override {
DCHECK(expression != nullptr);
DCHECK_GE(type, 0);
DCHECK_LT(type, VAR_ARRAY_CONSTANT_EXPRESSION_MAX);
if (solver()->state() != Solver::IN_SEARCH &&
var_array_constant_expressions_[type]->Find(vars, value) == nullptr) {
var_array_constant_expressions_[type]->UnsafeInsert(vars, value,
expression);
}
}
private:
std::vector<Constraint*> void_constraints_;
std::vector<VarConstantConstraintCache*> var_constant_constraints_;
std::vector<ExprExprConstraintCache*> expr_expr_constraints_;
std::vector<VarConstantConstantConstraintCache*>
var_constant_constant_constraints_;
std::vector<ExprIntExprCache*> expr_expressions_;
std::vector<ExprConstantIntExprCache*> expr_constant_expressions_;
std::vector<ExprExprIntExprCache*> expr_expr_expressions_;
std::vector<VarConstantConstantIntExprCache*>
var_constant_constant_expressions_;
std::vector<VarConstantArrayIntExprCache*> var_constant_array_expressions_;
std::vector<VarArrayIntExprCache*> var_array_expressions_;
std::vector<VarArrayConstantArrayIntExprCache*>
var_array_constant_array_expressions_;
std::vector<VarArrayConstantIntExprCache*> var_array_constant_expressions_;
std::vector<ExprExprConstantIntExprCache*> expr_expr_constant_expressions_;
};
} // namespace
ModelCache* BuildModelCache(Solver* solver) {
return new NonReversibleCache(solver);
}
ModelCache* Solver::Cache() const { return model_cache_.get(); }
} // namespace operations_research