/
LimitedDiscrepancyBranchingTest.java
82 lines (68 loc) · 2.63 KB
/
LimitedDiscrepancyBranchingTest.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
/*
* 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.search;
import minicp.cp.BranchingScheme;
import minicp.state.StateInt;
import minicp.state.StateManager;
import minicp.state.Trailer;
import minicp.util.NotImplementedExceptionAssume;
import minicp.util.Procedure;
import minicp.util.exception.NotImplementedException;
import org.javagrader.Grade;
import org.junit.jupiter.api.Test;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Grade(cpuTimeout = 1)
public class LimitedDiscrepancyBranchingTest {
@Grade
@Test
public void testExample1() {
try {
StateManager sm = new Trailer();
StateInt i = sm.makeStateInt(0);
int[] values = new int[4];
Supplier<Procedure[]> bs = () -> {
if (i.value() >= values.length)
return BranchingScheme.EMPTY;
else return BranchingScheme.branch(
() -> { // left branch
values[i.value()] = 0;
i.increment();
},
() -> { // right branch
values[i.value()] = 1;
i.increment();
});
};
LimitedDiscrepancyBranching bsDiscrepancy =
new LimitedDiscrepancyBranching(bs, 2);
DFSearch dfs = new DFSearch(sm, bsDiscrepancy);
dfs.onSolution(() -> {
int n1 = 0;
for (int k = 0; k < values.length; k++) {
n1 += values[k];
}
assertTrue(n1 <= 2);
});
SearchStatistics stats = dfs.solve();
assertEquals(11, stats.numberOfSolutions());
assertEquals(0, stats.numberOfFailures());
assertEquals(24, stats.numberOfNodes()); // root node does not count
} catch (NotImplementedException e) {
NotImplementedExceptionAssume.fail(e);
}
}
}