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
59
60
61 public OPBStringSolver() {
62 }
63
64
65
66
67 public OPBStringSolver(int initSize) {
68 super(initSize);
69 }
70
71 public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
72 boolean moreThan, BigInteger d) throws ContradictionException {
73 StringBuffer out = getOut();
74 assert lits.size() == coeffs.size();
75 nbOfConstraints ++;
76 if (moreThan) {
77 for (int i = 0; i <lits.size();i++)
78 out.append(coeffs.get(i)+" x"+ lits.get(i) + " ");
79 out.append(">= "+d+" ;\n");
80 }
81 else {
82 for (int i = 0; i <lits.size();i++)
83 out.append(coeffs.get(i).negate()+" x"+ lits.get(i) + " ");
84 out.append(">= "+d.negate()+" ;\n");
85 }
86 return null;
87 }
88
89 public void setObjectiveFunction(ObjectiveFunction obj) {
90 StringBuffer out = getOut();
91 StringBuffer tmp = new StringBuffer();
92 tmp.append(" #constraint= "+nbOfConstraints+" \n");
93 tmp.append("min : ");
94 IVecInt lits = obj.getVars();
95 IVec<BigInteger> coeffs = obj.getCoeffs();
96 for (int i = 0; i <lits.size();i++)
97 tmp.append(coeffs.get(i)+" x"+ lits.get(i) + " ");
98 tmp.append(" ;\n");
99 out.insert(indxConstrObj,tmp);
100 }
101
102 @Override
103 public IConstr addAtLeast(IVecInt literals, int degree)
104 throws ContradictionException {
105 StringBuffer out = getOut();
106 nbOfConstraints++;
107 for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
108 out.append("+1 x"+iterator.next() + " ");
109 out.append(">= "+degree+" ;\n");
110 return null;
111 }
112
113 @Override
114 public IConstr addAtMost(IVecInt literals, int degree)
115 throws ContradictionException {
116 StringBuffer out = getOut();
117 nbOfConstraints++;
118 for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
119 out.append("-1 x"+iterator.next() + " ");
120 out.append(">= "+(-degree)+" ;\n");
121 return null;
122 }
123
124 @Override
125 public IConstr addClause(IVecInt literals) throws ContradictionException {
126 StringBuffer out = getOut();
127 nbOfConstraints++;
128 for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
129 out.append("+1 x"+iterator.next() + " ");
130 out.append(">= 1 ;\n");
131 return null;
132 }
133
134
135
136
137 public String getExplanation() {
138
139 return null;
140 }
141
142
143
144
145 public void setListOfVariablesForExplanation(IVecInt listOfVariables) {
146
147
148 }
149
150 @Override
151 public String toString(){
152 return getOut().toString();
153 }
154
155 @Override
156 public String toString(String prefix) {
157 return "OPB output solver";
158 }
159
160 @Override
161 public int newVar(int howmany) {
162 StringBuffer out = getOut();
163 out.append("* #variable= " + howmany);
164 setNbVars(howmany);
165
166 indxConstrObj = out.length();
167 return 0;
168 }
169
170 @Override
171 public void setExpectedNumberOfClauses(int nb) {
172 }
173
174
175
176 }