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