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