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