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