1 |
| package org.sat4j.minisat.constraints.pb; |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| import java.math.BigInteger; |
12 |
| import java.util.HashMap; |
13 |
| import java.util.Map; |
14 |
| |
15 |
| import org.sat4j.minisat.core.ILits; |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class ConflictMap extends MapPb implements IConflict{ |
22 |
| |
23 |
| private final ILits voc; |
24 |
| |
25 |
0
| public static IConflict createConflict(PBConstr cpb) {
|
26 |
0
| Map<Integer, BigInteger> m = new HashMap<Integer, BigInteger>();
|
27 |
0
| for (int i = 0; i < cpb.size(); i++) {
|
28 |
| assert cpb.get(i) != 0; |
29 |
| assert cpb.getCoef(i).signum() > 0; |
30 |
0
| m.put(cpb.get(i), cpb.getCoef(i));
|
31 |
| } |
32 |
0
| return new ConflictMap(m, cpb.getDegree(), cpb.getVocabulary());
|
33 |
| } |
34 |
| |
35 |
| |
36 |
0
| ConflictMap(Map<Integer, BigInteger> m, BigInteger d, ILits voc) {
|
37 |
0
| super(m, d);
|
38 |
0
| this.voc = voc;
|
39 |
| } |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| private BigInteger coefMult = BigInteger.ZERO; |
46 |
| |
47 |
| private BigInteger coefMultCons = BigInteger.ZERO; |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
0
| public BigInteger resolve(PBConstr cpb, int litImplied) {
|
59 |
| assert litImplied > 1; |
60 |
0
| if (!coefs.containsKey(litImplied ^ 1)) {
|
61 |
| |
62 |
0
| return degree;
|
63 |
| } |
64 |
| |
65 |
| assert slackConflict().signum() <= 0; |
66 |
| assert degree.signum() >= 0; |
67 |
| |
68 |
| |
69 |
| |
70 |
0
| BigInteger[] coefsCons = null;
|
71 |
0
| BigInteger degreeCons = cpb.getDegree();
|
72 |
| |
73 |
| |
74 |
0
| int ind = 0;
|
75 |
0
| while (cpb.get(ind) != litImplied)
|
76 |
0
| ind++;
|
77 |
| |
78 |
| assert cpb.get(ind) == litImplied; |
79 |
| assert cpb.getCoef(ind) != BigInteger.ZERO; |
80 |
| |
81 |
0
| if (cpb.getCoef(ind).equals(BigInteger.ONE)) {
|
82 |
| |
83 |
0
| coefMultCons = coefs.get(litImplied ^ 1);
|
84 |
0
| coefMult = BigInteger.ONE;
|
85 |
| |
86 |
0
| degreeCons = (degreeCons.multiply(coefMultCons));
|
87 |
| } else { |
88 |
0
| if (coefs.get(litImplied ^ 1).equals(BigInteger.ONE)) {
|
89 |
| |
90 |
0
| coefMult = cpb.getCoef(ind);
|
91 |
0
| coefMultCons = BigInteger.ONE;
|
92 |
| |
93 |
0
| degree = degree.multiply(coefMult);
|
94 |
| } else { |
95 |
| |
96 |
| |
97 |
0
| WatchPb wpb = (WatchPb) cpb;
|
98 |
0
| coefsCons = wpb.getCoefs();
|
99 |
| assert positiveCoefs(coefsCons); |
100 |
0
| degreeCons = reduceUntilConflict(litImplied, ind, coefsCons,
|
101 |
| wpb); |
102 |
| |
103 |
0
| degreeCons = (degreeCons.multiply(coefMultCons));
|
104 |
0
| degree = degree.multiply(coefMult);
|
105 |
| } |
106 |
| |
107 |
0
| for (Integer i : coefs.keySet()) {
|
108 |
0
| coefs.put(i, coefs.get(i).multiply(coefMult));
|
109 |
| } |
110 |
| } |
111 |
| |
112 |
| |
113 |
0
| degree = cuttingPlane(cpb, degreeCons, coefsCons, coefMultCons);
|
114 |
| assert !coefs.containsKey(litImplied); |
115 |
| assert !coefs.containsKey(litImplied ^ 1); |
116 |
| assert degree.signum() > 0; |
117 |
| |
118 |
| |
119 |
0
| degree = saturation();
|
120 |
| assert slackConflict().signum() <= 0; |
121 |
| |
122 |
0
| return degree;
|
123 |
| } |
124 |
| |
125 |
0
| private BigInteger reduceUntilConflict(int litImplied, int ind,
|
126 |
| BigInteger[] reducedCoefs, WatchPb wpb) { |
127 |
0
| BigInteger slackResolve = BigInteger.ONE.negate();
|
128 |
0
| BigInteger slackThis = BigInteger.ZERO;
|
129 |
0
| BigInteger slackIndex = BigInteger.ZERO;
|
130 |
0
| BigInteger ppcm;
|
131 |
0
| BigInteger reducedDegree = wpb.getDegree();
|
132 |
| |
133 |
0
| do {
|
134 |
0
| if (slackResolve.signum() >= 0) {
|
135 |
| assert slackThis.signum() > 0; |
136 |
0
| BigInteger tmp = reduceInConstraint(wpb, reducedCoefs, ind,
|
137 |
| reducedDegree); |
138 |
| assert ((tmp.compareTo(reducedDegree) < 0) && (tmp |
139 |
| .compareTo(BigInteger.ONE) >= 0)); |
140 |
0
| reducedDegree = tmp;
|
141 |
| } |
142 |
| |
143 |
| assert coefs.get(litImplied ^ 1).signum() > 0; |
144 |
| assert reducedCoefs[ind].signum() > 0; |
145 |
| |
146 |
0
| BigInteger coefLitImplied = coefs.get(litImplied ^ 1);
|
147 |
0
| ppcm = ppcm(reducedCoefs[ind], coefLitImplied);
|
148 |
| assert ppcm.signum() > 0; |
149 |
0
| coefMult = ppcm.divide(coefLitImplied);
|
150 |
0
| coefMultCons = ppcm.divide(reducedCoefs[ind]);
|
151 |
| |
152 |
| assert coefMultCons.signum() > 0; |
153 |
| assert coefMult.signum() > 0; |
154 |
| assert coefMult.multiply(coefLitImplied).equals( |
155 |
| coefMultCons.multiply(reducedCoefs[ind])); |
156 |
| |
157 |
| |
158 |
0
| slackThis = wpb.slackConstraint(reducedCoefs, reducedDegree)
|
159 |
| .multiply(coefMultCons); |
160 |
0
| slackIndex = slackConflict().multiply(coefMult);
|
161 |
| |
162 |
| assert slackIndex.signum() <= 0; |
163 |
| |
164 |
| |
165 |
0
| slackResolve = slackThis.add(slackIndex);
|
166 |
0
| } while (slackResolve.signum() >= 0);
|
167 |
| assert coefMult.multiply(coefs.get(litImplied ^ 1)).equals( |
168 |
| coefMultCons.multiply(reducedCoefs[ind])); |
169 |
0
| return reducedDegree;
|
170 |
| |
171 |
| } |
172 |
| |
173 |
0
| public BigInteger slackConflict() {
|
174 |
0
| BigInteger poss = BigInteger.ZERO;
|
175 |
0
| BigInteger tmp;
|
176 |
| |
177 |
0
| for (Integer i : coefs.keySet()){
|
178 |
0
| tmp = coefs.get(i);
|
179 |
0
| if (tmp.signum() != 0 && !voc.isFalsified(i))
|
180 |
0
| poss = poss.add(tmp);
|
181 |
| } |
182 |
0
| return poss.subtract(degree);
|
183 |
| } |
184 |
| |
185 |
0
| public boolean isAssertive(int dl) {
|
186 |
0
| BigInteger slack = BigInteger.ZERO;
|
187 |
0
| BigInteger tmp;
|
188 |
0
| for (Integer i : coefs.keySet()) {
|
189 |
0
| tmp = coefs.get(i);
|
190 |
0
| if ((tmp.signum() > 0)
|
191 |
| && (((!voc.isFalsified(i)) || voc.getLevel(i) >= dl))) |
192 |
0
| slack = slack.add(tmp);
|
193 |
| } |
194 |
0
| slack = slack.subtract(degree);
|
195 |
0
| if (slack.signum() < 0)
|
196 |
0
| return false;
|
197 |
0
| for (Integer i : coefs.keySet()) {
|
198 |
0
| tmp = coefs.get(i);
|
199 |
0
| if ((tmp.signum() > 0)
|
200 |
| && (voc.isUnassigned(i) || voc.getLevel(i) >= dl) |
201 |
| && (slack.subtract(tmp).signum() < 0)) |
202 |
0
| return true;
|
203 |
| } |
204 |
0
| return false;
|
205 |
| } |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
0
| protected static BigInteger ppcm(BigInteger a, BigInteger b) {
|
217 |
0
| return a.divide(a.gcd(b)).multiply(b);
|
218 |
| } |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
0
| public BigInteger reduceInConstraint(WatchPb wpb,
|
227 |
| final BigInteger[] coefsBis, final int indLitImplied, |
228 |
| final BigInteger degreeBis) { |
229 |
| |
230 |
| assert degreeBis.compareTo(BigInteger.ONE) > 0; |
231 |
| |
232 |
0
| int lit = -1;
|
233 |
0
| for (int ind = 0; (ind < wpb.size()) && (lit == -1); ind++)
|
234 |
0
| if (coefsBis[ind].signum() != 0 && voc.isUnassigned(wpb.get(ind))) {
|
235 |
| assert coefsBis[ind].compareTo(degreeBis) < 0; |
236 |
0
| lit = ind;
|
237 |
| } |
238 |
| |
239 |
| |
240 |
0
| if (lit == -1)
|
241 |
0
| for (int ind = 0; (ind < wpb.size()) && (lit == -1); ind++)
|
242 |
0
| if ((coefsBis[ind].signum() != 0)
|
243 |
| && (voc.isSatisfied(wpb.get(ind))) |
244 |
| && (ind != indLitImplied)) |
245 |
0
| lit = ind;
|
246 |
| |
247 |
| |
248 |
| assert lit != -1; |
249 |
| |
250 |
| assert lit != indLitImplied; |
251 |
| |
252 |
| |
253 |
0
| BigInteger degUpdate = degreeBis.subtract(coefsBis[lit]);
|
254 |
0
| coefsBis[lit] = BigInteger.ZERO;
|
255 |
| |
256 |
| |
257 |
| assert degUpdate.signum() > 0; |
258 |
0
| BigInteger minimum = degUpdate;
|
259 |
0
| for (int i = 0; i < coefsBis.length; i++) {
|
260 |
0
| if (coefsBis[i].signum() > 0)
|
261 |
0
| minimum = minimum.min(coefsBis[i]);
|
262 |
0
| coefsBis[i] = degUpdate.min(coefsBis[i]);
|
263 |
| } |
264 |
0
| if (minimum.equals(degUpdate) && !degUpdate.equals(BigInteger.ONE)) {
|
265 |
| |
266 |
| |
267 |
0
| degUpdate = BigInteger.ONE;
|
268 |
0
| for (int i = 0; i < coefsBis.length; i++)
|
269 |
0
| if (coefsBis[i].signum() > 0)
|
270 |
0
| coefsBis[i] = degUpdate;
|
271 |
| } |
272 |
| |
273 |
| assert coefsBis[indLitImplied].signum() > 0; |
274 |
| assert degreeBis.compareTo(degUpdate) > 0; |
275 |
0
| return degUpdate;
|
276 |
| } |
277 |
| |
278 |
0
| private boolean positiveCoefs(final BigInteger[] coefsCons) {
|
279 |
0
| for (int i = 0; i < coefsCons.length; i++) {
|
280 |
0
| if (coefsCons[i].signum() <= 0)
|
281 |
0
| return false;
|
282 |
| } |
283 |
0
| return true;
|
284 |
| } |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
0
| public int getBacktrackLevel(int maxLevel) {
|
295 |
0
| int litLevel;
|
296 |
0
| int borneMax = maxLevel;
|
297 |
| |
298 |
| |
299 |
| assert isAssertive(borneMax); |
300 |
0
| int borneMin = -1;
|
301 |
| |
302 |
| |
303 |
0
| for (int lit : coefs.keySet()) {
|
304 |
0
| litLevel = voc.getLevel(lit);
|
305 |
0
| if (litLevel < borneMax && litLevel > borneMin)
|
306 |
0
| if (isAssertive(litLevel))
|
307 |
0
| borneMax = litLevel;
|
308 |
| else |
309 |
0
| borneMin = litLevel;
|
310 |
| } |
311 |
| |
312 |
| |
313 |
0
| int retour = 0;
|
314 |
0
| for (int lit : coefs.keySet()) {
|
315 |
0
| litLevel = voc.getLevel(lit);
|
316 |
0
| if (litLevel > retour && litLevel < borneMax)
|
317 |
0
| retour = litLevel;
|
318 |
| } |
319 |
0
| return retour;
|
320 |
| } |
321 |
| |
322 |
| } |
323 |
| |
324 |
| |