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.pb.reader;
29
30 import java.io.IOException;
31 import java.io.LineNumberReader;
32 import java.io.PrintWriter;
33 import java.io.Serializable;
34 import java.math.BigInteger;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.Scanner;
38
39 import org.sat4j.core.Vec;
40 import org.sat4j.core.VecInt;
41 import org.sat4j.pb.IPBSolver;
42 import org.sat4j.reader.ParseFormatException;
43 import org.sat4j.reader.Reader;
44 import org.sat4j.specs.ContradictionException;
45 import org.sat4j.specs.IProblem;
46 import org.sat4j.specs.IVec;
47 import org.sat4j.specs.IVecInt;
48
49
50
51
52
53
54
55
56
57 public class GoodOPBReader extends Reader implements Serializable {
58
59
60
61
62 private static final long serialVersionUID = 1L;
63
64 private static final String COMMENT_SYMBOL = "*";
65
66 private final IPBSolver solver;
67
68 private final Map<String, Integer> map = new HashMap<String, Integer>();
69
70 private final IVec<String> decode = new Vec<String>();
71
72
73
74
75 public GoodOPBReader(IPBSolver solver) {
76 this.solver = solver;
77 }
78
79 @Override
80 public final IProblem parseInstance(final java.io.Reader in)
81 throws ParseFormatException, ContradictionException, IOException {
82 return parseInstance(new LineNumberReader(in));
83 }
84
85 private IProblem parseInstance(LineNumberReader in)
86 throws ContradictionException, IOException {
87 solver.reset();
88 String line;
89 while ((line = in.readLine()) != null) {
90
91 line = line.trim();
92 if (line.endsWith(";")) {
93 line = line.substring(0, line.length() - 1);
94 }
95 parseLine(line);
96 }
97 return solver;
98 }
99
100 void parseLine(String line) throws ContradictionException {
101
102 if (line.startsWith(COMMENT_SYMBOL))
103 return;
104 if (line.startsWith("p"))
105 return;
106 if (line.startsWith("min:") || line.startsWith("min :"))
107 return;
108 if (line.startsWith("max:") || line.startsWith("max :"))
109 return;
110
111
112 int index = line.indexOf(":");
113 if (index != -1) {
114 line = line.substring(index + 1);
115 }
116
117 IVecInt lits = new VecInt();
118 IVec<BigInteger> coeffs = new Vec<BigInteger>();
119 Scanner stk = new Scanner(line)
120 .useDelimiter("\\s*\\*\\s*|\\s*\\+\\s*|\\s+");
121 while (stk.hasNext()) {
122 String token = stk.next();
123 if (">=".equals(token) || "<=".equals(token) || "=".equals(token)) {
124 assert stk.hasNext();
125 String tok = stk.next();
126
127 if (tok.startsWith("+")) {
128 tok = tok.substring(1);
129 }
130 BigInteger d = new BigInteger(tok);
131
132 try {
133 if (">=".equals(token) || "=".equals(token)) {
134 solver.addPseudoBoolean(lits, coeffs, true, d);
135 }
136 if ("<=".equals(token) || "=".equals(token)) {
137 solver.addPseudoBoolean(lits, coeffs, false, d);
138 }
139 } catch (ContradictionException ce) {
140 throw ce;
141 }
142 } else {
143
144
145 if ("+".equals(token)) {
146 assert stk.hasNext();
147 token = stk.next();
148 } else if ("-".equals(token)) {
149 assert stk.hasNext();
150 token = token + stk.next();
151 }
152 BigInteger coef;
153
154 try {
155
156 if (token.startsWith("+")) {
157 token = token.substring(1);
158 }
159 coef = new BigInteger(token);
160 assert stk.hasNext();
161 token = stk.next();
162 } catch (NumberFormatException nfe) {
163
164 coef = BigInteger.ONE;
165 }
166 if ("-".equals(token) || "~".equals(token)) {
167 assert stk.hasNext();
168 token = token + stk.next();
169 }
170 boolean negative = false;
171 if (token.startsWith("+")) {
172 token = token.substring(1);
173 } else if (token.startsWith("-")) {
174 token = token.substring(1);
175 assert coef.equals(BigInteger.ONE);
176 coef = BigInteger.ONE.negate();
177 } else if (token.startsWith("~")) {
178 token = token.substring(1);
179 negative = true;
180 }
181 Integer id = map.get(token);
182 if (id == null) {
183 id = decode.size()+1;
184 map.put(token, id);
185 decode.push(token);
186 }
187 coeffs.push(coef);
188 int lid = (negative ? -1 : 1) * id.intValue();
189 lits.push(lid);
190 assert coeffs.size() == lits.size();
191 }
192 }
193 }
194
195 @Override
196 public String decode(int[] model) {
197 StringBuffer stb = new StringBuffer();
198 for (int i = 0; i < model.length; i++) {
199 if (model[i] < 0) {
200 stb.append("-");
201 stb.append(decode.get(-model[i] - 1));
202 } else {
203 stb.append(decode.get(model[i] - 1));
204 }
205 stb.append(" ");
206 }
207 return stb.toString();
208 }
209
210 @Override
211 public void decode(int[] model, PrintWriter out) {
212 for (int i = 0; i < model.length; i++) {
213 if (model[i] < 0) {
214 out.print("-");
215 out.print(decode.get(-model[i] - 1));
216 } else {
217 out.print(decode.get(model[i] - 1));
218 }
219 out.print(" ");
220 }
221 }
222 }