1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| package org.sat4j.reader; |
29 |
| |
30 |
| import java.io.FileInputStream; |
31 |
| import java.io.FileNotFoundException; |
32 |
| import java.io.FileReader; |
33 |
| import java.io.IOException; |
34 |
| import java.io.InputStreamReader; |
35 |
| import java.io.LineNumberReader; |
36 |
| import java.io.Serializable; |
37 |
| import java.util.Scanner; |
38 |
| import java.util.StringTokenizer; |
39 |
| import java.util.zip.GZIPInputStream; |
40 |
| |
41 |
| import org.sat4j.core.VecInt; |
42 |
| import org.sat4j.specs.ContradictionException; |
43 |
| import org.sat4j.specs.IProblem; |
44 |
| import org.sat4j.specs.ISolver; |
45 |
| import org.sat4j.specs.IVecInt; |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| public class DimacsReader implements Reader, Serializable { |
69 |
| |
70 |
| private static final long serialVersionUID = 1L; |
71 |
| |
72 |
| protected int expectedNbOfConstr; |
73 |
| |
74 |
| protected final ISolver solver; |
75 |
| |
76 |
| private boolean checkConstrNb = true; |
77 |
| |
78 |
2026
| public DimacsReader(ISolver solver) {
|
79 |
2026
| this.solver = solver;
|
80 |
| } |
81 |
| |
82 |
0
| public void disableNumberOfConstraintCheck() {
|
83 |
0
| checkConstrNb = false;
|
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
0
| protected void skipComments(final LineNumberReader in) throws IOException {
|
95 |
0
| int c;
|
96 |
| |
97 |
0
| do {
|
98 |
0
| in.mark(4);
|
99 |
0
| c = in.read();
|
100 |
0
| if (c == 'c') {
|
101 |
0
| in.readLine();
|
102 |
| } else { |
103 |
0
| in.reset();
|
104 |
| } |
105 |
0
| } while (c == 'c');
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
0
| protected void readProblemLine(LineNumberReader in) throws IOException,
|
117 |
| ParseFormatException { |
118 |
| |
119 |
0
| String line = in.readLine();
|
120 |
| |
121 |
0
| if (line == null) {
|
122 |
0
| throw new ParseFormatException(
|
123 |
| "premature end of file: <p cnf ...> expected on line " |
124 |
| + in.getLineNumber()); |
125 |
| } |
126 |
0
| StringTokenizer stk = new StringTokenizer(line);
|
127 |
| |
128 |
0
| if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
|
129 |
| && stk.hasMoreTokens() && stk.nextToken().equals("cnf"))) { |
130 |
0
| throw new ParseFormatException(
|
131 |
| "problem line expected (p cnf ...) on line " |
132 |
| + in.getLineNumber()); |
133 |
| } |
134 |
| |
135 |
0
| int vars;
|
136 |
| |
137 |
| |
138 |
0
| vars = Integer.parseInt(stk.nextToken());
|
139 |
| assert vars > 0; |
140 |
0
| solver.newVar(vars);
|
141 |
| |
142 |
0
| expectedNbOfConstr = Integer.parseInt(stk.nextToken());
|
143 |
| assert expectedNbOfConstr > 0; |
144 |
| |
145 |
| } |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
0
| protected void readConstrs(LineNumberReader in) throws IOException,
|
158 |
| ParseFormatException, ContradictionException { |
159 |
0
| int lit;
|
160 |
0
| String line;
|
161 |
0
| Scanner scan;
|
162 |
| |
163 |
0
| int realNbOfConstr = 0;
|
164 |
| |
165 |
0
| IVecInt literals = new VecInt();
|
166 |
| |
167 |
0
| while (true) {
|
168 |
0
| line = in.readLine();
|
169 |
| |
170 |
0
| if (line == null) {
|
171 |
| |
172 |
0
| if (literals.size() > 0) {
|
173 |
| |
174 |
0
| solver.addClause(literals);
|
175 |
0
| realNbOfConstr++;
|
176 |
| } |
177 |
| |
178 |
0
| break;
|
179 |
| } |
180 |
| |
181 |
0
| if (line.startsWith("c ")) {
|
182 |
| |
183 |
0
| System.out.println("Found commmented line : " + line);
|
184 |
0
| continue;
|
185 |
| } |
186 |
0
| if (line.startsWith("%") && expectedNbOfConstr == realNbOfConstr) {
|
187 |
0
| System.out
|
188 |
| .println("Ignoring the rest of the file (SATLIB format"); |
189 |
0
| break;
|
190 |
| } |
191 |
0
| boolean added = handleConstr(line, literals);
|
192 |
0
| if (added) {
|
193 |
0
| realNbOfConstr++;
|
194 |
| } |
195 |
| } |
196 |
0
| if (checkConstrNb && expectedNbOfConstr != realNbOfConstr) {
|
197 |
0
| throw new ParseFormatException("wrong nbclauses parameter. Found "
|
198 |
| + realNbOfConstr + ", " + expectedNbOfConstr + " expected"); |
199 |
| } |
200 |
| } |
201 |
| |
202 |
0
| protected boolean handleConstr(String line, IVecInt literals)
|
203 |
| throws ContradictionException { |
204 |
0
| int lit;
|
205 |
0
| boolean added = false;
|
206 |
0
| Scanner scan;
|
207 |
0
| scan = new Scanner(line);
|
208 |
0
| while (scan.hasNext()) {
|
209 |
0
| lit = scan.nextInt();
|
210 |
| |
211 |
0
| if (lit != 0) {
|
212 |
0
| literals.push(lit);
|
213 |
| } else { |
214 |
0
| if (literals.size() > 0) {
|
215 |
0
| solver.addClause(literals);
|
216 |
0
| literals.clear();
|
217 |
0
| added = true;
|
218 |
| } |
219 |
| } |
220 |
| } |
221 |
0
| return added;
|
222 |
| } |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
0
| public IProblem parseInstance(String filename)
|
239 |
| throws FileNotFoundException, ParseFormatException, IOException, |
240 |
| ContradictionException { |
241 |
| |
242 |
0
| if (filename.endsWith(".gz")) {
|
243 |
0
| parseInstance(
|
244 |
| new LineNumberReader( |
245 |
| new InputStreamReader( |
246 |
| new GZIPInputStream(new FileInputStream(filename))))); |
247 |
| } else { |
248 |
0
| parseInstance(
|
249 |
| new LineNumberReader(new FileReader(filename))); |
250 |
| } |
251 |
0
| return solver;
|
252 |
| } |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
0
| public void parseInstance(LineNumberReader in) throws ParseFormatException,
|
263 |
| ContradictionException { |
264 |
0
| solver.reset();
|
265 |
0
| try {
|
266 |
0
| skipComments(in);
|
267 |
0
| readProblemLine(in);
|
268 |
0
| readConstrs(in);
|
269 |
| } catch (IOException e) { |
270 |
0
| throw new ParseFormatException(e);
|
271 |
| } catch (NumberFormatException e) { |
272 |
0
| throw new ParseFormatException("integer value expected on line "
|
273 |
| + in.getLineNumber(), e); |
274 |
| } |
275 |
| } |
276 |
| |
277 |
0
| public String decode(int[] model) {
|
278 |
0
| StringBuffer stb = new StringBuffer();
|
279 |
0
| for (int i = 0; i < model.length; i++) {
|
280 |
0
| stb.append(model[i]);
|
281 |
0
| stb.append(" ");
|
282 |
| } |
283 |
0
| stb.append("0");
|
284 |
0
| return stb.toString();
|
285 |
| } |
286 |
| |
287 |
0
| protected ISolver getSolver() {
|
288 |
0
| return solver;
|
289 |
| } |
290 |
| } |