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 TestClausalCardinalitiesCommanderEncoding {
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.COMMANDER);
58          this.policy.setAtMostKEncoding(EncodingStrategy.COMMANDER);
59          this.policy.setAtLeastOneEncoding(EncodingStrategy.COMMANDER);
60          this.policy.setAtLeastKEncoding(EncodingStrategy.COMMANDER);
61          this.policy.setExactlyOneEncoding(EncodingStrategy.COMMANDER);
62          this.policy.setExactlyKEncoding(EncodingStrategy.COMMANDER);
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("testCommanderAtMostOne");
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("testCommanderAtMostOne 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("testCommanderExactlyOne");
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("testCommanderExactlyOne 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("testCommanderAtLeastOne");
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("testCommanderAtLeastOne 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("testCommanderAtMost2");
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("testCommanderAtMost2 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("testCommanderAtLeast2");
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("testCommanderAtLeast2 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("testCommanderExactly2");
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("testCommanderExactly2 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("testCommanderAtMostOneWith8Vars");
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("testCommanderAtMostOneWith8Vars 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 }