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 |
| package org.sat4j.minisat.constraints.card; |
27 |
| |
28 |
| import java.io.Serializable; |
29 |
| |
30 |
| import org.sat4j.minisat.constraints.cnf.Lits; |
31 |
| import org.sat4j.minisat.core.Constr; |
32 |
| import org.sat4j.minisat.core.ILits; |
33 |
| import org.sat4j.minisat.core.Undoable; |
34 |
| import org.sat4j.minisat.core.UnitPropagationListener; |
35 |
| import org.sat4j.specs.ContradictionException; |
36 |
| import org.sat4j.specs.IVecInt; |
37 |
| |
38 |
| public class MinWatchCard implements Constr, Undoable, Serializable { |
39 |
| |
40 |
| private static final long serialVersionUID = 1L; |
41 |
| |
42 |
| public static final boolean ATLEAST = true; |
43 |
| public static final boolean ATMOST = false; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| protected int degree; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| private int[] lits; |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| private boolean moreThan; |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| protected int watchCumul; |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| private final ILits voc; |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
251810
| public MinWatchCard(ILits voc, IVecInt ps, boolean moreThan, int degree) {
|
84 |
| |
85 |
251810
| this.voc = voc;
|
86 |
251810
| this.degree = degree;
|
87 |
251810
| this.moreThan = moreThan;
|
88 |
| |
89 |
| |
90 |
251810
| int[] index = new int[voc.nVars() * 2 + 2];
|
91 |
251810
| for (int i = 0; i < index.length; i++)
|
92 |
493911772
| index[i] = 0;
|
93 |
| |
94 |
251810
| for (int i = 0; i < ps.size(); i++) {
|
95 |
1253945
| if (index[ps.get(i) ^ 1] == 0) {
|
96 |
1253932
| index[ps.get(i)]++;
|
97 |
| } else { |
98 |
13
| index[ps.get(i) ^ 1]--;
|
99 |
| } |
100 |
| } |
101 |
| |
102 |
251810
| int ind = 0;
|
103 |
251810
| while (ind < ps.size()) {
|
104 |
1253945
| if (index[ps.get(ind)] > 0) {
|
105 |
1253919
| index[ps.get(ind)]--;
|
106 |
1253919
| ind++;
|
107 |
| } else { |
108 |
| |
109 |
26
| if ((ps.get(ind) & 1) != 0)
|
110 |
13
| this.degree--;
|
111 |
26
| ps.set(ind, ps.last());
|
112 |
26
| ps.pop();
|
113 |
| } |
114 |
| } |
115 |
| |
116 |
| |
117 |
251810
| lits = new int[ps.size()];
|
118 |
251810
| ps.moveTo(lits);
|
119 |
| |
120 |
| |
121 |
251810
| normalize();
|
122 |
| |
123 |
| } |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
18412
| protected MinWatchCard(ILits voc, IVecInt ps, int degree){
|
142 |
| |
143 |
18412
| this.voc = voc;
|
144 |
18412
| this.degree = degree;
|
145 |
18412
| this.moreThan = ATLEAST;
|
146 |
| |
147 |
| |
148 |
18412
| lits = new int[ps.size()];
|
149 |
18412
| ps.moveTo(lits);
|
150 |
| |
151 |
| } |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
36282131
| public void calcReason(int p, IVecInt outReason) {
|
163 |
| |
164 |
| |
165 |
36282131
| for (int i = 0; i < lits.length; i++) {
|
166 |
| |
167 |
162856816
| if (voc.isFalsified(lits[i])) {
|
168 |
| |
169 |
130717796
| outReason.push(lits[i] ^ 1);
|
170 |
| } |
171 |
| } |
172 |
| } |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
0
| public double getActivity() {
|
181 |
| |
182 |
0
| return 0;
|
183 |
| } |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
0
| public void incActivity(double claInc) {
|
193 |
| |
194 |
| } |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
36531384
| public boolean learnt() {
|
203 |
36531384
| return false;
|
204 |
| } |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
267678
| protected static int linearisation(ILits voc, IVecInt ps) {
|
216 |
| |
217 |
267678
| int modif = 0;
|
218 |
| |
219 |
267678
| for (int i = 0; i < ps.size();) {
|
220 |
| |
221 |
1127815
| if (voc.isUnassigned(ps.get(i))) {
|
222 |
1127815
| i++;
|
223 |
| } else { |
224 |
| |
225 |
| |
226 |
0
| if (voc.isSatisfied(ps.get(i))) {
|
227 |
0
| modif--;
|
228 |
| } |
229 |
| |
230 |
| |
231 |
0
| ps.set(i, ps.last());
|
232 |
0
| ps.pop();
|
233 |
| } |
234 |
| } |
235 |
| |
236 |
| |
237 |
| |
238 |
| assert modif <= 0; |
239 |
| |
240 |
267678
| return modif;
|
241 |
| } |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
0
| public boolean locked() {
|
250 |
| |
251 |
0
| return true;
|
252 |
| } |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
249266
| public static MinWatchCard minWatchCardNew(UnitPropagationListener s,
|
272 |
| ILits voc, IVecInt ps, boolean moreThan, int degree) |
273 |
| throws ContradictionException { |
274 |
| |
275 |
249266
| int mydegree = degree + linearisation(voc, ps);
|
276 |
| |
277 |
249266
| if (ps.size() == 0 && mydegree > 0) {
|
278 |
0
| throw new ContradictionException();
|
279 |
249266
| } else if (ps.size() == mydegree || ps.size() <= 0) {
|
280 |
0
| for (int i = 0; i < ps.size(); i++)
|
281 |
0
| if (!s.enqueue(ps.get(i))) {
|
282 |
0
| throw new ContradictionException();
|
283 |
| } |
284 |
0
| return null;
|
285 |
| } |
286 |
| |
287 |
| |
288 |
249266
| MinWatchCard retour = new MinWatchCard(voc, ps, moreThan, mydegree);
|
289 |
| |
290 |
249266
| if (retour.degree <= 0)
|
291 |
13
| return null;
|
292 |
| |
293 |
249253
| retour.computeWatches();
|
294 |
| |
295 |
249253
| retour.computePropagation(s);
|
296 |
| |
297 |
249253
| return retour;
|
298 |
| } |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
251810
| public final void normalize() {
|
304 |
| |
305 |
251810
| if (!moreThan) {
|
306 |
| |
307 |
0
| this.degree = 0 - this.degree;
|
308 |
| |
309 |
0
| for (int indLit = 0; indLit < lits.length; indLit++) {
|
310 |
0
| lits[indLit] = lits[indLit] ^ 1;
|
311 |
0
| this.degree++;
|
312 |
| } |
313 |
0
| this.moreThan = true;
|
314 |
| } |
315 |
| } |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
154526295
| public boolean propagate(UnitPropagationListener s, int p) {
|
327 |
| |
328 |
| |
329 |
154526295
| if (watchCumul == degree) {
|
330 |
0
| voc.watch(p, this);
|
331 |
0
| return false;
|
332 |
| } |
333 |
| |
334 |
| |
335 |
154526295
| int indFalsified = 0;
|
336 |
154526295
| while ((lits[indFalsified] ^ 1) != p)
|
337 |
83702387
| indFalsified++;
|
338 |
| assert watchCumul > degree; |
339 |
| |
340 |
| |
341 |
154526295
| int indSwap = degree + 1;
|
342 |
154526295
| while (indSwap < lits.length && voc.isFalsified(lits[indSwap]))
|
343 |
211075869
| indSwap++;
|
344 |
| |
345 |
| |
346 |
154526295
| if (indSwap == lits.length) {
|
347 |
| |
348 |
139421852
| voc.watch(p, this);
|
349 |
| |
350 |
139421852
| watchCumul--;
|
351 |
| assert watchCumul == degree; |
352 |
139421852
| voc.undos(p).push(this);
|
353 |
| |
354 |
| |
355 |
139421852
| for (int i = 0; i <= degree; i++)
|
356 |
285019388
| if ((p != (lits[i] ^ 1)) && !s.enqueue(lits[i], this))
|
357 |
4151514
| return false;
|
358 |
| |
359 |
135270338
| return true;
|
360 |
| } |
361 |
| |
362 |
15104443
| int tmpInt = lits[indSwap];
|
363 |
15104443
| lits[indSwap] = lits[indFalsified];
|
364 |
15104443
| lits[indFalsified] = tmpInt;
|
365 |
| |
366 |
| |
367 |
15104443
| voc.watch(tmpInt ^ 1, this);
|
368 |
| |
369 |
15104443
| return true;
|
370 |
| } |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
0
| public void remove() {
|
376 |
0
| for (int i = 0; i <= degree; i++) {
|
377 |
0
| voc.watches(lits[i] ^ 1).remove(this);
|
378 |
| } |
379 |
| } |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
| |
386 |
| |
387 |
0
| public void rescaleBy(double d) {
|
388 |
| |
389 |
| } |
390 |
| |
391 |
| |
392 |
| |
393 |
| |
394 |
| |
395 |
| |
396 |
0
| public boolean simplify() {
|
397 |
| |
398 |
0
| for (int i = 0, count = 0; i < lits.length; i++)
|
399 |
0
| if (voc.isSatisfied(lits[i]) && (++count == degree))
|
400 |
0
| return true;
|
401 |
| |
402 |
0
| return false;
|
403 |
| } |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
0
| @Override
|
411 |
| public String toString() { |
412 |
0
| StringBuffer stb = new StringBuffer();
|
413 |
0
| stb.append("Card (" + lits.length + ") : ");
|
414 |
0
| if (lits.length > 0) {
|
415 |
| |
416 |
0
| stb.append(Lits.toString(this.lits[0]));
|
417 |
0
| stb.append("[");
|
418 |
0
| stb.append(voc.valueToString(lits[0]));
|
419 |
0
| stb.append("@");
|
420 |
0
| stb.append(voc.getLevel(lits[0]));
|
421 |
0
| stb.append("]");
|
422 |
0
| stb.append(" ");
|
423 |
| |
424 |
0
| for (int i = 1; i < lits.length; i++) {
|
425 |
| |
426 |
0
| stb.append(" + ");
|
427 |
0
| stb.append(Lits.toString(this.lits[i]));
|
428 |
0
| stb.append("[");
|
429 |
0
| stb.append(voc.valueToString(lits[i]));
|
430 |
0
| stb.append("@");
|
431 |
0
| stb.append(voc.getLevel(lits[i]));
|
432 |
0
| stb.append("]");
|
433 |
0
| stb.append(" ");
|
434 |
| |
435 |
| } |
436 |
0
| stb.append(">= ");
|
437 |
0
| stb.append(this.degree);
|
438 |
| } |
439 |
0
| return stb.toString();
|
440 |
| } |
441 |
| |
442 |
| |
443 |
| |
444 |
| |
445 |
| |
446 |
| |
447 |
| |
448 |
139419460
| public void undo(int p) {
|
449 |
| |
450 |
139419460
| watchCumul++;
|
451 |
| } |
452 |
| |
453 |
0
| public void setLearnt() {
|
454 |
0
| throw new UnsupportedOperationException();
|
455 |
| } |
456 |
| |
457 |
0
| public void register() {
|
458 |
0
| throw new UnsupportedOperationException();
|
459 |
| } |
460 |
| |
461 |
3358720
| public int size() {
|
462 |
3358720
| return lits.length;
|
463 |
| } |
464 |
| |
465 |
4135657
| public int get(int i) {
|
466 |
4135657
| return lits[i];
|
467 |
| } |
468 |
| |
469 |
0
| public void assertConstraint(UnitPropagationListener s) {
|
470 |
0
| throw new UnsupportedOperationException();
|
471 |
| } |
472 |
| |
473 |
270209
| protected void computeWatches() {
|
474 |
270209
| int indSwap = lits.length;
|
475 |
270209
| int tmpInt;
|
476 |
270209
| for (int i = 0; i <= degree && i < indSwap; i++) {
|
477 |
1085435
| while (voc.isFalsified(lits[i]) && --indSwap > i) {
|
478 |
253985
| tmpInt = lits[i];
|
479 |
253985
| lits[i] = lits[indSwap];
|
480 |
253985
| lits[indSwap] = tmpInt;
|
481 |
| } |
482 |
| |
483 |
| |
484 |
1085435
| if (!voc.isFalsified(lits[i])) {
|
485 |
1084302
| watchCumul++;
|
486 |
1084302
| voc.watch(lits[i] ^ 1, this);
|
487 |
| } |
488 |
| } |
489 |
270209
| if (learnt()) {
|
490 |
| |
491 |
| |
492 |
2544
| int free = 1;
|
493 |
2544
| while ((watchCumul <= degree) && (free > 0)) {
|
494 |
2544
| free = 0;
|
495 |
| |
496 |
2544
| int maxlevel = -1, maxi = -1;
|
497 |
2544
| for (int i = watchCumul; i < lits.length; i++) {
|
498 |
255118
| if (voc.isFalsified(lits[i])) {
|
499 |
255118
| free++;
|
500 |
255118
| int level = voc.getLevel(lits[i]);
|
501 |
255118
| if (level > maxlevel) {
|
502 |
6743
| maxi = i;
|
503 |
6743
| maxlevel = level;
|
504 |
| } |
505 |
| } |
506 |
| } |
507 |
2544
| if (free > 0) {
|
508 |
| assert maxi >= 0; |
509 |
2488
| voc.watch(lits[maxi] ^ 1, this);
|
510 |
2488
| tmpInt = lits[maxi];
|
511 |
2488
| lits[maxi] = lits[watchCumul];
|
512 |
2488
| lits[watchCumul] = tmpInt;
|
513 |
2488
| watchCumul++;
|
514 |
2488
| free--;
|
515 |
| assert free >= 0; |
516 |
| } |
517 |
| } |
518 |
| assert lits.length == 1 || watchCumul > 1; |
519 |
| } |
520 |
| |
521 |
| } |
522 |
| |
523 |
267665
| protected MinWatchCard computePropagation(UnitPropagationListener s)
|
524 |
| throws ContradictionException { |
525 |
| |
526 |
| |
527 |
267665
| if (watchCumul == degree) {
|
528 |
0
| for (int i = 0; i < lits.length; i++)
|
529 |
0
| if (!s.enqueue(lits[i])) {
|
530 |
0
| throw new ContradictionException();
|
531 |
| } |
532 |
0
| return null;
|
533 |
| } |
534 |
| |
535 |
| |
536 |
267665
| if (watchCumul < degree) {
|
537 |
0
| throw new ContradictionException();
|
538 |
| } |
539 |
267665
| return this;
|
540 |
| } |
541 |
| |
542 |
8362
| public int[] getLits() {
|
543 |
8362
| int[] tmp = new int[size()];
|
544 |
8362
| System.arraycopy(lits, 0, tmp, 0, size());
|
545 |
8362
| return tmp;
|
546 |
| } |
547 |
| |
548 |
494908
| public ILits getVocabulary() {
|
549 |
494908
| return voc;
|
550 |
| } |
551 |
| } |