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 |
| |
28 |
| package org.sat4j.minisat.constraints.cnf; |
29 |
| |
30 |
| import java.io.Serializable; |
31 |
| |
32 |
| import org.sat4j.minisat.core.Constr; |
33 |
| import org.sat4j.minisat.core.ILits; |
34 |
| import org.sat4j.minisat.core.UnitPropagationListener; |
35 |
| import org.sat4j.specs.ContradictionException; |
36 |
| import org.sat4j.specs.IVecInt; |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| public final class WLClause implements Constr, Serializable { |
49 |
| |
50 |
| private static final long serialVersionUID = 1L; |
51 |
| |
52 |
| private boolean learnt; |
53 |
| |
54 |
| private double activity; |
55 |
| |
56 |
| private final int[] lits; |
57 |
| |
58 |
| private final ILits voc; |
59 |
| |
60 |
| private static int counter = 0; |
61 |
| |
62 |
| private final int id = ++counter; |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
89272229
| public WLClause(IVecInt ps, ILits voc) {
|
72 |
89272229
| lits = new int[ps.size()];
|
73 |
89272229
| ps.moveTo(lits);
|
74 |
| assert ps.size() == 0; |
75 |
89272229
| this.voc = voc;
|
76 |
89272229
| activity = 0;
|
77 |
89272229
| learnt = false;
|
78 |
| } |
79 |
| |
80 |
0
| public int getId() {
|
81 |
0
| return id;
|
82 |
| } |
83 |
| |
84 |
1334
| public static void resetIds() {
|
85 |
1334
| counter = 0;
|
86 |
| } |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
3252522
| public static IVecInt sanityCheck(IVecInt ps, ILits voc,
|
108 |
| UnitPropagationListener s) throws ContradictionException { |
109 |
| |
110 |
| |
111 |
3252522
| for (int i = 0; i < ps.size();) {
|
112 |
| |
113 |
10002338
| if (!voc.isUnassigned(ps.get(i))) {
|
114 |
| |
115 |
| |
116 |
3
| if (voc.isSatisfied(ps.get(i))) {
|
117 |
| |
118 |
0
| return null;
|
119 |
| } |
120 |
| |
121 |
3
| ps.delete(i);
|
122 |
| |
123 |
| } else { |
124 |
| |
125 |
10002335
| i++;
|
126 |
| } |
127 |
| } |
128 |
| |
129 |
| |
130 |
3252522
| ps.sortUnique();
|
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
3252522
| for (int i = 0; i < ps.size() - 1; i++) {
|
136 |
6749758
| if (ps.get(i) == (ps.get(i + 1) ^ 1)) {
|
137 |
| |
138 |
169
| return null;
|
139 |
| } |
140 |
| } |
141 |
| |
142 |
3252353
| if (propagationCheck(ps, s))
|
143 |
2
| return null;
|
144 |
| |
145 |
3252341
| return ps;
|
146 |
| } |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
3252353
| static boolean propagationCheck(IVecInt ps, UnitPropagationListener s)
|
161 |
| throws ContradictionException { |
162 |
3252353
| if (ps.size() == 0) {
|
163 |
10
| throw new ContradictionException("Creating Empty clause ?");
|
164 |
3252343
| } else if (ps.size() == 1) {
|
165 |
2
| if (!s.enqueue(ps.get(0))) {
|
166 |
0
| throw new ContradictionException("Contradictory Unit Clauses");
|
167 |
| } |
168 |
2
| return true;
|
169 |
| } |
170 |
| |
171 |
3252341
| return false;
|
172 |
| } |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
106156363
| public void setLearnt() {
|
179 |
106156363
| learnt = true;
|
180 |
| } |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
19692818
| public void register() {
|
189 |
| assert lits.length > 1; |
190 |
19692818
| if (learnt) {
|
191 |
| |
192 |
18288476
| int maxi = 1;
|
193 |
18288476
| int maxlevel = voc.getLevel(lits[1]);
|
194 |
18288476
| for (int i = 2; i < lits.length; i++) {
|
195 |
805340452
| int level = voc.getLevel(lits[i]);
|
196 |
805340452
| if (level > maxlevel) {
|
197 |
50450800
| maxi = i;
|
198 |
50450800
| maxlevel = level;
|
199 |
| } |
200 |
| } |
201 |
18288476
| int l = lits[1];
|
202 |
18288476
| lits[1] = lits[maxi];
|
203 |
18288476
| lits[maxi] = l;
|
204 |
| } |
205 |
| |
206 |
| |
207 |
19692818
| voc.watch(lits[0] ^ 1, this);
|
208 |
19692818
| voc.watch(lits[1] ^ 1, this);
|
209 |
| } |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
1404342
| public static WLClause brandNewClause(UnitPropagationListener s, ILits voc,
|
225 |
| IVecInt literals) { |
226 |
1404342
| WLClause c = new WLClause(literals, voc);
|
227 |
1404342
| c.register();
|
228 |
1404342
| return c;
|
229 |
| } |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
403749064
| public void calcReason(int p, IVecInt outReason) {
|
237 |
| assert outReason.size() == 0; |
238 |
| assert (p == ILits.UNDEFINED) || (p == lits[0]); |
239 |
403749064
| for (int i = (p == ILits.UNDEFINED) ? 0 : 1; i < lits.length; i++) {
|
240 |
| assert voc.isFalsified(lits[i]); |
241 |
| outReason.push(lits[i] ^ 1); |
242 |
| } |
243 |
| } |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
18359455
| public void remove() {
|
251 |
18359455
| voc.watches(lits[0] ^ 1).remove(this);
|
252 |
18359455
| voc.watches(lits[1] ^ 1).remove(this);
|
253 |
| |
254 |
| } |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
3114877
| public boolean simplify() {
|
262 |
3114877
| for (int i = 0; i < lits.length; i++) {
|
263 |
28878189
| if (voc.isSatisfied(lits[i])) {
|
264 |
133662
| return true;
|
265 |
| } |
266 |
| } |
267 |
2981215
| return false;
|
268 |
| } |
269 |
| |
270 |
| public boolean propagate(UnitPropagationListener s, int p) { |
271 |
| |
272 |
| if (lits[0] == (p ^ 1)) { |
273 |
672383004
| lits[0] = lits[1];
|
274 |
672383004
| lits[1] = p ^ 1;
|
275 |
| } |
276 |
| assert lits[1] == (p ^ 1); |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
1757260241
| for (int i = 2; i < lits.length; i++) {
|
286 |
420047687
| if (!voc.isFalsified(lits[i])) {
|
287 |
1370046324
| lits[1] = lits[i];
|
288 |
1370046324
| lits[i] = p ^ 1;
|
289 |
1370046324
| voc.watch(lits[1] ^ 1, this);
|
290 |
1370046324
| return true;
|
291 |
| } |
292 |
| } |
293 |
| assert voc.isFalsified(lits[1]); |
294 |
| |
295 |
1337212554
| voc.watch(p, this);
|
296 |
| |
297 |
1337212554
| return s.enqueue(lits[0], this);
|
298 |
| } |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
73319224
| public boolean locked() {
|
304 |
73319224
| return voc.getReason(lits[0]) == this;
|
305 |
| } |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
1325360156
| public double getActivity() {
|
311 |
1325360156
| return activity;
|
312 |
| } |
313 |
| |
314 |
0
| @Override
|
315 |
| public String toString() { |
316 |
0
| StringBuffer stb = new StringBuffer();
|
317 |
0
| for (int i = 0; i < lits.length; i++) {
|
318 |
0
| stb.append(lits[i]);
|
319 |
0
| stb.append(" ");
|
320 |
0
| stb.append("[");
|
321 |
0
| stb.append(voc.valueToString(lits[i]));
|
322 |
0
| stb.append("]");
|
323 |
| } |
324 |
0
| return stb.toString();
|
325 |
| } |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
| public int get(int i) { |
335 |
| return lits[i]; |
336 |
| } |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
96574957
| public void incActivity(double claInc) {
|
342 |
96574957
| activity += claInc;
|
343 |
| } |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
83716
| public void rescaleBy(double d) {
|
349 |
83716
| activity *= d;
|
350 |
| } |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
403749064
| public boolean learnt() {
|
358 |
403749064
| return learnt;
|
359 |
| } |
360 |
| |
361 |
| public int size() { |
362 |
| return lits.length; |
363 |
| } |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
0
| public static int lastid() {
|
369 |
0
| return counter;
|
370 |
| } |
371 |
| |
372 |
87867887
| public void assertConstraint(UnitPropagationListener s) {
|
373 |
87867887
| boolean ret = s.enqueue(lits[0], this);
|
374 |
| assert ret; |
375 |
| } |
376 |
| } |