View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
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 TestClausalCardinalitiesBinomialEncoding {
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.BINOMIAL);
58          this.policy.setAtMostKEncoding(EncodingStrategy.BINOMIAL);
59          this.policy.setAtLeastOneEncoding(EncodingStrategy.BINOMIAL);
60          this.policy.setAtLeastKEncoding(EncodingStrategy.BINOMIAL);
61          this.policy.setExactlyOneEncoding(EncodingStrategy.BINOMIAL);
62          this.policy.setExactlyKEncoding(EncodingStrategy.BINOMIAL);
63  
64          this.solver = new ClausalCardinalitiesDecorator<ISolver>(
65                  SolverFactory.newDefault(), this.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("testBinomialAtMostOne");
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("testBinomialAtMostOne models");
93          }
94          while (iterator.isSatisfiable()) {
95              model = iterator.model();
96              assertNotNull(model);
97              if (MODE_DEBUG) {
98                  System.out.println(new VecInt(model));
99              }
100             cpt++;
101         }
102         assertEquals(6, cpt);
103     }
104 
105     @Test
106     public void testExactlyOne() throws ContradictionException,
107             TimeoutException {
108         this.solver.newVar(5);
109 
110         IVecInt clause = new VecInt();
111         clause.push(1).push(2).push(3).push(4).push(5);
112 
113         IConstr constr = this.solver.addExactly(clause, 1);
114         assertNotNull(constr);
115 
116         if (MODE_DEBUG) {
117             System.out.println();
118             System.out.println("testBinomialExactlyOne");
119             for (int i = 0; i < constr.size(); i++) {
120                 if (constr instanceof ConstrGroup) {
121                     System.out.println(((ConstrGroup) constr).getConstr(i));
122                 } else {
123                     System.out.println(constr);
124                 }
125             }
126         }
127 
128         ModelIterator iterator = new ModelIterator(this.solver);
129         int[] model = null;
130         int cpt = 0;
131 
132         if (MODE_DEBUG) {
133             System.out.println("testBinomialExactlyOne models");
134         }
135         while (iterator.isSatisfiable()) {
136             model = iterator.model();
137             assertNotNull(model);
138             if (MODE_DEBUG) {
139                 System.out.println(new VecInt(model));
140             }
141             cpt++;
142         }
143         assertEquals(5, cpt);
144     }
145 
146     @Test
147     public void testAtLeastOne() throws ContradictionException,
148             TimeoutException {
149         this.solver.newVar(5);
150 
151         IVecInt clause = new VecInt();
152         clause.push(1).push(2).push(3).push(4).push(5);
153 
154         IConstr constr = this.solver.addAtLeast(clause, 1);
155         assertNotNull(constr);
156 
157         if (MODE_DEBUG) {
158             System.out.println();
159             System.out.println("testBinomialAtLeastOne");
160             for (int i = 0; i < constr.size(); i++) {
161                 if (constr instanceof ConstrGroup) {
162                     System.out.println(((ConstrGroup) constr).getConstr(i));
163                 } else {
164                     System.out.println(constr);
165                 }
166             }
167         }
168 
169         ModelIterator iterator = new ModelIterator(this.solver);
170         int[] model = null;
171         int cpt = 0;
172 
173         if (MODE_DEBUG) {
174             System.out.println("testBinomialAtLeastOne models");
175         }
176         while (iterator.isSatisfiable()) {
177             model = iterator.model();
178             assertNotNull(model);
179             if (MODE_DEBUG) {
180                 System.out.println(new VecInt(model));
181             }
182             cpt++;
183         }
184         assertEquals(31, cpt);
185     }
186 
187     @Test
188     public void testAtMost2() throws ContradictionException, TimeoutException {
189 
190         this.solver.newVar(5);
191 
192         IVecInt clause = new VecInt();
193         clause.push(1).push(2).push(3).push(4).push(5);
194 
195         IConstr constr = this.solver.addAtMost(clause, 2);
196         assertNotNull(constr);
197 
198         if (MODE_DEBUG) {
199             System.out.println();
200             System.out.println("testBinomialAtMost2");
201             for (int i = 0; i < constr.size(); i++) {
202                 System.out.println(((ConstrGroup) constr).getConstr(i));
203             }
204         }
205 
206         ModelIterator iterator = new ModelIterator(this.solver);
207         int[] model = null;
208         int cpt = 0;
209 
210         if (MODE_DEBUG) {
211             System.out.println("testBinomialAtMost2 models");
212         }
213         while (iterator.isSatisfiable()) {
214             model = iterator.model();
215             assertNotNull(model);
216             if (MODE_DEBUG) {
217                 System.out.println(new VecInt(model));
218             }
219             cpt++;
220         }
221         assertEquals(16, cpt);
222     }
223 
224     @Test
225     public void testAtLeast2() throws ContradictionException, TimeoutException {
226 
227         this.solver.newVar(5);
228 
229         IVecInt clause = new VecInt();
230         clause.push(1).push(2).push(3).push(4).push(5);
231 
232         IConstr constr = this.solver.addAtLeast(clause, 2);
233         assertNotNull(constr);
234 
235         if (MODE_DEBUG) {
236             System.out.println();
237             System.out.println("testBinomialAtLeast2");
238             for (int i = 0; i < constr.size(); i++) {
239                 System.out.println(((ConstrGroup) constr).getConstr(i));
240             }
241         }
242 
243         ModelIterator iterator = new ModelIterator(this.solver);
244         int[] model = null;
245         int cpt = 0;
246 
247         if (MODE_DEBUG) {
248             System.out.println("testBinomialAtLeast2 models");
249         }
250         while (iterator.isSatisfiable()) {
251             model = iterator.model();
252             assertNotNull(model);
253             if (MODE_DEBUG) {
254                 System.out.println(new VecInt(model));
255             }
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("testBinomialExactly2");
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("testBinomialExactly2 models");
286         }
287         while (iterator.isSatisfiable()) {
288             model = iterator.model();
289             assertNotNull(model);
290             if (MODE_DEBUG) {
291                 System.out.println(new VecInt(model));
292             }
293             cpt++;
294         }
295         assertEquals(10, cpt);
296     }
297 
298     // @Test
299     // public void testSimpleCardCase() throws ContradictionException,
300     // TimeoutException {
301     // this.solver.newVar(5);
302     //
303     // boolean debug = false;
304     // IVecInt clause = new VecInt();
305     // clause.push(1).push(2).push(3).push(4).push(5);
306     //
307     // IConstr constr1 = this.solver.addClause(clause);
308     // assertNotNull(constr1);
309     //
310     // IConstr constr2 = this.solver.addAtMost(clause, 1);
311     // assertNotNull(constr2);
312     //
313     // if (debug) {
314     // for (int i = 0; i < constr2.size(); i++) {
315     // System.out.println(((ConstrGroup) constr2).getConstr(i));
316     // }
317     // }
318     //
319     // ModelIterator iterator = new ModelIterator(this.solver);
320     // int[] model = null;
321     // int cpt = 0;
322     //
323     // System.out.println("testSimpleCardCase models AMO + clause");
324     // while (iterator.isSatisfiable()) {
325     // model = iterator.model();
326     // assertNotNull(model);
327     // System.out.println(new VecInt(model));
328     // cpt++;
329     // }
330     // assertEquals(5, cpt);
331     //
332     // }
333     //
334     // @Test
335     // public void testSimpleCardCase2Power() throws ContradictionException,
336     // TimeoutException {
337     // this.solver.newVar(4);
338     //
339     // boolean debug = false;
340     // IVecInt clause = new VecInt();
341     // clause.push(1).push(2).push(3).push(4);
342     //
343     // IConstr constr1 = this.solver.addClause(clause);
344     // assertNotNull(constr1);
345     //
346     // IConstr constr2 = this.solver.addAtMost(clause, 1);
347     // assertNotNull(constr2);
348     //
349     // if (debug) {
350     // for (int i = 0; i < constr2.size(); i++) {
351     // System.out.println(((ConstrGroup) constr2).getConstr(i));
352     // }
353     // }
354     //
355     // ModelIterator iterator = new ModelIterator(this.solver);
356     // int[] model = null;
357     // int cpt = 0;
358     //
359     // System.out.println("testSimpleCardCase2Power models AMO + clause");
360     // while (iterator.isSatisfiable()) {
361     // model = iterator.model();
362     // assertNotNull(model);
363     // System.out.println(new VecInt(model));
364     // cpt++;
365     // }
366     // assertEquals(4, cpt);
367     //
368     // }
369     //
370     // @Test
371     // public void testSimpleCardCaseAMO() throws ContradictionException,
372     // TimeoutException {
373     // this.solver.newVar(5);
374     //
375     // boolean debug = false;
376     // IVecInt clause = new VecInt();
377     // clause.push(1).push(2).push(3).push(4).push(5);
378     //
379     // IConstr constr2 = this.solver.addAtMost(clause, 1);
380     // assertNotNull(constr2);
381     //
382     // if (debug) {
383     // System.out.println("Constraintes AMO");
384     // for (int i = 0; i < constr2.size(); i++) {
385     // System.out.println(((ConstrGroup) constr2).getConstr(i));
386     // }
387     // }
388     //
389     // ModelIterator iterator = new ModelIterator(this.solver);
390     // int[] model = null;
391     // int cpt = 0;
392     // System.out.println("testSimpleCardCase models AMO");
393     // while (iterator.isSatisfiable()) {
394     // model = iterator.model();
395     // assertNotNull(model);
396     // System.out.println(new VecInt(model));
397     // cpt++;
398     // }
399     // assertEquals(6, cpt);
400     //
401     // }
402     //
403     // @Test
404     // public void testSimpleCardCaseAMOWith8Variables()
405     // throws ContradictionException, TimeoutException {
406     // this.solver.newVar(8);
407     //
408     // boolean debug = false;
409     // IVecInt clause = new VecInt();
410     // clause.push(1).push(2).push(3).push(4).push(5).push(6).push(7).push(8);
411     //
412     // IConstr constr2 = this.solver.addAtMost(clause, 1);
413     // assertNotNull(constr2);
414     //
415     // if (debug) {
416     // System.out.println("Constraintes AMO 8 variables");
417     // for (int i = 0; i < constr2.size(); i++) {
418     // System.out.println(((ConstrGroup) constr2).getConstr(i));
419     // }
420     // }
421     //
422     // ModelIterator iterator = new ModelIterator(this.solver);
423     // int[] model = null;
424     // System.out.println("testSimpleCardCase models AMO 8 variables");
425     // int cpt = 0;
426     // while (iterator.isSatisfiable()) {
427     // model = iterator.model();
428     // assertNotNull(model);
429     // System.out.println(new VecInt(model));
430     // cpt++;
431     // }
432     // assertEquals(9, cpt);
433     //
434     // }
435     //
436     // @Test
437     // public void testSimpleCardCaseEO() throws ContradictionException,
438     // TimeoutException {
439     // this.solver.newVar(5);
440     //
441     // boolean debug = false;
442     // IVecInt clause = new VecInt();
443     // clause.push(1).push(2).push(3).push(4).push(5);
444     //
445     // IConstr constr = this.solver.addExactly(clause, 1);
446     // assertNotNull(constr);
447     //
448     // assertEquals(2, constr.size());
449     //
450     // if (debug) {
451     // System.out.println("Constraintes EO");
452     // for (int i = 0; i < constr.size(); i++) {
453     // System.out.println(((ConstrGroup) constr).getConstr(i));
454     // }
455     // }
456     //
457     // ModelIterator iterator = new ModelIterator(this.solver);
458     // int[] model = null;
459     // int cpt = 0;
460     // System.out.println("testSimpleCardCase models EO");
461     // while (iterator.isSatisfiable()) {
462     // model = iterator.model();
463     // assertNotNull(model);
464     // System.out.println(new VecInt(model));
465     // cpt++;
466     // }
467     // assertEquals(5, cpt);
468     //
469     // }
470     //
471     // @Test
472     // public void testSimpleCardCaseFor2() throws ContradictionException,
473     // TimeoutException {
474     // this.solver.newVar(5);
475     // IVecInt clause = new VecInt();
476     // clause.push(1).push(2).push(3).push(4).push(5);
477     // IConstr constr1 = this.solver.addClause(clause);
478     // assertNotNull(constr1);
479     // IConstr constr2 = this.solver.addAtMost(clause, 2);
480     // assertNotNull(constr2);
481     // ModelIterator iterator = new ModelIterator(this.solver);
482     // int[] model = null;
483     // int cpt = 0;
484     // System.out
485     // .println("testSimpleCardCaseFor2 - AMO + clauses - 5 variables");
486     // while (iterator.isSatisfiable()) {
487     // model = iterator.model();
488     // assertNotNull(model);
489     // System.out.println(new VecInt(model));
490     // cpt++;
491     // }
492     // assertEquals(15, cpt);
493     // }
494     //
495     // @Test
496     // public void testSimpleCardCaseFor2With7Variables()
497     // throws ContradictionException, TimeoutException {
498     //
499     // boolean debug = false;
500     //
501     // int nbVar = 7;
502     // this.solver.newVar(nbVar);
503     // IVecInt clause = new VecInt();
504     // clause.push(1).push(2).push(3).push(4).push(5).push(6).push(7);
505     // IConstr constr1 = this.solver.addClause(clause);
506     // assertNotNull(constr1);
507     // IConstr constr2 = this.solver.addAtMost(clause, 2);
508     // assertNotNull(constr2);
509     //
510     // if (debug) {
511     // System.out
512     // .println("Constraints Simple card case for 2 with 7 variables");
513     // for (int i = 0; i < constr2.size(); i++) {
514     // System.out.println(((ConstrGroup) constr2).getConstr(i));
515     // }
516     // }
517     //
518     // ModelIterator iterator = new ModelIterator(this.solver);
519     // int[] model = null;
520     // int cpt = 0;
521     // System.out
522     // .println("testSimpleCardCaseFor2With7Variables models AMO + clause - 7 variables");
523     // while (iterator.isSatisfiable()) {
524     // model = iterator.model();
525     // assertNotNull(model);
526     // System.out.println(new VecInt(model));
527     // cpt++;
528     // }
529     // assertEquals(28, cpt);
530     // }
531     //
532     // @Test
533     // public void testSimpleCardCaseFor2With8Variables()
534     // throws ContradictionException, TimeoutException {
535     //
536     // boolean debug = false;
537     //
538     // int nbVar = 8;
539     // this.solver.newVar(nbVar);
540     // IVecInt clause = new VecInt();
541     // clause.push(1).push(2).push(3).push(4).push(5).push(6).push(7).push(8);
542     // IConstr constr1 = this.solver.addClause(clause);
543     // assertNotNull(constr1);
544     // IConstr constr2 = this.solver.addAtMost(clause, 2);
545     // assertNotNull(constr2);
546     //
547     // if (debug) {
548     // System.out
549     // .println("Constraints Simple card case for 2 with 8 variables");
550     // for (int i = 0; i < constr2.size(); i++) {
551     // System.out.println(((ConstrGroup) constr2).getConstr(i));
552     // }
553     // }
554     //
555     // ModelIterator iterator = new ModelIterator(this.solver);
556     // int[] model = null;
557     // int cpt = 0;
558     // System.out
559     // .println("testSimpleCardCaseFor2With8Variables models AMO + clause - 8 variables");
560     // while (iterator.isSatisfiable()) {
561     // model = iterator.model();
562     // assertNotNull(model);
563     // System.out.println(new VecInt(model));
564     // cpt++;
565     // }
566     // assertEquals(36, cpt);
567     // }
568     //
569     // @Test
570     // public void testSimpleCardCaseFor4With11Variables()
571     // throws ContradictionException, TimeoutException {
572     //
573     // boolean debug = false;
574     //
575     // int nbVar = 11;
576     // this.solver.newVar(nbVar);
577     // IVecInt clause = new VecInt();
578     // clause.push(1).push(2).push(3).push(4).push(5).push(6).push(7).push(8)
579     // .push(9).push(10).push(11);
580     // IConstr constr1 = this.solver.addClause(clause);
581     // assertNotNull(constr1);
582     // IConstr constr2 = this.solver.addAtMost(clause, 4);
583     // assertNotNull(constr2);
584     //
585     // if (debug) {
586     // System.out
587     // .println("Constraints Simple card case for 4 with 11 variables");
588     // for (int i = 0; i < constr2.size(); i++) {
589     // System.out.println(((ConstrGroup) constr2).getConstr(i));
590     // }
591     // }
592     //
593     // ModelIterator iterator = new ModelIterator(this.solver);
594     // int[] model = null;
595     // System.out
596     // .println("testSimpleCardCaseFor4With11Variables models AMO + clause - 11 variables");
597     // int cpt = 0;
598     // while (iterator.isSatisfiable()) {
599     // model = iterator.model();
600     // assertNotNull(model);
601     // System.out.println(new VecInt(model));
602     // cpt++;
603     // }
604     // assertEquals(561, cpt);
605     // }
606     //
607     // @Test
608     // public void testSimpleCardCaseForUnsat() throws ContradictionException,
609     // TimeoutException {
610     // this.solver.newVar(5);
611     // IVecInt clause = new VecInt();
612     // clause.push(1).push(2).push(3).push(4).push(5);
613     // IConstr constr1 = this.solver.addClause(clause);
614     // assertNotNull(constr1);
615     // IConstr constr2 = this.solver.addAtMost(clause, 0);
616     // assertNotNull(constr2);
617     // assertFalse(this.solver.isSatisfiable());
618     // }
619 
620     // @Test
621     // public void testName() {
622     // System.out.println(this.solver.toString());
623     // }
624 }