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.core.Constr; |
31 |
| import org.sat4j.minisat.core.ILits; |
32 |
| import org.sat4j.minisat.core.Undoable; |
33 |
| import org.sat4j.minisat.core.UnitPropagationListener; |
34 |
| import org.sat4j.specs.ContradictionException; |
35 |
| import org.sat4j.specs.IVecInt; |
36 |
| |
37 |
| public class MinWatchCard implements Constr, Undoable, Serializable { |
38 |
| |
39 |
| private static final long serialVersionUID = 1L; |
40 |
| |
41 |
| public static final boolean ATLEAST = true; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| private int degree; |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| private int[] lits; |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| private boolean moreThan; |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| private int watchCumul; |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| private ILits voc; |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
249266
| private MinWatchCard(ILits voc, IVecInt ps, boolean moreThan, int degree) {
|
81 |
| |
82 |
249266
| this.voc = voc;
|
83 |
249266
| this.degree = degree;
|
84 |
249266
| this.moreThan = moreThan;
|
85 |
| |
86 |
| |
87 |
249266
| int[] index = new int[voc.nVars() * 2 + 2];
|
88 |
249266
| for (int i = 0; i < index.length; i++)
|
89 |
481099300
| index[i] = 0;
|
90 |
| |
91 |
249266
| for (int i = 0; i < ps.size(); i++) {
|
92 |
767399
| if (index[ps.get(i) ^ 1] != 0) {
|
93 |
13
| index[ps.get(i) ^ 1]--;
|
94 |
| } else { |
95 |
767386
| index[ps.get(i)]++;
|
96 |
| } |
97 |
| } |
98 |
| |
99 |
249266
| int ind = 0;
|
100 |
249266
| while (ind < ps.size()) {
|
101 |
767399
| if (index[ps.get(ind)] > 0) {
|
102 |
767373
| index[ps.get(ind)]--;
|
103 |
767373
| ind++;
|
104 |
| } else { |
105 |
26
| if ((ps.get(ind) & 1) != 0)
|
106 |
13
| this.degree--;
|
107 |
26
| ps.set(ind, ps.last());
|
108 |
26
| ps.pop();
|
109 |
| } |
110 |
| } |
111 |
| |
112 |
| |
113 |
249266
| lits = new int[ps.size()];
|
114 |
249266
| ps.moveTo(lits);
|
115 |
| |
116 |
| |
117 |
249266
| normalize();
|
118 |
| |
119 |
| } |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
24416258
| public void calcReason(int p, IVecInt outReason) {
|
131 |
| |
132 |
| |
133 |
24416258
| for (int i = 0; i < lits.length; i++) {
|
134 |
| |
135 |
108752714
| if (voc.isFalsified(lits[i])) {
|
136 |
| |
137 |
87117107
| outReason.push(lits[i] ^ 1);
|
138 |
| } |
139 |
| } |
140 |
| } |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
0
| public double getActivity() {
|
149 |
| |
150 |
0
| return 0;
|
151 |
| } |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
0
| public void incActivity(double claInc) {
|
161 |
| |
162 |
| } |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
24416258
| public boolean learnt() {
|
171 |
| |
172 |
24416258
| return false;
|
173 |
| } |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
249266
| private static int linearisation(ILits voc, IVecInt ps) {
|
185 |
| |
186 |
249266
| int modif = 0;
|
187 |
| |
188 |
249266
| for (int i = 0; i < ps.size();) {
|
189 |
| |
190 |
767399
| if (!voc.isUnassigned(ps.get(i))) {
|
191 |
| |
192 |
| |
193 |
0
| if (voc.isSatisfied(ps.get(i))) {
|
194 |
0
| modif--;
|
195 |
| } |
196 |
| |
197 |
| |
198 |
0
| ps.set(i, ps.last());
|
199 |
0
| ps.pop();
|
200 |
| } else { |
201 |
| |
202 |
767399
| i++;
|
203 |
| } |
204 |
| } |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
249266
| return modif;
|
210 |
| } |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
0
| public boolean locked() {
|
219 |
| |
220 |
0
| return true;
|
221 |
| } |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
249266
| public static MinWatchCard minWatchCardNew(UnitPropagationListener s,
|
240 |
| ILits voc, IVecInt ps, boolean moreThan, int degree) |
241 |
| throws ContradictionException { |
242 |
| |
243 |
249266
| degree += linearisation(voc, ps);
|
244 |
| |
245 |
249266
| if (ps.size() == 0) {
|
246 |
0
| throw new ContradictionException("Cr?ation d'une clause vide");
|
247 |
249266
| } else if (ps.size() == degree) {
|
248 |
0
| for (int i = 0; i < ps.size(); i++)
|
249 |
0
| if (!s.enqueue(ps.get(i))) {
|
250 |
0
| throw new ContradictionException(
|
251 |
| "Contradiction avec le litt?ral impliqu?."); |
252 |
| } |
253 |
0
| return null;
|
254 |
| } |
255 |
| |
256 |
| |
257 |
249266
| MinWatchCard retour = new MinWatchCard(voc, ps, moreThan, degree);
|
258 |
| |
259 |
249266
| retour.normalize();
|
260 |
| |
261 |
249266
| if (degree <= 0)
|
262 |
0
| return null;
|
263 |
| |
264 |
| |
265 |
249266
| int indSwap = retour.lits.length;
|
266 |
249266
| int tmpInt;
|
267 |
249266
| for (int i = 0; i <= retour.degree && i < indSwap; i++) {
|
268 |
498517
| while (voc.isFalsified(retour.lits[i]) && --indSwap <= i) {
|
269 |
0
| tmpInt = retour.lits[i];
|
270 |
0
| retour.lits[i] = retour.lits[indSwap];
|
271 |
0
| retour.lits[indSwap] = tmpInt;
|
272 |
| } |
273 |
| |
274 |
| |
275 |
498517
| if (!voc.isFalsified(retour.lits[i])) {
|
276 |
498517
| retour.watchCumul++;
|
277 |
498517
| voc.watch(retour.lits[i] ^ 1, retour);
|
278 |
| } |
279 |
| } |
280 |
| |
281 |
| |
282 |
249266
| if (retour.watchCumul <= retour.degree) {
|
283 |
| |
284 |
2
| if (retour.watchCumul == retour.degree) {
|
285 |
2
| for (int i = 0; i < retour.lits.length; i++)
|
286 |
0
| if (!s.enqueue(retour.lits[i])) {
|
287 |
0
| throw new ContradictionException(
|
288 |
| "Contradiction avec le litt?ral impliqu?."); |
289 |
| } |
290 |
2
| return null;
|
291 |
| } |
292 |
0
| throw new ContradictionException("Contrainte non-satisfiable");
|
293 |
| } |
294 |
| |
295 |
249264
| return retour;
|
296 |
| } |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
498532
| public void normalize() {
|
302 |
| |
303 |
498532
| if (!moreThan) {
|
304 |
| |
305 |
0
| this.degree = 0 - this.degree;
|
306 |
| |
307 |
0
| for (int indLit = 0; indLit < lits.length; indLit++) {
|
308 |
0
| lits[indLit] = lits[indLit] ^ 1;
|
309 |
0
| this.degree++;
|
310 |
| } |
311 |
0
| this.moreThan = true;
|
312 |
| } |
313 |
| } |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
102228277
| public boolean propagate(UnitPropagationListener s, int p) {
|
325 |
| |
326 |
| |
327 |
102228277
| if (watchCumul == degree) {
|
328 |
0
| voc.watch(p, this);
|
329 |
0
| return false;
|
330 |
| } |
331 |
| |
332 |
| |
333 |
102228277
| int indFalsified = -1;
|
334 |
102228277
| while ((lits[++indFalsified] ^ 1) != p)
|
335 |
| ; |
336 |
| |
337 |
| |
338 |
102228277
| int indSwap = degree + 1;
|
339 |
102228277
| while (indSwap < lits.length && voc.isFalsified(lits[indSwap]))
|
340 |
130709938
| indSwap++;
|
341 |
| |
342 |
| |
343 |
102228277
| if (indSwap == lits.length) {
|
344 |
| |
345 |
92275136
| voc.watch(p, this);
|
346 |
| |
347 |
92275136
| watchCumul--;
|
348 |
| assert watchCumul == degree; |
349 |
92275136
| voc.undos(p).push(this);
|
350 |
| |
351 |
| |
352 |
92275136
| for (int i = 0; i <= degree; i++)
|
353 |
184350418
| if ((p != (lits[i] ^ 1)) &&!s.enqueue(lits[i], this))
|
354 |
2780692
| return false;
|
355 |
| |
356 |
89494444
| return true;
|
357 |
| } |
358 |
| |
359 |
9953141
| int tmpInt = lits[indSwap];
|
360 |
9953141
| lits[indSwap] = lits[indFalsified];
|
361 |
9953141
| lits[indFalsified] = tmpInt;
|
362 |
| |
363 |
| |
364 |
9953141
| voc.watch(tmpInt ^ 1, this);
|
365 |
| |
366 |
9953141
| return true;
|
367 |
| } |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
13516
| public void remove() {
|
373 |
13516
| for (int i = 0; i <= degree; i++) {
|
374 |
27032
| voc.watches(lits[i] ^ 1).remove(this);
|
375 |
| } |
376 |
| } |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
0
| public void rescaleBy(double d) {
|
385 |
| |
386 |
| } |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
| |
392 |
| |
393 |
417164
| public boolean simplify() {
|
394 |
| |
395 |
417164
| for (int i = 0, count = 0; i < lits.length; i++)
|
396 |
1405717
| if (voc.isSatisfied(lits[i]))
|
397 |
13517
| if (++count == degree)
|
398 |
13516
| return true;
|
399 |
| |
400 |
403648
| return false;
|
401 |
| } |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
0
| @Override
|
409 |
| public String toString() { |
410 |
0
| StringBuffer stb = new StringBuffer();
|
411 |
| |
412 |
0
| if (lits.length > 0) {
|
413 |
0
| if (voc.isUnassigned(lits[0])) {
|
414 |
0
| stb.append(this.lits[0]);
|
415 |
0
| stb.append(" ");
|
416 |
| } |
417 |
0
| for (int i = 1; i < lits.length; i++) {
|
418 |
0
| if (voc.isUnassigned(lits[i])) {
|
419 |
0
| stb.append(" + ");
|
420 |
0
| stb.append(this.lits[i]);
|
421 |
0
| stb.append(" ");
|
422 |
| } |
423 |
| } |
424 |
0
| stb.append(">= ");
|
425 |
0
| stb.append(this.degree);
|
426 |
| } |
427 |
0
| return stb.toString();
|
428 |
| } |
429 |
| |
430 |
| |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
| |
436 |
92272985
| public void undo(int p) {
|
437 |
| |
438 |
92272985
| watchCumul++;
|
439 |
| } |
440 |
| |
441 |
0
| public void setLearnt() {
|
442 |
0
| throw new UnsupportedOperationException();
|
443 |
| } |
444 |
| |
445 |
0
| public void register() {
|
446 |
0
| throw new UnsupportedOperationException();
|
447 |
| } |
448 |
| |
449 |
0
| public int size() {
|
450 |
0
| return lits.length;
|
451 |
| } |
452 |
| |
453 |
0
| public int get(int i) {
|
454 |
0
| return lits[i];
|
455 |
| } |
456 |
| |
457 |
0
| public void assertConstraint(UnitPropagationListener s) {
|
458 |
0
| throw new UnsupportedOperationException();
|
459 |
| } |
460 |
| } |