16
16
using Xunit ;
17
17
using Google . OrTools . Sat ;
18
18
19
+ public class SolutionCounter : CpSolverSolutionCallback
20
+ {
21
+ public SolutionCounter ( )
22
+ {
23
+ Console . WriteLine ( "SolutionCounter Ctor" ) ;
24
+ solution_count_ = 0 ;
25
+ }
26
+
27
+ public override void OnSolutionCallback ( )
28
+ {
29
+ solution_count_ += 1 ;
30
+ }
31
+
32
+ public int SolutionCount
33
+ {
34
+ get {
35
+ return solution_count_ ;
36
+ }
37
+ set {
38
+ solution_count_ = value ;
39
+ }
40
+ }
41
+ private int solution_count_ ;
42
+ }
43
+
44
+ public class SolutionDivisionCounter : SolutionCounter
45
+ {
46
+ public SolutionDivisionCounter ( int result , IntVar a , IntVar b ) : base ( )
47
+ {
48
+ result_ = result ;
49
+ a_ = a ;
50
+ b_ = b ;
51
+ }
52
+
53
+ public override void OnSolutionCallback ( )
54
+ {
55
+ base . OnSolutionCallback ( ) ;
56
+ foreach ( IntVar v in new IntVar [ ] { a_ , b_ } )
57
+ {
58
+ Console . Write ( String . Format ( "{0}={1} " , v . ToString ( ) , Value ( v ) ) ) ;
59
+ }
60
+ Console . WriteLine ( ) ;
61
+ Assert . Equal ( result_ , Value ( a_ ) / Value ( b_ ) ) ;
62
+ }
63
+
64
+ private int result_ ;
65
+ private IntVar a_ ;
66
+ private IntVar b_ ;
67
+ }
68
+
69
+ public class SolutionModuloCounter : SolutionCounter
70
+ {
71
+ public SolutionModuloCounter ( int result , IntVar a , IntVar b ) : base ( )
72
+ {
73
+ result_ = result ;
74
+ a_ = a ;
75
+ b_ = b ;
76
+ }
77
+
78
+ public override void OnSolutionCallback ( )
79
+ {
80
+ base . OnSolutionCallback ( ) ;
81
+ foreach ( IntVar v in new IntVar [ ] { a_ , b_ } )
82
+ {
83
+ Console . Write ( String . Format ( "{0}={1} " , v . ToString ( ) , Value ( v ) ) ) ;
84
+ }
85
+ Console . WriteLine ( ) ;
86
+ Assert . Equal ( result_ , Value ( a_ ) % Value ( b_ ) ) ;
87
+ }
88
+
89
+ private int result_ ;
90
+ private IntVar a_ ;
91
+ private IntVar b_ ;
92
+ }
93
+
19
94
namespace Google . OrTools . Tests
20
95
{
21
96
public class SatSolverTest
@@ -257,15 +332,19 @@ public void Division()
257
332
model . AddDivisionEquality ( 3 , v1 , v2 ) ;
258
333
// Console.WriteLine(model.Model);
259
334
335
+ SolutionDivisionCounter solution_callback = new SolutionDivisionCounter ( 3 , v1 , v2 ) ;
336
+
260
337
CpSolver solver = new CpSolver ( ) ;
261
- CpSolverStatus status = solver . Solve ( model ) ;
338
+ // Tell the solver to search for all solutions.
339
+ solver . StringParameters = "enumerate_all_solutions:true" ;
340
+ CpSolverStatus status = solver . Solve ( model , solution_callback ) ;
341
+
262
342
Assert . Equal ( CpSolverStatus . Optimal , status ) ;
343
+ Assert . Equal ( 0 , solver . ObjectiveValue ) ;
344
+
345
+ Assert . Equal ( 5 , solution_callback . SolutionCount ) ;
263
346
264
347
CpSolverResponse response = solver . Response ;
265
- Assert . Equal ( 3 , solver . Value ( v1 ) ) ;
266
- Assert . Equal ( 1 , solver . Value ( v2 ) ) ;
267
- Assert . Equal ( new long [ ] { 3 , 1 } , response . Solution ) ;
268
- Assert . Equal ( 0 , response . ObjectiveValue ) ;
269
348
// Console.WriteLine("response = " + response.ToString());
270
349
}
271
350
@@ -278,15 +357,19 @@ public void Modulo()
278
357
model . AddModuloEquality ( 3 , v1 , v2 ) ;
279
358
// Console.WriteLine(model.Model);
280
359
360
+ SolutionModuloCounter solution_callback = new SolutionModuloCounter ( 3 , v1 , v2 ) ;
361
+
281
362
CpSolver solver = new CpSolver ( ) ;
282
- CpSolverStatus status = solver . Solve ( model ) ;
363
+ // Tell the solver to search for all solutions.
364
+ solver . StringParameters = "enumerate_all_solutions:true" ;
365
+ CpSolverStatus status = solver . Solve ( model , solution_callback ) ;
366
+
283
367
Assert . Equal ( CpSolverStatus . Optimal , status ) ;
368
+ Assert . Equal ( 0 , solver . ObjectiveValue ) ;
369
+
370
+ Assert . Equal ( 11 , solution_callback . SolutionCount ) ;
284
371
285
372
CpSolverResponse response = solver . Response ;
286
- Assert . Equal ( 3 , solver . Value ( v1 ) ) ;
287
- Assert . Equal ( 4 , solver . Value ( v2 ) ) ;
288
- Assert . Equal ( new long [ ] { 3 , 4 } , response . Solution ) ;
289
- Assert . Equal ( 0 , response . ObjectiveValue ) ;
290
373
// Console.WriteLine("response = " + response.ToString());
291
374
}
292
375
@@ -496,25 +579,59 @@ public void LinearExprBoolVarOperatorTest()
496
579
}
497
580
498
581
[ Fact ]
499
- public void LinearExprNotBoolVarOperatorTest ( )
582
+ public void TrueLiteralAsExpressionTest ( )
500
583
{
501
- Console . WriteLine ( "LinearExprBoolVarNotOperatorTest " ) ;
584
+ Console . WriteLine ( "TrueLiteralAsExpressionTest " ) ;
502
585
CpModel model = new CpModel ( ) ;
503
- ILiteral v = model . NewBoolVar ( "v" ) ;
504
- LinearExpr e = v . NotAsExpr ( ) * 2 ;
586
+ ILiteral v = model . TrueLiteral ( ) ;
587
+ LinearExpr e = v . AsExpr ( ) * 2 ;
588
+ Console . WriteLine ( e ) ;
589
+ e = 2 * v . AsExpr ( ) ;
505
590
Console . WriteLine ( e ) ;
506
- e = 2 * v . NotAsExpr ( ) ;
591
+ e = v . AsExpr ( ) + 2 ;
507
592
Console . WriteLine ( e ) ;
508
- e = v . NotAsExpr ( ) + 2 ;
593
+ e = 2 + v . AsExpr ( ) ;
509
594
Console . WriteLine ( e ) ;
510
- e = 2 + v . NotAsExpr ( ) ;
595
+ e = v . AsExpr ( ) ;
511
596
Console . WriteLine ( e ) ;
512
- e = v . NotAsExpr ( ) ;
597
+ e = - v . AsExpr ( ) ;
513
598
Console . WriteLine ( e ) ;
514
- e = - v . NotAsExpr ( ) ;
599
+ e = 1 - v . AsExpr ( ) ;
515
600
Console . WriteLine ( e ) ;
516
- e = 1 - v . NotAsExpr ( ) ;
601
+ e = v . AsExpr ( ) - 1 ;
517
602
Console . WriteLine ( e ) ;
603
+ }
604
+
605
+ [ Fact ]
606
+ public void FalseLiteralAsExpressionTest ( )
607
+ {
608
+ Console . WriteLine ( "FalseLiteralAsExpressionTest" ) ;
609
+ CpModel model = new CpModel ( ) ;
610
+ ILiteral v = model . FalseLiteral ( ) ;
611
+ LinearExpr e = v . AsExpr ( ) * 2 ;
612
+ Console . WriteLine ( e ) ;
613
+ e = 2 * v . AsExpr ( ) ;
614
+ Console . WriteLine ( e ) ;
615
+ e = v . AsExpr ( ) + 2 ;
616
+ Console . WriteLine ( e ) ;
617
+ e = 2 + v . AsExpr ( ) ;
618
+ Console . WriteLine ( e ) ;
619
+ e = v . AsExpr ( ) ;
620
+ Console . WriteLine ( e ) ;
621
+ e = - v . AsExpr ( ) ;
622
+ Console . WriteLine ( e ) ;
623
+ e = 1 - v . AsExpr ( ) ;
624
+ Console . WriteLine ( e ) ;
625
+ e = v . AsExpr ( ) - 1 ;
626
+ Console . WriteLine ( e ) ;
627
+ }
628
+
629
+ [ Fact ]
630
+ public void LinearExprNotBoolVarOperatorTest ( )
631
+ {
632
+ Console . WriteLine ( "LinearExprBoolVarNotOperatorTest" ) ;
633
+ CpModel model = new CpModel ( ) ;
634
+ ILiteral v = model . NewBoolVar ( "v" ) ; TestInter
518
635
e = v . NotAsExpr ( ) - 1 ;
519
636
Console . WriteLine ( e ) ;
520
637
}
@@ -600,7 +717,7 @@ public void TestInterval()
600
717
IntervalVar i = model . NewFixedSizeIntervalVar ( v , 3 , "i" ) ;
601
718
Assert . Equal ( "v" , i . StartExpr ( ) . ToString ( ) ) ;
602
719
Assert . Equal ( "3" , i . SizeExpr ( ) . ToString ( ) ) ;
603
- Assert . Equal ( "v + 3" , i . EndExpr ( ) . ToString ( ) ) ;
720
+ Assert . Equal ( "( v + 3) " , i . EndExpr ( ) . ToString ( ) ) ;
604
721
}
605
722
}
606
723
} // namespace Google.OrTools.Tests
0 commit comments