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