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.pb.constraints.pb;
29
30 import java.math.BigInteger;
31
32 import org.sat4j.minisat.constraints.card.MinWatchCard;
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.IVecInt;
37
38 public final class MinWatchCardPB extends MinWatchCard implements PBConstr {
39
40
41
42
43 private static final long serialVersionUID = 1L;
44
45 private final BigInteger bigDegree;
46
47 public MinWatchCardPB(ILits voc, IVecInt ps, boolean moreThan, int degree) {
48 super(voc, ps, moreThan, degree);
49
50 this.bigDegree = BigInteger.valueOf(this.degree);
51 }
52
53 public MinWatchCardPB(ILits voc, IVecInt ps, int degree) {
54 super(voc, ps, degree);
55 this.bigDegree = BigInteger.valueOf(this.degree);
56 }
57
58
59
60
61
62
63 public BigInteger getCoef(int literal) {
64 return BigInteger.ONE;
65 }
66
67
68
69
70
71
72 public BigInteger getDegree() {
73 return bigDegree;
74 }
75
76 public BigInteger[] getCoefs() {
77 BigInteger[] tmp = new BigInteger[size()];
78 for (int i = 0; i < tmp.length; i++)
79 tmp[i] = BigInteger.ONE;
80 return tmp;
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public static MinWatchCardPB normalizedMinWatchCardPBNew(
98 UnitPropagationListener s, ILits voc, IVecInt ps, int degree)
99 throws ContradictionException {
100 return minWatchCardPBNew(s, voc, ps, ATLEAST, degree, true);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public static MinWatchCardPB minWatchCardPBNew(UnitPropagationListener s,
120 ILits voc, IVecInt ps, boolean moreThan, int degree)
121 throws ContradictionException {
122 return minWatchCardPBNew(s, voc, ps, moreThan, degree, false);
123 }
124
125 private static MinWatchCardPB minWatchCardPBNew(UnitPropagationListener s,
126 ILits voc, IVecInt ps, boolean moreThan, int degree,
127 boolean normalized) throws ContradictionException {
128 int mydegree = degree + linearisation(voc, ps);
129
130 if (ps.size() == 0 && mydegree > 0) {
131 throw new ContradictionException();
132 } else if (ps.size() == mydegree || ps.size() <= 0) {
133 for (int i = 0; i < ps.size(); i++)
134 if (!s.enqueue(ps.get(i))) {
135 throw new ContradictionException();
136 }
137 return null;
138 }
139
140
141 MinWatchCardPB retour = null;
142 if (normalized)
143 retour = new MinWatchCardPB(voc, ps, mydegree);
144 else
145 retour = new MinWatchCardPB(voc, ps, moreThan, mydegree);
146
147 if (retour.bigDegree.signum() <= 0)
148 return null;
149
150 retour.computeWatches();
151
152 return ((MinWatchCardPB) retour.computePropagation(s));
153 }
154
155
156
157
158 private boolean learnt = false;
159
160
161
162
163
164
165
166 @Override
167 public boolean learnt() {
168 return learnt;
169 }
170
171 @Override
172 public void setLearnt() {
173 learnt = true;
174 }
175
176 @Override
177 public void register() {
178 assert learnt;
179 computeWatches();
180 }
181
182 @Override
183 public void assertConstraint(UnitPropagationListener s) {
184 for (int i = 0; i < size(); i++) {
185 if (getVocabulary().isUnassigned(get(i))) {
186 boolean ret = s.enqueue(get(i), this);
187 assert ret;
188 }
189 }
190 }
191
192 public IVecInt computeAnImpliedClause() {
193 return null;
194 }
195
196 }