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.assertTrue;
34  
35  import org.junit.Test;
36  import org.sat4j.core.VecInt;
37  import org.sat4j.minisat.SolverFactory;
38  import org.sat4j.specs.ContradictionException;
39  import org.sat4j.specs.ISolver;
40  import org.sat4j.specs.IVecInt;
41  import org.sat4j.specs.TimeoutException;
42  
43  public class BackboneTest {
44  
45      @Test
46      public void testEasyCaseWithOnlyOneModel() throws ContradictionException,
47              TimeoutException {
48          ISolver solver = SolverFactory.newDefault();
49          IVecInt clause = new VecInt();
50          clause.push(1).push(2).push(3);
51          solver.addClause(clause);
52          clause.clear();
53          clause.push(-1).push(-2);
54          solver.addClause(clause);
55          clause.clear();
56          clause.push(-1).push(-3);
57          solver.addClause(clause);
58          clause.clear();
59          clause.push(1);
60          solver.addClause(clause);
61          clause.clear();
62          IVecInt backbone = Backbone.compute(solver);
63          assertTrue(backbone.contains(1));
64          assertTrue(backbone.contains(-2));
65          assertTrue(backbone.contains(-3));
66      }
67  
68      @Test
69      public void testEmptyBackbone() throws ContradictionException,
70              TimeoutException {
71          ISolver solver = SolverFactory.newDefault();
72          IVecInt clause = new VecInt();
73          clause.push(1).push(2).push(3);
74          solver.addClause(clause);
75          clause.clear();
76          clause.push(-1).push(-2);
77          solver.addClause(clause);
78          clause.clear();
79          clause.push(-1).push(-3);
80          solver.addClause(clause);
81          clause.clear();
82          IVecInt backbone = Backbone.compute(solver);
83          assertEquals(0, backbone.size());
84      }
85  
86      @Test
87      public void testCaseWithUnsatProblem() throws ContradictionException,
88              TimeoutException {
89          ISolver solver = SolverFactory.newDefault();
90          IVecInt clause = new VecInt();
91          clause.push(1).push(2);
92          solver.addClause(clause);
93          clause.clear();
94          clause.push(-1).push(2);
95          solver.addClause(clause);
96          clause.clear();
97          clause.push(1).push(-2);
98          solver.addClause(clause);
99          clause.clear();
100         clause.push(-1).push(-2);
101         solver.addClause(clause);
102         clause.clear();
103         IVecInt backbone = Backbone.compute(solver);
104         assertEquals(0, backbone.size());
105     }
106 
107     /**
108      * Testcase to check that the problem with unit clauses does no longer
109      * occurs.
110      * 
111      * Bug report from Sebastian Henneberg
112      * 
113      * 
114      */
115     @Test
116     public void testBugUnitClauses() throws ContradictionException,
117             TimeoutException {
118         ISolver solver1 = SolverFactory.newDefault();
119         ISolver solver2 = SolverFactory.newDefault();
120         ISolver solver3 = SolverFactory.newDefault();
121 
122         int[][] cnf1 = new int[][] { new int[] { 1 }, new int[] { 1, -2 },
123                 new int[] { 1, -3 }, new int[] { -1, 2 } };
124         // A & (A v -B) & (A v -C) & (-A v B)
125         // (-A v B) & (A v -B) & (A v -C) & A | using a different order
126         int[][] cnf2 = new int[][] { new int[] { -1, 2 }, new int[] { 1, -2 },
127                 new int[] { 1, -3 }, new int[] { 1 } };
128         // (-A v B) & (A v -B) & (A v -C) & A
129         // (-A v C) & (A v -C) & (A v -B) & A | swap B and C (2 <-> 3)
130         // (A v -B) & (-A v C) & (A v -C) & A | shift the first 3 clauses to the
131         // right
132         int[][] cnf3 = new int[][] { new int[] { 1, -2 }, new int[] { -1, 3 },
133                 new int[] { 1, -3 }, new int[] { 1 } };
134 
135         for (int[] is : cnf1) {
136             solver1.addClause(new VecInt(is));
137         }
138         for (int[] is : cnf2) {
139             solver2.addClause(new VecInt(is));
140         }
141         for (int[] is : cnf3) {
142             solver3.addClause(new VecInt(is));
143         }
144 
145         IVecInt vecInt1 = Backbone.compute(solver1);
146         assertEquals(vecInt1.size(), 2);
147         assertTrue(vecInt1.contains(1));
148         assertTrue(vecInt1.contains(2));
149 
150         IVecInt vecInt2 = Backbone.compute(solver2);
151         assertEquals(vecInt2.size(), 2);
152         assertTrue(vecInt2.contains(1));
153         assertTrue(vecInt2.contains(2));
154 
155         IVecInt vecInt3 = Backbone.compute(solver3);
156         assertEquals(vecInt3.size(), 2);
157         assertTrue(vecInt3.contains(1));
158         assertTrue(vecInt3.contains(3));
159     }
160 }