/
DisjunctiveTest.java
362 lines (320 loc) · 12.8 KB
/
DisjunctiveTest.java
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
/*
* mini-cp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3
* as published by the Free Software Foundation.
*
* mini-cp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mini-cp. If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
*
* Copyright (c) 2018. by Laurent Michel, Pierre Schaus, Pascal Van Hentenryck
*/
package minicp.engine.constraints;
import minicp.engine.SolverTest;
import minicp.engine.core.BoolVar;
import minicp.engine.core.IntVar;
import minicp.engine.core.Solver;
import minicp.search.DFSearch;
import minicp.search.SearchStatistics;
import minicp.util.exception.InconsistencyException;
import minicp.util.exception.NotImplementedException;
import minicp.util.NotImplementedExceptionAssume;
import org.javagrader.Grade;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import static minicp.cp.BranchingScheme.firstFail;
import static minicp.cp.Factory.*;
import static org.junit.jupiter.api.Assertions.*;
@Grade(value = 2, cpuTimeout = 1)
public class DisjunctiveTest extends SolverTest {
private static void decomposeDisjunctive(IntVar[] start, int[] duration) {
Solver cp = start[0].getSolver();
IntVar[] end = makeIntVarArray(start.length, i -> plus(start[i], duration[i]));
for (int i = 0; i < start.length; i++) {
for (int j = i + 1; j < start.length; j++) {
// i before j or j before i:
BoolVar iBeforej = makeBoolVar(cp);
BoolVar jBeforei = not(iBeforej);
cp.post(new IsLessOrEqualVar(iBeforej, end[i], start[j]));
cp.post(new IsLessOrEqualVar(jBeforei, end[j], start[i]));
}
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testAllDiffDisjunctive(Solver cp) {
try {
IntVar[] s = makeIntVarArray(cp, 5, 5);
int[] d = new int[5];
Arrays.fill(d, 1);
cp.post(new Disjunctive(s, d));
SearchStatistics stats = makeDfs(cp, firstFail(s)).solve();
assertEquals( 120, stats.numberOfSolutions());
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testNotRemovingSolutions(Solver cp) {
try {
IntVar[] s = makeIntVarArray(cp, 4, 20);
int[] d = new int[]{5, 4, 6, 7};
DFSearch dfs = makeDfs(cp, firstFail(s));
cp.getStateManager().saveState();
cp.post(new Disjunctive(s, d));
SearchStatistics stat1 = dfs.solve();
cp.getStateManager().restoreState();
decomposeDisjunctive(s, d);
SearchStatistics stat2 = dfs.solve();
assertEquals(stat1.numberOfSolutions(), stat2.numberOfSolutions());
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testBinaryDecomposition(Solver cp) {
IntVar s1 = makeIntVar(cp, 0, 10);
int d1 = 10;
IntVar s2 = makeIntVar(cp, 6, 15);
int d2 = 6;
try {
cp.post(new Disjunctive(new IntVar[]{s1, s2}, new int[]{d1, d2}));
assertEquals(10, s2.min());
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testOverloadChecker(Solver cp) {
IntVar sA = makeIntVar(cp, 0, 9);
int d1 = 5;
IntVar sB = makeIntVar(cp, 1, 10);
int d2 = 5;
IntVar sC = makeIntVar(cp, 3, 7);
int d3 = 6;
Disjunctive disjunctive = new Disjunctive(new IntVar[]{sA, sB, sC}, new int[]{d1, d2, d3});
// Test the method by itself:
cp.getStateManager().withNewState(() -> {
try {
disjunctive.overLoadChecker();
fail();
} catch (InconsistencyException ignored) {
;
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
});
// Integration test by posting the constraint:
try {
disjunctive.post();
fail();
} catch (InconsistencyException e) {
assert (true);
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testDetectablePrecedence(Solver cp) {
IntVar sA = makeIntVar(cp, 0, 9);
int d1 = 5;
IntVar sB = makeIntVar(cp, 1, 10);
int d2 = 5;
IntVar sC = makeIntVar(cp, 8, 15);
int d3 = 3;
Disjunctive disjunctive = new Disjunctive(new IntVar[]{sA, sB, sC}, new int[]{d1, d2, d3});
// Test the method by itself:
cp.getStateManager().withNewState(() -> {
try {
assertTrue(disjunctive.detectablePrecedence());
assertEquals(10, sC.min(), "detectable precedence should set sC.min() to 10");
assertFalse(disjunctive.detectablePrecedence());
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
});
// Integration test by posting the constraint:
try {
disjunctive.post();
assertEquals(10, sC.min(), "detectable precedence should set sC.min() to 10");
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testNotLast(Solver cp) {
IntVar sA = makeIntVar(cp, 0, 9);
int d1 = 5;
IntVar sB = makeIntVar(cp, 1, 10);
int d2 = 5;
IntVar sC = makeIntVar(cp, 3, 9);
int d3 = 4;
Disjunctive disjunctive = new Disjunctive(new IntVar[]{sA, sB, sC}, new int[]{d1, d2, d3});
// Test the method by itself:
cp.getStateManager().withNewState(() -> {
try {
assertTrue(disjunctive.notLast());
assertEquals(6, sC.max(), "not last should set sC.max() to 6");
assertTrue(disjunctive.notLast());
assertEquals(1, sA.max(), "not last should set sA.max() to 1");
assertFalse(disjunctive.notLast(), "the second not last iteration should not detect any changes");
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
});
// Integration test by posting the constraint:
try {
disjunctive.post();
assertEquals(6, sC.max(), "not last should set sC.max() to 6");
assertEquals(1, sA.max(), "not last should set sA.max() to 1");
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testUsingManualLoop(Solver cp) {
IntVar[] s = new IntVar[] {
makeIntVar(cp, 0, 19),
makeIntVar(cp, 9, 17),
makeIntVar(cp, 0, 14),
makeIntVar(cp, 8, 16)
};
int[] d = new int[]{4, 5, 6, 7};
Disjunctive disjunctive = new Disjunctive(s, d);
// performDetPrec[i] = true iff the detectable precedence pruning method returns true at iteration i,
// false otherwise:
boolean[] performDetPrec = new boolean[]{false, false, true, false};
// If performDetPrec[i] is false, then the not last pruning method is to be called at iteration i and
// return true.
// minDetPrec[i][j] = s[j].min() after running the detectable precedence pruning method at iteration i:
int[][] minDetPrec = new int[][]{
{0, 9, 0, 8},
{0, 9, 0, 8},
{0, 10, 0, 10},
{0, 10, 0, 10}
};
// maxDetPrec[i][j] = s[j].max() after running the detectable precedence pruning method at iteration i:
int[][] maxDetPrec = new int[][]{
{19, 17, 14, 16},
{15, 17, 13, 16},
{13, 17, 11, 16},
{13, 17, 11, 16}
};
// minNotLast[i][j] = s[j].min() after possibly running the not last pruning method at iteration i:
int[][] minNotLast = new int[][]{
{0, 9, 0, 8},
{0, 9, 0, 8},
{0, 10, 0, 10},
{0, 10, 0, 10}
};
// maxNotLast[i][j] = s[j].max() after possibly running the not last pruning method at iteration i:
int[][] maxNotLast = new int[][]{
{15, 17, 13, 16},
{13, 17, 11, 16},
{13, 17, 11, 16},
{12, 17, 10, 16}
};
try {
for (int i = 0; i < performDetPrec.length; i++) {
disjunctive.overLoadChecker();
String message = String.format("iteration: %d", i);
assertEquals(performDetPrec[i], disjunctive.detectablePrecedence(), message);
for (int j = 0; j < s.length; j++) {
assertEquals(minDetPrec[i][j], s[j].min(), message);
assertEquals(maxDetPrec[i][j], s[j].max(), message);
assertEquals(1 + maxDetPrec[i][j] - minDetPrec[i][j], s[j].size(), message);
}
if (!performDetPrec[i]) {
assertEquals(!performDetPrec[i], disjunctive.notLast());
}
for (int j = 0; j < s.length; j++) {
assertEquals(minNotLast[i][j], s[j].min(), message);
assertEquals(maxNotLast[i][j], s[j].max(), message);
assertEquals(1 + maxNotLast[i][j] - minNotLast[i][j], s[j].size(), message);
}
}
disjunctive.overLoadChecker();
assertFalse(disjunctive.detectablePrecedence());
assertFalse(disjunctive.notLast());
for (int j = 0; j < s.length; j++) {
int lastIndex = performDetPrec.length - 1;
assertEquals(minNotLast[lastIndex][j], s[j].min());
assertEquals(maxNotLast[lastIndex][j], s[j].max());
assertEquals(1 + maxNotLast[lastIndex][j] - minNotLast[lastIndex][j], s[j].size());
}
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testDisjunctive1(Solver cp) {
IntVar sA = makeIntVar(cp, 0, 9);
int d1 = 5;
IntVar sB = makeIntVar(cp, 1, 10);
int d2 = 5;
IntVar sC = makeIntVar(cp, 8, 15);
int d3 = 3;
try {
new Disjunctive(new IntVar[]{sA, sB, sC}, new int[]{d1, d2, d3}).post();
assertEquals(10, sA.size());
assertEquals(10, sB.size());
assertEquals(6, sC.size());
assertEquals(10, sC.min());
assertEquals(15, sC.max());
} catch (InconsistencyException e) {
fail();
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
@ParameterizedTest
@MethodSource("getSolver")
public void testDisjunctive2(Solver cp) {
IntVar sA = makeIntVar(cp, 0, 9);
int d1 = 5;
IntVar sB = makeIntVar(cp, 1, 10);
int d2 = 5;
IntVar sC = makeIntVar(cp, 3, 9);
int d3 = 4;
try {
new Disjunctive(new IntVar[]{sA, sB, sC}, new int[]{d1, d2, d3}).post();
assertEquals(0, sA.min());
assertEquals(1, sA.max());
assertEquals(9, sB.min());
assertEquals(10, sB.max());
assertEquals(5, sC.min());
assertEquals(6, sC.max());
} catch (InconsistencyException e) {
fail("should not fail");
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
}