| 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 |
|
package org.sat4j; |
| 26 |
|
|
| 27 |
|
import java.io.PrintWriter; |
| 28 |
|
|
| 29 |
|
import org.sat4j.specs.ContradictionException; |
| 30 |
|
import org.sat4j.specs.IOptimizationProblem; |
| 31 |
|
import org.sat4j.specs.IProblem; |
| 32 |
|
import org.sat4j.specs.TimeoutException; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
@author |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
@SuppressWarnings("PMD") |
|
|
|
| 0% |
Uncovered Elements: 53 (53) |
Complexity: 11 |
Complexity Density: 0,36 |
|
| 43 |
|
public abstract class AbstractOptimizationLauncher extends AbstractLauncher { |
| 44 |
|
|
| 45 |
|
private static final String CURRENT_OPTIMUM_VALUE_PREFIX = "o "; |
| 46 |
|
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 5 |
Complexity Density: 0,36 |
|
| 47 |
0
|
@Override... |
| 48 |
|
protected void displayResult() { |
| 49 |
0
|
if (solver == null) |
| 50 |
0
|
return; |
| 51 |
0
|
PrintWriter out = getLogWriter(); |
| 52 |
0
|
solver.printStat(out, COMMENT_PREFIX); |
| 53 |
0
|
ExitCode exitCode = getExitCode(); |
| 54 |
0
|
out.println(ANSWER_PREFIX + exitCode); |
| 55 |
0
|
if (exitCode == ExitCode.SATISFIABLE |
| 56 |
|
|| exitCode == ExitCode.OPTIMUM_FOUND) { |
| 57 |
0
|
out.print(SOLUTION_PREFIX); |
| 58 |
0
|
getReader().decode(solver.model(), out); |
| 59 |
0
|
out.println(); |
| 60 |
0
|
IOptimizationProblem optproblem = (IOptimizationProblem) solver; |
| 61 |
0
|
if (!optproblem.hasNoObjectiveFunction()) { |
| 62 |
0
|
log("objective function=" + optproblem.calculateObjective()); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
} |
| 66 |
0
|
log("Total wall clock time (ms): " |
| 67 |
|
+ (System.currentTimeMillis() - getBeginTime()) / 1000.0); |
| 68 |
|
} |
| 69 |
|
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 7 |
Complexity Density: 0,37 |
|
| 70 |
0
|
@Override... |
| 71 |
|
protected void solve(IProblem problem) throws TimeoutException { |
| 72 |
0
|
boolean isSatisfiable = false; |
| 73 |
|
|
| 74 |
0
|
IOptimizationProblem optproblem = (IOptimizationProblem) problem; |
| 75 |
|
|
| 76 |
0
|
try { |
| 77 |
0
|
while (optproblem.admitABetterSolution()) { |
| 78 |
0
|
if (!isSatisfiable) { |
| 79 |
0
|
if (optproblem.nonOptimalMeansSatisfiable()) { |
| 80 |
0
|
setExitCode(ExitCode.SATISFIABLE); |
| 81 |
0
|
if (optproblem.hasNoObjectiveFunction()) { |
| 82 |
0
|
return; |
| 83 |
|
} |
| 84 |
0
|
log("SATISFIABLE"); |
| 85 |
|
} |
| 86 |
0
|
isSatisfiable = true; |
| 87 |
0
|
log("OPTIMIZING..."); |
| 88 |
|
} |
| 89 |
0
|
log("Got one! Elapsed wall clock time (in seconds):" |
| 90 |
|
+ (System.currentTimeMillis() - getBeginTime()) |
| 91 |
|
/ 1000.0); |
| 92 |
0
|
getLogWriter().println( |
| 93 |
|
CURRENT_OPTIMUM_VALUE_PREFIX |
| 94 |
|
+ optproblem.calculateObjective()); |
| 95 |
0
|
optproblem.discard(); |
| 96 |
|
} |
| 97 |
0
|
if (isSatisfiable) { |
| 98 |
0
|
setExitCode(ExitCode.OPTIMUM_FOUND); |
| 99 |
|
} else { |
| 100 |
0
|
setExitCode(ExitCode.UNSATISFIABLE); |
| 101 |
|
} |
| 102 |
|
} catch (ContradictionException ex) { |
| 103 |
0
|
assert isSatisfiable; |
| 104 |
0
|
setExitCode(ExitCode.OPTIMUM_FOUND); |
| 105 |
|
} |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
} |