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.minisat.core;
31  
32  import junit.framework.TestCase;
33  
34  import org.sat4j.core.VecInt;
35  import org.sat4j.minisat.SolverFactory;
36  import org.sat4j.specs.ContradictionException;
37  import org.sat4j.specs.ISolver;
38  import org.sat4j.specs.IVecInt;
39  import org.sat4j.specs.TimeoutException;
40  
41  public class TestAtMost extends TestCase {
42  
43      private ISolver solver;
44  
45      public TestAtMost(String s) {
46          super(s);
47      }
48  
49      @Override
50      protected void setUp() {
51          this.solver = SolverFactory.newMiniSATHeap();
52      }
53  
54      public void testUnEssaiSat() throws TimeoutException {
55          try {
56              this.solver.reset();
57              // 3 variables
58              this.solver.newVar(3);
59              // premi?re contrainte de cardinalit?
60              // a V b V c >= 2
61              IVecInt vec = new VecInt();
62              vec.push(1);
63              vec.push(2);
64              vec.push(3);
65              this.solver.addAtLeast(vec, 2);
66  
67              // Deuxi?me contrainte de cardinalit?
68              // a V non b >= 2
69              vec = new VecInt();
70              vec.push(1);
71              vec.push(-2);
72              this.solver.addAtLeast(vec, 2);
73  
74              boolean isSat = this.solver.isSatisfiable();
75  
76              assertTrue(isSat);
77          } catch (ContradictionException e) {
78              assertTrue(false);
79          }
80      }
81  
82      public void testUnEssaiUnsat() throws TimeoutException {
83          try {
84              this.solver.reset();
85              // 2 variables
86              this.solver.newVar(2);
87              // premi?re contrainte de cardinalit?
88              // a + not b >= 1
89              IVecInt vecLit = new VecInt();
90              vecLit.push(1);
91              vecLit.push(-2);
92              this.solver.addAtLeast(vecLit, 1);
93  
94              // Deuxi?me contrainte de cardinalit?
95              // not a >= 1
96              vecLit = new VecInt();
97              vecLit.push(-1);
98              this.solver.addAtLeast(vecLit, 1);
99  
100             // Troisi?me contrainte de cardinalit?
101             // b >= 1
102             vecLit = new VecInt();
103             vecLit.push(2);
104             this.solver.addAtLeast(vecLit, 1);
105 
106             boolean isSat = this.solver.isSatisfiable();
107 
108             assertFalse(isSat);
109         } catch (ContradictionException e) {
110             assertTrue(false);
111         }
112 
113     }
114 
115     public void test2Sat() throws TimeoutException {
116         try {
117             this.solver.reset();
118             // 2 variables
119             this.solver.newVar(2);
120             // premi?re contrainte de cardinalit?
121             // a + not b <=3
122             IVecInt vecLit = new VecInt();
123             vecLit.push(1);
124             vecLit.push(-2);
125             this.solver.addAtMost(vecLit, 3);
126 
127             boolean isSat = this.solver.isSatisfiable();
128 
129             assertTrue(isSat);
130         } catch (ContradictionException e) {
131             assertTrue(false);
132         }
133     }
134 
135     public void test4Unsat() throws TimeoutException {
136         try {
137             this.solver.reset();
138             // 2 variables
139             this.solver.newVar(2);
140             // premi?re contrainte de cardinalit?
141             // a + not b >=3
142             IVecInt vecLit = new VecInt();
143             vecLit.push(1);
144             vecLit.push(-2);
145 
146             this.solver.addAtLeast(vecLit, 3);
147 
148             this.solver.isSatisfiable();
149 
150             fail();
151         } catch (ContradictionException e) {
152         }
153     }
154 
155     public void test3Unsat() throws TimeoutException {
156         try {
157             this.solver.reset();
158             // 2 variables
159             this.solver.newVar(2);
160             // contrainte de cardinalit?
161             // a >= 1
162             IVecInt vecLit = new VecInt();
163             vecLit.push(1);
164             this.solver.addAtLeast(vecLit, 1);
165             // contrainte de cardinalit?
166             // b >= 1
167             vecLit = new VecInt();
168             vecLit.push(2);
169             this.solver.addAtLeast(vecLit, 1);
170             // contrainte de cardinalit?
171             // a + b <=1
172             vecLit = new VecInt();
173             vecLit.push(1);
174             vecLit.push(2);
175             this.solver.addAtMost(vecLit, 1);
176 
177             this.solver.isSatisfiable();
178 
179             fail();
180         } catch (ContradictionException e) {
181         }
182 
183     }
184 
185     public void test5Sat() throws TimeoutException {
186         try {
187             this.solver.reset();
188             // 2 variables
189             this.solver.newVar(2);
190             // premi\x{FFFD}re contrainte de cardinalit\x{FFFD}
191             // a + not b <=0
192             IVecInt vecLit = new VecInt();
193             vecLit.push(1);
194             vecLit.push(-2);
195 
196             this.solver.addAtMost(vecLit, 0);
197 
198             boolean isSat = this.solver.isSatisfiable();
199 
200             assertTrue(isSat);
201         } catch (ContradictionException e) {
202             assertTrue(false);
203         }
204     }
205 } // TestAtMost