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 { return solution_count_ ; }
35
+ set { solution_count_ = value ; }
36
+ }
37
+ private int solution_count_ ;
38
+ }
39
+
40
+ public class SolutionDivisionCounter : SolutionCounter
41
+ {
42
+ public SolutionDivisionCounter ( int result , IntVar a , IntVar b ) : base ( )
43
+ {
44
+ result_ = result ;
45
+ a_ = a ;
46
+ b_ = b ;
47
+ }
48
+
49
+ public override void OnSolutionCallback ( )
50
+ {
51
+ base . OnSolutionCallback ( ) ;
52
+ foreach ( IntVar v in new IntVar [ ] { a_ , b_ } )
53
+ {
54
+ Console . Write ( String . Format ( "{0}={1} " , v . ToString ( ) , Value ( v ) ) ) ;
55
+ }
56
+ Console . WriteLine ( ) ;
57
+ Assert . Equal ( result_ , Value ( a_ ) / Value ( b_ ) ) ;
58
+ }
59
+
60
+ private int result_ ;
61
+ private IntVar a_ ;
62
+ private IntVar b_ ;
63
+ }
64
+
65
+ public class SolutionModuloCounter : SolutionCounter
66
+ {
67
+ public SolutionModuloCounter ( int result , IntVar a , IntVar b ) : base ( )
68
+ {
69
+ result_ = result ;
70
+ a_ = a ;
71
+ b_ = b ;
72
+ }
73
+
74
+ public override void OnSolutionCallback ( )
75
+ {
76
+ base . OnSolutionCallback ( ) ;
77
+ foreach ( IntVar v in new IntVar [ ] { a_ , b_ } )
78
+ {
79
+ Console . Write ( String . Format ( "{0}={1} " , v . ToString ( ) , Value ( v ) ) ) ;
80
+ }
81
+ Console . WriteLine ( ) ;
82
+ Assert . Equal ( result_ , Value ( a_ ) % Value ( b_ ) ) ;
83
+ }
84
+
85
+ private int result_ ;
86
+ private IntVar a_ ;
87
+ private IntVar b_ ;
88
+ }
89
+
19
90
namespace Google . OrTools . Tests
20
91
{
21
92
public class SatSolverTest
@@ -257,16 +328,20 @@ public void Division()
257
328
model . AddDivisionEquality ( 3 , v1 , v2 ) ;
258
329
// Console.WriteLine(model.Model);
259
330
331
+ SolutionDivisionCounter solution_callback = new SolutionDivisionCounter ( 3 , v1 , v2 ) ;
332
+
260
333
CpSolver solver = new CpSolver ( ) ;
261
- CpSolverStatus status = solver . Solve ( model ) ;
334
+ // Tell the solver to search for all solutions.
335
+ solver . StringParameters = "enumerate_all_solutions:true" ;
336
+ CpSolverStatus status = solver . Solve ( model , solution_callback ) ;
337
+
262
338
Assert . Equal ( CpSolverStatus . Optimal , status ) ;
339
+ Assert . Equal ( 0 , solver . ObjectiveValue ) ;
340
+
341
+ Assert . Equal ( 5 , solution_callback . SolutionCount ) ;
263
342
264
343
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
- // Console.WriteLine("response = " + response.ToString());
344
+ //Console.WriteLine("response = " + response.ToString());
270
345
}
271
346
272
347
[ Fact ]
@@ -278,16 +353,20 @@ public void Modulo()
278
353
model . AddModuloEquality ( 3 , v1 , v2 ) ;
279
354
// Console.WriteLine(model.Model);
280
355
356
+ SolutionModuloCounter solution_callback = new SolutionModuloCounter ( 3 , v1 , v2 ) ;
357
+
281
358
CpSolver solver = new CpSolver ( ) ;
282
- CpSolverStatus status = solver . Solve ( model ) ;
359
+ // Tell the solver to search for all solutions.
360
+ solver . StringParameters = "enumerate_all_solutions:true" ;
361
+ CpSolverStatus status = solver . Solve ( model , solution_callback ) ;
362
+
283
363
Assert . Equal ( CpSolverStatus . Optimal , status ) ;
364
+ Assert . Equal ( 0 , solver . ObjectiveValue ) ;
365
+
366
+ Assert . Equal ( 11 , solution_callback . SolutionCount ) ;
284
367
285
368
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
- // Console.WriteLine("response = " + response.ToString());
369
+ //Console.WriteLine("response = " + response.ToString());
291
370
}
292
371
293
372
[ Fact ]
0 commit comments