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.tools.DimacsStringSolver;
38
39
40
41
42
43
44
45
46
47 public class OPBStringSolver extends DimacsStringSolver implements 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
82
83
84 public OPBStringSolver() {
85 }
86
87
88
89
90 public OPBStringSolver(int initSize) {
91 super(initSize);
92 }
93
94 public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
95 boolean moreThan, BigInteger d) throws ContradictionException {
96 StringBuffer out = getOut();
97 assert lits.size() == coeffs.size();
98 nbOfConstraints++;
99 if (moreThan) {
100 for (int i = 0; i < lits.size(); i++)
101 out.append(coeffs.get(i) + " x" + lits.get(i) + " ");
102 out.append(">= " + d + " ;\n");
103 } else {
104 for (int i = 0; i < lits.size(); i++)
105 out.append(coeffs.get(i).negate() + " x" + lits.get(i) + " ");
106 out.append(">= " + d.negate() + " ;\n");
107 }
108 return FAKE_CONSTR;
109 }
110
111 public void setObjectiveFunction(ObjectiveFunction obj) {
112 this.obj = obj;
113 }
114
115 @Override
116 public IConstr addAtLeast(IVecInt literals, int degree)
117 throws ContradictionException {
118 StringBuffer out = getOut();
119 nbOfConstraints++;
120 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();)
121 out.append("+1 x" + iterator.next() + " ");
122 out.append(">= " + degree + " ;\n");
123 return FAKE_CONSTR;
124 }
125
126 @Override
127 public IConstr addAtMost(IVecInt literals, int degree)
128 throws ContradictionException {
129 StringBuffer out = getOut();
130 nbOfConstraints++;
131 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();)
132 out.append("-1 x" + iterator.next() + " ");
133 out.append(">= " + (-degree) + " ;\n");
134 return FAKE_CONSTR;
135 }
136
137 @Override
138 public IConstr addClause(IVecInt literals) throws ContradictionException {
139 StringBuffer out = getOut();
140 nbOfConstraints++;
141 int lit;
142 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
143 lit = iterator.next();
144 if (lit > 0) {
145 out.append("+1 x" + lit + " ");
146 } else {
147 out.append("+1 ~x" + -lit + " ");
148 }
149 }
150 out.append(">= 1 ;\n");
151 return FAKE_CONSTR;
152 }
153
154
155
156
157
158
159 public String getExplanation() {
160
161 return null;
162 }
163
164
165
166
167
168
169
170
171 public void setListOfVariablesForExplanation(IVecInt listOfVariables) {
172
173
174 }
175
176 @Override
177 public String toString() {
178 StringBuffer out = getOut();
179 if (!inserted) {
180 StringBuffer tmp = new StringBuffer();
181 tmp.append("* #variable= " + nVars());
182 tmp.append(" #constraint= " + nbOfConstraints + " \n");
183 if (obj != null) {
184 tmp.append("min: ");
185 tmp.append(obj);
186 tmp.append(" ;\n");
187 }
188 out.insert(indxConstrObj, tmp.toString());
189 inserted = true;
190 }
191 return out.toString();
192 }
193
194 @Override
195 public String toString(String prefix) {
196 return "OPB output solver";
197 }
198
199 @Override
200 public int newVar(int howmany) {
201 StringBuffer out = getOut();
202 setNbVars(howmany);
203
204 indxConstrObj = out.length();
205 out.append("\n");
206 return howmany;
207 }
208
209 @Override
210 public void setExpectedNumberOfClauses(int nb) {
211 }
212
213 public ObjectiveFunction getObjectiveFunction() {
214 return obj;
215 }
216
217 }