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 TestClausalCardinalitiesBinaryEncoding {
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.BINARY);
58          this.policy.setAtMostKEncoding(EncodingStrategy.BINARY);
59          this.policy.setAtLeastOneEncoding(EncodingStrategy.BINARY);
60          this.policy.setAtLeastKEncoding(EncodingStrategy.BINARY);
61          this.policy.setExactlyOneEncoding(EncodingStrategy.BINARY);
62          this.policy.setExactlyKEncoding(EncodingStrategy.BINARY);
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("testBinaryAtMostOne");
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("testBinaryAtMostOne 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("testBinaryExactlyOne");
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("testBinaryExactlyOne 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("testBinaryAtLeastOne");
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("testBinaryAtLeastOne 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("testBinaryAtMost2");
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("testBinaryAtMost2 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("testBinaryAtLeast2");
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("testBinaryAtLeast2 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("testBinaryExactly2");
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("testBinaryExactly2 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 }