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