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