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.Undoable;
35 import org.sat4j.minisat.core.UnitPropagationListener;
36 import org.sat4j.specs.IVecInt;
37
38
39
40
41
42 public class CBClause implements Constr, Undoable, Serializable {
43
44 private static final long serialVersionUID = 1L;
45
46 protected int falsified;
47
48 private boolean learnt;
49
50 protected final int[] lits;
51
52 protected final ILits voc;
53
54 private double activity;
55
56 public static CBClause brandNewClause(UnitPropagationListener s, ILits voc,
57 IVecInt literals) {
58 CBClause c = new CBClause(literals, voc);
59 c.register();
60 return c;
61 }
62
63
64
65
66 public CBClause(IVecInt ps, ILits voc, boolean learnt) {
67 this.learnt = learnt;
68 this.lits = new int[ps.size()];
69 this.voc = voc;
70 ps.moveTo(this.lits);
71 }
72
73 public CBClause(IVecInt ps, ILits voc) {
74 this(ps, voc, false);
75 }
76
77
78
79
80 public void remove(UnitPropagationListener upl) {
81 for (int i = 0; i < lits.length; i++) {
82 voc.watches(lits[i] ^ 1).remove(this);
83 }
84 }
85
86
87
88
89
90
91
92 public boolean propagate(UnitPropagationListener s, int p) {
93 voc.undos(p).push(this);
94 falsified++;
95 assert falsified != lits.length;
96 if (falsified == lits.length - 1) {
97
98 for (int i = 0; i < lits.length; i++) {
99 if (!voc.isFalsified(lits[i])) {
100 return s.enqueue(lits[i], this);
101 }
102 }
103
104
105
106 return false;
107 }
108 return true;
109 }
110
111
112
113
114
115
116 public boolean simplify() {
117 for (int p : lits) {
118 if (voc.isSatisfied(p)) {
119 return true;
120 }
121 }
122 return false;
123 }
124
125
126
127
128
129
130 public void undo(int p) {
131 falsified--;
132 }
133
134
135
136
137
138
139
140 public void calcReason(int p, IVecInt outReason) {
141 assert outReason.size() == 0;
142 for (int q : lits) {
143 assert voc.isFalsified(q) || q == p;
144 if (voc.isFalsified(q)) {
145 outReason.push(q ^ 1);
146 }
147 }
148 assert (p == ILits.UNDEFINED) || (outReason.size() == lits.length - 1);
149 }
150
151
152
153
154
155
156 public boolean learnt() {
157 return learnt;
158 }
159
160
161
162
163
164
165 public void incActivity(double claInc) {
166 if (learnt) {
167 activity += claInc;
168 }
169 }
170
171
172
173
174
175
176 public double getActivity() {
177 return activity;
178 }
179
180
181
182
183
184
185 public boolean locked() {
186 return voc.getReason(lits[0]) == this;
187 }
188
189
190
191
192
193
194 public void setLearnt() {
195 learnt = true;
196 }
197
198
199
200
201
202
203 public void register() {
204 for (int p : lits) {
205 voc.watch(p ^ 1, this);
206 }
207 if (learnt) {
208 for (int p : lits) {
209 if (voc.isFalsified(p)) {
210 voc.undos(p ^ 1).push(this);
211 falsified++;
212 }
213 }
214 assert falsified == lits.length - 1;
215 }
216 }
217
218
219
220
221
222
223 public void rescaleBy(double d) {
224 activity *= d;
225 }
226
227
228
229
230
231
232 public int size() {
233 return lits.length;
234 }
235
236
237
238
239
240
241 public int get(int i) {
242 return lits[i];
243 }
244
245
246
247
248
249
250
251
252 public void assertConstraint(UnitPropagationListener s) {
253 assert voc.isUnassigned(lits[0]);
254 boolean ret = s.enqueue(lits[0], this);
255 assert ret;
256 }
257
258 @Override
259 public String toString() {
260 StringBuffer stb = new StringBuffer();
261 for (int i = 0; i < lits.length; i++) {
262 stb.append(lits[i]);
263 stb.append("[");
264 stb.append(voc.valueToString(lits[i]));
265 stb.append("]");
266 stb.append(" ");
267 }
268 return stb.toString();
269 }
270
271
272
273
274 public void forwardActivity(double claInc) {
275 if (!learnt) {
276 activity += claInc;
277 }
278 }
279 }