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;
29
30 import java.math.BigInteger;
31
32 import org.sat4j.core.Vec;
33 import org.sat4j.core.VecInt;
34 import org.sat4j.minisat.constraints.cnf.Clauses;
35 import org.sat4j.pb.constraints.pb.IDataStructurePB;
36 import org.sat4j.pb.constraints.pb.PBConstr;
37 import org.sat4j.pb.constraints.pb.Pseudos;
38 import org.sat4j.specs.ContradictionException;
39 import org.sat4j.specs.IVec;
40 import org.sat4j.specs.IVecInt;
41
42 public abstract class AbstractPBClauseCardConstrDataStructure extends
43 AbstractPBDataStructureFactory {
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50 static final BigInteger MAX_INT_VALUE = BigInteger
51 .valueOf(Integer.MAX_VALUE);
52
53
54
55
56
57
58
59 @Override
60 protected PBConstr constraintFactory(IVecInt literals, IVecInt coefs,
61 boolean moreThan, int degree) throws ContradictionException {
62 return constraintFactory(literals, Pseudos.toVecBigInt(coefs),
63 moreThan, BigInteger.valueOf(degree));
64 }
65
66
67
68
69
70
71
72 @Override
73 protected PBConstr constraintFactory(IVecInt literals, IVecInt coefs,
74 int degree) {
75 return constraintFactory(literals, Pseudos.toVecBigInt(coefs), BigInteger.valueOf(degree));
76 }
77
78
79
80
81
82
83
84 @Override
85 protected PBConstr constraintFactory(IVecInt literals,
86 IVec<BigInteger> coefs, boolean moreThan, BigInteger degree)
87 throws ContradictionException {
88 IDataStructurePB mpb = Pseudos.niceParameters(literals, coefs,
89 moreThan, degree, getVocabulary());
90 if (mpb == null)
91 return null;
92 int size = mpb.size();
93 int[] theLists = new int[size];
94 BigInteger[] normCoefs = new BigInteger[size];
95 mpb.buildConstraintFromMapPb(theLists, normCoefs);
96 if (mpb.getDegree().equals(BigInteger.ONE)) {
97 IVecInt v = Clauses.sanityCheck(new VecInt(theLists), getVocabulary(),
98 solver);
99 if (v == null)
100 return null;
101 return constructClause(v);
102 }
103 if (coefficientsEqualToOne(new Vec<BigInteger>(normCoefs))) {
104 assert mpb.getDegree().compareTo(MAX_INT_VALUE) < 0;
105 return constructCard(new VecInt(theLists), mpb.getDegree().intValue());
106 }
107
108 return constructPB(theLists,normCoefs,mpb.getDegree());
109 }
110
111
112
113
114
115
116
117 @Override
118 protected PBConstr constraintFactory(IDataStructurePB dspb) {
119 if (dspb.getDegree().equals(BigInteger.ONE)) {
120 return constructLearntClause(dspb);
121 }
122 if (dspb.isCardinality()) {
123 return constructLearntCard(dspb);
124 }
125 return constructLearntPB(dspb);
126 }
127
128
129
130
131
132
133
134 @Override
135 protected PBConstr constraintFactory(IVecInt literals,
136 IVec<BigInteger> coefs, BigInteger degree) {
137 if (degree.equals(BigInteger.ONE)) {
138 return constructLearntClause(literals);
139 }
140 if (coefficientsEqualToOne(coefs)) {
141 return constructLearntCard(literals, degree.intValue());
142 }
143 return constructLearntPB(literals, coefs, degree);
144 }
145
146
147
148 static boolean coefficientsEqualToOne(IVec<BigInteger> coeffs) {
149 for (int i = 0; i < coeffs.size(); i++)
150 if (!coeffs.get(i).equals(BigInteger.ONE))
151 return false;
152 return true;
153 }
154
155 abstract protected PBConstr constructClause(IVecInt v) throws ContradictionException;
156
157 abstract protected PBConstr constructCard(IVecInt theLits, int degree)
158 throws ContradictionException;
159
160 abstract protected PBConstr constructPB(IDataStructurePB mpb)
161 throws ContradictionException;
162
163 abstract protected PBConstr constructPB(int[] theLits, BigInteger[] coefs, BigInteger degree)
164 throws ContradictionException;
165
166 abstract protected PBConstr constructLearntClause(IVecInt literals);
167
168 abstract protected PBConstr constructLearntCard(IVecInt literals, int degree);
169
170 abstract protected PBConstr constructLearntPB(IVecInt literals,
171 IVec<BigInteger> coefs, BigInteger degree);
172
173 abstract protected PBConstr constructLearntClause(IDataStructurePB dspb);
174
175 abstract protected PBConstr constructLearntCard(IDataStructurePB dspb);
176
177 abstract protected PBConstr constructLearntPB(IDataStructurePB dspb);
178
179
180 }