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