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