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;
29
30 import java.math.BigInteger;
31 import java.util.Map;
32
33 import org.sat4j.specs.ContradictionException;
34 import org.sat4j.specs.IConstr;
35 import org.sat4j.specs.IVec;
36 import org.sat4j.specs.IVecInt;
37 import org.sat4j.specs.IteratorInt;
38 import org.sat4j.tools.DimacsStringSolver;
39
40
41
42
43
44
45
46 public class UserFriendlyPBStringSolver<T> extends DimacsStringSolver implements
47 IPBSolver {
48
49 private static final String FAKE_I_CONSTR_MSG = "Fake IConstr";
50
51
52
53
54 private static final long serialVersionUID = 1L;
55
56 private int indxConstrObj;
57
58 private int nbOfConstraints;
59
60 private ObjectiveFunction obj;
61
62 private boolean inserted = false;
63
64 private static final IConstr FAKE_CONSTR = new IConstr() {
65
66 public int size() {
67 throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
68 }
69
70 public boolean learnt() {
71 throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
72 }
73
74 public double getActivity() {
75 throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
76 }
77
78 public int get(int i) {
79 throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
80 }
81 };
82
83 private Map<Integer, T> mapping;
84
85
86
87
88 public UserFriendlyPBStringSolver() {
89 }
90
91
92
93
94 public UserFriendlyPBStringSolver(int initSize) {
95 super(initSize);
96 }
97
98 public void setMapping(Map<Integer, T> mapping) {
99 this.mapping = mapping;
100 }
101
102 public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
103 boolean moreThan, BigInteger d) throws ContradictionException {
104 StringBuffer out = getOut();
105 assert lits.size() == coeffs.size();
106 nbOfConstraints++;
107 if (moreThan) {
108 for (int i = 0; i < lits.size(); i++) {
109 out.append(coeffs.get(i) + " " + mapping.get(lits.get(i))
110 + " + ");
111 }
112 out.append(">= " + d + " ;\n");
113 } else {
114 for (int i = 0; i < lits.size(); i++) {
115 out.append(coeffs.get(i) + " " + mapping.get(lits.get(i))
116 + " + ");
117 }
118 out.append("<= " + d + " ;\n");
119 }
120 return FAKE_CONSTR;
121 }
122
123 public void setObjectiveFunction(ObjectiveFunction obj) {
124 this.obj = obj;
125 }
126
127 @Override
128 public IConstr addAtLeast(IVecInt literals, int degree)
129 throws ContradictionException {
130 StringBuffer out = getOut();
131 nbOfConstraints++;
132 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
133 out.append(mapping.get(iterator.next()));
134 out.append(" ");
135 if (iterator.hasNext()) {
136 out.append("+ ");
137 }
138 }
139 out.append(">= " + degree + " ;\n");
140 return FAKE_CONSTR;
141 }
142
143 @Override
144 public IConstr addAtMost(IVecInt literals, int degree)
145 throws ContradictionException {
146 StringBuffer out = getOut();
147 nbOfConstraints++;
148 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
149 out.append(mapping.get(iterator.next()));
150 out.append(" ");
151 if (iterator.hasNext()) {
152 out.append("+ ");
153 }
154 }
155 out.append("<= " + degree + " ;\n");
156 return FAKE_CONSTR;
157 }
158
159 @Override
160 public IConstr addClause(IVecInt literals) throws ContradictionException {
161 StringBuffer out = getOut();
162 nbOfConstraints++;
163 int lit;
164 boolean beginning = true;
165 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
166 lit = iterator.next();
167 if (lit > 0) {
168 if (beginning) {
169 out.append("-> ");
170 beginning = false;
171 }
172 out.append(mapping.get(lit));
173 } else {
174 out.append(mapping.get(-lit));
175 }
176 out.append(" ");
177 if (iterator.hasNext() && !beginning) {
178 out.append("OR ");
179 }
180 }
181 if (beginning) {
182 out.append("-> false");
183 }
184 out.append(" ;\n");
185 return FAKE_CONSTR;
186 }
187
188
189
190
191
192
193 public String getExplanation() {
194
195 return null;
196 }
197
198
199
200
201
202
203
204
205 public void setListOfVariablesForExplanation(IVecInt listOfVariables) {
206
207
208 }
209
210 @Override
211 public String toString() {
212 StringBuffer out = getOut();
213 if (!inserted) {
214 StringBuffer tmp = new StringBuffer();
215 tmp.append("* #variable= ");
216 tmp.append(nVars());
217 tmp.append(" #constraint= ");
218 tmp.append(nbOfConstraints);
219 tmp.append(" \n");
220 if (obj != null) {
221 tmp.append("min: ");
222 tmp.append(decode(obj));
223 tmp.append(" ;\n");
224 }
225 out.insert(indxConstrObj, tmp.toString());
226 inserted = true;
227 }
228 return out.toString();
229 }
230
231 private Object decode(ObjectiveFunction obj2) {
232 StringBuffer stb = new StringBuffer();
233 IVecInt vars = obj2.getVars();
234 IVec<BigInteger> coeffs = obj2.getCoeffs();
235 int lit;
236 for (int i = 0; i < vars.size(); i++) {
237 stb.append(coeffs.get(i));
238 stb.append(" ");
239 lit = vars.get(i);
240 if (lit > 0) {
241 stb.append(mapping.get(lit));
242 } else {
243 stb.append("~");
244 stb.append(mapping.get(-lit));
245 }
246 stb.append(" ");
247 }
248
249 return stb.toString();
250 }
251
252 @Override
253 public String toString(String prefix) {
254 return "OPB output solver";
255 }
256
257 @Override
258 public int newVar(int howmany) {
259 StringBuffer out = getOut();
260 setNbVars(howmany);
261
262 indxConstrObj = out.length();
263 out.append("\n");
264 return howmany;
265 }
266
267 @Override
268 public void setExpectedNumberOfClauses(int nb) {
269 }
270
271 public ObjectiveFunction getObjectiveFunction() {
272 return obj;
273 }
274
275 }