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