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 public class InsertionStrategy implements MinimizationStrategy {
65
66
67
68
69 private static final long serialVersionUID = 1L;
70
71 private boolean computationCanceled;
72
73
74
75
76 public void cancelExplanationComputation() {
77 this.computationCanceled = true;
78 }
79
80
81
82
83 public IVecInt explain(ISolver solver, Map<Integer, ?> constrs,
84 IVecInt assumps) throws TimeoutException {
85 this.computationCanceled = false;
86 IVecInt encodingAssumptions = new VecInt(constrs.size()
87 + assumps.size());
88 assumps.copyTo(encodingAssumptions);
89 IVecInt firstExplanation = solver.unsatExplanation();
90 if (firstExplanation.size() == 1) {
91 IVecInt results = new VecInt();
92 results.push(-firstExplanation.get(0));
93 return results;
94 }
95 if (solver.isVerbose()) {
96 System.out.print(solver.getLogPrefix() + "initial unsat core ");
97 firstExplanation.sort();
98 for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
99 System.out.print(constrs.get(-it.next()));
100 System.out.print(" ");
101 }
102 System.out.println();
103 }
104 for (int i = 0; i < firstExplanation.size();) {
105 if (assumps.contains(firstExplanation.get(i))) {
106 firstExplanation.delete(i);
107 } else {
108 i++;
109 }
110 }
111 Set<Integer> constraintsVariables = constrs.keySet();
112 IVecInt remainingVariables = new VecInt(constraintsVariables.size());
113 for (Integer v : constraintsVariables) {
114 remainingVariables.push(v);
115 }
116 int p;
117 for (IteratorInt it = firstExplanation.iterator(); it.hasNext();) {
118 p = it.next();
119 if (p < 0) {
120 p = -p;
121 }
122 remainingVariables.remove(p);
123 encodingAssumptions.push(p);
124 }
125 remainingVariables.copyTo(encodingAssumptions);
126 boolean shouldContinue;
127 int startingPoint = assumps.size();
128 do {
129 shouldContinue = false;
130 int i = startingPoint;
131 encodingAssumptions.set(i, -encodingAssumptions.get(i));
132 assert encodingAssumptions.get(i) < 0;
133 while (!this.computationCanceled
134 && solver.isSatisfiable(encodingAssumptions)) {
135 i++;
136 assert encodingAssumptions.get(i) > 0;
137 encodingAssumptions.set(i, -encodingAssumptions.get(i));
138 }
139 if (!this.computationCanceled && i > startingPoint) {
140 assert !solver.isSatisfiable(encodingAssumptions);
141 if (i < encodingAssumptions.size()) {
142
143
144 int tmp = encodingAssumptions.get(i);
145 for (int j = i; j > startingPoint; j--) {
146 encodingAssumptions.set(j,
147 -encodingAssumptions.get(j - 1));
148 }
149 encodingAssumptions.set(startingPoint, tmp);
150 if (solver.isVerbose()) {
151 System.out.println(solver.getLogPrefix()
152 + constrs.get(tmp) + " is mandatory ");
153 }
154 }
155 shouldContinue = true;
156 }
157 startingPoint++;
158 } while (!this.computationCanceled && shouldContinue
159 && solver.isSatisfiable(encodingAssumptions));
160 if (this.computationCanceled) {
161 throw new TimeoutException();
162 }
163 IVecInt constrsKeys = new VecInt(startingPoint);
164 for (int i = assumps.size(); i < startingPoint; i++) {
165 constrsKeys.push(-encodingAssumptions.get(i));
166 }
167 return constrsKeys;
168 }
169
170 @Override
171 public String toString() {
172 return "Replay (Insertion-based) minimization strategy";
173 }
174 }