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