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
118
119
120
121
122
123
124 for (int i = 2; i < mylits.length; i++) {
125 if (!voc.isFalsified(mylits[i])) {
126 mylits[1] = mylits[i];
127 mylits[i] = p ^ 1;
128 voc.watch(mylits[1] ^ 1, this);
129 return true;
130 }
131 }
132
133
134 voc.watch(p, this);
135
136 return s.enqueue(mylits[0], this);
137 }
138
139
140
141
142 public boolean locked() {
143 return voc.getReason(lits[0]) == this;
144 }
145
146
147
148
149 public double getActivity() {
150 return activity;
151 }
152
153 @Override
154 public String toString() {
155 StringBuffer stb = new StringBuffer();
156 for (int i = 0; i < lits.length; i++) {
157 stb.append(Lits.toString(lits[i]));
158 stb.append("[");
159 stb.append(voc.valueToString(lits[i]));
160 stb.append("]");
161 stb.append(" ");
162 }
163 return stb.toString();
164 }
165
166
167
168
169
170
171
172
173
174 public int get(int i) {
175 return lits[i];
176 }
177
178
179
180
181 public void rescaleBy(double d) {
182 activity *= d;
183 }
184
185 public int size() {
186 return lits.length;
187 }
188
189 public void assertConstraint(UnitPropagationListener s) {
190 boolean ret = s.enqueue(lits[0], this);
191 assert ret;
192 }
193
194 public ILits getVocabulary() {
195 return voc;
196 }
197
198 public int[] getLits() {
199 int[] tmp = new int[size()];
200 System.arraycopy(lits, 0, tmp, 0, size());
201 return tmp;
202 }
203
204 @Override
205 public boolean equals(Object obj) {
206 if (obj == null)
207 return false;
208 try {
209 WLClause wcl = (WLClause) obj;
210 if (lits.length != wcl.lits.length)
211 return false;
212 boolean ok;
213 for (int lit : lits) {
214 ok = false;
215 for (int lit2 : wcl.lits)
216 if (lit == lit2) {
217 ok = true;
218 break;
219 }
220 if (!ok)
221 return false;
222 }
223 return true;
224 } catch (ClassCastException e) {
225 return false;
226 }
227 }
228
229 @Override
230 public int hashCode() {
231 long sum = 0;
232 for (int p : lits) {
233 sum += p;
234 }
235 return (int) sum / lits.length;
236 }
237
238 }