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 package org.sat4j;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertFalse;
35 import static org.junit.Assert.assertTrue;
36 import static org.junit.Assert.fail;
37
38 import java.util.Arrays;
39
40 import org.junit.Test;
41 import org.sat4j.core.VecInt;
42 import org.sat4j.minisat.SolverFactory;
43 import org.sat4j.specs.ContradictionException;
44 import org.sat4j.specs.ISolver;
45 import org.sat4j.specs.IVecInt;
46 import org.sat4j.specs.TimeoutException;
47 import org.sat4j.tools.Minimal4CardinalityModel;
48 import org.sat4j.tools.Minimal4InclusionModel;
49 import org.sat4j.tools.ModelIterator;
50 import org.sat4j.tools.SearchEnumeratorListener;
51 import org.sat4j.tools.SolutionCounter;
52 import org.sat4j.tools.SolutionFoundListener;
53
54
55
56
57
58
59
60 public class ModelIteratorTest {
61
62 @Test
63 public void testModelIterator() {
64 try {
65 ISolver solver = new ModelIterator(SolverFactory.newDefault());
66 solver.newVar(3);
67 IVecInt clause = new VecInt();
68 clause.push(1);
69 clause.push(2);
70 clause.push(3);
71 solver.addClause(clause);
72 clause.clear();
73 clause.push(-1);
74 clause.push(-2);
75 clause.push(-3);
76 solver.addClause(clause);
77 int counter = 0;
78 while (solver.isSatisfiable()) {
79 solver.model();
80 counter++;
81 }
82 assertEquals(6, counter);
83 } catch (ContradictionException e) {
84 fail();
85 } catch (TimeoutException e) {
86 fail();
87 }
88 }
89
90 @Test
91 public void testInnerModelIterator() {
92 try {
93 ISolver solver = SolverFactory.newDefault();
94 SolutionFoundListener sfl = new SolutionFoundListener() {
95
96 public void onSolutionFound(int[] solution) {
97 System.out.println(new VecInt(solution));
98 }
99
100 public void onSolutionFound(IVecInt solution) {
101 throw new UnsupportedOperationException(
102 "Not implemented yet!");
103 }
104
105 public void onUnsatTermination() {
106
107 }
108
109 };
110 SearchEnumeratorListener enumerator = new SearchEnumeratorListener(
111 sfl);
112 solver.setSearchListener(enumerator);
113 solver.newVar(3);
114 IVecInt clause = new VecInt();
115 clause.push(1);
116 clause.push(2);
117 clause.push(3);
118 solver.addClause(clause);
119 clause.clear();
120 clause.push(-1);
121 clause.push(-2);
122 clause.push(-3);
123 solver.addClause(clause);
124 assertTrue(solver.isSatisfiable());
125 assertEquals(6, enumerator.getNumberOfSolutionFound());
126 } catch (ContradictionException e) {
127 fail();
128 } catch (TimeoutException e) {
129 fail();
130 }
131 }
132
133 @Test
134 public void testInplicantCoverIterator() {
135 try {
136 ModelIterator solver = new ModelIterator(SolverFactory.newDefault());
137 solver.newVar(3);
138 IVecInt clause = new VecInt();
139 clause.push(1);
140 clause.push(2);
141 clause.push(3);
142 solver.addClause(clause);
143 clause.clear();
144 clause.push(-1);
145 clause.push(-2);
146 clause.push(-3);
147 solver.addClause(clause);
148 while (solver.isSatisfiable()) {
149 int[] prime = solver.primeImplicant();
150 System.out.println(Arrays.toString(prime));
151 }
152 assertEquals(6, solver.numberOfModelsFoundSoFar());
153 } catch (ContradictionException e) {
154 fail();
155 } catch (TimeoutException e) {
156 fail();
157 }
158 }
159
160 @Test
161 public void testModelIteratorLimit() {
162 try {
163 ISolver solver = new ModelIterator(SolverFactory.newDefault(), 3);
164 solver.newVar(3);
165 IVecInt clause = new VecInt();
166 clause.push(1);
167 clause.push(2);
168 clause.push(3);
169 solver.addClause(clause);
170 clause.clear();
171 clause.push(-1);
172 clause.push(-2);
173 clause.push(-3);
174 solver.addClause(clause);
175 int counter = 0;
176 while (solver.isSatisfiable()) {
177 solver.model();
178 counter++;
179 }
180 assertEquals(3, counter);
181 } catch (ContradictionException e) {
182 fail();
183 } catch (TimeoutException e) {
184 fail();
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 @Test
246 public void testCardModel() {
247 try {
248 ISolver solver = new Minimal4CardinalityModel(
249 SolverFactory.newDefault());
250 solver.newVar(3);
251 IVecInt clause = new VecInt();
252 clause.push(1);
253 clause.push(-2);
254 clause.push(3);
255 solver.addClause(clause);
256 clause.clear();
257 clause.push(-1);
258 clause.push(2);
259 clause.push(-3);
260 solver.addClause(clause);
261 int counter = 0;
262 while (solver.isSatisfiable() && counter < 10) {
263 solver.model();
264 counter++;
265 }
266 assertEquals(10, counter);
267 } catch (ContradictionException e) {
268 fail();
269 } catch (TimeoutException e) {
270 fail();
271 }
272 }
273
274 @Test
275 public void testIncModel() {
276 try {
277 ISolver solver = new Minimal4InclusionModel(
278 SolverFactory.newDefault());
279 solver.newVar(3);
280 IVecInt clause = new VecInt();
281 clause.push(1);
282 clause.push(-2);
283 clause.push(3);
284 solver.addClause(clause);
285 clause.clear();
286 clause.push(-1);
287 clause.push(2);
288 clause.push(-3);
289 solver.addClause(clause);
290 int counter = 0;
291 while (solver.isSatisfiable() && counter < 10) {
292 solver.model();
293 counter++;
294 }
295 assertEquals(10, counter);
296 } catch (ContradictionException e) {
297 fail();
298 } catch (TimeoutException e) {
299 fail();
300 }
301 }
302
303 @Test
304 public void testIsSatisfiableVecInt() {
305 try {
306 ISolver solver = SolverFactory.newDefault();
307 solver.newVar(3);
308 IVecInt clause = new VecInt();
309 clause.push(1);
310 clause.push(2);
311 clause.push(3);
312 solver.addClause(clause);
313 clause.clear();
314 clause.push(-1);
315 clause.push(-2);
316 clause.push(-3);
317 solver.addClause(clause);
318 assertTrue(solver.isSatisfiable());
319 IVecInt cube = new VecInt();
320 cube.push(1);
321 assertTrue(solver.isSatisfiable(cube));
322
323 cube.push(2);
324 assertEquals(2, cube.size());
325 assertTrue(solver.isSatisfiable(cube));
326
327 cube.push(-3);
328 assertEquals(3, cube.size());
329 assertTrue(solver.isSatisfiable(cube));
330
331 cube.pop();
332 cube.push(3);
333 assertEquals(3, cube.size());
334
335 boolean sat = solver.isSatisfiable(cube);
336
337
338
339 assertFalse(sat);
340 } catch (ContradictionException e) {
341 fail();
342 } catch (TimeoutException e) {
343 fail();
344 }
345 }
346
347 @Test(timeout = 6000)
348 public void testGlobalTimeoutCounter() {
349 SolutionCounter counter = new SolutionCounter(
350 SolverFactory.newDefault());
351 IVecInt clause = new VecInt();
352 for (int i = 1; i < 100; i++) {
353 clause.push(i);
354 }
355 try {
356 counter.addClause(clause);
357 counter.setTimeout(3);
358 counter.countSolutions();
359 } catch (TimeoutException e) {
360 assertTrue(counter.lowerBound() > 0);
361 } catch (ContradictionException e) {
362 fail();
363 }
364 }
365
366 @Test(timeout = 6000)
367 public void testGlobalTimeoutIterator() {
368 ModelIterator iterator = new ModelIterator(SolverFactory.newDefault());
369 IVecInt clause = new VecInt();
370 for (int i = 1; i < 100; i++) {
371 clause.push(i);
372 }
373 try {
374 iterator.addClause(clause);
375 iterator.setTimeout(3);
376 while (iterator.isSatisfiable()) {
377 iterator.model();
378 }
379 } catch (TimeoutException e) {
380
381 } catch (ContradictionException e) {
382 fail();
383 }
384 }
385
386 @Test(timeout = 12000)
387 public void testSpecificValues() throws ContradictionException,
388 TimeoutException {
389 assertEquals(3L, count(2));
390 assertEquals(7L, count(3));
391 assertEquals(15L, count(4));
392 assertEquals(31L, count(5));
393 assertEquals(63L, count(6));
394 assertEquals(127L, count(7));
395 assertEquals(255L, count(8));
396 assertEquals(511L, count(9));
397 }
398
399 private long count(int size) throws ContradictionException,
400 TimeoutException {
401 SolutionCounter counter = new SolutionCounter(
402 SolverFactory.newDefault());
403 IVecInt clause = new VecInt();
404 for (int i = 1; i <= size; i++) {
405 clause.push(i);
406 }
407 counter.addClause(clause);
408 counter.setTimeout(10);
409 return counter.countSolutions();
410 }
411 }