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)
166 obj.
Vars.Add(-v1 - 1);
168 obj.ScalingFactor = -1;
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";
399 List<IntVar> vars =
new List<IntVar>();
400 List<long> coeffs =
new List<long>();
402 for (
int i = 0; i < 100000; ++i)
408 var watch = System.Diagnostics.Stopwatch.StartNew();
411 var elapsedMs = watch.ElapsedMilliseconds;
412 output_.WriteLine($
"Long: Elapsed time {elapsedMs}");
419 List<IntVar> vars =
new List<IntVar>();
420 List<int> coeffs =
new List<int>();
422 for (
int i = 0; i < 100000; ++i)
428 var watch = System.Diagnostics.Stopwatch.StartNew();
431 var elapsedMs = watch.ElapsedMilliseconds;
432 output_.WriteLine($
"Int: Elapsed time {elapsedMs}");
439 List<LinearExpr> exprs =
new List<LinearExpr>();
441 for (
int i = 0; i < 100000; ++i)
446 var watch = System.Diagnostics.Stopwatch.StartNew();
449 var elapsedMs = watch.ElapsedMilliseconds;
450 output_.WriteLine($
"Exprs: Elapsed time {elapsedMs}");
457 List<IntVar> vars =
new List<IntVar>();
458 List<long> coeffs =
new List<long>();
460 for (
int i = 0; i < 100000; ++i)
466 var watch = System.Diagnostics.Stopwatch.StartNew();
468 for (
int i = 0; i < 100000; ++i)
470 obj.
AddTerm(vars[i], coeffs[i]);
474 var elapsedMs = watch.ElapsedMilliseconds;
475 output_.WriteLine($
"Proto: Elapsed time {elapsedMs}");
481 output_.WriteLine(
"LinearExprStaticCompileTest");
487 long[] c1 =
new long[] { 2L, 4L };
488 int[] c2 =
new int[] { 2, 4 };
490 output_.WriteLine(e1.ToString());
492 output_.WriteLine(e2.ToString());
494 output_.WriteLine(e3.ToString());
496 output_.WriteLine(e4.ToString());
498 output_.WriteLine(e5.ToString());
500 output_.WriteLine(e6.ToString());
502 output_.WriteLine(e7.ToString());
504 output_.WriteLine(e8.ToString());
506 output_.WriteLine(e9.ToString());
512 output_.WriteLine(
"LinearExprBuilderCompileTest");
518 long[] c1 =
new long[] { 2L, 4L };
519 int[] c2 =
new int[] { 2, 4 };
521 output_.WriteLine(e1.ToString());
523 output_.WriteLine(e2.ToString());
525 output_.WriteLine(e3.ToString());
527 output_.WriteLine(e4.ToString());
529 output_.WriteLine(e5.ToString());
531 output_.WriteLine(e6.ToString());
533 output_.WriteLine(e7.ToString());
535 output_.WriteLine(e8.ToString());
537 output_.WriteLine(e9.ToString());
539 output_.WriteLine(e10.ToString());
541 output_.WriteLine(e11.ToString());
543 output_.WriteLine(e12.ToString());
545 output_.WriteLine(e13.ToString());
547 output_.WriteLine(e14.ToString());
549 output_.WriteLine(e15.ToString());
555 output_.WriteLine(
"LinearExprIntVarOperatorTest");
559 output_.WriteLine(e.ToString());
561 output_.WriteLine(e.ToString());
563 output_.WriteLine(e.ToString());
565 output_.WriteLine(e.ToString());
567 output_.WriteLine(e.ToString());
569 output_.WriteLine(e.ToString());
571 output_.WriteLine(e.ToString());
573 output_.WriteLine(e.ToString());
579 output_.WriteLine(
"LinearExprBoolVarOperatorTest");
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());
597 output_.WriteLine(e.ToString());
603 output_.WriteLine(
"TrueLiteralAsExpressionTest");
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());
621 output_.WriteLine(e.ToString());
627 output_.WriteLine(
"FalseLiteralAsExpressionTest");
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());
645 output_.WriteLine(e.ToString());
651 output_.WriteLine(
"LinearExprBoolVarNotOperatorTest");
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());
669 output_.WriteLine(e.ToString());
677 model.
Add(-100000 <= v1 + 2 * v2 <= 100000);
679 Assert.True(model.
ExportToFile(
"test_model_dotnet.pbtxt"));
680 output_.WriteLine(
"Model written to file");
686 string model_str =
@"
689 { ""name"": ""C"", ""domain"": [ ""1"", ""9"" ] },
690 { ""name"": ""P"", ""domain"": [ ""0"", ""9"" ] },
691 { ""name"": ""I"", ""domain"": [ ""1"", ""9"" ] },
692 { ""name"": ""S"", ""domain"": [ ""0"", ""9"" ] },
693 { ""name"": ""F"", ""domain"": [ ""1"", ""9"" ] },
694 { ""name"": ""U"", ""domain"": [ ""0"", ""9"" ] },
695 { ""name"": ""N"", ""domain"": [ ""0"", ""9"" ] },
696 { ""name"": ""T"", ""domain"": [ ""1"", ""9"" ] },
697 { ""name"": ""R"", ""domain"": [ ""0"", ""9"" ] },
698 { ""name"": ""E"", ""domain"": [ ""0"", ""9"" ] }
701 { ""allDiff"": { ""exprs"": [
702 { ""vars"": [""0""], ""coeffs"": [""1""] },
703 { ""vars"": [""1""], ""coeffs"": [""1""] },
704 { ""vars"": [""2""], ""coeffs"": [""1""] },
705 { ""vars"": [""3""], ""coeffs"": [""1""] },
706 { ""vars"": [""4""], ""coeffs"": [""1""] },
707 { ""vars"": [""5""], ""coeffs"": [""1""] },
708 { ""vars"": [""6""], ""coeffs"": [""1""] },
709 { ""vars"": [""7""], ""coeffs"": [""1""] },
710 { ""vars"": [""8""], ""coeffs"": [""1""] },
711 { ""vars"": [""9""], ""coeffs"": [""1""] } ] } },
712 { ""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"" ] } }
718 output_.WriteLine(response.
ToString());
724 output_.WriteLine(
"CaptureLog test");
736 solver.StringParameters =
"log_search_progress:true log_to_stdout:false";
740 Assert.NotEmpty(log);
741 Assert.Contains(
"OPTIMAL", log);
747 output_.WriteLine(
"TestInterval test");
750 IntervalVar i = model.NewFixedSizeIntervalVar(v, 3,
"i");
751 Assert.Equal(
"v", i.
StartExpr().ToString());
752 Assert.Equal(
"3", i.
SizeExpr().ToString());
753 Assert.Equal(
"(v + 3)", i.
EndExpr().ToString());
759 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)