forked from mlubin/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SatSolverTests.cs
612 lines (558 loc) · 22.7 KB
/
SatSolverTests.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
// 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 System.Collections.Generic;
using Xunit;
using Google.OrTools.Sat;
namespace Google.OrTools.Tests
{
public class SatSolverTest
{
private const int LargeCount = 100000;
static IntegerVariableProto NewIntegerVariable(long lb, long ub)
{
IntegerVariableProto var = new IntegerVariableProto();
var.Domain.Add(lb);
var.Domain.Add(ub);
return var;
}
static ConstraintProto NewLinear2(int v1, int v2, long c1, long c2, long lb, long ub)
{
LinearConstraintProto linear = new LinearConstraintProto();
linear.Vars.Add(v1);
linear.Vars.Add(v2);
linear.Coeffs.Add(c1);
linear.Coeffs.Add(c2);
linear.Domain.Add(lb);
linear.Domain.Add(ub);
ConstraintProto ct = new ConstraintProto();
ct.Linear = linear;
return ct;
}
static ConstraintProto NewLinear3(int v1, int v2, int v3, long c1, long c2, long c3, long lb, long ub)
{
LinearConstraintProto linear = new LinearConstraintProto();
linear.Vars.Add(v1);
linear.Vars.Add(v2);
linear.Vars.Add(v3);
linear.Coeffs.Add(c1);
linear.Coeffs.Add(c2);
linear.Coeffs.Add(c3);
linear.Domain.Add(lb);
linear.Domain.Add(ub);
ConstraintProto ct = new ConstraintProto();
ct.Linear = linear;
return ct;
}
static CpObjectiveProto NewMinimize1(int v1, long c1)
{
CpObjectiveProto obj = new CpObjectiveProto();
obj.Vars.Add(v1);
obj.Coeffs.Add(c1);
return obj;
}
static CpObjectiveProto NewMaximize1(int v1, long c1)
{
CpObjectiveProto obj = new CpObjectiveProto();
obj.Vars.Add(-v1 - 1);
obj.Coeffs.Add(c1);
obj.ScalingFactor = -1;
return obj;
}
static CpObjectiveProto NewMaximize2(int v1, int v2, long c1, long c2)
{
CpObjectiveProto obj = new CpObjectiveProto();
obj.Vars.Add(-v1 - 1);
obj.Vars.Add(-v2 - 1);
obj.Coeffs.Add(c1);
obj.Coeffs.Add(c2);
obj.ScalingFactor = -1;
return obj;
}
// CpModelProto
[Fact]
public void SimpleLinearModelProto()
{
CpModelProto model = new CpModelProto();
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Variables.Add(NewIntegerVariable(-1000000, 1000000));
model.Constraints.Add(NewLinear2(0, 1, 1, 1, -1000000, 100000));
model.Constraints.Add(NewLinear3(0, 1, 2, 1, 2, -1, 0, 100000));
model.Objective = NewMaximize1(2, 1);
// Console.WriteLine("model = " + model.ToString());
SolveWrapper solve_wrapper = new SolveWrapper();
CpSolverResponse response = solve_wrapper.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, response.Status);
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] { 10, 10, 30 }, response.Solution);
// Console.WriteLine("response = " + response.ToString());
}
[Fact]
public void SimpleLinearModelProto2()
{
CpModelProto model = new CpModelProto();
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Constraints.Add(NewLinear2(0, 1, 1, 1, -1000000, 100000));
model.Objective = NewMaximize2(0, 1, 1, -2);
// Console.WriteLine("model = " + model.ToString());
SolveWrapper solve_wrapper = new SolveWrapper();
CpSolverResponse response = solve_wrapper.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, response.Status);
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] { 10, -10 }, response.Solution);
// Console.WriteLine("response = " + response.ToString());
}
// CpModel
[Fact]
public void SimpleLinearModel()
{
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
IntVar v3 = model.NewIntVar(-100000, 100000, "v3");
model.AddLinearConstraint(v1 + v2, -1000000, 100000);
model.AddLinearConstraint(v1 + 2 * v2 - v3, 0, 100000);
model.Maximize(v3);
Assert.Equal(v1.Domain.FlattenedIntervals(), new long[] { -10, 10 });
// Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] { 10, 10, 30 }, response.Solution);
// Console.WriteLine("response = " + response.ToString());
}
[Fact]
public void SimpleLinearModel2()
{
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
model.AddLinearConstraint(v1 + v2, -1000000, 100000);
model.Maximize(v1 - 2 * v2);
// Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] { 10, -10 }, response.Solution);
// Console.WriteLine("response = " + response.ToString());
}
[Fact]
public void SimpleLinearModel3()
{
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
model.Add(-100000 <= v1 + 2 * v2 <= 100000);
model.Minimize(v1 - 2 * v2);
// Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(-10, solver.Value(v1));
Assert.Equal(10, solver.Value(v2));
Assert.Equal(new long[] { -10, 10 }, response.Solution);
Assert.Equal(-30, solver.Value(v1 - 2 * v2));
Assert.Equal(-30, response.ObjectiveValue);
// Console.WriteLine("response = " + response.ToString());
}
[Fact]
public void NegativeIntVar()
{
CpModel model = new CpModel();
IntVar boolvar = model.NewBoolVar("boolvar");
IntVar x = model.NewIntVar(0, 10, "x");
IntVar delta = model.NewIntVar(-5, 5, "delta");
IntVar squaredDelta = model.NewIntVar(0, 25, "squaredDelta");
model.Add(x == boolvar * 4);
model.Add(delta == x - 5);
model.AddMultiplicationEquality(squaredDelta, new IntVar[] { delta, delta });
model.Minimize(squaredDelta);
// Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
CpSolverResponse response = solver.Response;
Console.WriteLine("response = " + response.ToString());
Assert.Equal(CpSolverStatus.Optimal, status);
Assert.Equal(1, solver.Value(boolvar));
Assert.Equal(4, solver.Value(x));
Assert.Equal(-1, solver.Value(delta));
Assert.Equal(1, solver.Value(squaredDelta));
Assert.Equal(new long[] { 1, 4, -1, 1 }, response.Solution);
Assert.Equal(1.0, response.ObjectiveValue, 5);
}
[Fact]
public void NegativeSquareVar()
{
CpModel model = new CpModel();
BoolVar boolvar = model.NewBoolVar("boolvar");
IntVar x = model.NewIntVar(0, 10, "x");
IntVar delta = model.NewIntVar(-5, 5, "delta");
IntVar squaredDelta = model.NewIntVar(0, 25, "squaredDelta");
model.Add(x == 4).OnlyEnforceIf(boolvar);
model.Add(x == 0).OnlyEnforceIf(boolvar.Not());
model.Add(delta == x - 5);
long[,] tuples = { { -5, 25 }, { -4, 16 }, { -3, 9 }, { -2, 4 }, { -1, 1 }, { 0, 0 },
{ 1, 1 }, { 2, 4 }, { 3, 9 }, { 4, 16 }, { 5, 25 } };
model.AddAllowedAssignments(new IntVar[] { delta, squaredDelta }).AddTuples(tuples);
model.Minimize(squaredDelta);
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
CpSolverResponse response = solver.Response;
Assert.Equal(1, solver.Value(boolvar));
Assert.Equal(4, solver.Value(x));
Assert.Equal(-1, solver.Value(delta));
Assert.Equal(1, solver.Value(squaredDelta));
Assert.Equal(new long[] { 1, 4, -1, 1 }, response.Solution);
Assert.Equal(1.0, response.ObjectiveValue, 6);
}
[Fact]
public void Division()
{
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(0, 10, "v1");
IntVar v2 = model.NewIntVar(1, 10, "v2");
model.AddDivisionEquality(3, v1, v2);
// Console.WriteLine(model.Model);
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(3, solver.Value(v1));
Assert.Equal(1, solver.Value(v2));
Assert.Equal(new long[] { 3, 1 }, response.Solution);
Assert.Equal(0, response.ObjectiveValue);
// Console.WriteLine("response = " + response.ToString());
}
[Fact]
public void Modulo()
{
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(1, 10, "v1");
IntVar v2 = model.NewIntVar(1, 10, "v2");
model.AddModuloEquality(3, v1, v2);
// Console.WriteLine(model.Model);
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(3, solver.Value(v1));
Assert.Equal(4, solver.Value(v2));
Assert.Equal(new long[] { 3, 4 }, response.Solution);
Assert.Equal(0, response.ObjectiveValue);
// Console.WriteLine("response = " + response.ToString());
}
[Fact]
public void LargeWeightedSumLong()
{
CpModel model = new CpModel();
model.Model.Variables.Capacity = LargeCount;
List<IntVar> vars = new List<IntVar>(LargeCount);
List<long> coeffs = new List<long>(LargeCount);
for (int i = 0; i < LargeCount; ++i)
{
vars.Add(model.NewBoolVar(""));
coeffs.Add(i + 1);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
model.Minimize(LinearExpr.WeightedSum(vars, coeffs));
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Long: Elapsed time {elapsedMs}");
}
[Fact]
public void LargeWeightedSumInt()
{
CpModel model = new CpModel();
model.Model.Variables.Capacity = LargeCount;
List<IntVar> vars = new List<IntVar>(LargeCount);
List<int> coeffs = new List<int>(LargeCount);
for (int i = 0; i < LargeCount; ++i)
{
vars.Add(model.NewBoolVar(""));
coeffs.Add(i);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
model.Minimize(LinearExpr.WeightedSum(vars, coeffs));
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Int: Elapsed time {elapsedMs}");
}
[Fact]
public void LargeWeightedSumExpr()
{
CpModel model = new CpModel();
model.Model.Variables.Capacity = LargeCount;
List<LinearExpr> exprs = new List<LinearExpr>(LargeCount);
for (int i = 0; i < LargeCount; ++i)
{
exprs.Add(model.NewBoolVar("") * i);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
model.Minimize(LinearExpr.Sum(exprs));
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Exprs: Elapsed time {elapsedMs}");
}
[Fact]
public void LargeWeightedSumBuilder()
{
CpModel model = new CpModel();
model.Model.Variables.Capacity = LargeCount;
List<IntVar> vars = new List<IntVar>(LargeCount);
List<long> coeffs = new List<long>(LargeCount);
for (int i = 0; i < LargeCount; ++i)
{
vars.Add(model.NewBoolVar(""));
coeffs.Add(i + 1);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
LinearExprBuilder obj = LinearExpr.NewBuilder();
for (int i = 0; i < LargeCount; ++i)
{
obj.AddTerm(vars[i], coeffs[i]);
}
model.Minimize(obj);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Proto: Elapsed time {elapsedMs}");
}
[Fact]
public void LinearExprStaticCompileTest()
{
Console.WriteLine("LinearExprStaticCompileTest");
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
BoolVar b1 = model.NewBoolVar("b1");
BoolVar b2 = model.NewBoolVar("b2");
long[] c1 = new long[] { 2L, 4L };
int[] c2 = new int[] { 2, 4 };
LinearExpr e1 = LinearExpr.Sum(new IntVar[] { v1, v2 });
Console.WriteLine(e1.ToString());
LinearExpr e2 = LinearExpr.Sum(new ILiteral[] { b1, b2 });
Console.WriteLine(e2.ToString());
LinearExpr e3 = LinearExpr.Sum(new BoolVar[] { b1, b2 });
Console.WriteLine(e3.ToString());
LinearExpr e4 = LinearExpr.WeightedSum(new IntVar[] { v1, v2 }, c1);
Console.WriteLine(e4.ToString());
LinearExpr e5 = LinearExpr.WeightedSum(new ILiteral[] { b1, b2 }, c1);
Console.WriteLine(e5.ToString());
LinearExpr e6 = LinearExpr.WeightedSum(new BoolVar[] { b1, b2 }, c1);
Console.WriteLine(e6.ToString());
LinearExpr e7 = LinearExpr.WeightedSum(new IntVar[] { v1, v2 }, c2);
Console.WriteLine(e7.ToString());
LinearExpr e8 = LinearExpr.WeightedSum(new ILiteral[] { b1, b2 }, c2);
Console.WriteLine(e8.ToString());
LinearExpr e9 = LinearExpr.WeightedSum(new BoolVar[] { b1, b2 }, c2);
Console.WriteLine(e9.ToString());
}
[Fact]
public void LinearExprBuilderCompileTest()
{
Console.WriteLine("LinearExprBuilderCompileTest");
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
BoolVar b1 = model.NewBoolVar("b1");
BoolVar b2 = model.NewBoolVar("b2");
long[] c1 = new long[] { 2L, 4L };
int[] c2 = new int[] { 2, 4 };
LinearExpr e1 = LinearExpr.NewBuilder().AddSum(new IntVar[] { v1, v2 });
Console.WriteLine(e1.ToString());
LinearExpr e2 = LinearExpr.NewBuilder().AddSum(new ILiteral[] { b1, b2 });
Console.WriteLine(e2.ToString());
LinearExpr e3 = LinearExpr.NewBuilder().AddSum(new BoolVar[] { b1, b2 });
Console.WriteLine(e3.ToString());
LinearExpr e4 = LinearExpr.NewBuilder().AddWeightedSum(new IntVar[] { v1, v2 }, c1);
Console.WriteLine(e4.ToString());
LinearExpr e5 = LinearExpr.NewBuilder().AddWeightedSum(new ILiteral[] { b1, b2 }, c1);
Console.WriteLine(e5.ToString());
LinearExpr e6 = LinearExpr.NewBuilder().AddWeightedSum(new BoolVar[] { b1, b2 }, c1);
Console.WriteLine(e6.ToString());
LinearExpr e7 = LinearExpr.NewBuilder().AddWeightedSum(new IntVar[] { v1, v2 }, c2);
Console.WriteLine(e7.ToString());
LinearExpr e8 = LinearExpr.NewBuilder().AddWeightedSum(new ILiteral[] { b1, b2 }, c2);
Console.WriteLine(e8.ToString());
LinearExpr e9 = LinearExpr.NewBuilder().AddWeightedSum(new BoolVar[] { b1, b2 }, c2);
Console.WriteLine(e9.ToString());
LinearExpr e10 = LinearExpr.NewBuilder().Add(v1);
Console.WriteLine(e10.ToString());
LinearExpr e11 = LinearExpr.NewBuilder().Add(b1);
Console.WriteLine(e11.ToString());
LinearExpr e12 = LinearExpr.NewBuilder().Add(b1.Not());
Console.WriteLine(e12.ToString());
LinearExpr e13 = LinearExpr.NewBuilder().AddTerm(v1, -1);
Console.WriteLine(e13.ToString());
LinearExpr e14 = LinearExpr.NewBuilder().AddTerm(b1, -1);
Console.WriteLine(e14.ToString());
LinearExpr e15 = LinearExpr.NewBuilder().AddTerm(b1.Not(), -2);
Console.WriteLine(e15.ToString());
}
[Fact]
public void LinearExprIntVarOperatorTest()
{
Console.WriteLine("LinearExprIntVarOperatorTest");
CpModel model = new CpModel();
IntVar v = model.NewIntVar(-10, 10, "v");
LinearExpr e = v * 2;
Console.WriteLine(e);
e = 2 * v;
Console.WriteLine(e);
e = v + 2;
Console.WriteLine(e);
e = 2 + v;
Console.WriteLine(e);
e = v;
Console.WriteLine(e);
e = -v;
Console.WriteLine(e);
e = 1 - v;
Console.WriteLine(e);
e = v - 1;
Console.WriteLine(e);
}
[Fact]
public void LinearExprBoolVarOperatorTest()
{
Console.WriteLine("LinearExprBoolVarOperatorTest");
CpModel model = new CpModel();
BoolVar v = model.NewBoolVar("v");
LinearExpr e = v * 2;
Console.WriteLine(e);
e = 2 * v;
Console.WriteLine(e);
e = v + 2;
Console.WriteLine(e);
e = 2 + v;
Console.WriteLine(e);
e = v;
Console.WriteLine(e);
e = -v;
Console.WriteLine(e);
e = 1 - v;
Console.WriteLine(e);
e = v - 1;
Console.WriteLine(e);
}
[Fact]
public void LinearExprNotBoolVarOperatorTest()
{
Console.WriteLine("LinearExprBoolVarNotOperatorTest");
CpModel model = new CpModel();
ILiteral v = model.NewBoolVar("v");
LinearExpr e = v.NotAsExpr() * 2;
Console.WriteLine(e);
e = 2 * v.NotAsExpr();
Console.WriteLine(e);
e = v.NotAsExpr() + 2;
Console.WriteLine(e);
e = 2 + v.NotAsExpr();
Console.WriteLine(e);
e = v.NotAsExpr();
Console.WriteLine(e);
e = -v.NotAsExpr();
Console.WriteLine(e);
e = 1 - v.NotAsExpr();
Console.WriteLine(e);
e = v.NotAsExpr() - 1;
Console.WriteLine(e);
}
[Fact]
public void ExportModel()
{
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
model.Add(-100000 <= v1 + 2 * v2 <= 100000);
model.Minimize(v1 - 2 * v2);
Assert.True(model.ExportToFile("test_model_dotnet.pbtxt"));
Console.WriteLine("Model written to file");
}
[Fact]
public void SolveFromString()
{
string model_str = @"
{
""variables"": [
{ ""name"": ""C"", ""domain"": [ ""1"", ""9"" ] },
{ ""name"": ""P"", ""domain"": [ ""0"", ""9"" ] },
{ ""name"": ""I"", ""domain"": [ ""1"", ""9"" ] },
{ ""name"": ""S"", ""domain"": [ ""0"", ""9"" ] },
{ ""name"": ""F"", ""domain"": [ ""1"", ""9"" ] },
{ ""name"": ""U"", ""domain"": [ ""0"", ""9"" ] },
{ ""name"": ""N"", ""domain"": [ ""0"", ""9"" ] },
{ ""name"": ""T"", ""domain"": [ ""1"", ""9"" ] },
{ ""name"": ""R"", ""domain"": [ ""0"", ""9"" ] },
{ ""name"": ""E"", ""domain"": [ ""0"", ""9"" ] }
],
""constraints"": [
{ ""allDiff"": { ""exprs"": [
{ ""vars"": [""0""], ""coeffs"": [""1""] },
{ ""vars"": [""1""], ""coeffs"": [""1""] },
{ ""vars"": [""2""], ""coeffs"": [""1""] },
{ ""vars"": [""3""], ""coeffs"": [""1""] },
{ ""vars"": [""4""], ""coeffs"": [""1""] },
{ ""vars"": [""5""], ""coeffs"": [""1""] },
{ ""vars"": [""6""], ""coeffs"": [""1""] },
{ ""vars"": [""7""], ""coeffs"": [""1""] },
{ ""vars"": [""8""], ""coeffs"": [""1""] },
{ ""vars"": [""9""], ""coeffs"": [""1""] } ] } },
{ ""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"" ] } }
]
}";
CpModelProto model = Google.Protobuf.JsonParser.Default.Parse<CpModelProto>(model_str);
SolveWrapper solve_wrapper = new SolveWrapper();
CpSolverResponse response = solve_wrapper.Solve(model);
Console.WriteLine(response);
}
[Fact]
public void CaptureLog()
{
Console.WriteLine("CaptureLog test");
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
IntVar v3 = model.NewIntVar(-100000, 100000, "v3");
model.AddLinearConstraint(v1 + v2, -1000000, 100000);
model.AddLinearConstraint(v1 + 2 * v2 - v3, 0, 100000);
model.Maximize(v3);
Assert.Equal(v1.Domain.FlattenedIntervals(), new long[] { -10, 10 });
// Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
solver.StringParameters = "log_search_progress:true log_to_stdout:false";
string log = "";
solver.SetLogCallback(message => log += message + "\n");
solver.Solve(model);
Assert.NotEmpty(log);
Assert.Contains("OPTIMAL", log);
}
[Fact]
public void TestInterval()
{
Console.WriteLine("TestInterval test");
CpModel model = new CpModel();
IntVar v = model.NewIntVar(-10, 10, "v");
IntervalVar i = model.NewFixedSizeIntervalVar(v, 3, "i");
Assert.Equal("v", i.StartExpr().ToString());
Assert.Equal("3", i.SizeExpr().ToString());
Assert.Equal("v + 3", i.EndExpr().ToString());
}
}
} // namespace Google.OrTools.Tests