15using System.Collections.Generic;
19using Xunit.Abstractions;
37 return solution_count_;
40 solution_count_ = value;
43 private int solution_count_;
58 base.OnSolutionCallback();
60 var solutionVariablesReport =
new StringBuilder();
63 solutionVariablesReport.Append($
"{v}={Value(v)} ");
65 solutionVariablesReport.AppendLine();
66 output_.WriteLine(solutionVariablesReport.ToString());
70 private readonly
int result_;
71 private readonly
IntVar a_;
72 private readonly
IntVar b_;
73 private readonly ITestOutputHelper output_;
88 base.OnSolutionCallback();
90 var solutionVariablesReport =
new StringBuilder();
93 solutionVariablesReport.Append($
"{v}={Value(v)} ");
95 solutionVariablesReport.AppendLine();
96 output_.WriteLine(solutionVariablesReport.ToString());
100 private readonly
int result_;
101 private readonly
IntVar a_;
102 private readonly
IntVar b_;
103 private readonly ITestOutputHelper output_;
110 private readonly ITestOutputHelper output_;
114 this.output_ = output;
125 static ConstraintProto NewLinear2(
int v1,
int v2,
long c1,
long c2,
long lb,
long ub)
139 static ConstraintProto NewLinear3(
int v1,
int v2,
int v3,
long c1,
long c2,
long c3,
long lb,
long ub)
163 static CpObjectiveProto NewMaximize1(
int v1,
long c1)
165 CpObjectiveProto obj =
new CpObjectiveProto();
166 obj.
Vars.Add(-v1 - 1);
168 obj.ScalingFactor = -1;
172 static CpObjectiveProto NewMaximize2(
int v1,
int v2,
long c1,
long c2)
174 CpObjectiveProto obj =
new CpObjectiveProto();
175 obj.
Vars.Add(-v1 - 1);
176 obj.
Vars.Add(-v2 - 1);
179 obj.ScalingFactor = -1;
188 model.
Variables.Add(NewIntegerVariable(-10, 10));
189 model.
Variables.Add(NewIntegerVariable(-10, 10));
190 model.
Variables.Add(NewIntegerVariable(-1000000, 1000000));
191 model.
Constraints.Add(NewLinear2(0, 1, 1, 1, -1000000, 100000));
192 model.
Constraints.Add(NewLinear3(0, 1, 2, 1, 2, -1, 0, 100000));
193 model.Objective = NewMaximize1(2, 1);
199 Assert.Equal(
new long[] { 10, 10, 30 }, response.
Solution);
207 model.
Variables.Add(NewIntegerVariable(-10, 10));
208 model.
Variables.Add(NewIntegerVariable(-10, 10));
209 model.
Constraints.Add(NewLinear2(0, 1, 1, 1, -1000000, 100000));
210 model.Objective = NewMaximize2(0, 1, 1, -2);
217 Assert.Equal(
new long[] { 10, -10 }, response.
Solution);
241 Assert.Equal(
new long[] { 10, 10, 30 }, response.
Solution);
261 Assert.Equal(
new long[] { 10, -10 }, response.
Solution);
271 model.
Add(-100000 <= v1 + 2 * v2 <= 100000);
280 Assert.Equal(-10, solver.
Value(v1));
281 Assert.Equal(10, solver.
Value(v2));
282 Assert.Equal(
new long[] { -10, 10 }, response.
Solution);
283 Assert.Equal(-30, solver.
Value(v1 - 2 * v2));
296 model.
Add(x == boolvar * 4);
297 model.
Add(delta == x - 5);
305 output_.WriteLine($
"response = {response}");
309 Assert.Equal(1, solver.
Value(boolvar));
310 Assert.Equal(4, solver.
Value(x));
311 Assert.Equal(-1, solver.
Value(delta));
312 Assert.Equal(1, solver.
Value(squaredDelta));
313 Assert.Equal(
new long[] { 1, 4, -1, 1 }, response.
Solution);
327 model.
Add(delta == x - 5);
328 long[,] tuples = { { -5, 25 }, { -4, 16 }, { -3, 9 }, { -2, 4 }, { -1, 1 }, { 0, 0 },
329 { 1, 1 }, { 2, 4 }, { 3, 9 }, { 4, 16 }, { 5, 25 } };
337 Assert.Equal(1, solver.
Value(boolvar));
338 Assert.Equal(4, solver.
Value(x));
339 Assert.Equal(-1, solver.
Value(delta));
340 Assert.Equal(1, solver.
Value(squaredDelta));
341 Assert.Equal(
new long[] { 1, 4, -1, 1 }, response.
Solution);
351 model.AddDivisionEquality(3, v1, v2);
358 solver.StringParameters =
"enumerate_all_solutions:true";
376 model.AddModuloEquality(3, v1, v2);
383 solver.StringParameters =
"enumerate_all_solutions:true";
401 model.
AddElement(v1 + 2,
new int[] { 1, 3, 5 }, 5 - v2);
421 List<IntVar> vars =
new List<IntVar>();
422 List<long> coeffs =
new List<long>();
424 for (
int i = 0; i < 100000; ++i)
430 var watch = System.Diagnostics.Stopwatch.StartNew();
433 var elapsedMs = watch.ElapsedMilliseconds;
434 output_.WriteLine($
"Long: Elapsed time {elapsedMs}");
441 List<IntVar> vars =
new List<IntVar>();
442 List<int> coeffs =
new List<int>();
444 for (
int i = 0; i < 100000; ++i)
450 var watch = System.Diagnostics.Stopwatch.StartNew();
453 var elapsedMs = watch.ElapsedMilliseconds;
454 output_.WriteLine($
"Int: Elapsed time {elapsedMs}");
461 List<LinearExpr> exprs =
new List<LinearExpr>();
463 for (
int i = 0; i < 100000; ++i)
468 var watch = System.Diagnostics.Stopwatch.StartNew();
471 var elapsedMs = watch.ElapsedMilliseconds;
472 output_.WriteLine($
"Exprs: Elapsed time {elapsedMs}");
479 List<IntVar> vars =
new List<IntVar>();
480 List<long> coeffs =
new List<long>();
482 for (
int i = 0; i < 100000; ++i)
488 var watch = System.Diagnostics.Stopwatch.StartNew();
490 for (
int i = 0; i < 100000; ++i)
492 obj.
AddTerm(vars[i], coeffs[i]);
496 var elapsedMs = watch.ElapsedMilliseconds;
497 output_.WriteLine($
"Proto: Elapsed time {elapsedMs}");
503 output_.WriteLine(
"LinearExprStaticCompileTest");
509 long[] c1 =
new long[] { 2L, 4L };
510 int[] c2 =
new int[] { 2, 4 };
512 output_.WriteLine(e1.ToString());
514 output_.WriteLine(e2.ToString());
516 output_.WriteLine(e3.ToString());
518 output_.WriteLine(e4.ToString());
520 output_.WriteLine(e5.ToString());
522 output_.WriteLine(e6.ToString());
524 output_.WriteLine(e7.ToString());
526 output_.WriteLine(e8.ToString());
528 output_.WriteLine(e9.ToString());
534 output_.WriteLine(
"LinearExprBuilderCompileTest");
540 long[] c1 =
new long[] { 2L, 4L };
541 int[] c2 =
new int[] { 2, 4 };
543 output_.WriteLine(e1.ToString());
545 output_.WriteLine(e2.ToString());
547 output_.WriteLine(e3.ToString());
549 output_.WriteLine(e4.ToString());
551 output_.WriteLine(e5.ToString());
553 output_.WriteLine(e6.ToString());
555 output_.WriteLine(e7.ToString());
557 output_.WriteLine(e8.ToString());
559 output_.WriteLine(e9.ToString());
561 output_.WriteLine(e10.ToString());
563 output_.WriteLine(e11.ToString());
565 output_.WriteLine(e12.ToString());
567 output_.WriteLine(e13.ToString());
569 output_.WriteLine(e14.ToString());
571 output_.WriteLine(e15.ToString());
577 output_.WriteLine(
"LinearExprIntVarOperatorTest");
581 output_.WriteLine(e.ToString());
583 output_.WriteLine(e.ToString());
585 output_.WriteLine(e.ToString());
587 output_.WriteLine(e.ToString());
589 output_.WriteLine(e.ToString());
591 output_.WriteLine(e.ToString());
593 output_.WriteLine(e.ToString());
595 output_.WriteLine(e.ToString());
601 output_.WriteLine(
"LinearExprBoolVarOperatorTest");
605 output_.WriteLine(e.ToString());
607 output_.WriteLine(e.ToString());
609 output_.WriteLine(e.ToString());
611 output_.WriteLine(e.ToString());
613 output_.WriteLine(e.ToString());
615 output_.WriteLine(e.ToString());
617 output_.WriteLine(e.ToString());
619 output_.WriteLine(e.ToString());
625 output_.WriteLine(
"TrueLiteralAsExpressionTest");
629 output_.WriteLine(e.ToString());
631 output_.WriteLine(e.ToString());
633 output_.WriteLine(e.ToString());
635 output_.WriteLine(e.ToString());
637 output_.WriteLine(e.ToString());
639 output_.WriteLine(e.ToString());
641 output_.WriteLine(e.ToString());
643 output_.WriteLine(e.ToString());
649 output_.WriteLine(
"FalseLiteralAsExpressionTest");
653 output_.WriteLine(e.ToString());
655 output_.WriteLine(e.ToString());
657 output_.WriteLine(e.ToString());
659 output_.WriteLine(e.ToString());
661 output_.WriteLine(e.ToString());
663 output_.WriteLine(e.ToString());
665 output_.WriteLine(e.ToString());
667 output_.WriteLine(e.ToString());
673 output_.WriteLine(
"LinearExprBoolVarNotOperatorTest");
677 output_.WriteLine(e.ToString());
679 output_.WriteLine(e.ToString());
681 output_.WriteLine(e.ToString());
683 output_.WriteLine(e.ToString());
685 output_.WriteLine(e.ToString());
687 output_.WriteLine(e.ToString());
689 output_.WriteLine(e.ToString());
691 output_.WriteLine(e.ToString());
699 model.
Add(-100000 <= v1 + 2 * v2 <= 100000);
701 Assert.True(model.
ExportToFile(
"test_model_dotnet.pbtxt"));
702 output_.WriteLine(
"Model written to file");
708 string model_str =
@"
711 { ""name"": ""C"", ""domain"": [ ""1"", ""9"" ] },
712 { ""name"": ""P"", ""domain"": [ ""0"", ""9"" ] },
713 { ""name"": ""I"", ""domain"": [ ""1"", ""9"" ] },
714 { ""name"": ""S"", ""domain"": [ ""0"", ""9"" ] },
715 { ""name"": ""F"", ""domain"": [ ""1"", ""9"" ] },
716 { ""name"": ""U"", ""domain"": [ ""0"", ""9"" ] },
717 { ""name"": ""N"", ""domain"": [ ""0"", ""9"" ] },
718 { ""name"": ""T"", ""domain"": [ ""1"", ""9"" ] },
719 { ""name"": ""R"", ""domain"": [ ""0"", ""9"" ] },
720 { ""name"": ""E"", ""domain"": [ ""0"", ""9"" ] }
723 { ""allDiff"": { ""exprs"": [
724 { ""vars"": [""0""], ""coeffs"": [""1""] },
725 { ""vars"": [""1""], ""coeffs"": [""1""] },
726 { ""vars"": [""2""], ""coeffs"": [""1""] },
727 { ""vars"": [""3""], ""coeffs"": [""1""] },
728 { ""vars"": [""4""], ""coeffs"": [""1""] },
729 { ""vars"": [""5""], ""coeffs"": [""1""] },
730 { ""vars"": [""6""], ""coeffs"": [""1""] },
731 { ""vars"": [""7""], ""coeffs"": [""1""] },
732 { ""vars"": [""8""], ""coeffs"": [""1""] },
733 { ""vars"": [""9""], ""coeffs"": [""1""] } ] } },
734 { ""linear"": { ""vars"": [ 6, 5, 9, 4, 3, 7, 8, 2, 0, 1 ], ""coeffs"": [ ""1"", ""0"", ""-1"", ""100"", ""1"", ""-1000"", ""-100"", ""10"", ""10"", ""1"" ], ""domain"": [ ""0"", ""0"" ] } }
740 output_.WriteLine(response.
ToString());
746 output_.WriteLine(
"CaptureLog test");
758 solver.StringParameters =
"log_search_progress:true log_to_stdout:false";
762 Assert.NotEmpty(log);
763 Assert.Contains(
"OPTIMAL", log);
769 output_.WriteLine(
"TestInterval test");
772 IntervalVar i = model.NewFixedSizeIntervalVar(v, 3,
"i");
773 Assert.Equal(
"v", i.
StartExpr().ToString());
774 Assert.Equal(
"3", i.
SizeExpr().ToString());
775 Assert.Equal(
"(v + 3)", i.
EndExpr().ToString());
781 output_.WriteLine(
"TestHint test");
long[] FlattenedIntervals()
override void OnSolutionCallback()
override void OnSolutionCallback()
SolutionDivisionCounter(int result, IntVar a, IntVar b, ITestOutputHelper output)
override void OnSolutionCallback()
SolutionModuloCounter(int result, IntVar a, IntVar b, ITestOutputHelper output)