Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ConstraintSolverTests.cs
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14using System;
15using Xunit;
18
20{
22{
23 [Fact]
25 {
26 int[] input = { 5, 11, 17 };
27 long[] output = ToInt64Vector(input);
28 Assert.Equal(3, output.Length);
29 Assert.Equal(5, output[0]);
30 Assert.Equal(11, output[1]);
31 Assert.Equal(17, output[2]);
32 }
33
34 [Fact]
35 public void IntVarConstructor()
36 {
37 Solver solver = new Solver("Solver");
38 Assert.NotNull(solver);
39 IntVar x = solver.MakeIntVar(3, 7, "x");
40 Assert.Equal(3, x.Min());
41 Assert.Equal(7, x.Max());
42 Assert.Equal("x(3..7)", x.ToString());
43 Assert.True(x.HasName());
44 Assert.Equal("x", x.Name());
45 x.SetName("renamed");
46 Assert.Equal("renamed", x.Name());
47 }
48
49 [Fact]
51 {
52 Solver solver = new Solver("Solver");
53 Assert.NotNull(solver);
54 IntVar x = solver.MakeIntVar(3, 7, "x");
55 Assert.Equal("x(3..7)", x.ToString());
56
57 // Unary operator
58 Constraint c0 = (x == 5);
59 Assert.Equal("(x(3..7) == 5)", c0.ToString());
60 IntExpr e1 = -c0;
61 Assert.Equal("-(Watch<x == 5>(0 .. 1))", e1.ToString());
62 IntExpr e2 = c0.Abs();
63 Assert.Equal("Watch<x == 5>(0 .. 1)", e2.ToString());
64 IntExpr e3 = c0.Square();
65 Assert.Equal("IntSquare(Watch<x == 5>(0 .. 1))", e3.ToString());
66
67 // Relational operator with a scalar
68 Constraint c1 = x == 5;
69 Assert.Equal("(x(3..7) == 5)", c1.ToString());
70 Constraint c2 = x >= 5;
71 Assert.Equal("(x(3..7) >= 5)", c2.ToString());
72 Constraint c3 = x > 5;
73 Assert.Equal("(x(3..7) >= 6)", c3.ToString());
74 Constraint c4 = x <= 5;
75 Assert.Equal("(x(3..7) <= 5)", c4.ToString());
76 Constraint c5 = x < 5;
77 Assert.Equal("(x(3..7) <= 4)", c5.ToString());
78 Constraint c6 = x != 5;
79 Assert.Equal("(x(3..7) != 5)", c6.ToString());
80 Constraint c7 = x == 2;
81 Assert.Equal("FalseConstraint()", c7.ToString());
82 }
83
84 [Fact]
85 public void IntExprConstructor()
86 {
87 Solver solver = new Solver("Solver");
88 Assert.NotNull(solver);
89 IntVar x = solver.MakeIntVar(3, 13, "x");
90 Assert.Equal(3, x.Min());
91 Assert.Equal(13, x.Max());
92
93 // Unary Operator
94 IntExpr e1 = -(x == 7);
95 Assert.Equal("-(Watch<x == 7>(0 .. 1))", e1.ToString());
96 IntExpr e2 = (x == 7).Abs();
97 Assert.Equal("Watch<x == 7>(0 .. 1)", e2.ToString());
98 IntExpr e3 = (x == 7).Square();
99 Assert.Equal("IntSquare(Watch<x == 7>(0 .. 1))", e3.ToString());
100 }
101
102 [Fact]
104 {
105 Solver solver = new Solver("Solver");
106 Assert.NotNull(solver);
107 IntVar x = solver.MakeIntVar(3, 13, "x");
108 Assert.Equal(3, x.Min());
109 Assert.Equal(13, x.Max());
110
111 // Unary Operator
112 IntExpr e1 = -(x >= 7);
113 Assert.Equal("-(Watch<x >= 7>(0 .. 1))", e1.ToString());
114 IntExpr e2 = (x >= 7).Abs();
115 Assert.Equal("Watch<x >= 7>(0 .. 1)", e2.ToString());
116 IntExpr e3 = (x >= 7).Square();
117 Assert.Equal("IntSquare(Watch<x >= 7>(0 .. 1))", e3.ToString());
118 }
119
120 [Fact]
122 {
123 Solver solver = new Solver("Solver");
124 Assert.NotNull(solver);
125 IntVar x = solver.MakeIntVar(3, 13, "x");
126 Assert.Equal(3, x.Min());
127 Assert.Equal(13, x.Max());
128
129 Constraint c1 = (x == 7);
130 Assert.Equal("(x(3..13) == 7)", c1.ToString());
131
132 // Arithmetic operator with a scalar
133 IntExpr e2a = c1 + 1;
134 Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2a.ToString());
135 IntExpr e2b = 1 + c1;
136 Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2b.ToString());
137
138 IntExpr e2c = c1 - 1;
139 Assert.Equal("(Watch<x == 7>(0 .. 1) + -1)", e2c.ToString());
140 IntExpr e2d = 1 - c1;
141 Assert.Equal("Not(Watch<x == 7>(0 .. 1))", e2d.ToString());
142
143 IntExpr e2e = c1 * 2;
144 Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2e.ToString());
145 IntExpr e2f = 2 * c1;
146 Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2f.ToString());
147
148 IntExpr e2g = c1 / 4;
149 Assert.Equal("(Watch<x == 7>(0 .. 1) div 4)", e2g.ToString());
150
151 // Relational operator with a scalar
152 Constraint c8a = c1 == 1;
153 Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8a.ToString());
154 Constraint c8b = 1 == c1;
155 Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8b.ToString());
156
157 Constraint c8c = c1 != 1;
158 Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8c.ToString());
159 Constraint c8d = 1 != c1;
160 Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8d.ToString());
161
162 Constraint c8e = c1 >= 1;
163 Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8e.ToString());
164 Constraint c8f = 1 >= c1;
165 Assert.Equal("TrueConstraint()", c8f.ToString());
166
167 Constraint c8g = c1 > 1;
168 Assert.Equal("FalseConstraint()", c8g.ToString());
169 Constraint c8h = 1 > c1;
170 Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8h.ToString());
171
172 Constraint c8i = c1 <= 1;
173 Assert.Equal("TrueConstraint()", c8i.ToString());
174 Constraint c8j = 1 <= c1;
175 Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8j.ToString());
176
177 Constraint c8k = c1 < 1;
178 Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8k.ToString());
179 Constraint c8l = 1 < c1;
180 Assert.Equal("FalseConstraint()", c8l.ToString());
181 }
182
183 [Fact]
185 {
186 Solver solver = new Solver("Solver");
187 Assert.NotNull(solver);
188 IntVar x = solver.MakeIntVar(3, 13, "x");
189 Assert.Equal(3, x.Min());
190 Assert.Equal(13, x.Max());
191
192 Constraint c1 = x == 7;
193 Assert.Equal("(x(3..13) == 7)", c1.ToString());
194
195 IntVar y = solver.MakeIntVar(5, 17, "y");
196 Assert.Equal(5, y.Min());
197 Assert.Equal(17, y.Max());
198
199 // Arithmetic operator with IntVar
200 IntExpr e3a = c1 + y;
201 Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3a.ToString());
202 IntExpr e3b = y + c1;
203 Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3b.ToString());
204
205 IntExpr e3c = c1 - y;
206 Assert.Equal("(Watch<x == 7>(0 .. 1) - y(5..17))", e3c.ToString());
207 IntExpr e3d = y - c1;
208 Assert.Equal("(y(5..17) - Watch<x == 7>(0 .. 1))", e3d.ToString());
209
210 IntExpr e3e = c1 * y;
211 Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3e.ToString());
212 IntExpr e3f = y * c1;
213 Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3f.ToString());
214
215 // Relational operator with an IntVar
216 Constraint c9a = c1 == y;
217 Assert.Equal("Watch<x == 7>(0 .. 1) == y(5..17)", c9a.ToString());
218 Constraint c9b = y == c1;
219 Assert.Equal("y(5..17) == Watch<x == 7>(0 .. 1)", c9b.ToString());
220
221 Constraint c9c = c1 != y;
222 Assert.Equal("Watch<x == 7>(0 .. 1) != y(5..17)", c9c.ToString());
223 Constraint c9d = y != c1;
224 Assert.Equal("y(5..17) != Watch<x == 7>(0 .. 1)", c9d.ToString());
225
226 Constraint c9e = c1 >= y;
227 Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9e.ToString());
228 Constraint c9f = y >= c1;
229 Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9f.ToString());
230
231 Constraint c9g = c1 > y;
232 Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9g.ToString());
233 Constraint c9h = y > c1;
234 Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9h.ToString());
235
236 Constraint c9i = c1 <= y;
237 Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9i.ToString());
238 Constraint c9j = y <= c1;
239 Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9j.ToString());
240
241 Constraint c9k = c1 < y;
242 Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9k.ToString());
243 Constraint c9l = y < c1;
244 Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9l.ToString());
245 }
246
247 [Fact]
249 {
250 Solver solver = new Solver("Solver");
251 Assert.NotNull(solver);
252 IntVar x = solver.MakeIntVar(3, 13, "x");
253 Assert.Equal(3, x.Min());
254 Assert.Equal(13, x.Max());
255 Constraint c1 = x == 7;
256 Assert.Equal("(x(3..13) == 7)", c1.ToString());
257
258 IntVar y = solver.MakeIntVar(5, 17, "y");
259 Assert.Equal(5, y.Min());
260 Assert.Equal(17, y.Max());
261
262 // Arithmetic operator with an IntExpr
263 IntExpr e11a = c1 + (y == 11);
264 Assert.Equal("(Watch<x == 7>(0 .. 1) + Watch<y == 11>(0 .. 1))", e11a.ToString());
265 IntExpr e11b = (y == 11) + c1;
266 Assert.Equal("(Watch<x == 7>(0 .. 1) + Watch<y == 11>(0 .. 1))", e11b.ToString());
267 IntExpr e11c = c1 - (y == 11);
268 Assert.Equal("(Watch<x == 7>(0 .. 1) - Watch<y == 11>(0 .. 1))", e11c.ToString());
269 IntExpr e11d = (y == 11) - c1;
270 Assert.Equal("(Watch<y == 11>(0 .. 1) - Watch<x == 7>(0 .. 1))", e11d.ToString());
271 IntExpr e11e = c1 * (y == 11);
272 Assert.Equal("(Watch<x == 7>(0 .. 1) * Watch<y == 11>(0 .. 1))", e11e.ToString());
273 IntExpr e11f = (y == 11) * c1;
274 Assert.Equal("(Watch<x == 7>(0 .. 1) * Watch<y == 11>(0 .. 1))", e11f.ToString());
275
276 // Relational operator with an IntExpr
277 Constraint c12a = c1 == (y == 11);
278 Assert.Equal("Watch<x == 7>(0 .. 1) == Watch<y == 11>(0 .. 1)", c12a.ToString());
279 Constraint c12b = (y == 11) == c1;
280 Assert.Equal("Watch<y == 11>(0 .. 1) == Watch<x == 7>(0 .. 1)", c12b.ToString());
281 Constraint c12c = c1 != (y == 11);
282 Assert.Equal("Watch<x == 7>(0 .. 1) != Watch<y == 11>(0 .. 1)", c12c.ToString());
283 Constraint c12d = (y == 11) != c1;
284 Assert.Equal("Watch<y == 11>(0 .. 1) != Watch<x == 7>(0 .. 1)", c12d.ToString());
285 Constraint c12e = c1 >= (y == 11);
286 Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c12e.ToString());
287 Constraint c12f = (y == 11) >= c1;
288 Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c12f.ToString());
289 Constraint c12g = c1 > (y == 11);
290 Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c12g.ToString());
291 Constraint c12h = (y == 11) > c1;
292 Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c12h.ToString());
293 Constraint c12i = c1 <= (y == 11);
294 Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c12i.ToString());
295 Constraint c12j = (y == 11) <= c1;
296 Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c12j.ToString());
297 Constraint c12k = c1 < (y == 11);
298 Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c12k.ToString());
299 Constraint c12l = (y == 11) < c1;
300 Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c12l.ToString());
301 }
302
303 [Fact]
305 {
306 Solver solver = new Solver("Solver");
307 Assert.NotNull(solver);
308 IntVar x = solver.MakeIntVar(3, 13, "x");
309 Assert.Equal(3, x.Min());
310 Assert.Equal(13, x.Max());
311 Constraint c1 = x == 7;
312 Assert.Equal("(x(3..13) == 7)", c1.ToString());
313
314 IntVar y = solver.MakeIntVar(5, 17, "y");
315 Assert.Equal(5, y.Min());
316 Assert.Equal(17, y.Max());
317 Constraint c2 = y == 11;
318 Assert.Equal("(y(5..17) == 11)", c2.ToString());
319
320 // Relational operator with a Constraint
321 Constraint c10a = c1 == c2;
322 Assert.Equal("Watch<x == 7>(0 .. 1) == Watch<y == 11>(0 .. 1)", c10a.ToString());
323
324 Constraint c10b = c1 != c2;
325 Assert.Equal("Watch<x == 7>(0 .. 1) != Watch<y == 11>(0 .. 1)", c10b.ToString());
326
327 Constraint c10c = c1 <= c2;
328 Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c10c.ToString());
329 Constraint c10d = c1 >= c2;
330 Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c10d.ToString());
331
332 Constraint c10e = c1 > c2;
333 Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c10e.ToString());
334 Constraint c10f = c1 < c2;
335 Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c10f.ToString());
336 }
337
338 [Fact]
339 public void IntExprAndScalar()
340 {
341 Solver solver = new Solver("Solver");
342 Assert.NotNull(solver);
343 IntVar x = solver.MakeIntVar(3, 13, "x");
344 Assert.Equal(3, x.Min());
345 Assert.Equal(13, x.Max());
346
347 // Arithmetic operator with a scalar
348 IntExpr e2a = (x == 7) + 1;
349 Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2a.ToString());
350 IntExpr e2b = 1 + (x == 7);
351 Assert.Equal("(Watch<x == 7>(0 .. 1) + 1)", e2b.ToString());
352
353 IntExpr e2c = (x == 7) - 1;
354 Assert.Equal("(Watch<x == 7>(0 .. 1) + -1)", e2c.ToString());
355 IntExpr e2d = 1 - (x == 7);
356 Assert.Equal("Not(Watch<x == 7>(0 .. 1))", e2d.ToString());
357
358 IntExpr e2e = (x == 7) * 2;
359 Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2e.ToString());
360 IntExpr e2f = 2 * (x == 7);
361 Assert.Equal("(Watch<x == 7>(0 .. 1) * 2)", e2f.ToString());
362
363 IntExpr e2g = (x == 7) / 4;
364 Assert.Equal("(Watch<x == 7>(0 .. 1) div 4)", e2g.ToString());
365
366 // Relational operator with a scalar
367 Constraint c8a = (x == 7) == 1;
368 Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8a.ToString());
369 Constraint c8b = 1 == (x == 7);
370 Assert.Equal("(Watch<x == 7>(0 .. 1) == 1)", c8b.ToString());
371
372 Constraint c8c = (x == 7) != 1;
373 Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8c.ToString());
374 Constraint c8d = 1 != (x == 7);
375 Assert.Equal("(Watch<x == 7>(0 .. 1) != 1)", c8d.ToString());
376
377 Constraint c8e = (x == 7) >= 1;
378 Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8e.ToString());
379 Constraint c8f = 1 >= (x == 7);
380 Assert.Equal("TrueConstraint()", c8f.ToString());
381
382 Constraint c8g = (x == 7) > 1;
383 Assert.Equal("FalseConstraint()", c8g.ToString());
384 Constraint c8h = 1 > (x == 7);
385 Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8h.ToString());
386
387 Constraint c8i = (x == 7) <= 1;
388 Assert.Equal("TrueConstraint()", c8i.ToString());
389 Constraint c8j = 1 <= (x == 7);
390 Assert.Equal("(Watch<x == 7>(0 .. 1) >= 1)", c8j.ToString());
391
392 Constraint c8k = (x == 7) < 1;
393 Assert.Equal("(Watch<x == 7>(0 .. 1) <= 0)", c8k.ToString());
394 Constraint c8l = 1 < (x == 7);
395 Assert.Equal("FalseConstraint()", c8l.ToString());
396 }
397
398 [Fact]
399 public void IntExprAndIntVar()
400 {
401 Solver solver = new Solver("Solver");
402 Assert.NotNull(solver);
403 IntVar x = solver.MakeIntVar(3, 13, "x");
404 Assert.Equal(3, x.Min());
405 Assert.Equal(13, x.Max());
406
407 IntVar y = solver.MakeIntVar(5, 17, "y");
408 Assert.Equal(5, y.Min());
409 Assert.Equal(17, y.Max());
410
411 // Arithmetic operator with IntVar
412 IntExpr e3a = (x == 7) + y;
413 Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3a.ToString());
414 IntExpr e3b = y + (x == 7);
415 Assert.Equal("(Watch<x == 7>(0 .. 1) + y(5..17))", e3b.ToString());
416
417 IntExpr e3c = (x == 7) - y;
418 Assert.Equal("(Watch<x == 7>(0 .. 1) - y(5..17))", e3c.ToString());
419 IntExpr e3d = y - (x == 7);
420 Assert.Equal("(y(5..17) - Watch<x == 7>(0 .. 1))", e3d.ToString());
421
422 IntExpr e3e = (x == 7) * y;
423 Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3e.ToString());
424 IntExpr e3f = y * (x == 7);
425 Assert.Equal("(Watch<x == 7>(0 .. 1) * y(5..17))", e3f.ToString());
426
427 // Relational operator with an IntVar
428 Constraint c9a = (x == 7) == y;
429 Assert.Equal("Watch<x == 7>(0 .. 1) == y(5..17)", c9a.ToString());
430 Constraint c9b = y == (x == 7);
431 Assert.Equal("y(5..17) == Watch<x == 7>(0 .. 1)", c9b.ToString());
432
433 Constraint c9c = (x == 7) != y;
434 Assert.Equal("Watch<x == 7>(0 .. 1) != y(5..17)", c9c.ToString());
435 Constraint c9d = y != (x == 7);
436 Assert.Equal("y(5..17) != Watch<x == 7>(0 .. 1)", c9d.ToString());
437
438 Constraint c9e = (x == 7) >= y;
439 Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9e.ToString());
440 Constraint c9f = y >= (x == 7);
441 Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9f.ToString());
442
443 Constraint c9g = (x == 7) > y;
444 Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9g.ToString());
445 Constraint c9h = y > (x == 7);
446 Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9h.ToString());
447
448 Constraint c9i = (x == 7) <= y;
449 Assert.Equal("Watch<x == 7>(0 .. 1) <= y(5..17)", c9i.ToString());
450 Constraint c9j = y <= (x == 7);
451 Assert.Equal("y(5..17) <= Watch<x == 7>(0 .. 1)", c9j.ToString());
452
453 Constraint c9k = (x == 7) < y;
454 Assert.Equal("Watch<x == 7>(0 .. 1) < y(5..17)", c9k.ToString());
455 Constraint c9l = y < (x == 7);
456 Assert.Equal("y(5..17) < Watch<x == 7>(0 .. 1)", c9l.ToString());
457 }
458
459 [Fact]
460 public void IntExprAndIntExpr()
461 {
462 Solver solver = new Solver("Solver");
463 Assert.NotNull(solver);
464 IntVar x = solver.MakeIntVar(3, 13, "x");
465 Assert.Equal(3, x.Min());
466 Assert.Equal(13, x.Max());
467
468 IntVar y = solver.MakeIntVar(5, 17, "y");
469 Assert.Equal(5, y.Min());
470 Assert.Equal(17, y.Max());
471
472 // Relational operator between IntExpr
473 Constraint c10a = (x == 7) == (y == 11);
474 Assert.Equal("Watch<x == 7>(0 .. 1) == Watch<y == 11>(0 .. 1)", c10a.ToString());
475
476 Constraint c10b = (x == 7) != (y == 11);
477 Assert.Equal("Watch<x == 7>(0 .. 1) != Watch<y == 11>(0 .. 1)", c10b.ToString());
478
479 Constraint c10c = (x == 7) <= (y == 11);
480 Assert.Equal("Watch<x == 7>(0 .. 1) <= Watch<y == 11>(0 .. 1)", c10c.ToString());
481 Constraint c10d = (x == 7) >= (y == 11);
482 Assert.Equal("Watch<y == 11>(0 .. 1) <= Watch<x == 7>(0 .. 1)", c10d.ToString());
483
484 Constraint c10e = (x == 7) > (y == 11);
485 Assert.Equal("Watch<y == 11>(0 .. 1) < Watch<x == 7>(0 .. 1)", c10e.ToString());
486 Constraint c10f = (x == 7) < (y == 11);
487 Assert.Equal("Watch<x == 7>(0 .. 1) < Watch<y == 11>(0 .. 1)", c10f.ToString());
488 }
489
490 [Fact]
492 {
493 Solver solver = new Solver("Solver");
494 Assert.NotNull(solver);
495 IntVar x = solver.MakeIntVar(3, 13, "x");
496 Assert.Equal(3, x.Min());
497 Assert.Equal(13, x.Max());
498
499 // Arithmetic operator with a scalar
500 IntExpr e2a = (x >= 7) + 1;
501 Assert.Equal("(Watch<x >= 7>(0 .. 1) + 1)", e2a.ToString());
502 IntExpr e2b = 1 + (x >= 7);
503 Assert.Equal("(Watch<x >= 7>(0 .. 1) + 1)", e2b.ToString());
504
505 IntExpr e2c = (x >= 7) - 1;
506 Assert.Equal("(Watch<x >= 7>(0 .. 1) + -1)", e2c.ToString());
507 IntExpr e2d = 1 - (x >= 7);
508 Assert.Equal("Not(Watch<x >= 7>(0 .. 1))", e2d.ToString());
509
510 IntExpr e2e = (x >= 7) * 2;
511 Assert.Equal("(Watch<x >= 7>(0 .. 1) * 2)", e2e.ToString());
512 IntExpr e2f = 2 * (x >= 7);
513 Assert.Equal("(Watch<x >= 7>(0 .. 1) * 2)", e2f.ToString());
514
515 // Relational operator with a scalar
516 Constraint c8a = (x >= 7) == 1;
517 Assert.Equal("(Watch<x >= 7>(0 .. 1) == 1)", c8a.ToString());
518 Constraint c8b = 1 == (x >= 7);
519 Assert.Equal("(Watch<x >= 7>(0 .. 1) == 1)", c8b.ToString());
520
521 Constraint c8c = (x >= 7) != 1;
522 Assert.Equal("(Watch<x >= 7>(0 .. 1) != 1)", c8c.ToString());
523 Constraint c8d = 1 != (x >= 7);
524 Assert.Equal("(Watch<x >= 7>(0 .. 1) != 1)", c8d.ToString());
525
526 Constraint c8e = (x >= 7) >= 1;
527 Assert.Equal("(Watch<x >= 7>(0 .. 1) >= 1)", c8e.ToString());
528 Constraint c8f = 1 >= (x >= 7);
529 Assert.Equal("TrueConstraint()", c8f.ToString());
530
531 Constraint c8g = (x >= 7) > 1;
532 Assert.Equal("FalseConstraint()", c8g.ToString());
533 Constraint c8h = 1 > (x >= 7);
534 Assert.Equal("(Watch<x >= 7>(0 .. 1) <= 0)", c8h.ToString());
535
536 Constraint c8i = (x >= 7) <= 1;
537 Assert.Equal("TrueConstraint()", c8i.ToString());
538 Constraint c8j = 1 <= (x >= 7);
539 Assert.Equal("(Watch<x >= 7>(0 .. 1) >= 1)", c8j.ToString());
540
541 Constraint c8k = (x >= 7) < 1;
542 Assert.Equal("(Watch<x >= 7>(0 .. 1) <= 0)", c8k.ToString());
543 Constraint c8l = 1 < (x >= 7);
544 Assert.Equal("FalseConstraint()", c8l.ToString());
545 }
546
547 [Fact]
549 {
550 Solver solver = new Solver("Solver");
551 Assert.NotNull(solver);
552 IntVar x = solver.MakeIntVar(3, 13, "x");
553 Assert.Equal(3, x.Min());
554 Assert.Equal(13, x.Max());
555
556 IntVar y = solver.MakeIntVar(5, 17, "y");
557 Assert.Equal(5, y.Min());
558 Assert.Equal(17, y.Max());
559
560 // Arithmetic operator with IntVar
561 IntExpr e3a = (x >= 7) + y;
562 Assert.Equal("(Watch<x >= 7>(0 .. 1) + y(5..17))", e3a.ToString());
563 IntExpr e3b = y + (x >= 7);
564 Assert.Equal("(Watch<x >= 7>(0 .. 1) + y(5..17))", e3b.ToString());
565
566 IntExpr e3c = (x >= 7) - y;
567 Assert.Equal("(Watch<x >= 7>(0 .. 1) - y(5..17))", e3c.ToString());
568 IntExpr e3d = y - (x >= 7);
569 Assert.Equal("(y(5..17) - Watch<x >= 7>(0 .. 1))", e3d.ToString());
570
571 IntExpr e3e = (x >= 7) * y;
572 Assert.Equal("(Watch<x >= 7>(0 .. 1) * y(5..17))", e3e.ToString());
573 IntExpr e3f = y * (x >= 7);
574 Assert.Equal("(Watch<x >= 7>(0 .. 1) * y(5..17))", e3f.ToString());
575
576 // Relational operator with an IntVar
577 Constraint c9a = (x >= 7) == y;
578 Assert.Equal("Watch<x >= 7>(0 .. 1) == y(5..17)", c9a.ToString());
579 Constraint c9b = y == (x >= 7);
580 Assert.Equal("y(5..17) == Watch<x >= 7>(0 .. 1)", c9b.ToString());
581
582 Constraint c9c = (x >= 7) != y;
583 Assert.Equal("Watch<x >= 7>(0 .. 1) != y(5..17)", c9c.ToString());
584 Constraint c9d = y != (x >= 7);
585 Assert.Equal("y(5..17) != Watch<x >= 7>(0 .. 1)", c9d.ToString());
586
587 Constraint c9e = (x >= 7) >= y;
588 Assert.Equal("y(5..17) <= Watch<x >= 7>(0 .. 1)", c9e.ToString());
589 Constraint c9f = y >= (x >= 7);
590 Assert.Equal("Watch<x >= 7>(0 .. 1) <= y(5..17)", c9f.ToString());
591
592 Constraint c9g = (x >= 7) > y;
593 Assert.Equal("y(5..17) < Watch<x >= 7>(0 .. 1)", c9g.ToString());
594 Constraint c9h = y > (x >= 7);
595 Assert.Equal("Watch<x >= 7>(0 .. 1) < y(5..17)", c9h.ToString());
596
597 Constraint c9i = (x >= 7) <= y;
598 Assert.Equal("Watch<x >= 7>(0 .. 1) <= y(5..17)", c9i.ToString());
599 Constraint c9j = y <= (x >= 7);
600 Assert.Equal("y(5..17) <= Watch<x >= 7>(0 .. 1)", c9j.ToString());
601
602 Constraint c9k = (x >= 7) < y;
603 Assert.Equal("Watch<x >= 7>(0 .. 1) < y(5..17)", c9k.ToString());
604 Constraint c9l = y < (x >= 7);
605 Assert.Equal("y(5..17) < Watch<x >= 7>(0 .. 1)", c9l.ToString());
606 }
607
608 [Fact]
610 {
611 Solver solver = new Solver("Solver");
612 Assert.NotNull(solver);
613 IntVar x = solver.MakeIntVar(3, 13, "x");
614 Assert.Equal(3, x.Min());
615 Assert.Equal(13, x.Max());
616
617 IntVar y = solver.MakeIntVar(5, 17, "y");
618 Assert.Equal(5, y.Min());
619 Assert.Equal(17, y.Max());
620
621 // Relational operator between IntExpr
622 Constraint c10a = (x >= 7) == (y >= 11);
623 Assert.Equal("Watch<x >= 7>(0 .. 1) == Watch<y >= 11>(0 .. 1)", c10a.ToString());
624
625 Constraint c10b = (x >= 7) != (y >= 11);
626 Assert.Equal("Watch<x >= 7>(0 .. 1) != Watch<y >= 11>(0 .. 1)", c10b.ToString());
627
628 Constraint c10c = (x >= 7) <= (y >= 11);
629 Assert.Equal("Watch<x >= 7>(0 .. 1) <= Watch<y >= 11>(0 .. 1)", c10c.ToString());
630 Constraint c10d = (x >= 7) >= (y >= 11);
631 Assert.Equal("Watch<y >= 11>(0 .. 1) <= Watch<x >= 7>(0 .. 1)", c10d.ToString());
632
633 Constraint c10e = (x >= 7) > (y >= 11);
634 Assert.Equal("Watch<y >= 11>(0 .. 1) < Watch<x >= 7>(0 .. 1)", c10e.ToString());
635 Constraint c10f = (x >= 7) < (y >= 11);
636 Assert.Equal("Watch<x >= 7>(0 .. 1) < Watch<y >= 11>(0 .. 1)", c10f.ToString());
637 }
638
639 [Fact]
640 public void Downcast()
641 {
642 Solver solver = new Solver("Solver");
643 Assert.NotNull(solver);
644 IntVar x = solver.MakeIntVar(2, 17, "x");
645 IntExpr e = x + 5;
646 IntVar y = e.Var();
647 Assert.Equal("(x(2..17) + 5)", y.ToString());
648 }
649
650 [Fact]
651 public void Sequence()
652 {
653 Solver solver = new Solver("Solver");
654 Assert.NotNull(solver);
655 IntervalVar[] intervals = solver.MakeFixedDurationIntervalVarArray(10, 0, 10, 5, false, "task");
656 DisjunctiveConstraint disjunctive = intervals.Disjunctive("Sequence");
657 SequenceVar var = disjunctive.SequenceVar();
658 Assignment ass = solver.MakeAssignment();
659 ass.Add(var);
660 ass.SetForwardSequence(var, new int[] { 1, 3, 5 });
661 int[] seq = ass.ForwardSequence(var);
662 Assert.Equal(3, seq.Length);
663 Assert.Equal(1, seq[0]);
664 Assert.Equal(3, seq[1]);
665 Assert.Equal(5, seq[2]);
666 }
667
668 // A simple demon that simply sets the maximum of a fixed IntVar to 10 when
669 // it's being called.
670 class SetMaxDemon : NetDemon
671 {
672 public SetMaxDemon(IntVar x)
673 {
674 x_ = x;
675 }
676 public override void Run(Solver s)
677 {
678 x_.SetMax(10);
679 }
680 private IntVar x_;
681 }
682
683 [Fact]
684 public void Demon()
685 {
686 Solver solver = new Solver("DemonTest");
687 Assert.NotNull(solver);
688 IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
689 NetDemon demon = new SetMaxDemon(x);
690 Assert.Equal(11, x.Max());
691 demon.Run(solver);
692 Assert.Equal(10, x.Max());
693 }
694
695 // This constraint has a single target variable x. It enforces x >= 5 upon
696 // InitialPropagate() and invokes the SetMaxDemon when x changes its range.
697 class SetMinAndMaxConstraint : NetConstraint
698 {
699 public SetMinAndMaxConstraint(Solver solver, IntVar x) : base(solver)
700 {
701 x_ = x;
702 }
703 public override void Post()
704 {
705 // Always store the demon in the constraint to avoid it being reclaimed
706 // by the GC.
707 demon_ = new SetMaxDemon(x_);
708 x_.WhenBound(demon_);
709 }
710 public override void InitialPropagate()
711 {
712 x_.SetMin(5);
713 }
714 private IntVar x_;
715 private Demon demon_;
716 }
717
718 [Fact]
720 {
721 Solver solver = new Solver("TestConstraint");
722 Assert.NotNull(solver);
723 IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
724 Constraint ct = new SetMinAndMaxConstraint(solver, x);
725 solver.Add(ct);
727 solver.NewSearch(db);
728 Assert.Equal(-1, x.Min());
729 Assert.Equal(11, x.Max());
730
731 Assert.True(solver.NextSolution());
732 Assert.Equal(6, x.Min());
733 Assert.Equal(6, x.Max());
734
735 Assert.True(solver.NextSolution());
736 Assert.Equal(10, x.Min());
737 Assert.Equal(10, x.Max());
738
739 Assert.False(solver.NextSolution());
740 solver.EndSearch();
741 }
742
743 // // This constraint has a single target variable x. It enforces x >= 5,
744 // but only at the leaf of the search tree: it doesn't change x's bounds,
745 // and simply fails if x is bound and is < 5.
746 class DumbGreaterOrEqualToFive : NetConstraint
747 {
748 public DumbGreaterOrEqualToFive(Solver solver, IntVar x) : base(solver)
749 {
750 x_ = x;
751 }
752 public override void Post()
753 {
755 x_.WhenRange(demon_);
756 }
757 public override void InitialPropagate()
758 {
759 if (x_.Bound())
760 {
761 if (x_.Value() < 5)
762 {
763 solver().Fail();
764 }
765 }
766 }
767 private IntVar x_;
768 private Demon demon_;
769 }
770
771 [Fact]
772 public void FailingConstraint()
773 {
774 Solver solver = new Solver("TestConstraint");
775 Assert.NotNull(solver);
776 IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
777 Constraint ct = new DumbGreaterOrEqualToFive(solver, x);
778 solver.Add(ct);
780 solver.NewSearch(db);
781 Assert.True(solver.NextSolution());
782 Assert.Equal(6, x.Min());
783 solver.EndSearch();
784 }
785
786 [Fact]
787 public void DomainIterator()
788 {
789 Solver solver = new Solver("TestConstraint");
790 Assert.NotNull(solver);
791 IntVar x = solver.MakeIntVar(new int[] { 2, 4, -1, 6, 11, 10 }, "x");
792 ulong count = 0;
793 foreach (long value in x.GetDomain())
794 {
795 count++;
796 }
797 Assert.Equal(count, x.Size());
798 }
799
800 class CountHoles : NetDemon
801 {
802 public CountHoles(IntVar x)
803 {
804 x_ = x;
805 count_ = 0;
806 }
807 public override void Run(Solver s)
808 {
809 foreach (long removed in x_.GetHoles())
810 {
811 count_++;
812 }
813 }
814 public int count()
815 {
816 return count_;
817 }
818 private IntVar x_;
819 private int count_;
820 }
821
822 class RemoveThreeValues : NetConstraint
823 {
824 public RemoveThreeValues(Solver solver, IntVar x) : base(solver)
825 {
826 x_ = x;
827 }
828 public override void Post()
829 {
830 demon_ = new CountHoles(x_);
831 x_.WhenDomain(demon_);
832 }
833 public override void InitialPropagate()
834 {
835 x_.RemoveValues(new long[] { 3, 5, 7 });
836 }
837 public int count()
838 {
839 return demon_.count();
840 }
841 private IntVar x_;
842 private CountHoles demon_;
843 }
844
845 [Fact]
846 public void HoleIteratorTest()
847 {
848 Solver solver = new Solver("TestConstraint");
849 Assert.NotNull(solver);
850 IntVar x = solver.MakeIntVar(0, 10, "x");
851 RemoveThreeValues ct = new RemoveThreeValues(solver, x);
852 solver.Add(ct);
854 solver.Solve(db);
855 Assert.Equal(3, ct.count());
856 }
857
858 // TODO(user): Improve search log tests; currently only tests coverage.
859 void RunSearchLog(in SearchMonitor searchlog)
860 {
861 searchlog.EnterSearch();
862 searchlog.ExitSearch();
863 searchlog.AcceptSolution();
864 searchlog.AtSolution();
865 searchlog.BeginFail();
866 searchlog.NoMoreSolutions();
867 searchlog.BeginInitialPropagation();
868 searchlog.EndInitialPropagation();
869 }
870
871 [Fact]
872 public void SearchLog()
873 {
874 Solver solver = new Solver("TestSearchLog");
875 Assert.NotNull(solver);
876 IntVar var = solver.MakeIntVar(1, 1, "Variable");
877 OptimizeVar objective = solver.MakeMinimize(var, 1);
878 SearchMonitor searchlog = solver.MakeSearchLog(0);
879 RunSearchLog(in searchlog);
880 GC.KeepAlive(solver);
881 }
882
883 [Theory]
884 [InlineData(false)]
885 [InlineData(true)]
886 public void SearchLogWithCallback(bool callGC)
887 {
888 Solver solver = new Solver("TestSearchLog");
889 Assert.NotNull(solver);
890 IntVar var = solver.MakeIntVar(1, 1, "Variable");
891 OptimizeVar objective = solver.MakeMinimize(var, 1);
892 int count = 0;
893 SearchMonitor searchlog = solver.MakeSearchLog(0, // branch period
894 () =>
895 {
896 count++;
897 return "display callback...";
898 });
899 if (callGC)
900 {
901 GC.Collect();
902 GC.WaitForPendingFinalizers();
903 }
904 RunSearchLog(in searchlog);
905 GC.KeepAlive(solver);
906 Assert.Equal(1, count);
907 }
908
909 [Theory]
910 [InlineData(false)]
911 [InlineData(true)]
912 public void SearchLogWithObjectiveAndCallback(bool callGC)
913 {
914 Solver solver = new Solver("TestSearchLog");
915 Assert.NotNull(solver);
916 IntVar var = solver.MakeIntVar(1, 1, "Variable");
917 OptimizeVar objective = solver.MakeMinimize(var, 1);
918 int count = 0;
919 SearchMonitor searchlog = solver.MakeSearchLog(0, // branch period
920 objective, // objective var to monitor
921 () =>
922 {
923 count++;
924 return "OptimizeVar display callback";
925 });
926 if (callGC)
927 {
928 GC.Collect();
929 GC.WaitForPendingFinalizers();
930 }
931 RunSearchLog(in searchlog);
932 GC.KeepAlive(solver);
933 Assert.Equal(1, count);
934 }
935
936 [Theory]
937 [InlineData(false)]
938 [InlineData(true)]
939 public void SearchLogWithIntVarAndCallback(bool callGC)
940 {
941 Solver solver = new Solver("TestSearchLog");
942 Assert.NotNull(solver);
943 IntVar var = solver.MakeIntVar(1, 1, "Variable");
944 OptimizeVar objective = solver.MakeMinimize(var, 1);
945 int count = 0;
946 SearchMonitor searchlog = solver.MakeSearchLog(0, // branch period
947 var, // int var to monitor
948 () =>
949 {
950 count++;
951 return "IntVar display callback";
952 });
953 if (callGC)
954 {
955 GC.Collect();
956 GC.WaitForPendingFinalizers();
957 }
958 RunSearchLog(in searchlog);
959 GC.KeepAlive(solver);
960 Assert.Equal(1, count);
961 }
962}
963} // namespace Google.OrTools.Tests
void SetForwardSequence(SequenceVar var, int[] forward_sequence)
virtual void WhenRange(Demon d)
Definition IntExpr.cs:123
virtual void RemoveValues(long[] values)
Definition IntVar.cs:91
virtual void WhenDomain(Demon d)
Definition IntVar.cs:108
virtual void WhenBound(Demon d)
Definition IntVar.cs:100
DecisionBuilder MakePhase(IntVarVector vars, int var_str, int val_str)
Definition Solver.cs:2396
SearchMonitor MakeSearchLog(int branch_period)
Definition Solver.cs:2166
IntVar MakeIntVar(long min, long max, string name)
Definition Solver.cs:510
static readonly int ASSIGN_MIN_VALUE
Definition Solver.cs:3099
IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration, bool optional)
OptimizeVar MakeMinimize(IntVar v, long step)
Definition Solver.cs:1904
void NewSearch(DecisionBuilder db)
static readonly int CHOOSE_FIRST_UNBOUND
Definition Solver.cs:3083
bool Solve(DecisionBuilder db, SearchMonitorVector monitors)
Definition Solver.cs:308
Demon MakeConstraintInitialPropagateCallback(Constraint ct)
Definition Solver.cs:1206