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 |
| package org.sat4j.minisat.constraints.pb; |
27 |
| |
28 |
| import java.io.Serializable; |
29 |
| import java.math.BigInteger; |
30 |
| |
31 |
| import org.sat4j.core.Vec; |
32 |
| import org.sat4j.core.VecInt; |
33 |
| import org.sat4j.minisat.core.ILits; |
34 |
| import org.sat4j.minisat.core.UnitPropagationListener; |
35 |
| import org.sat4j.specs.ContradictionException; |
36 |
| import org.sat4j.specs.IVec; |
37 |
| import org.sat4j.specs.IVecInt; |
38 |
| |
39 |
| public class MaxWatchPb extends WatchPb implements Serializable { |
40 |
| |
41 |
| private static final long serialVersionUID = 1L; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
809035
| private MaxWatchPb(ILits voc, IVecInt ps, IVec<BigInteger> bigCoefs,
|
58 |
| boolean moreThan, BigInteger bigDeg) { |
59 |
| |
60 |
809035
| super(ps, bigCoefs, moreThan, bigDeg);
|
61 |
809035
| this.voc = voc;
|
62 |
| |
63 |
809035
| activity = 0;
|
64 |
809035
| watchCumul = BigInteger.ZERO;
|
65 |
809035
| locked = false;
|
66 |
| } |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
808947
| @Override
|
74 |
| protected void computeWatches() throws ContradictionException { |
75 |
| assert watchCumul.equals(BigInteger.ZERO); |
76 |
808947
| for (int i = 0; i < lits.length; i++) {
|
77 |
| |
78 |
4445701
| voc.watch(lits[i] ^ 1, this);
|
79 |
4445701
| watchCumul = watchCumul.add(coefs[i]);
|
80 |
| } |
81 |
| assert watchCumul.compareTo(recalcLeftSide()) >= 0; |
82 |
808947
| if (!learnt) {
|
83 |
779799
| if (watchCumul.compareTo(degree) < 0) {
|
84 |
16
| throw new ContradictionException("non satisfiable constraint");
|
85 |
| } |
86 |
| } |
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
779783
| @Override
|
95 |
| protected void computePropagation(UnitPropagationListener s) |
96 |
| throws ContradictionException { |
97 |
| |
98 |
779783
| int ind = 0;
|
99 |
779783
| while (ind < coefs.length
|
100 |
| && watchCumul.subtract(coefs[ind]).compareTo(degree) < 0) { |
101 |
274
| if (voc.isUnassigned(lits[ind])) {
|
102 |
209
| if (!s.enqueue(lits[ind], this)) {
|
103 |
0
| throw new ContradictionException(
|
104 |
| "non satisfiable constraint"); |
105 |
| } |
106 |
| } |
107 |
274
| ind++;
|
108 |
| } |
109 |
| assert watchCumul.compareTo(recalcLeftSide()) >= 0; |
110 |
| } |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
499336
| public static MaxWatchPb maxWatchPbNew(UnitPropagationListener s,
|
127 |
| ILits voc, IVecInt ps, IVecInt coefs, boolean moreThan, int degree) |
128 |
| throws ContradictionException { |
129 |
499336
| return maxWatchPbNew(s, voc, ps, toVecBigInt(coefs), moreThan,
|
130 |
| toBigInt(degree)); |
131 |
| } |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
779799
| public static MaxWatchPb maxWatchPbNew(UnitPropagationListener s,
|
148 |
| ILits voc, IVecInt ps, IVec<BigInteger> coefs, boolean moreThan, |
149 |
| BigInteger degree) throws ContradictionException { |
150 |
| |
151 |
| |
152 |
779799
| VecInt litsVec = new VecInt();
|
153 |
779799
| IVec<BigInteger> coefsVec = new Vec<BigInteger>();
|
154 |
779799
| ps.copyTo(litsVec);
|
155 |
779799
| coefs.copyTo(coefsVec);
|
156 |
| |
157 |
779799
| niceParameter(litsVec, coefsVec);
|
158 |
| |
159 |
779799
| MaxWatchPb outclause = new MaxWatchPb(voc, litsVec, coefsVec, moreThan,
|
160 |
| degree); |
161 |
| |
162 |
779799
| if (outclause.degree.signum() <= 0)
|
163 |
0
| return null;
|
164 |
779799
| outclause.computeWatches();
|
165 |
779783
| outclause.computePropagation(s);
|
166 |
| |
167 |
779783
| return outclause;
|
168 |
| |
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
260273874
| public boolean propagate(UnitPropagationListener s, int p) {
|
181 |
260273874
| voc.watch(p, this);
|
182 |
| |
183 |
| assert watchCumul.compareTo(recalcLeftSide()) >= 0; |
184 |
| |
185 |
| |
186 |
260273874
| int indiceP = 0;
|
187 |
260273874
| while ((lits[indiceP] ^ 1) != p)
|
188 |
1951799705
| indiceP++;
|
189 |
| |
190 |
260273874
| BigInteger coefP = coefs[indiceP];
|
191 |
| |
192 |
260273874
| if (watchCumul.subtract(coefP).compareTo(degree) < 0) {
|
193 |
| |
194 |
| |
195 |
| assert !isSatisfiable(); |
196 |
5214729
| return false;
|
197 |
| } |
198 |
| |
199 |
| |
200 |
255059145
| voc.undos(p).push(this);
|
201 |
255059145
| watchCumul = watchCumul.subtract(coefP);
|
202 |
| |
203 |
| |
204 |
255059145
| int ind = 0;
|
205 |
255059145
| while (ind < coefs.length
|
206 |
| && watchCumul.subtract(coefs[ind]).compareTo(degree) < 0) { |
207 |
701865992
| if (voc.isUnassigned(lits[ind])) {
|
208 |
170216664
| if (!s.enqueue(lits[ind], this)) {
|
209 |
| |
210 |
| |
211 |
| assert !isSatisfiable(); |
212 |
0
| return false;
|
213 |
| } |
214 |
| |
215 |
| |
216 |
| |
217 |
| } |
218 |
701865992
| ind++;
|
219 |
| } |
220 |
| |
221 |
| assert learnt || watchCumul.compareTo(recalcLeftSide()) >= 0; |
222 |
| assert watchCumul.compareTo(recalcLeftSide()) >= 0; |
223 |
255059145
| return true;
|
224 |
| } |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
31916
| public void remove() {
|
230 |
31916
| for (int i = 0; i < lits.length; i++) {
|
231 |
206495
| if (!voc.isFalsified(lits[i]))
|
232 |
176759
| voc.watches(lits[i] ^ 1).remove(this);
|
233 |
| } |
234 |
| } |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
254984678
| public void undo(int p) {
|
243 |
254984678
| int indiceP = 0;
|
244 |
254984678
| while ((lits[indiceP] ^ 1) != p)
|
245 |
1923729045
| indiceP++;
|
246 |
| |
247 |
| assert coefs[indiceP].signum() > 0; |
248 |
| |
249 |
254984678
| watchCumul = watchCumul.add(coefs[indiceP]);
|
250 |
| } |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
0
| public static WatchPb watchPbNew(ILits voc, IVecInt lits, IVecInt coefs,
|
256 |
| boolean moreThan, int degree) { |
257 |
0
| return new MaxWatchPb(voc, lits, toVecBigInt(coefs), moreThan,
|
258 |
| toBigInt(degree)); |
259 |
| } |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
29236
| public static WatchPb watchPbNew(ILits voc, IVecInt lits,
|
265 |
| IVec<BigInteger> coefs, boolean moreThan, BigInteger degree) { |
266 |
29236
| return new MaxWatchPb(voc, lits, coefs, moreThan, degree);
|
267 |
| } |
268 |
| } |