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