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 static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  import org.junit.Test;
37  import org.sat4j.core.VecInt;
38  import org.sat4j.minisat.SolverFactory;
39  import org.sat4j.specs.ContradictionException;
40  import org.sat4j.specs.ISolver;
41  import org.sat4j.specs.TimeoutException;
42  
43  /**
44   * 
45   * @author daniel
46   * @since 2.2
47   */
48  public class BugReset {
49  
50      @Test
51      public void testBugKostya() throws TimeoutException, ContradictionException {
52          ISolver solver = SolverFactory.newDefault();
53          solver.setTimeout(3600);
54  
55          boolean res;
56  
57          // test empty
58          assertTrue(solver.isSatisfiable());
59          solver.reset();
60  
61          // test one statement
62          solver.newVar(1);
63          int[] clause = new int[] { -4 };
64          // the addClause method in this case returns null! It is imposible to
65          // remove this
66          // fact from a knowledge base. Javadoc does not say anything about this
67          // exception.
68          solver.addClause(new VecInt(clause));
69          res = solver.isSatisfiable();
70          assertTrue(res);
71          solver.reset();
72  
73          // test multiply statements
74          solver.newVar(4);
75          clause = new int[] { -1, -2, -3, 4 };
76          solver.addClause(new VecInt(clause));
77          clause = new int[] { 1 };
78          solver.addClause(new VecInt(clause));
79          clause = new int[] { 2 };
80          solver.addClause(new VecInt(clause));
81          clause = new int[] { 3 };
82          solver.addClause(new VecInt(clause));
83          assertTrue(solver.isSatisfiable()); // ArrayIndexOutOfBoundsException
84      }
85  
86      @Test
87      public void testWithReset() throws TimeoutException, ContradictionException {
88          ISolver solver = SolverFactory.newDefault();
89          int[] clause;
90          boolean res;
91  
92          // prepare the solver to accept MAXVAR variables. MANDATORY
93          solver.newVar(4);
94          // not mandatory for SAT solving. MANDATORY for MAXSAT solving
95          solver.setExpectedNumberOfClauses(6);
96          // Feed the solver using Dimacs format, using arrays of int
97          // (best option to avoid dependencies on SAT4J IVecInt)
98  
99          // test empty
100         res = solver.isSatisfiable();
101         assertTrue(res);
102         solver.reset();
103 
104         // test one clause
105         solver.newVar(1);
106         clause = new int[] { -4 };
107         solver.addClause(new VecInt(clause));
108         res = solver.isSatisfiable();
109         assertTrue(res);
110         solver.reset();
111 
112         // test multiply statements
113         solver.newVar(4);
114         addData(solver);
115         assertTrue(solver.isSatisfiable());
116 
117         clause = new int[] { -4 };
118         solver.addClause(new VecInt(clause));
119         try {
120             addData(solver);
121             res = solver.isSatisfiable();
122         } catch (ContradictionException e) {
123             res = false;
124         }
125         assertFalse(res);
126         solver.reset();
127     }
128 
129     private void addData(ISolver solver) throws ContradictionException {
130         int[] clause = new int[] { -1, -2, -3, 4 };
131         solver.addClause(new VecInt(clause));
132 
133         clause = new int[] { 1 };
134         solver.addClause(new VecInt(clause));
135 
136         clause = new int[] { 2 };
137         solver.addClause(new VecInt(clause));
138 
139         clause = new int[] { 3 };
140         solver.addClause(new VecInt(clause));
141 
142     }
143 
144     @Test
145     public void problemTest() throws TimeoutException, ContradictionException {
146         ISolver solver = SolverFactory.newDefault();
147         solver.setTimeout(3600);
148         solver.newVar(4);
149         solver.setExpectedNumberOfClauses(5);
150 
151         for (int i = 0; i < 10; i++) {
152             solve(solver, new int[] {}, true);
153             solve(solver, new int[] { -4 }, false);
154         }
155     }
156 
157     private void solve(ISolver solver, int[] clause, boolean value)
158             throws ContradictionException, TimeoutException {
159         boolean res = true;
160         try {
161             if (clause.length > 0) {
162                 solver.addClause(new VecInt(clause));
163             }
164             clause = new int[] { -1, 2, 4 };
165             solver.addClause(new VecInt(clause));
166             clause = new int[] { 1, -2 };
167             solver.addClause(new VecInt(clause));
168             clause = new int[] { 1, 2 };
169             solver.addClause(new VecInt(clause));
170             clause = new int[] { -1, -2 };
171             solver.addClause(new VecInt(clause));
172 
173         } catch (ContradictionException e) {
174             res = false;
175         }
176         if (res) {
177             res = solver.isSatisfiable();
178         }
179         solver.reset();
180         assertEquals(res, value);
181     }
182 
183     @Test
184     public void problemTest2() throws TimeoutException, ContradictionException {
185         boolean create = false;
186 
187         ISolver solver = null;
188         if (!create) {
189             solver = getSolver(solver, 4, 5);
190         }
191 
192         for (int i = 0; i < 10; i++) {
193             solve2(getSolver(solver, 4, 5), new int[] { -4 }, false);
194             solve2(getSolver(solver, 4, 5), new int[] {}, true);
195         }
196     }
197 
198     private void solve2(ISolver solver, int[] clause, boolean value)
199             throws ContradictionException, TimeoutException {
200         boolean res = true;
201 
202         try {
203 
204             int[] lclause = new int[] { -1, -2, -3, 4 };
205             solver.addClause(new VecInt(lclause));
206             lclause = new int[] { 1 };
207             solver.addClause(new VecInt(lclause));
208             lclause = new int[] { 2 };
209             solver.addClause(new VecInt(lclause));
210             lclause = new int[] { 3 };
211             solver.addClause(new VecInt(lclause));
212             if (clause.length > 0) {
213                 solver.addClause(new VecInt(clause));
214             }
215 
216         } catch (ContradictionException e) {
217             res = false;
218         }
219         if (res) {
220             res = solver.isSatisfiable();
221         }
222         assertEquals(res, value);
223     }
224 
225     private ISolver getSolver(ISolver solver, int vars, int clauses) {
226         if (solver == null) {
227             solver = SolverFactory.newDefault();
228             solver.setTimeout(3600);
229             solver.newVar(vars);
230             solver.setExpectedNumberOfClauses(clauses);
231         } else {
232             solver.reset();
233             solver.clearLearntClauses();
234         }
235         return solver;
236     }
237 }