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
29
30 package org.sat4j;
31
32 import java.io.PrintWriter;
33
34 import org.sat4j.specs.ContradictionException;
35 import org.sat4j.specs.IOptimizationProblem;
36 import org.sat4j.specs.IProblem;
37 import org.sat4j.specs.TimeoutException;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @Deprecated
52 public abstract class AbstractOptimizationLauncher extends AbstractLauncher {
53
54
55
56
57 private static final long serialVersionUID = 1L;
58
59 private static final String CURRENT_OPTIMUM_VALUE_PREFIX = "o ";
60
61 private boolean incomplete = false;
62
63 private boolean displaySolutionLine = true;
64
65 @Override
66 protected void setIncomplete(boolean value) {
67 this.incomplete = value;
68 }
69
70 @Override
71 protected void setDisplaySolutionLine(boolean value) {
72 this.displaySolutionLine = value;
73 }
74
75 @Override
76 protected void displayResult() {
77 displayAnswer();
78
79 log("Total wall clock time (in seconds): "
80 + (System.currentTimeMillis() - getBeginTime()) / 1000.0);
81 }
82
83 protected void displayAnswer() {
84 if (this.solver == null) {
85 return;
86 }
87 System.out.flush();
88 PrintWriter out = getLogWriter();
89 out.flush();
90 this.solver.printStat(out, COMMENT_PREFIX);
91 this.solver.printInfos(out, COMMENT_PREFIX);
92 ExitCode exitCode = getExitCode();
93 out.println(ILauncherMode.ANSWER_PREFIX + exitCode);
94 if (exitCode == ExitCode.SATISFIABLE
95 || exitCode == ExitCode.OPTIMUM_FOUND || this.incomplete
96 && exitCode == ExitCode.UPPER_BOUND) {
97 if (this.displaySolutionLine) {
98 out.print(ILauncherMode.SOLUTION_PREFIX);
99 getReader().decode(this.solver.model(), out);
100 out.println();
101 }
102 IOptimizationProblem optproblem = (IOptimizationProblem) this.solver;
103 if (!optproblem.hasNoObjectiveFunction()) {
104 log("objective function=" + optproblem.getObjectiveValue());
105 }
106 }
107 }
108
109 @Override
110 protected void solve(IProblem problem) throws TimeoutException {
111 boolean isSatisfiable = false;
112
113 IOptimizationProblem optproblem = (IOptimizationProblem) problem;
114
115 try {
116 while (optproblem.admitABetterSolution()) {
117 if (!isSatisfiable) {
118 if (optproblem.nonOptimalMeansSatisfiable()) {
119 setExitCode(ExitCode.SATISFIABLE);
120 if (optproblem.hasNoObjectiveFunction()) {
121 return;
122 }
123 log("SATISFIABLE");
124 } else if (this.incomplete) {
125 setExitCode(ExitCode.UPPER_BOUND);
126 }
127 isSatisfiable = true;
128 log("OPTIMIZING...");
129 }
130 log("Got one! Elapsed wall clock time (in seconds):"
131 + (System.currentTimeMillis() - getBeginTime())
132 / 1000.0);
133 getLogWriter().println(
134 CURRENT_OPTIMUM_VALUE_PREFIX
135 + optproblem.getObjectiveValue());
136 optproblem.discardCurrentSolution();
137 }
138 if (isSatisfiable) {
139 setExitCode(ExitCode.OPTIMUM_FOUND);
140 } else {
141 setExitCode(ExitCode.UNSATISFIABLE);
142 }
143 } catch (ContradictionException ex) {
144 assert isSatisfiable;
145 setExitCode(ExitCode.OPTIMUM_FOUND);
146 }
147 }
148
149 }