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.xplain;
31
32 import java.util.Map;
33 import java.util.Set;
34
35 import org.sat4j.core.VecInt;
36 import org.sat4j.specs.ISolver;
37 import org.sat4j.specs.IVecInt;
38 import org.sat4j.specs.IteratorInt;
39 import org.sat4j.specs.TimeoutException;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class QuickXplainStrategy implements MinimizationStrategy {
67
68
69
70
71 private static final long serialVersionUID = 1L;
72
73 private boolean computationCanceled;
74
75 public void cancelExplanationComputation() {
76 this.computationCanceled = true;
77 }
78
79 public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
80 IVecInt assumps) throws TimeoutException {
81 this.computationCanceled = false;
82 IVecInt encodingAssumptions = new VecInt(constrs.size()
83 + assumps.size());
84 assumps.copyTo(encodingAssumptions);
85 IVecInt firstExplanation = solver.unsatExplanation();
86 IVecInt results = new VecInt(firstExplanation.size());
87 if (firstExplanation.size() == 1) {
88 results.push(-firstExplanation.get(0));
89 return results;
90 }
91 if (solver.isVerbose()) {
92 System.out.print(solver.getLogPrefix() + "initial unsat core ");
93 firstExplanation.sort();
94 for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
95 System.out.print(constrs.get(-it.next()));
96 System.out.print(" ");
97 }
98 System.out.println();
99 }
100 for (int i = 0; i < firstExplanation.size();) {
101 if (assumps.contains(firstExplanation.get(i))) {
102 firstExplanation.delete(i);
103 } else {
104 i++;
105 }
106 }
107 Set<Integer> constraintsVariables = constrs.keySet();
108 IVecInt remainingVariables = new VecInt(constraintsVariables.size());
109 for (Integer v : constraintsVariables) {
110 remainingVariables.push(v);
111 }
112 int p;
113 for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
114 p = it.next();
115 if (p < 0) {
116 p = -p;
117 }
118 remainingVariables.remove(p);
119 encodingAssumptions.push(p);
120 }
121 int unsatcorelimit = encodingAssumptions.size() - 1;
122
123 remainingVariables.copyTo(encodingAssumptions);
124 computeExplanation(solver, constrs, encodingAssumptions,
125 assumps.size(), unsatcorelimit, results);
126 return results;
127 }
128
129 private void computeExplanation(ISolver solver, Map<Integer, ?> constrs,
130 IVecInt encodingAssumptions, int start, int end, IVecInt result)
131 throws TimeoutException {
132 if (solver.isVerbose()) {
133 System.out.println(solver.getLogPrefix() + "qxplain " + start + "/"
134 + end);
135 }
136 if (!solver.isSatisfiable(encodingAssumptions)) {
137 return;
138 }
139 if (start == end) {
140 result.push(encodingAssumptions.get(start));
141 encodingAssumptions.set(start, -encodingAssumptions.get(start));
142 if (solver.isVerbose()) {
143 System.out.println(solver.getLogPrefix()
144 + constrs.get(-encodingAssumptions.get(start))
145 + " is mandatory ");
146 }
147 return;
148 }
149 int split = (end + start) / 2;
150 if (split < end) {
151 for (int j = start; j <= split; j++) {
152 encodingAssumptions.set(j, -encodingAssumptions.get(j));
153 }
154 computeExplanation(solver, constrs, encodingAssumptions, split + 1,
155 end, result);
156 }
157 if (start <= split) {
158 for (int j = start; j <= split; j++) {
159 encodingAssumptions.set(j, -encodingAssumptions.get(j));
160 }
161 computeExplanation(solver, constrs, encodingAssumptions, start,
162 split, result);
163 }
164 if (this.computationCanceled) {
165 throw new TimeoutException();
166 }
167 }
168
169 @Override
170 public String toString() {
171 return "QuickXplain (AAAI 2004 version) minimization strategy";
172 }
173
174 }