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; |
29 |
| |
30 |
| import static java.lang.System.exit; |
31 |
| import static java.lang.System.out; |
32 |
| |
33 |
| import java.io.BufferedReader; |
34 |
| import java.io.FileNotFoundException; |
35 |
| import java.io.IOException; |
36 |
| import java.io.InputStreamReader; |
37 |
| import java.net.URL; |
38 |
| |
39 |
| import org.apache.commons.beanutils.BeanUtils; |
40 |
| import org.sat4j.core.ASolverFactory; |
41 |
| import org.sat4j.reader.InstanceReader; |
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.ISolver; |
47 |
| import org.sat4j.specs.TimeoutException; |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| public class Lanceur { |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| public enum ExitCode { |
74 |
| OPTIMUM_FOUND(30, "OPTIMUM FOUND"), SATISFIABLE(10), UNKNOWN(0), UNSATISFIABLE( |
75 |
| 20); |
76 |
| |
77 |
| |
78 |
| private final int value; |
79 |
| |
80 |
| |
81 |
| private final String str; |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
0
| ExitCode(final int i) {
|
90 |
0
| this.value = i;
|
91 |
0
| str = null;
|
92 |
| } |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
0
| ExitCode(final int i, final String str) {
|
104 |
0
| this.value = i;
|
105 |
0
| this.str = str;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
0
| public int value() {
|
112 |
0
| return value;
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
0
| @Override
|
120 |
| public String toString() { |
121 |
0
| if (str != null) {
|
122 |
0
| return str;
|
123 |
| } |
124 |
0
| return super.toString();
|
125 |
| } |
126 |
| } |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
0
| public static void main(final String[] args) {
|
136 |
0
| Lanceur lanceur = new Lanceur();
|
137 |
0
| lanceur.run(args);
|
138 |
| } |
139 |
| |
140 |
| protected long begintime; |
141 |
| |
142 |
| protected ExitCode exitcode = ExitCode.UNKNOWN; |
143 |
| |
144 |
| protected ASolverFactory factory; |
145 |
| |
146 |
| protected Reader reader; |
147 |
| |
148 |
| protected int argindex = 0; |
149 |
| |
150 |
| private Thread shutdownHook = new Thread() { |
151 |
0
| @Override
|
152 |
| public void run() { |
153 |
0
| displayResult(solver, begintime, exitcode);
|
154 |
| } |
155 |
| }; |
156 |
| |
157 |
| protected ISolver solver; |
158 |
| |
159 |
0
| Lanceur() {
|
160 |
0
| Runtime.getRuntime().addShutdownHook(shutdownHook);
|
161 |
| } |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
0
| protected ISolver configureSolver(String[] args) {
|
168 |
0
| factory = new org.sat4j.minisat.SolverFactory();
|
169 |
| |
170 |
0
| if (args.length < 1) {
|
171 |
0
| usage();
|
172 |
0
| exit(-1);
|
173 |
| } |
174 |
| |
175 |
0
| ISolver asolver;
|
176 |
0
| if (args.length == argindex + 1) {
|
177 |
0
| asolver = factory.defaultSolver();
|
178 |
| } else { |
179 |
0
| asolver = factory.createSolverByName(args[argindex++]);
|
180 |
| |
181 |
0
| int others = argindex + 1;
|
182 |
0
| while (args.length > others) {
|
183 |
0
| String[] param = args[others].split("=");
|
184 |
| assert param.length == 2; |
185 |
0
| System.out.println("c setting " + param[0] + " to " + param[1]);
|
186 |
0
| try {
|
187 |
0
| BeanUtils.setProperty(asolver, param[0], param[1]);
|
188 |
| } catch (Exception e) { |
189 |
0
| System.out.println("c Cannot set parameter : "
|
190 |
| + args[others]); |
191 |
| } |
192 |
0
| others++;
|
193 |
| } |
194 |
| } |
195 |
0
| out.println(asolver.toString("c "));
|
196 |
0
| out.println("c timeout: " + asolver.getTimeout() + "s");
|
197 |
0
| return asolver;
|
198 |
| } |
199 |
| |
200 |
0
| private void usage() {
|
201 |
0
| out
|
202 |
| .println("Usage: java -jar sat4j.jar [<solver>] <cnffile> [<timeout>]"); |
203 |
0
| showAvailableSolvers();
|
204 |
| } |
205 |
| |
206 |
0
| protected Reader createReader(ISolver solver) {
|
207 |
0
| return new InstanceReader(solver);
|
208 |
| } |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
0
| private void displayHeader() throws IOException {
|
214 |
0
| out
|
215 |
| .println("c SAT4J: a SATisfiability library for Java (c) 2004-2005 Daniel Le Berre"); |
216 |
0
| out
|
217 |
| .println("c This is free software under the GNU LGPL licence. See www.sat4j.org for details."); |
218 |
0
| URL url = Lanceur.class.getResource("/sat4j.version");
|
219 |
0
| if (url != null) {
|
220 |
0
| BufferedReader in = new BufferedReader(new InputStreamReader(url
|
221 |
| .openStream())); |
222 |
0
| out.println("c version " + in.readLine());
|
223 |
0
| in.close();
|
224 |
| } else { |
225 |
0
| out.println("c no version file found!!!");
|
226 |
| } |
227 |
| } |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
0
| protected void displayResult(final ISolver solver, final long begintime,
|
235 |
| final ExitCode exitcode) { |
236 |
0
| if (solver != null) {
|
237 |
0
| double cputime = (System.currentTimeMillis() - begintime) / 1000.0;
|
238 |
0
| solver.printStat(System.out, "c ");
|
239 |
0
| out.println("s " + exitcode);
|
240 |
0
| if (exitcode == ExitCode.SATISFIABLE) {
|
241 |
0
| int[] model = solver.model();
|
242 |
0
| out.println("v " + reader.decode(model));
|
243 |
| } |
244 |
0
| out.println("c Total CPU time (ms) : " + cputime);
|
245 |
| } |
246 |
| } |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
0
| private IProblem readProblem(String[] args, ISolver solver, long begintime)
|
262 |
| throws FileNotFoundException, ParseFormatException, IOException, |
263 |
| ContradictionException { |
264 |
0
| out.println("c solving " + args[argindex]);
|
265 |
0
| out.println("c reading problem ... ");
|
266 |
0
| reader = createReader(solver);
|
267 |
0
| IProblem problem = reader.parseInstance(args[argindex]);
|
268 |
0
| out.println("c ... done. Time "
|
269 |
| + (System.currentTimeMillis() - begintime) / 1000.0 + " ms."); |
270 |
0
| out.println("c #vars " + solver.nVars());
|
271 |
0
| out.println("c #constraints " + solver.nConstraints());
|
272 |
0
| return problem;
|
273 |
| } |
274 |
| |
275 |
0
| public void run(String[] args) {
|
276 |
| |
277 |
0
| try {
|
278 |
0
| displayHeader();
|
279 |
| |
280 |
0
| solver = configureSolver(args);
|
281 |
| |
282 |
0
| begintime = System.currentTimeMillis();
|
283 |
| |
284 |
0
| IProblem problem = readProblem(args, solver, begintime);
|
285 |
| |
286 |
0
| try {
|
287 |
0
| solve(problem);
|
288 |
| } catch (TimeoutException e) { |
289 |
0
| out.println("c timeout");
|
290 |
| } |
291 |
| } catch (FileNotFoundException e) { |
292 |
0
| e.printStackTrace();
|
293 |
| } catch (IOException e) { |
294 |
0
| e.printStackTrace();
|
295 |
| } catch (ContradictionException e) { |
296 |
0
| exitcode = ExitCode.UNSATISFIABLE;
|
297 |
0
| out.println("c (trivial inconsistency)");
|
298 |
| } catch (ParseFormatException e) { |
299 |
0
| e.printStackTrace();
|
300 |
| } |
301 |
0
| exit(exitcode.value());
|
302 |
| } |
303 |
| |
304 |
0
| void showAvailableSolvers() {
|
305 |
0
| if (factory != null) {
|
306 |
0
| out.println("Available solvers: ");
|
307 |
0
| String[] names = factory.solverNames();
|
308 |
0
| for (int i = 0; i < names.length; i++) {
|
309 |
0
| out.println(names[i]);
|
310 |
| } |
311 |
| } |
312 |
| } |
313 |
| |
314 |
0
| protected void solve(IProblem problem) throws TimeoutException {
|
315 |
0
| boolean isSat = problem.isSatisfiable();
|
316 |
0
| exitcode = isSat ? ExitCode.SATISFIABLE : ExitCode.UNSATISFIABLE;
|
317 |
| } |
318 |
| } |