forked from mlubin/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstraintSolverTests.cs
934 lines (815 loc) · 34 KB
/
ConstraintSolverTests.cs
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
// 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.
using System;
using Xunit;
using Google.OrTools.ConstraintSolver;
using static Google.OrTools.ConstraintSolver.operations_research_constraint_solver;
namespace Google.OrTools.Tests
{
public class ConstraintSolverTest
{
[Fact]
public void IntVectorToInt64Vector()
{
int[] input = { 5, 11, 17 };
long[] output = ToInt64Vector(input);
Assert.Equal(3, output.Length);
Assert.Equal(5, output[0]);
Assert.Equal(11, output[1]);
Assert.Equal(17, output[2]);
}
[Fact]
public void IntVarConstructor()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 7, "x");
Assert.Equal(3, x.Min());
Assert.Equal(7, x.Max());
Assert.Equal("x(3..7)", x.ToString());
}
[Fact]
public void ConstraintConstructor()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 7, "x");
Assert.Equal("x(3..7)", x.ToString());
// Unary operator
Constraint c0 = (x == 5);
Assert.Equal("(x(3..7) == 5)", c0.ToString());
IntExpr e1 = -c0;
Assert.Equal("-(Watch<x == 5>(0 .. 1))", e1.ToString());
IntExpr e2 = c0.Abs();
Assert.Equal("Watch<x == 5>(0 .. 1)", e2.ToString());
IntExpr e3 = c0.Square();
Assert.Equal("IntSquare(Watch<x == 5>(0 .. 1))", e3.ToString());
// Relational operator with a scalar
Constraint c1 = x == 5;
Assert.Equal("(x(3..7) == 5)", c1.ToString());
Constraint c2 = x >= 5;
Assert.Equal("(x(3..7) >= 5)", c2.ToString());
Constraint c3 = x > 5;
Assert.Equal("(x(3..7) >= 6)", c3.ToString());
Constraint c4 = x <= 5;
Assert.Equal("(x(3..7) <= 5)", c4.ToString());
Constraint c5 = x < 5;
Assert.Equal("(x(3..7) <= 4)", c5.ToString());
Constraint c6 = x != 5;
Assert.Equal("(x(3..7) != 5)", c6.ToString());
Constraint c7 = x == 2;
Assert.Equal("FalseConstraint()", c7.ToString());
}
[Fact]
public void IntExprConstructor()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
// Unary Operator
IntExpr e1 = -(x == 7);
Assert.Equal("-(Watch<x == 7>(0 .. 1))", e1.ToString());
IntExpr e2 = (x == 7).Abs();
Assert.Equal("Watch<x == 7>(0 .. 1)", e2.ToString());
IntExpr e3 = (x == 7).Square();
Assert.Equal("IntSquare(Watch<x == 7>(0 .. 1))", e3.ToString());
}
[Fact]
public void GreaterIntExprConstructor()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
// Unary Operator
IntExpr e1 = -(x >= 7);
Assert.Equal("-(Watch<x >= 7>(0 .. 1))", e1.ToString());
IntExpr e2 = (x >= 7).Abs();
Assert.Equal("Watch<x >= 7>(0 .. 1)", e2.ToString());
IntExpr e3 = (x >= 7).Square();
Assert.Equal("IntSquare(Watch<x >= 7>(0 .. 1))", e3.ToString());
}
[Fact]
public void ConstraintAndScalar()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
Constraint c1 = (x == 7);
Assert.Equal("(x(3..13) == 7)", c1.ToString());
// Arithmetic operator with a scalar
IntExpr e2a = c1 + 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2a.ToString());
IntExpr e2b = 1 + c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2b.ToString());
IntExpr e2c = c1 - 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) + -1)", e2c.ToString());
IntExpr e2d = 1 - c1;
Assert.Equal("Not(Watch<x == 7>(0 .. 1))", e2d.ToString());
IntExpr e2e = c1 * 2;
Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2e.ToString());
IntExpr e2f = 2 * c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2f.ToString());
IntExpr e2g = c1 / 4;
Assert.Equal("(Watch<x == 7>(0 .. 1) div 4)", e2g.ToString());
// Relational operator with a scalar
Constraint c8a = c1 == 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8a.ToString());
Constraint c8b = 1 == c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8b.ToString());
Constraint c8c = c1 != 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8c.ToString());
Constraint c8d = 1 != c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8d.ToString());
Constraint c8e = c1 >= 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8e.ToString());
Constraint c8f = 1 >= c1;
Assert.Equal("TrueConstraint()", c8f.ToString());
Constraint c8g = c1 > 1;
Assert.Equal("FalseConstraint()", c8g.ToString());
Constraint c8h = 1 > c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8h.ToString());
Constraint c8i = c1 <= 1;
Assert.Equal("TrueConstraint()", c8i.ToString());
Constraint c8j = 1 <= c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8j.ToString());
Constraint c8k = c1 < 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8k.ToString());
Constraint c8l = 1 < c1;
Assert.Equal("FalseConstraint()", c8l.ToString());
}
[Fact]
public void ConstraintAndIntVar()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
Constraint c1 = x == 7;
Assert.Equal("(x(3..13) == 7)", c1.ToString());
IntVar y = solver.MakeIntVar(5, 17, "y");
Assert.Equal(5, y.Min());
Assert.Equal(17, y.Max());
// Arithmetic operator with IntVar
IntExpr e3a = c1 + y;
Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3a.ToString());
IntExpr e3b = y + c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3b.ToString());
IntExpr e3c = c1 - y;
Assert.Equal("(Watch<x == 7>(0 .. 1) - y(5..17))", e3c.ToString());
IntExpr e3d = y - c1;
Assert.Equal("(y(5..17) - Watch<x == 7>(0 .. 1))", e3d.ToString());
IntExpr e3e = c1 * y;
Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3e.ToString());
IntExpr e3f = y * c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3f.ToString());
// Relational operator with an IntVar
Constraint c9a = c1 == y;
Assert.Equal("Watch<x == 7>(0 .. 1) == y(5..17)", c9a.ToString());
Constraint c9b = y == c1;
Assert.Equal("y(5..17) == Watch<x == 7>(0 .. 1)", c9b.ToString());
Constraint c9c = c1 != y;
Assert.Equal("Watch<x == 7>(0 .. 1) != y(5..17)", c9c.ToString());
Constraint c9d = y != c1;
Assert.Equal("y(5..17) != Watch<x == 7>(0 .. 1)", c9d.ToString());
Constraint c9e = c1 >= y;
Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9e.ToString());
Constraint c9f = y >= c1;
Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9f.ToString());
Constraint c9g = c1 > y;
Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9g.ToString());
Constraint c9h = y > c1;
Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9h.ToString());
Constraint c9i = c1 <= y;
Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9i.ToString());
Constraint c9j = y <= c1;
Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9j.ToString());
Constraint c9k = c1 < y;
Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9k.ToString());
Constraint c9l = y < c1;
Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9l.ToString());
}
[Fact]
public void ConstraintAndIntExpr()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
Constraint c1 = x == 7;
Assert.Equal("(x(3..13) == 7)", c1.ToString());
IntVar y = solver.MakeIntVar(5, 17, "y");
Assert.Equal(5, y.Min());
Assert.Equal(17, y.Max());
// Arithmetic operator with an IntExpr
IntExpr e11a = c1 + (y == 11);
Assert.Equal("(Watch<x == 7>(0 .. 1) + Watch<y == 11>(0 .. 1))", e11a.ToString());
IntExpr e11b = (y == 11) + c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) + Watch<y == 11>(0 .. 1))", e11b.ToString());
IntExpr e11c = c1 - (y == 11);
Assert.Equal("(Watch<x == 7>(0 .. 1) - Watch<y == 11>(0 .. 1))", e11c.ToString());
IntExpr e11d = (y == 11) - c1;
Assert.Equal("(Watch<y == 11>(0 .. 1) - Watch<x == 7>(0 .. 1))", e11d.ToString());
IntExpr e11e = c1 * (y == 11);
Assert.Equal("(Watch<x == 7>(0 .. 1) * Watch<y == 11>(0 .. 1))", e11e.ToString());
IntExpr e11f = (y == 11) * c1;
Assert.Equal("(Watch<x == 7>(0 .. 1) * Watch<y == 11>(0 .. 1))", e11f.ToString());
// Relational operator with an IntExpr
Constraint c12a = c1 == (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) == Watch<y == 11>(0 .. 1)", c12a.ToString());
Constraint c12b = (y == 11) == c1;
Assert.Equal("Watch<y == 11>(0 .. 1) == Watch<x == 7>(0 .. 1)", c12b.ToString());
Constraint c12c = c1 != (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) != Watch<y == 11>(0 .. 1)", c12c.ToString());
Constraint c12d = (y == 11) != c1;
Assert.Equal("Watch<y == 11>(0 .. 1) != Watch<x == 7>(0 .. 1)", c12d.ToString());
Constraint c12e = c1 >= (y == 11);
Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c12e.ToString());
Constraint c12f = (y == 11) >= c1;
Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c12f.ToString());
Constraint c12g = c1 > (y == 11);
Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c12g.ToString());
Constraint c12h = (y == 11) > c1;
Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c12h.ToString());
Constraint c12i = c1 <= (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c12i.ToString());
Constraint c12j = (y == 11) <= c1;
Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c12j.ToString());
Constraint c12k = c1 < (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c12k.ToString());
Constraint c12l = (y == 11) < c1;
Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c12l.ToString());
}
[Fact]
public void ConstraintAndConstraint()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
Constraint c1 = x == 7;
Assert.Equal("(x(3..13) == 7)", c1.ToString());
IntVar y = solver.MakeIntVar(5, 17, "y");
Assert.Equal(5, y.Min());
Assert.Equal(17, y.Max());
Constraint c2 = y == 11;
Assert.Equal("(y(5..17) == 11)", c2.ToString());
// Relational operator with a Constraint
Constraint c10a = c1 == c2;
Assert.Equal("Watch<x == 7>(0 .. 1) == Watch<y == 11>(0 .. 1)", c10a.ToString());
Constraint c10b = c1 != c2;
Assert.Equal("Watch<x == 7>(0 .. 1) != Watch<y == 11>(0 .. 1)", c10b.ToString());
Constraint c10c = c1 <= c2;
Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c10c.ToString());
Constraint c10d = c1 >= c2;
Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c10d.ToString());
Constraint c10e = c1 > c2;
Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c10e.ToString());
Constraint c10f = c1 < c2;
Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c10f.ToString());
}
[Fact]
public void IntExprAndScalar()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
// Arithmetic operator with a scalar
IntExpr e2a = (x == 7) + 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2a.ToString());
IntExpr e2b = 1 + (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2b.ToString());
IntExpr e2c = (x == 7) - 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) + -1)", e2c.ToString());
IntExpr e2d = 1 - (x == 7);
Assert.Equal("Not(Watch<x == 7>(0 .. 1))", e2d.ToString());
IntExpr e2e = (x == 7) * 2;
Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2e.ToString());
IntExpr e2f = 2 * (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2f.ToString());
IntExpr e2g = (x == 7) / 4;
Assert.Equal("(Watch<x == 7>(0 .. 1) div 4)", e2g.ToString());
// Relational operator with a scalar
Constraint c8a = (x == 7) == 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8a.ToString());
Constraint c8b = 1 == (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8b.ToString());
Constraint c8c = (x == 7) != 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8c.ToString());
Constraint c8d = 1 != (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8d.ToString());
Constraint c8e = (x == 7) >= 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8e.ToString());
Constraint c8f = 1 >= (x == 7);
Assert.Equal("TrueConstraint()", c8f.ToString());
Constraint c8g = (x == 7) > 1;
Assert.Equal("FalseConstraint()", c8g.ToString());
Constraint c8h = 1 > (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8h.ToString());
Constraint c8i = (x == 7) <= 1;
Assert.Equal("TrueConstraint()", c8i.ToString());
Constraint c8j = 1 <= (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8j.ToString());
Constraint c8k = (x == 7) < 1;
Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8k.ToString());
Constraint c8l = 1 < (x == 7);
Assert.Equal("FalseConstraint()", c8l.ToString());
}
[Fact]
public void IntExprAndIntVar()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
IntVar y = solver.MakeIntVar(5, 17, "y");
Assert.Equal(5, y.Min());
Assert.Equal(17, y.Max());
// Arithmetic operator with IntVar
IntExpr e3a = (x == 7) + y;
Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3a.ToString());
IntExpr e3b = y + (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3b.ToString());
IntExpr e3c = (x == 7) - y;
Assert.Equal("(Watch<x == 7>(0 .. 1) - y(5..17))", e3c.ToString());
IntExpr e3d = y - (x == 7);
Assert.Equal("(y(5..17) - Watch<x == 7>(0 .. 1))", e3d.ToString());
IntExpr e3e = (x == 7) * y;
Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3e.ToString());
IntExpr e3f = y * (x == 7);
Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3f.ToString());
// Relational operator with an IntVar
Constraint c9a = (x == 7) == y;
Assert.Equal("Watch<x == 7>(0 .. 1) == y(5..17)", c9a.ToString());
Constraint c9b = y == (x == 7);
Assert.Equal("y(5..17) == Watch<x == 7>(0 .. 1)", c9b.ToString());
Constraint c9c = (x == 7) != y;
Assert.Equal("Watch<x == 7>(0 .. 1) != y(5..17)", c9c.ToString());
Constraint c9d = y != (x == 7);
Assert.Equal("y(5..17) != Watch<x == 7>(0 .. 1)", c9d.ToString());
Constraint c9e = (x == 7) >= y;
Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9e.ToString());
Constraint c9f = y >= (x == 7);
Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9f.ToString());
Constraint c9g = (x == 7) > y;
Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9g.ToString());
Constraint c9h = y > (x == 7);
Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9h.ToString());
Constraint c9i = (x == 7) <= y;
Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9i.ToString());
Constraint c9j = y <= (x == 7);
Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9j.ToString());
Constraint c9k = (x == 7) < y;
Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9k.ToString());
Constraint c9l = y < (x == 7);
Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9l.ToString());
}
[Fact]
public void IntExprAndIntExpr()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
IntVar y = solver.MakeIntVar(5, 17, "y");
Assert.Equal(5, y.Min());
Assert.Equal(17, y.Max());
// Relational operator between IntExpr
Constraint c10a = (x == 7) == (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) == Watch<y == 11>(0 .. 1)", c10a.ToString());
Constraint c10b = (x == 7) != (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) != Watch<y == 11>(0 .. 1)", c10b.ToString());
Constraint c10c = (x == 7) <= (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c10c.ToString());
Constraint c10d = (x == 7) >= (y == 11);
Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c10d.ToString());
Constraint c10e = (x == 7) > (y == 11);
Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c10e.ToString());
Constraint c10f = (x == 7) < (y == 11);
Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c10f.ToString());
}
[Fact]
public void GreaterIntExprAndScalar()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
// Arithmetic operator with a scalar
IntExpr e2a = (x >= 7) + 1;
Assert.Equal("(Watch<x >= 7>(0 .. 1) + 1)", e2a.ToString());
IntExpr e2b = 1 + (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) + 1)", e2b.ToString());
IntExpr e2c = (x >= 7) - 1;
Assert.Equal("(Watch<x >= 7>(0 .. 1) + -1)", e2c.ToString());
IntExpr e2d = 1 - (x >= 7);
Assert.Equal("Not(Watch<x >= 7>(0 .. 1))", e2d.ToString());
IntExpr e2e = (x >= 7) * 2;
Assert.Equal("(Watch<x >= 7>(0 .. 1) * 2)", e2e.ToString());
IntExpr e2f = 2 * (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) * 2)", e2f.ToString());
// Relational operator with a scalar
Constraint c8a = (x >= 7) == 1;
Assert.Equal("(Watch<x >= 7>(0 .. 1) == 1)", c8a.ToString());
Constraint c8b = 1 == (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) == 1)", c8b.ToString());
Constraint c8c = (x >= 7) != 1;
Assert.Equal("(Watch<x >= 7>(0 .. 1) != 1)", c8c.ToString());
Constraint c8d = 1 != (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) != 1)", c8d.ToString());
Constraint c8e = (x >= 7) >= 1;
Assert.Equal("(Watch<x >= 7>(0 .. 1) >= 1)", c8e.ToString());
Constraint c8f = 1 >= (x >= 7);
Assert.Equal("TrueConstraint()", c8f.ToString());
Constraint c8g = (x >= 7) > 1;
Assert.Equal("FalseConstraint()", c8g.ToString());
Constraint c8h = 1 > (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) <= 0)", c8h.ToString());
Constraint c8i = (x >= 7) <= 1;
Assert.Equal("TrueConstraint()", c8i.ToString());
Constraint c8j = 1 <= (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) >= 1)", c8j.ToString());
Constraint c8k = (x >= 7) < 1;
Assert.Equal("(Watch<x >= 7>(0 .. 1) <= 0)", c8k.ToString());
Constraint c8l = 1 < (x >= 7);
Assert.Equal("FalseConstraint()", c8l.ToString());
}
[Fact]
public void GreaterIntExprAndIntVar()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
IntVar y = solver.MakeIntVar(5, 17, "y");
Assert.Equal(5, y.Min());
Assert.Equal(17, y.Max());
// Arithmetic operator with IntVar
IntExpr e3a = (x >= 7) + y;
Assert.Equal("(Watch<x >= 7>(0 .. 1) + y(5..17))", e3a.ToString());
IntExpr e3b = y + (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) + y(5..17))", e3b.ToString());
IntExpr e3c = (x >= 7) - y;
Assert.Equal("(Watch<x >= 7>(0 .. 1) - y(5..17))", e3c.ToString());
IntExpr e3d = y - (x >= 7);
Assert.Equal("(y(5..17) - Watch<x >= 7>(0 .. 1))", e3d.ToString());
IntExpr e3e = (x >= 7) * y;
Assert.Equal("(Watch<x >= 7>(0 .. 1) * y(5..17))", e3e.ToString());
IntExpr e3f = y * (x >= 7);
Assert.Equal("(Watch<x >= 7>(0 .. 1) * y(5..17))", e3f.ToString());
// Relational operator with an IntVar
Constraint c9a = (x >= 7) == y;
Assert.Equal("Watch<x >= 7>(0 .. 1) == y(5..17)", c9a.ToString());
Constraint c9b = y == (x >= 7);
Assert.Equal("y(5..17) == Watch<x >= 7>(0 .. 1)", c9b.ToString());
Constraint c9c = (x >= 7) != y;
Assert.Equal("Watch<x >= 7>(0 .. 1) != y(5..17)", c9c.ToString());
Constraint c9d = y != (x >= 7);
Assert.Equal("y(5..17) != Watch<x >= 7>(0 .. 1)", c9d.ToString());
Constraint c9e = (x >= 7) >= y;
Assert.Equal("y(5..17) <= Watch<x >= 7>(0 .. 1)", c9e.ToString());
Constraint c9f = y >= (x >= 7);
Assert.Equal("Watch<x >= 7>(0 .. 1) <= y(5..17)", c9f.ToString());
Constraint c9g = (x >= 7) > y;
Assert.Equal("y(5..17) < Watch<x >= 7>(0 .. 1)", c9g.ToString());
Constraint c9h = y > (x >= 7);
Assert.Equal("Watch<x >= 7>(0 .. 1) < y(5..17)", c9h.ToString());
Constraint c9i = (x >= 7) <= y;
Assert.Equal("Watch<x >= 7>(0 .. 1) <= y(5..17)", c9i.ToString());
Constraint c9j = y <= (x >= 7);
Assert.Equal("y(5..17) <= Watch<x >= 7>(0 .. 1)", c9j.ToString());
Constraint c9k = (x >= 7) < y;
Assert.Equal("Watch<x >= 7>(0 .. 1) < y(5..17)", c9k.ToString());
Constraint c9l = y < (x >= 7);
Assert.Equal("y(5..17) < Watch<x >= 7>(0 .. 1)", c9l.ToString());
}
[Fact]
public void GreaterIntExprAndGreaterIntExpr()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 13, "x");
Assert.Equal(3, x.Min());
Assert.Equal(13, x.Max());
IntVar y = solver.MakeIntVar(5, 17, "y");
Assert.Equal(5, y.Min());
Assert.Equal(17, y.Max());
// Relational operator between IntExpr
Constraint c10a = (x >= 7) == (y >= 11);
Assert.Equal("Watch<x >= 7>(0 .. 1) == Watch<y >= 11>(0 .. 1)", c10a.ToString());
Constraint c10b = (x >= 7) != (y >= 11);
Assert.Equal("Watch<x >= 7>(0 .. 1) != Watch<y >= 11>(0 .. 1)", c10b.ToString());
Constraint c10c = (x >= 7) <= (y >= 11);
Assert.Equal("Watch<x >= 7>(0 .. 1) <= Watch<y >= 11>(0 .. 1)", c10c.ToString());
Constraint c10d = (x >= 7) >= (y >= 11);
Assert.Equal("Watch<y >= 11>(0 .. 1) <= Watch<x >= 7>(0 .. 1)", c10d.ToString());
Constraint c10e = (x >= 7) > (y >= 11);
Assert.Equal("Watch<y >= 11>(0 .. 1) < Watch<x >= 7>(0 .. 1)", c10e.ToString());
Constraint c10f = (x >= 7) < (y >= 11);
Assert.Equal("Watch<x >= 7>(0 .. 1) < Watch<y >= 11>(0 .. 1)", c10f.ToString());
}
[Fact]
public void Downcast()
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(2, 17, "x");
IntExpr e = x + 5;
IntVar y = e.Var();
Assert.Equal("(x(2..17) + 5)", y.ToString());
}
[Fact]
public void Sequence()
{
Solver solver = new Solver("Solver");
IntervalVar[] intervals = solver.MakeFixedDurationIntervalVarArray(10, 0, 10, 5, false, "task");
DisjunctiveConstraint disjunctive = intervals.Disjunctive("Sequence");
SequenceVar var = disjunctive.SequenceVar();
Assignment ass = solver.MakeAssignment();
ass.Add(var);
ass.SetForwardSequence(var, new int[] { 1, 3, 5 });
int[] seq = ass.ForwardSequence(var);
Assert.Equal(3, seq.Length);
Assert.Equal(1, seq[0]);
Assert.Equal(3, seq[1]);
Assert.Equal(5, seq[2]);
}
// A simple demon that simply sets the maximum of a fixed IntVar to 10 when
// it's being called.
class SetMaxDemon : NetDemon
{
public SetMaxDemon(IntVar x)
{
x_ = x;
}
public override void Run(Solver s)
{
x_.SetMax(10);
}
private IntVar x_;
}
[Fact]
public void Demon()
{
Solver solver = new Solver("DemonTest");
IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
NetDemon demon = new SetMaxDemon(x);
Assert.Equal(11, x.Max());
demon.Run(solver);
Assert.Equal(10, x.Max());
}
// This constraint has a single target variable x. It enforces x >= 5 upon
// InitialPropagate() and invokes the SetMaxDemon when x changes its range.
class SetMinAndMaxConstraint : NetConstraint
{
public SetMinAndMaxConstraint(Solver solver, IntVar x) : base(solver)
{
x_ = x;
}
public override void Post()
{
// Always store the demon in the constraint to avoid it being reclaimed
// by the GC.
demon_ = new SetMaxDemon(x_);
x_.WhenBound(demon_);
}
public override void InitialPropagate()
{
x_.SetMin(5);
}
private IntVar x_;
private Demon demon_;
}
[Fact]
public void MinAndMaxConstraint()
{
Solver solver = new Solver("TestConstraint");
IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
Constraint ct = new SetMinAndMaxConstraint(solver, x);
solver.Add(ct);
DecisionBuilder db = solver.MakePhase(x, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
Assert.Equal(-1, x.Min());
Assert.Equal(11, x.Max());
Assert.True(solver.NextSolution());
Assert.Equal(6, x.Min());
Assert.Equal(6, x.Max());
Assert.True(solver.NextSolution());
Assert.Equal(10, x.Min());
Assert.Equal(10, x.Max());
Assert.False(solver.NextSolution());
solver.EndSearch();
}
// // This constraint has a single target variable x. It enforces x >= 5,
// but only at the leaf of the search tree: it doesn't change x's bounds,
// and simply fails if x is bound and is < 5.
class DumbGreaterOrEqualToFive : NetConstraint
{
public DumbGreaterOrEqualToFive(Solver solver, IntVar x) : base(solver)
{
x_ = x;
}
public override void Post()
{
demon_ = solver().MakeConstraintInitialPropagateCallback(this);
x_.WhenRange(demon_);
}
public override void InitialPropagate()
{
if (x_.Bound())
{
if (x_.Value() < 5)
{
solver().Fail();
}
}
}
private IntVar x_;
private Demon demon_;
}
[Fact]
public void FailingConstraint()
{
Solver solver = new Solver("TestConstraint");
IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
Constraint ct = new DumbGreaterOrEqualToFive(solver, x);
solver.Add(ct);
DecisionBuilder db = solver.MakePhase(x, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
Assert.True(solver.NextSolution());
Assert.Equal(6, x.Min());
solver.EndSearch();
}
[Fact]
public void DomainIterator()
{
Solver solver = new Solver("TestConstraint");
IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
ulong count = 0;
foreach (long value in x.GetDomain())
{
count++;
}
Assert.Equal(count, x.Size());
}
class CountHoles : NetDemon
{
public CountHoles(IntVar x)
{
x_ = x;
count_ = 0;
}
public override void Run(Solver s)
{
foreach (long removed in x_.GetHoles())
{
count_++;
}
}
public int count()
{
return count_;
}
private IntVar x_;
private int count_;
}
class RemoveThreeValues : NetConstraint
{
public RemoveThreeValues(Solver solver, IntVar x) : base(solver)
{
x_ = x;
}
public override void Post()
{
demon_ = new CountHoles(x_);
x_.WhenDomain(demon_);
}
public override void InitialPropagate()
{
x_.RemoveValues(new long[] { 3, 5, 7 });
}
public int count()
{
return demon_.count();
}
private IntVar x_;
private CountHoles demon_;
}
[Fact]
public void HoleIteratorTest()
{
Solver solver = new Solver("TestConstraint");
IntVar x = solver.MakeIntVar(0, 10, "x");
RemoveThreeValues ct = new RemoveThreeValues(solver, x);
solver.Add(ct);
DecisionBuilder db = solver.MakePhase(x, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE);
solver.Solve(db);
Assert.Equal(3, ct.count());
}
// TODO(user): Improve search log tests; currently only tests coverage.
void RunSearchLog(in SearchMonitor searchlog)
{
searchlog.EnterSearch();
searchlog.ExitSearch();
searchlog.AcceptSolution();
searchlog.AtSolution();
searchlog.BeginFail();
searchlog.NoMoreSolutions();
searchlog.BeginInitialPropagation();
searchlog.EndInitialPropagation();
}
[Fact]
public void SearchLog()
{
Solver solver = new Solver("TestSearchLog");
IntVar var = solver.MakeIntVar(1, 1, "Variable");
OptimizeVar objective = solver.MakeMinimize(var, 1);
SearchMonitor searchlog = solver.MakeSearchLog(0);
RunSearchLog(in searchlog);
GC.KeepAlive(solver);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SearchLogWithCallback(bool callGC)
{
Solver solver = new Solver("TestSearchLog");
IntVar var = solver.MakeIntVar(1, 1, "Variable");
OptimizeVar objective = solver.MakeMinimize(var, 1);
int count = 0;
SearchMonitor searchlog = solver.MakeSearchLog(0, // branch period
() =>
{
count++;
return "display callback...";
});
if (callGC)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
RunSearchLog(in searchlog);
GC.KeepAlive(solver);
Assert.Equal(1, count);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SearchLogWithObjectiveAndCallback(bool callGC)
{
Solver solver = new Solver("TestSearchLog");
IntVar var = solver.MakeIntVar(1, 1, "Variable");
OptimizeVar objective = solver.MakeMinimize(var, 1);
int count = 0;
SearchMonitor searchlog = solver.MakeSearchLog(0, // branch period
objective, // objective var to monitor
() =>
{
count++;
return "OptimizeVar display callback";
});
if (callGC)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
RunSearchLog(in searchlog);
GC.KeepAlive(solver);
Assert.Equal(1, count);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SearchLogWithIntVarAndCallback(bool callGC)
{
Solver solver = new Solver("TestSearchLog");
IntVar var = solver.MakeIntVar(1, 1, "Variable");
OptimizeVar objective = solver.MakeMinimize(var, 1);
int count = 0;
SearchMonitor searchlog = solver.MakeSearchLog(0, // branch period
var, // int var to monitor
() =>
{
count++;
return "IntVar display callback";
});
if (callGC)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
RunSearchLog(in searchlog);
GC.KeepAlive(solver);
Assert.Equal(1, count);
}
}
} // namespace Google.OrTools.Tests