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.tools;
31
32 import org.sat4j.core.VecInt;
33 import org.sat4j.specs.ContradictionException;
34 import org.sat4j.specs.IOptimizationProblem;
35 import org.sat4j.specs.ISolver;
36 import org.sat4j.specs.IVecInt;
37 import org.sat4j.specs.TimeoutException;
38
39 public class OptToSatAdapter extends SolverDecorator<ISolver> {
40
41
42
43
44 private static final long serialVersionUID = 1L;
45
46 IOptimizationProblem problem;
47
48 boolean optimalValueForced = false;
49 private final IVecInt assumps = new VecInt();
50
51 private long begin;
52
53 private final SolutionFoundListener sfl;
54
55 public OptToSatAdapter(IOptimizationProblem problem) {
56 this(problem, SolutionFoundListener.VOID);
57 }
58
59 public OptToSatAdapter(IOptimizationProblem problem,
60 SolutionFoundListener sfl) {
61 super((ISolver) problem);
62 this.problem = problem;
63 this.sfl = sfl;
64 }
65
66 @Override
67 public void reset() {
68 super.reset();
69 this.optimalValueForced = false;
70 }
71
72 @Override
73 public boolean isSatisfiable() throws TimeoutException {
74 return isSatisfiable(VecInt.EMPTY);
75 }
76
77 @Override
78 public boolean isSatisfiable(boolean global) throws TimeoutException {
79 return isSatisfiable();
80 }
81
82 @Override
83 public boolean isSatisfiable(IVecInt myAssumps, boolean global)
84 throws TimeoutException {
85 return isSatisfiable(myAssumps);
86 }
87
88 @Override
89 public boolean isSatisfiable(IVecInt myAssumps) throws TimeoutException {
90 this.assumps.clear();
91 myAssumps.copyTo(this.assumps);
92 this.begin = System.currentTimeMillis();
93 if (this.problem.hasNoObjectiveFunction()) {
94 return this.problem.isSatisfiable(myAssumps);
95 }
96 boolean satisfiable = false;
97 try {
98 while (this.problem.admitABetterSolution(myAssumps)) {
99 satisfiable = true;
100 sfl.onSolutionFound(this.problem.model());
101 this.problem.discardCurrentSolution();
102 if (isVerbose()) {
103 System.out.println(getLogPrefix()
104 + "Current objective function value: "
105 + this.problem.getObjectiveValue() + "("
106 + (System.currentTimeMillis() - this.begin)
107 / 1000.0 + "s)");
108 }
109 }
110 sfl.onUnsatTermination();
111 } catch (ContradictionException ce) {
112 sfl.onUnsatTermination();
113 }
114 return satisfiable;
115 }
116
117 @Override
118 public int[] model() {
119 return this.problem.model();
120 }
121
122 @Override
123 public boolean model(int var) {
124 return this.problem.model(var);
125 }
126
127 @Override
128 public int[] modelWithInternalVariables() {
129 return decorated().modelWithInternalVariables();
130 }
131
132 @Override
133 public int[] findModel() throws TimeoutException {
134 if (isSatisfiable()) {
135 return model();
136 }
137 return null;
138 }
139
140 @Override
141 public int[] findModel(IVecInt assumps) throws TimeoutException {
142 if (isSatisfiable(assumps)) {
143 return model();
144 }
145 return null;
146 }
147
148 @Override
149 public String toString(String prefix) {
150 return prefix + "Optimization to SAT adapter\n"
151 + super.toString(prefix);
152 }
153
154
155
156
157
158
159
160 public boolean isOptimal() {
161 return this.problem.isOptimal();
162 }
163 }