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