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