|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.sat4j; |
|
8 |
| |
|
9 |
| |
|
10 |
| import java.io.PrintWriter; |
|
11 |
| |
|
12 |
| import org.sat4j.specs.ContradictionException; |
|
13 |
| import org.sat4j.specs.IOptimizationProblem; |
|
14 |
| import org.sat4j.specs.IProblem; |
|
15 |
| import org.sat4j.specs.TimeoutException; |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| @SuppressWarnings("PMD") |
|
25 |
| public abstract class AbstractOptimizationLauncher extends AbstractLauncher { |
|
26 |
| |
|
27 |
| private static final String CURRENT_OPTIMUM_VALUE_PREFIX = "o "; |
|
28 |
| |
|
29 |
0
| @Override
|
|
30 |
| protected void displayResult() { |
|
31 |
0
| if (solver == null)
|
|
32 |
0
| return;
|
|
33 |
0
| PrintWriter out = getLogWriter();
|
|
34 |
0
| solver.printStat(out, COMMENT_PREFIX);
|
|
35 |
0
| ExitCode exitCode = getExitCode();
|
|
36 |
0
| out.println(ANSWER_PREFIX + exitCode);
|
|
37 |
0
| if (exitCode == ExitCode.SATISFIABLE
|
|
38 |
| || exitCode == ExitCode.OPTIMUM_FOUND) { |
|
39 |
0
| out.println(SOLUTION_PREFIX + getReader().decode(solver.model()));
|
|
40 |
0
| IOptimizationProblem optproblem = (IOptimizationProblem)solver;
|
|
41 |
0
| if (!optproblem.hasNoObjectiveFunction()) {
|
|
42 |
0
| log("objective function=" + optproblem.calculateObjective());
|
|
43 |
| } |
|
44 |
| |
|
45 |
| } |
|
46 |
0
| log("Total wall clock time (ms): "
|
|
47 |
| + (System.currentTimeMillis() - getBeginTime()) / 1000.0); |
|
48 |
| } |
|
49 |
| |
|
50 |
0
| @Override
|
|
51 |
| protected void solve(IProblem problem) throws TimeoutException { |
|
52 |
0
| boolean isSatisfiable = false;
|
|
53 |
| |
|
54 |
0
| IOptimizationProblem optproblem = (IOptimizationProblem) problem;
|
|
55 |
| |
|
56 |
0
| try {
|
|
57 |
0
| while (optproblem.admitABetterSolution()) {
|
|
58 |
0
| if (!isSatisfiable) {
|
|
59 |
0
| if (optproblem.nonOptimalMeansSatisfiable()) {
|
|
60 |
0
| setExitCode(ExitCode.SATISFIABLE);
|
|
61 |
0
| if (optproblem.hasNoObjectiveFunction()) {
|
|
62 |
0
| return;
|
|
63 |
| } |
|
64 |
0
| log("SATISFIABLE");
|
|
65 |
| } |
|
66 |
0
| isSatisfiable = true;
|
|
67 |
0
| log("OPTIMIZING...");
|
|
68 |
| } |
|
69 |
0
| log("Got one! Elapsed wall clock time (in seconds):"
|
|
70 |
| + (System.currentTimeMillis() - getBeginTime()) |
|
71 |
| / 1000.0); |
|
72 |
0
| getLogWriter().println(CURRENT_OPTIMUM_VALUE_PREFIX
|
|
73 |
| + optproblem.calculateObjective()); |
|
74 |
0
| optproblem.discard();
|
|
75 |
| } |
|
76 |
0
| if (isSatisfiable) {
|
|
77 |
0
| setExitCode(ExitCode.OPTIMUM_FOUND);
|
|
78 |
| } else { |
|
79 |
0
| setExitCode(ExitCode.UNSATISFIABLE);
|
|
80 |
| } |
|
81 |
| } catch (ContradictionException ex) { |
|
82 |
| assert isSatisfiable; |
|
83 |
0
| setExitCode(ExitCode.OPTIMUM_FOUND);
|
|
84 |
| } |
|
85 |
| } |
|
86 |
| |
|
87 |
| } |