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