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.lang.reflect.Field;
31 import java.math.BigInteger;
32
33 import org.sat4j.minisat.constraints.AbstractDataStructureFactory;
34 import org.sat4j.minisat.constraints.cnf.Clauses;
35 import org.sat4j.minisat.constraints.cnf.LearntBinaryClause;
36 import org.sat4j.minisat.constraints.cnf.LearntHTClause;
37 import org.sat4j.minisat.constraints.cnf.Lits;
38 import org.sat4j.minisat.constraints.cnf.OriginalBinaryClause;
39 import org.sat4j.minisat.constraints.cnf.OriginalHTClause;
40 import org.sat4j.minisat.constraints.cnf.UnitClause;
41 import org.sat4j.minisat.core.Constr;
42 import org.sat4j.minisat.core.ILits;
43 import org.sat4j.pb.constraints.pb.AtLeastPB;
44 import org.sat4j.pb.constraints.pb.IDataStructurePB;
45 import org.sat4j.pb.constraints.pb.Pseudos;
46 import org.sat4j.pb.core.PBDataStructureFactory;
47 import org.sat4j.specs.ContradictionException;
48 import org.sat4j.specs.IVec;
49 import org.sat4j.specs.IVecInt;
50
51
52
53
54
55 public abstract class AbstractPBDataStructureFactory extends
56 AbstractDataStructureFactory implements PBDataStructureFactory {
57
58 interface INormalizer {
59 PBContainer nice(IVecInt ps, IVec<BigInteger> bigCoefs,
60 boolean moreThan, BigInteger bigDeg, ILits voc)
61 throws ContradictionException;
62 }
63
64 public static final INormalizer FOR_COMPETITION = new INormalizer() {
65
66 private static final long serialVersionUID = 1L;
67
68 public PBContainer nice(IVecInt literals, IVec<BigInteger> coefs,
69 boolean moreThan, BigInteger degree, ILits voc)
70 throws ContradictionException {
71 int[] theLits = new int[literals.size()];
72 literals.copyTo(theLits);
73 BigInteger[] normCoefs = new BigInteger[coefs.size()];
74 coefs.copyTo(normCoefs);
75 BigInteger degRes = Pseudos.niceParametersForCompetition(theLits,
76 normCoefs, moreThan, degree);
77 return new PBContainer(theLits, normCoefs, degRes);
78
79 }
80
81 };
82
83 public static final INormalizer NO_COMPETITION = new INormalizer() {
84
85 private static final long serialVersionUID = 1L;
86
87 public PBContainer nice(IVecInt literals, IVec<BigInteger> coefs,
88 boolean moreThan, BigInteger degree, ILits voc)
89 throws ContradictionException {
90 IDataStructurePB res = Pseudos.niceParameters(literals, coefs,
91 moreThan, degree, voc);
92 int size = res.size();
93 int[] theLits = new int[size];
94 BigInteger[] theCoefs = new BigInteger[size];
95 res.buildConstraintFromMapPb(theLits, theCoefs);
96 BigInteger theDegree = res.getDegree();
97 return new PBContainer(theLits, theCoefs, theDegree);
98 }
99 };
100
101 private INormalizer norm = FOR_COMPETITION;
102
103 protected INormalizer getNormalizer() {
104 return norm;
105 }
106
107 public void setNormalizer(String simp) {
108 Field f;
109 try {
110 f = AbstractPBDataStructureFactory.class.getDeclaredField(simp);
111 norm = (INormalizer) f.get(this);
112 } catch (Exception e) {
113 e.printStackTrace();
114 norm = FOR_COMPETITION;
115 }
116 }
117
118 public void setNormalizer(INormalizer normalizer) {
119 norm = normalizer;
120 }
121
122
123
124
125 private static final long serialVersionUID = 1L;
126
127 public Constr createClause(IVecInt literals) throws ContradictionException {
128 IVecInt v = Clauses.sanityCheck(literals, getVocabulary(), solver);
129 if (v == null)
130 return null;
131 if (v.size() == 2) {
132 return OriginalBinaryClause.brandNewClause(solver, getVocabulary(),
133 v);
134 }
135 return OriginalHTClause.brandNewClause(solver, getVocabulary(), v);
136 }
137
138 public Constr createUnregisteredClause(IVecInt literals) {
139 if (literals.size() == 1) {
140 return new UnitClause(literals.last());
141 }
142 if (literals.size() == 2) {
143 return new LearntBinaryClause(literals, getVocabulary());
144 }
145 return new LearntHTClause(literals, getVocabulary());
146 }
147
148 @Override
149 public Constr createCardinalityConstraint(IVecInt literals, int degree)
150 throws ContradictionException {
151 return AtLeastPB.atLeastNew(solver, getVocabulary(), literals, degree);
152 }
153
154 public Constr createPseudoBooleanConstraint(IVecInt literals,
155 IVec<BigInteger> coefs, boolean moreThan, BigInteger degree)
156 throws ContradictionException {
157 PBContainer res = getNormalizer().nice(literals, coefs, moreThan,
158 degree, getVocabulary());
159 return constraintFactory(res.lits, res.coefs, res.degree);
160 }
161
162 public Constr createUnregisteredPseudoBooleanConstraint(
163 IDataStructurePB dspb) {
164 return learntConstraintFactory(dspb);
165 }
166
167 protected abstract Constr constraintFactory(int[] literals,
168 BigInteger[] coefs, BigInteger degree)
169 throws ContradictionException;
170
171 protected abstract Constr learntConstraintFactory(IDataStructurePB dspb);
172
173 @Override
174 protected ILits createLits() {
175 return new Lits();
176 }
177
178 }