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.tools;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertNotNull;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.sat4j.core.ConstrGroup;
38 import org.sat4j.core.VecInt;
39 import org.sat4j.minisat.SolverFactory;
40 import org.sat4j.specs.ContradictionException;
41 import org.sat4j.specs.IConstr;
42 import org.sat4j.specs.ISolver;
43 import org.sat4j.specs.IVecInt;
44 import org.sat4j.specs.TimeoutException;
45 import org.sat4j.tools.encoding.EncodingStrategy;
46 import org.sat4j.tools.encoding.Policy;
47
48 public class TestClausalCardinalitiesSequentialEncoding {
49
50 private ISolver solver;
51 private Policy policy;
52 private final static boolean MODE_DEBUG = false;
53
54 @Before
55 public void setUp() {
56 this.policy = new Policy();
57 this.policy.setAtMostOneEncoding(EncodingStrategy.SEQUENTIAL);
58 this.policy.setAtMostKEncoding(EncodingStrategy.SEQUENTIAL);
59 this.policy.setAtLeastOneEncoding(EncodingStrategy.SEQUENTIAL);
60 this.policy.setAtLeastKEncoding(EncodingStrategy.SEQUENTIAL);
61 this.policy.setExactlyOneEncoding(EncodingStrategy.SEQUENTIAL);
62 this.policy.setExactlyKEncoding(EncodingStrategy.SEQUENTIAL);
63
64 this.solver = new ClausalCardinalitiesDecorator<ISolver>(
65 SolverFactory.newDefault(), policy);
66 }
67
68 @Test
69 public void testAtMostOne() throws ContradictionException, TimeoutException {
70
71 this.solver.newVar(5);
72
73 IVecInt clause = new VecInt();
74 clause.push(1).push(2).push(3).push(4).push(5);
75
76 IConstr constr = this.solver.addAtMost(clause, 1);
77 assertNotNull(constr);
78
79 if (MODE_DEBUG) {
80 System.out.println();
81 System.out.println("testSequentialAtMostOne");
82 for (int i = 0; i < constr.size(); i++) {
83 System.out.println(((ConstrGroup) constr).getConstr(i));
84 }
85 }
86
87 ModelIterator iterator = new ModelIterator(this.solver);
88 int[] model = null;
89 int cpt = 0;
90
91 if (MODE_DEBUG)
92 System.out.println("testSequentialAtMostOne models");
93 while (iterator.isSatisfiable()) {
94 model = iterator.model();
95 assertNotNull(model);
96 if (MODE_DEBUG)
97 System.out.println(new VecInt(model));
98 cpt++;
99 }
100 assertEquals(6, cpt);
101 }
102
103 @Test
104 public void testExactlyOne() throws ContradictionException,
105 TimeoutException {
106 this.solver.newVar(5);
107
108 IVecInt clause = new VecInt();
109 clause.push(1).push(2).push(3).push(4).push(5);
110
111 IConstr constr = this.solver.addExactly(clause, 1);
112 assertNotNull(constr);
113
114 if (MODE_DEBUG) {
115 System.out.println();
116 System.out.println("testSequentialExactlyOne");
117 for (int i = 0; i < constr.size(); i++) {
118 if (constr instanceof ConstrGroup)
119 System.out.println(((ConstrGroup) constr).getConstr(i));
120 else
121 System.out.println(constr);
122 }
123 }
124
125 ModelIterator iterator = new ModelIterator(this.solver);
126 int[] model = null;
127 int cpt = 0;
128
129 if (MODE_DEBUG)
130 System.out.println("testSequentialExactlyOne models");
131 while (iterator.isSatisfiable()) {
132 model = iterator.model();
133 assertNotNull(model);
134 if (MODE_DEBUG)
135 System.out.println(new VecInt(model));
136 cpt++;
137 }
138 assertEquals(5, cpt);
139 }
140
141 @Test
142 public void testAtLeastOne() throws ContradictionException,
143 TimeoutException {
144 this.solver.newVar(5);
145
146 IVecInt clause = new VecInt();
147 clause.push(1).push(2).push(3).push(4).push(5);
148
149 IConstr constr = this.solver.addAtLeast(clause, 1);
150 assertNotNull(constr);
151
152 if (MODE_DEBUG) {
153 System.out.println();
154 System.out.println("testSequentialAtLeastOne");
155 for (int i = 0; i < constr.size(); i++) {
156 if (constr instanceof ConstrGroup)
157 System.out.println(((ConstrGroup) constr).getConstr(i));
158 else
159 System.out.println(constr);
160 }
161 }
162
163 ModelIterator iterator = new ModelIterator(this.solver);
164 int[] model = null;
165 int cpt = 0;
166
167 if (MODE_DEBUG)
168 System.out.println("testSequentialAtLeastOne models");
169 while (iterator.isSatisfiable()) {
170 model = iterator.model();
171 assertNotNull(model);
172 if (MODE_DEBUG)
173 System.out.println(new VecInt(model));
174 cpt++;
175 }
176 assertEquals(31, cpt);
177 }
178
179 @Test
180 public void testAtMost2() throws ContradictionException, TimeoutException {
181
182 this.solver.newVar(5);
183
184 IVecInt clause = new VecInt();
185 clause.push(1).push(2).push(3).push(4).push(5);
186
187 IConstr constr = this.solver.addAtMost(clause, 2);
188 assertNotNull(constr);
189
190 if (MODE_DEBUG) {
191 System.out.println();
192 System.out.println("testSequentialAtMost2");
193 if (constr instanceof ConstrGroup) {
194 for (int i = 0; i < constr.size(); i++) {
195
196 System.out.println(((ConstrGroup) constr).getConstr(i));
197
198 }
199 } else {
200 System.out.println(constr);
201 }
202 }
203
204 ModelIterator iterator = new ModelIterator(this.solver);
205 int[] model = null;
206 int cpt = 0;
207
208 if (MODE_DEBUG)
209 System.out.println("testSequentialAtMost2 models");
210 while (iterator.isSatisfiable()) {
211 model = iterator.model();
212 assertNotNull(model);
213 if (MODE_DEBUG)
214 System.out.println(new VecInt(model));
215 cpt++;
216 }
217 assertEquals(16, cpt);
218 }
219
220 @Test
221 public void testAtLeast2() throws ContradictionException, TimeoutException {
222
223 this.solver.newVar(5);
224
225 IVecInt clause = new VecInt();
226 clause.push(1).push(2).push(3).push(4).push(5);
227
228 IConstr constr = this.solver.addAtLeast(clause, 2);
229 assertNotNull(constr);
230
231 if (MODE_DEBUG) {
232 System.out.println();
233 System.out.println("testSequentialAtLeast2");
234 if (constr instanceof ConstrGroup) {
235 for (int i = 0; i < constr.size(); i++) {
236
237 System.out.println(((ConstrGroup) constr).getConstr(i));
238
239 }
240 } else {
241 System.out.println(constr);
242 }
243 }
244
245 ModelIterator iterator = new ModelIterator(this.solver);
246 int[] model = null;
247 int cpt = 0;
248
249 if (MODE_DEBUG)
250 System.out.println("testSequentialAtLeast2 models");
251 while (iterator.isSatisfiable()) {
252 model = iterator.model();
253 assertNotNull(model);
254 if (MODE_DEBUG)
255 System.out.println(new VecInt(model));
256 cpt++;
257 }
258 assertEquals(26, cpt);
259 }
260
261 @Test
262 public void testExactly2() throws ContradictionException, TimeoutException {
263
264 this.solver.newVar(5);
265
266 IVecInt clause = new VecInt();
267 clause.push(1).push(2).push(3).push(4).push(5);
268
269 IConstr constr = this.solver.addExactly(clause, 2);
270 assertNotNull(constr);
271
272 if (MODE_DEBUG) {
273 System.out.println();
274 System.out.println("testSequentialExactly2");
275 for (int i = 0; i < constr.size(); i++) {
276 System.out.println(((ConstrGroup) constr).getConstr(i));
277 }
278 }
279
280 ModelIterator iterator = new ModelIterator(this.solver);
281 int[] model = null;
282 int cpt = 0;
283
284 if (MODE_DEBUG)
285 System.out.println("testSequentialExactly2 models");
286 while (iterator.isSatisfiable()) {
287 model = iterator.model();
288 assertNotNull(model);
289 if (MODE_DEBUG)
290 System.out.println(new VecInt(model));
291 cpt++;
292 }
293 assertEquals(10, cpt);
294 }
295
296 @Test
297 public void testAtMostOneWith8Vars() throws ContradictionException,
298 TimeoutException {
299
300 this.solver.newVar(8);
301
302 IVecInt clause = new VecInt();
303 clause.push(1).push(2).push(3).push(4).push(5).push(6).push(7).push(8);
304
305 IConstr constr = this.solver.addAtMost(clause, 1);
306 assertNotNull(constr);
307
308 if (MODE_DEBUG) {
309 System.out.println();
310 System.out.println("testSequentialAtMostOneWith8Vars");
311 for (int i = 0; i < constr.size(); i++) {
312 System.out.println(((ConstrGroup) constr).getConstr(i));
313 }
314 }
315
316 ModelIterator iterator = new ModelIterator(this.solver);
317 int[] model = null;
318 int cpt = 0;
319
320 if (MODE_DEBUG)
321 System.out.println("testSequentialAtMostOneWith8Vars models");
322 while (iterator.isSatisfiable()) {
323 model = iterator.model();
324 assertNotNull(model);
325 if (MODE_DEBUG)
326 System.out.println(new VecInt(model));
327 cpt++;
328 }
329 assertEquals(9, cpt);
330 }
331
332 @Test
333 public void testExactly4With11Vars() throws ContradictionException,
334 TimeoutException {
335
336 int nVar = 11;
337 this.solver.newVar(nVar);
338
339 int degree = 4;
340
341 IVecInt clause = new VecInt();
342 for (int i = 1; i <= nVar; i++) {
343 clause.push(i);
344 }
345
346 IConstr constr = this.solver.addExactly(clause, degree);
347 assertNotNull(constr);
348
349 if (MODE_DEBUG) {
350 System.out.println();
351 System.out.println("testExactly4With11Vars");
352 for (int i = 0; i < constr.size(); i++) {
353 System.out.println(((ConstrGroup) constr).getConstr(i));
354 }
355 }
356
357 ModelIterator iterator = new ModelIterator(this.solver);
358 int[] model = null;
359 int cpt = 0;
360
361 if (MODE_DEBUG)
362 System.out.println("testExactly4With11Vars models");
363 while (iterator.isSatisfiable()) {
364 model = iterator.model();
365 assertNotNull(model);
366 if (MODE_DEBUG)
367 System.out.println(new VecInt(model));
368 cpt++;
369 }
370 assertEquals(330, cpt);
371 }
372
373 @Test
374 public void testAtMost4With11Vars() throws ContradictionException,
375 TimeoutException {
376
377 int nVar = 11;
378 this.solver.newVar(nVar);
379
380 int degree = 4;
381
382 IVecInt clause = new VecInt();
383 for (int i = 1; i <= nVar; i++) {
384 clause.push(i);
385 }
386
387 IConstr constr = this.solver.addAtMost(clause, degree);
388 assertNotNull(constr);
389
390 if (MODE_DEBUG) {
391 System.out.println();
392 System.out.println("testAtMost4With11Vars");
393 if (constr instanceof ConstrGroup) {
394 for (int i = 0; i < constr.size(); i++) {
395 System.out.println(((ConstrGroup) constr).getConstr(i));
396 }
397 } else {
398 System.out.println(constr);
399 }
400 }
401
402 ModelIterator iterator = new ModelIterator(this.solver);
403 int[] model = null;
404 int cpt = 0;
405
406 if (MODE_DEBUG)
407 System.out.println("testAtMost4With11Vars models");
408 while (iterator.isSatisfiable()) {
409 model = iterator.model();
410 assertNotNull(model);
411 if (MODE_DEBUG)
412 System.out.println(new VecInt(model));
413 cpt++;
414 }
415 assertEquals(562, cpt);
416 }
417 }