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 package org.sat4j.pb;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertFalse;
34 import static org.junit.Assert.assertTrue;
35 import static org.junit.Assert.fail;
36
37 import java.math.BigInteger;
38
39 import org.junit.Test;
40 import org.sat4j.core.Vec;
41 import org.sat4j.core.VecInt;
42 import org.sat4j.specs.ContradictionException;
43 import org.sat4j.specs.IConstr;
44 import org.sat4j.specs.IProblem;
45 import org.sat4j.specs.IVec;
46 import org.sat4j.specs.IVecInt;
47 import org.sat4j.specs.TimeoutException;
48 import org.sat4j.tools.ModelIterator;
49
50 public class TestLonca {
51
52 @Test
53 public void testIteratingWithNoObjectiveFunction() {
54 IPBSolver solver = buildSolver1();
55
56 IProblem problem = solver;
57 int nbModel = 0;
58 try {
59 while (problem.isSatisfiable()) {
60 int[] mod = problem.model();
61 solver.addBlockingClause(new VecInt(invert(mod)));
62 nbModel++;
63 }
64 } catch (TimeoutException e) {
65 fail();
66 } catch (ContradictionException e) {
67 fail();
68 }
69 assertEquals(4, nbModel);
70 }
71
72 @Test
73 public void testIteratingWithObjectiveFunctionCard() {
74 IPBSolver solver = buildSolver2();
75 IProblem problem = solver;
76 int nbModel = 0;
77 try {
78 while (problem.isSatisfiable()) {
79 int[] mod = problem.model();
80 solver.addBlockingClause(new VecInt(invert(mod)));
81 nbModel++;
82 }
83 } catch (TimeoutException e) {
84 fail();
85 } catch (ContradictionException e) {
86 fail();
87 }
88 assertEquals(4, nbModel);
89 }
90
91 @Test
92 public void testIteratingWithObjectiveFunctionPseudo() {
93 IPBSolver solver = buildSolver3();
94 IProblem problem = solver;
95 int nbModel = 0;
96 try {
97 while (problem.isSatisfiable()) {
98 int[] mod = problem.model();
99 solver.addBlockingClause(new VecInt(invert(mod)));
100 nbModel++;
101 }
102 } catch (TimeoutException e) {
103 fail();
104 } catch (ContradictionException e) {
105 fail();
106 }
107 assertEquals(4, nbModel);
108 }
109
110 @Test
111 public void testIteratingWithObjectiveFunctionWithDecorator() {
112 IPBSolver solver = buildSolver2();
113
114 IProblem problem = new ModelIterator(solver);
115 int nbModel = 0;
116 try {
117 while (problem.isSatisfiable()) {
118 problem.model();
119 nbModel++;
120 }
121 } catch (TimeoutException e) {
122 fail();
123 }
124 assertEquals(4, nbModel);
125 }
126
127 private static int[] invert(int[] mod) {
128 int[] res = new int[mod.length];
129 for (int i = 0; i < res.length; i++) {
130 res[i] = -mod[i];
131 }
132 return res;
133 }
134
135 private static IPBSolver buildSolver1() {
136 IPBSolver solver = new OptToPBSATAdapter(new PseudoOptDecorator(
137 SolverFactory.newResolution()));
138
139 try {
140 solver.addClause(new VecInt(new int[] { 1, 2, 3 }));
141 solver.addClause(new VecInt(new int[] { -1, -2 }));
142 solver.addClause(new VecInt(new int[] { -2, -3 }));
143 } catch (ContradictionException e) {
144 fail();
145 }
146 return solver;
147 }
148
149 private static IPBSolver buildSolver2() {
150 IPBSolver solver = buildSolver1();
151 IVecInt vars = new VecInt(new int[] { 1, 2, 3 });
152 IVec<BigInteger> coeffs = new Vec<BigInteger>(new BigInteger[] {
153 BigInteger.valueOf(1), BigInteger.valueOf(1),
154 BigInteger.valueOf(1) });
155 ObjectiveFunction func = new ObjectiveFunction(vars, coeffs);
156 solver.setObjectiveFunction(func);
157 return solver;
158 }
159
160 private static IPBSolver buildSolver3() {
161 IPBSolver solver = buildSolver1();
162 IVecInt vars = new VecInt(new int[] { 1, 2, 3 });
163 IVec<BigInteger> coeffs = new Vec<BigInteger>(new BigInteger[] {
164 BigInteger.valueOf(8), BigInteger.valueOf(4),
165 BigInteger.valueOf(2) });
166 ObjectiveFunction func = new ObjectiveFunction(vars, coeffs);
167
168 solver.setObjectiveFunction(func);
169 return solver;
170 }
171
172 @Test
173 public void testRemovalOfConstraintsPropagatingLiterals()
174 throws ContradictionException, TimeoutException {
175 IPBSolver solver = buildSolver1();
176 IVecInt literals = new VecInt(new int[] { 4, 5, 6, 7 });
177 IVecInt coeffs = new VecInt(new int[] { 12, 10, 8, 6 });
178 IConstr c1 = solver.addAtMost(literals, coeffs, 8);
179 IConstr c2 = solver.addAtMost(literals, coeffs, 6);
180 assertTrue(solver.isSatisfiable());
181 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
182 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
183 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 6 })));
184 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
185 solver.removeConstr(c2);
186 assertTrue(solver.isSatisfiable());
187 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
188 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
189 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 6 })));
190 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
191 solver.removeConstr(c1);
192 assertTrue(solver.isSatisfiable());
193 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 4 })));
194 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 5 })));
195 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 6 })));
196 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
197
198 }
199
200 @Test
201 public void testRemovalOfConstraintsPropagatingLiteralsBis()
202 throws ContradictionException, TimeoutException {
203 IPBSolver solver = buildSolver1();
204 IVecInt literals = new VecInt(new int[] { 4, 5, 6, 7 });
205 IVecInt coeffs = new VecInt(new int[] { 12, 10, 8, 6 });
206 IConstr c1 = solver.addAtMost(literals, coeffs, 6);
207 IConstr c2 = solver.addAtMost(literals, coeffs, 8);
208 assertTrue(solver.isSatisfiable());
209 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
210 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
211 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 6 })));
212 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
213 solver.removeConstr(c2);
214 assertTrue(solver.isSatisfiable());
215 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
216 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
217 assertFalse(solver.isSatisfiable(new VecInt(new int[] { 6 })));
218 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
219 solver.removeConstr(c1);
220 assertTrue(solver.isSatisfiable());
221 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 4 })));
222 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 5 })));
223 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 6 })));
224 assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
225
226 }
227 }