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;
31  
32  import static org.junit.Assert.assertEquals;
33  
34  import java.io.FileNotFoundException;
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.HashSet;
38  import java.util.List;
39  import java.util.Set;
40  
41  import org.junit.Test;
42  import org.sat4j.core.VecInt;
43  import org.sat4j.minisat.SolverFactory;
44  import org.sat4j.reader.LecteurDimacs;
45  import org.sat4j.reader.ParseFormatException;
46  import org.sat4j.reader.Reader;
47  import org.sat4j.specs.ContradictionException;
48  import org.sat4j.specs.IProblem;
49  import org.sat4j.specs.ISolver;
50  import org.sat4j.specs.IVecInt;
51  import org.sat4j.specs.TimeoutException;
52  
53  public class TestSatAssumps {
54      @Test
55      public void testIterativeAssumptionCallsWithSet()
56              throws FileNotFoundException, ParseFormatException, IOException,
57              ContradictionException, TimeoutException {
58          ISolver satSolver = SolverFactory.newDefault();
59          Reader reader = new LecteurDimacs(satSolver);
60          IProblem p = reader.parseInstance("src/test/testfiles/Eshop-fm.dimacs");
61  
62          List<Integer> vars = new ArrayList<Integer>();
63          for (int i = 1; i <= p.nVars(); i++) {
64              vars.add(-i);
65              vars.add(i);
66          }
67  
68          Set<Integer> sol = new HashSet<Integer>();
69  
70          for (int i = 0; i < vars.size(); i++) {
71              ISolver satSolverOracle = SolverFactory.newDefault();
72              Reader readerOracle = new LecteurDimacs(satSolverOracle);
73              readerOracle.parseInstance("src/test/testfiles/Eshop-fm.dimacs");
74  
75              int varnr = vars.get(i);
76  
77              int assumpsArray[] = new int[sol.size() + 1];
78              int c = 0;
79              for (int a : sol) {
80                  assumpsArray[c] = a;
81                  c++;
82              }
83              assumpsArray[assumpsArray.length - 1] = varnr;
84              IVecInt assumps = new VecInt(assumpsArray);
85              // Check
86              if (satSolver.isSatisfiable(assumps)) {
87                  sol.add(varnr);
88              }
89  
90              assertEquals(satSolverOracle.isSatisfiable(assumps),
91                      satSolver.isSatisfiable(assumps));
92          }
93      }
94  
95      @Test
96      public void testIterativeAssumptionCallsWithList()
97              throws FileNotFoundException, ParseFormatException, IOException,
98              ContradictionException, TimeoutException {
99          ISolver satSolver = SolverFactory.newDefault();
100         Reader reader = new LecteurDimacs(satSolver);
101         IProblem p = reader.parseInstance("src/test/testfiles/Eshop-fm.dimacs");
102 
103         List<Integer> vars = new ArrayList<Integer>();
104         for (int i = 1; i <= p.nVars(); i++) {
105             vars.add(-i);
106             vars.add(i);
107         }
108 
109         List<Integer> sol = new ArrayList<Integer>();
110 
111         for (int i = 0; i < vars.size(); i++) {
112             ISolver satSolverOracle = SolverFactory.newDefault();
113             Reader readerOracle = new LecteurDimacs(satSolverOracle);
114             readerOracle.parseInstance("src/test/testfiles/Eshop-fm.dimacs");
115 
116             int varnr = vars.get(i);
117 
118             int assumpsArray[] = new int[sol.size() + 1];
119             int c = 0;
120             for (int a : sol) {
121                 assumpsArray[c] = a;
122                 c++;
123             }
124             assumpsArray[assumpsArray.length - 1] = varnr;
125             IVecInt assumps = new VecInt(assumpsArray);
126 
127             // Check
128             if (satSolver.isSatisfiable(assumps)) {
129                 sol.add(varnr);
130             }
131 
132             assertEquals(satSolverOracle.isSatisfiable(assumps),
133                     satSolver.isSatisfiable(assumps));
134         }
135     }
136 
137     @Test
138     public void testIterativeCorrectWay() throws FileNotFoundException,
139             ParseFormatException, IOException, ContradictionException,
140             TimeoutException {
141         ISolver satSolver = SolverFactory.newDefault();
142         Reader reader = new LecteurDimacs(satSolver);
143         IProblem p = reader.parseInstance("src/test/testfiles/Eshop-fm.dimacs");
144 
145         Set<Integer> sol = new HashSet<Integer>();
146 
147         for (int i = 1; i <= p.nVars(); i++) {
148             ISolver satSolverOracle = SolverFactory.newDefault();
149             Reader readerOracle = new LecteurDimacs(satSolverOracle);
150             readerOracle.parseInstance("src/test/testfiles/Eshop-fm.dimacs");
151 
152             int assumpsArray[] = new int[sol.size() + 1];
153             int c = 0;
154             for (int a : sol) {
155                 assumpsArray[c] = a;
156                 c++;
157             }
158             assumpsArray[assumpsArray.length - 1] = -i;
159             IVecInt assumps = new VecInt(assumpsArray);
160 
161             // Check
162             assertEquals(satSolverOracle.isSatisfiable(assumps),
163                     satSolver.isSatisfiable(assumps));
164             if (satSolver.isSatisfiable(assumps)) {
165                 sol.add(-i);
166                 continue;
167             }
168             assumpsArray[assumpsArray.length - 1] = i;
169             satSolverOracle = SolverFactory.newDefault();
170             readerOracle = new LecteurDimacs(satSolverOracle);
171             readerOracle.parseInstance("src/test/testfiles/Eshop-fm.dimacs");
172             assertEquals(satSolverOracle.isSatisfiable(assumps),
173                     satSolver.isSatisfiable(assumps));
174             if (satSolver.isSatisfiable(assumps)) {
175                 sol.add(i);
176             }
177         }
178     }
179 }