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.minisat.core.Constr;
36 import org.sat4j.pb.constraints.pb.IDataStructurePB;
37 import org.sat4j.specs.ContradictionException;
38 import org.sat4j.specs.IVec;
39 import org.sat4j.specs.IVecInt;
40
41 public abstract class AbstractPBClauseCardConstrDataStructure extends
42 AbstractPBDataStructureFactory {
43
44
45
46
47 private static final long serialVersionUID = 1L;
48
49 static final BigInteger MAX_INT_VALUE = BigInteger
50 .valueOf(Integer.MAX_VALUE);
51
52
53
54
55
56
57
58
59 @Override
60 protected Constr constraintFactory(int[] literals, BigInteger[] coefs,
61 BigInteger degree) throws ContradictionException {
62 if (degree.equals(BigInteger.ONE)) {
63 IVecInt v = Clauses.sanityCheck(new VecInt(literals),
64 getVocabulary(), solver);
65 if (v == null)
66 return null;
67 return constructClause(v);
68 }
69 if (coefficientsEqualToOne(coefs)) {
70 assert degree.compareTo(MAX_INT_VALUE) < 0;
71 return constructCard(new VecInt(literals), degree.intValue());
72 }
73 return constructPB(literals, coefs, degree);
74 }
75
76
77
78
79
80
81
82 @Override
83 protected Constr learntConstraintFactory(IDataStructurePB dspb) {
84 if (dspb.getDegree().equals(BigInteger.ONE)) {
85 IVecInt literals = new VecInt();
86 IVec<BigInteger> resCoefs = new Vec<BigInteger>();
87 dspb.buildConstraintFromConflict(literals, resCoefs);
88
89 int indLit = dspb.getAssertiveLiteral();
90 if (indLit > -1) {
91 int tmp = literals.get(indLit);
92 literals.set(indLit, literals.get(0));
93 literals.set(0, tmp);
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 return constructLearntClause(literals);
108 }
109 if (dspb.isCardinality()) {
110 return constructLearntCard(dspb);
111 }
112 return constructLearntPB(dspb);
113 }
114
115 static boolean coefficientsEqualToOne(BigInteger[] coefs) {
116 for (int i = 0; i < coefs.length; i++)
117 if (!coefs[i].equals(BigInteger.ONE))
118 return false;
119 return true;
120 }
121
122 abstract protected Constr constructClause(IVecInt v);
123
124 abstract protected Constr constructCard(IVecInt theLits, int degree)
125 throws ContradictionException;
126
127 abstract protected Constr constructPB(int[] theLits, BigInteger[] coefs,
128 BigInteger degree) throws ContradictionException;
129
130 abstract protected Constr constructLearntClause(IVecInt literals);
131
132 abstract protected Constr constructLearntCard(IDataStructurePB dspb);
133
134 abstract protected Constr constructLearntPB(IDataStructurePB dspb);
135
136 }