| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
package org.sat4j.reader; |
| 5 |
|
|
| 6 |
|
import java.io.IOException; |
| 7 |
|
import java.math.BigInteger; |
| 8 |
|
import java.util.HashMap; |
| 9 |
|
import java.util.Map; |
| 10 |
|
|
| 11 |
|
import org.sat4j.core.Vec; |
| 12 |
|
import org.sat4j.core.VecInt; |
| 13 |
|
import org.sat4j.specs.ContradictionException; |
| 14 |
|
import org.sat4j.specs.ISolver; |
| 15 |
|
import org.sat4j.specs.IVec; |
| 16 |
|
import org.sat4j.specs.IVecInt; |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
@author |
| 20 |
|
|
| 21 |
|
|
|
|
|
| 0% |
Uncovered Elements: 127 (127) |
Complexity: 18 |
Complexity Density: 0,33 |
|
| 22 |
|
public class OPBReader2007 extends OPBReader2006 { |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
private static final long serialVersionUID = 1L; |
| 29 |
|
|
| 30 |
|
private int nbProducts, productSize; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
@param |
| 36 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 37 |
0
|
public OPBReader2007(ISolver solver) {... |
| 38 |
0
|
super(solver); |
| 39 |
|
|
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 43 |
0
|
@Override... |
| 44 |
|
protected boolean isGoodFirstCharacter(char c){ |
| 45 |
0
|
return Character.isLetter(c) || c == '_' || c=='~'; |
| 46 |
|
} |
| 47 |
|
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0,5 |
|
| 48 |
0
|
@Override... |
| 49 |
|
protected void checkId(StringBuffer s) throws ParseFormatException{ |
| 50 |
|
|
| 51 |
0
|
int cpt = 1; |
| 52 |
0
|
if (s.charAt(0)=='~') cpt=2; |
| 53 |
0
|
int varID = Integer.parseInt(s.substring(cpt)); |
| 54 |
0
|
if (varID > nbVars) { |
| 55 |
0
|
throw new ParseFormatException( |
| 56 |
|
"Variable identifier larger than #variables in metadata."); |
| 57 |
|
} |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
private int nbNewSymbols; |
| 64 |
|
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 4 |
Complexity Density: 0,29 |
|
| 65 |
0
|
@Override... |
| 66 |
|
protected void readTerm(StringBuffer coeff, StringBuffer var) |
| 67 |
|
throws IOException, ParseFormatException, ContradictionException { |
| 68 |
0
|
readInteger(coeff); |
| 69 |
|
|
| 70 |
0
|
skipSpaces(); |
| 71 |
|
|
| 72 |
0
|
var.setLength(0); |
| 73 |
0
|
IVec<String> tmpLit = new Vec<String>(); |
| 74 |
0
|
StringBuffer tmpVar = new StringBuffer(); |
| 75 |
0
|
while (readIdentifier(tmpVar)){ |
| 76 |
0
|
tmpLit = tmpLit.push(tmpVar.toString()); |
| 77 |
0
|
skipSpaces(); |
| 78 |
|
} |
| 79 |
0
|
if (tmpLit.size() == 0) |
| 80 |
0
|
throw new ParseFormatException("identifier expected"); |
| 81 |
0
|
if (tmpLit.size() == 1){ |
| 82 |
|
|
| 83 |
0
|
var.append(tmpLit.last()); |
| 84 |
0
|
tmpLit.pop(); |
| 85 |
|
} |
| 86 |
|
else { |
| 87 |
|
|
| 88 |
0
|
var.append(linearizeProduct(tmpLit)); |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
@param |
| 96 |
|
|
| 97 |
|
@param |
| 98 |
|
|
| 99 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0,25 |
|
| 100 |
0
|
protected void literalInAProduct(String var, IVecInt lits) {... |
| 101 |
0
|
int beginning = ((var.charAt(0) == '~') ? 2 : 1 ); |
| 102 |
0
|
int id = Integer.parseInt(var.substring(beginning)); |
| 103 |
0
|
int lid = ((var.charAt(0) == '~') ? -1 : 1) * id; |
| 104 |
0
|
lits.push(lid); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
@param |
| 111 |
|
|
| 112 |
|
@param |
| 113 |
|
|
| 114 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0,25 |
|
| 115 |
0
|
protected void negateLiteralInAProduct(String var, IVecInt lits) {... |
| 116 |
0
|
int beginning = ((var.charAt(0) == '~') ? 2 : 1 ); |
| 117 |
0
|
int id = Integer.parseInt(var.substring(beginning)); |
| 118 |
0
|
int lid = ((var.charAt(0) == '~') ? 1 : -1) * id; |
| 119 |
0
|
lits.push(lid); |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
@throws |
| 128 |
|
@throws |
| 129 |
|
|
|
|
|
| 0% |
Uncovered Elements: 34 (34) |
Complexity: 8 |
Complexity Density: 0,33 |
|
| 130 |
0
|
@Override... |
| 131 |
|
protected void readMetaData() throws IOException, ParseFormatException { |
| 132 |
0
|
char c; |
| 133 |
0
|
String s; |
| 134 |
|
|
| 135 |
|
|
| 136 |
0
|
c = get(); |
| 137 |
0
|
if (c != '*') |
| 138 |
0
|
throw new ParseFormatException( |
| 139 |
|
"First line of input file should be a comment"); |
| 140 |
|
|
| 141 |
0
|
s = readWord(); |
| 142 |
0
|
if (eof() || !"#variable=".equals(s)) |
| 143 |
0
|
throw new ParseFormatException( |
| 144 |
|
"First line should contain #variable= as first keyword"); |
| 145 |
|
|
| 146 |
0
|
nbVars = Integer.parseInt(readWord()); |
| 147 |
0
|
nbNewSymbols = nbVars+1; |
| 148 |
|
|
| 149 |
0
|
s = readWord(); |
| 150 |
0
|
if (eof() || !"#constraint=".equals(s)) |
| 151 |
0
|
throw new ParseFormatException( |
| 152 |
|
"First line should contain #constraint= as second keyword"); |
| 153 |
|
|
| 154 |
0
|
nbConstr = Integer.parseInt(readWord()); |
| 155 |
|
|
| 156 |
0
|
s = readWord(); |
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
0
|
if (s.equals("#product=")) { |
| 161 |
0
|
nbProducts = Integer.parseInt(readWord()); |
| 162 |
0
|
nbVars += nbProducts; |
| 163 |
0
|
nbConstr += 2*nbProducts; |
| 164 |
|
} |
| 165 |
|
|
| 166 |
0
|
s = readWord(); |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
0
|
if (s.equals("sizeproduct=")) |
| 172 |
0
|
productSize = Integer.parseInt(readWord()); |
| 173 |
|
|
| 174 |
|
|
| 175 |
0
|
in.readLine(); |
| 176 |
|
|
| 177 |
|
|
| 178 |
0
|
metaData(nbVars, nbConstr); |
| 179 |
|
} |
| 180 |
|
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 181 |
0
|
@Override... |
| 182 |
|
protected int translateVarToId(String var) { |
| 183 |
0
|
int beginning = ((var.charAt(0) == '~') ? 2 : 1 ); |
| 184 |
0
|
int id = Integer.parseInt(var.substring(beginning)); |
| 185 |
0
|
return ((var.charAt(0) == '~') ? -1 : 1) * id; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0,2 |
|
| 189 |
0
|
private String linearizeProduct(IVec<String> tmpLit) ... |
| 190 |
|
throws ContradictionException { |
| 191 |
0
|
tmpLit.sort(String.CASE_INSENSITIVE_ORDER); |
| 192 |
0
|
String newVar = getProductVariable(tmpLit); |
| 193 |
0
|
if (newVar == null) { |
| 194 |
|
|
| 195 |
0
|
newVar = "X" + String.valueOf(nbNewSymbols++); |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
0
|
productStore.put(newVar,tmpLit); |
| 201 |
0
|
IVecInt newLits = new VecInt(); |
| 202 |
0
|
for(String lit : tmpLit) |
| 203 |
0
|
negateLiteralInAProduct(lit,newLits); |
| 204 |
0
|
literalInAProduct(newVar.toString(),newLits); |
| 205 |
0
|
solver.addClause(newLits); |
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
0
|
newLits.clear(); |
| 210 |
0
|
IVec<BigInteger> newCoefs = new Vec<BigInteger>(); |
| 211 |
0
|
for(String lit : tmpLit){ |
| 212 |
0
|
literalInAProduct(lit,newLits); |
| 213 |
0
|
newCoefs.push(BigInteger.ONE); |
| 214 |
|
} |
| 215 |
0
|
literalInAProduct(newVar.toString(),newLits); |
| 216 |
0
|
newCoefs.push(new BigInteger(String.valueOf(-tmpLit.size()))); |
| 217 |
0
|
solver.addPseudoBoolean(newLits, newCoefs, true, BigInteger.ZERO); |
| 218 |
0
|
nbConstraintsRead += 2; |
| 219 |
|
} |
| 220 |
0
|
return newVar; |
| 221 |
|
} |
| 222 |
|
|
| 223 |
|
private Map<String,IVec<String>> productStore = new HashMap<String,IVec<String>>(); |
| 224 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0,75 |
|
| 225 |
0
|
private String getProductVariable(IVec<String> lits){... |
| 226 |
0
|
for(Map.Entry<String,IVec<String>> c : productStore.entrySet()) |
| 227 |
0
|
if (c.getValue().equals(lits)) |
| 228 |
0
|
return c.getKey(); |
| 229 |
0
|
return null; |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
|
| 233 |
|
} |