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 public class QuickXplain2001Strategy implements MinimizationStrategy {
64
65
66
67
68 private static final long serialVersionUID = 1L;
69
70 private boolean computationCanceled;
71
72 public void cancelExplanationComputation() {
73 this.computationCanceled = true;
74 }
75
76 public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
77 IVecInt assumps) throws TimeoutException {
78 this.computationCanceled = false;
79 IVecInt encodingAssumptions = new VecInt(constrs.size()
80 + assumps.size());
81 assumps.copyTo(encodingAssumptions);
82 IVecInt firstExplanation = solver.unsatExplanation();
83 if (solver.isVerbose()) {
84 System.out.print(solver.getLogPrefix() + "initial unsat core ");
85 firstExplanation.sort();
86 for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
87 System.out.print(constrs.get(-it.next()));
88 System.out.print(" ");
89 }
90 System.out.println();
91 }
92 Set<Integer> constraintsVariables = constrs.keySet();
93 int p;
94 for (int i = 0; i < firstExplanation.size(); i++) {
95 if (constraintsVariables.contains(p = -firstExplanation.get(i))) {
96 encodingAssumptions.push(p);
97 }
98 }
99 IVecInt results = new VecInt(encodingAssumptions.size());
100 computeExplanation(solver, encodingAssumptions, assumps.size(),
101 encodingAssumptions.size() - 1, results);
102 return results;
103 }
104
105 private void computeExplanation(ISolver solver,
106 IVecInt encodingAssumptions, int start, int end, IVecInt result)
107 throws TimeoutException {
108 if (!solver.isSatisfiable(encodingAssumptions)) {
109 return;
110 }
111 int i = start;
112 encodingAssumptions.set(i, -encodingAssumptions.get(i));
113 assert encodingAssumptions.get(i) < 0;
114 while (!this.computationCanceled
115 && solver.isSatisfiable(encodingAssumptions)) {
116 if (i == end) {
117 for (int j = start; j <= end; j++) {
118 encodingAssumptions.set(j, -encodingAssumptions.get(j));
119 }
120 return;
121 }
122 i++;
123 assert encodingAssumptions.get(i) > 0;
124 encodingAssumptions.set(i, -encodingAssumptions.get(i));
125 }
126 result.push(-encodingAssumptions.get(i));
127 if (start == i) {
128 return;
129 }
130 int newend = i - 1;
131 int split = (newend + start) / 2;
132 if (split < newend) {
133 for (int j = split + 1; j < i; j++) {
134 encodingAssumptions.set(j, -encodingAssumptions.get(j));
135 }
136 computeExplanation(solver, encodingAssumptions, split + 1, newend,
137 result);
138 }
139 if (start <= split) {
140 for (int j = start; j <= split; j++) {
141 encodingAssumptions.set(j, -encodingAssumptions.get(j));
142 }
143 computeExplanation(solver, encodingAssumptions, start, split,
144 result);
145 }
146 if (this.computationCanceled) {
147 throw new TimeoutException();
148 }
149 }
150
151 @Override
152 public String toString() {
153 return "QuickXplain (IJCAI WS 2001 version) minimization strategy";
154 }
155 }