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