use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class SATTest method readCNF.
private void readCNF(final SATSolver solver, final File file) throws IOException {
solver.reset();
final BufferedReader reader = new BufferedReader(new FileReader(file));
while (reader.ready()) {
final String line = reader.readLine();
if (line.startsWith("p cnf")) {
break;
}
}
String[] tokens;
final List<Literal> literals = new ArrayList<>();
while (reader.ready()) {
tokens = reader.readLine().split("\\s+");
if (tokens.length >= 2) {
assert "0".equals(tokens[tokens.length - 1]);
literals.clear();
for (int i = 0; i < tokens.length - 1; i++) {
if (!tokens[i].isEmpty()) {
final int parsedLit = Integer.parseInt(tokens[i]);
final String var = "v" + Math.abs(parsedLit);
literals.add(parsedLit > 0 ? this.f.literal(var, true) : this.f.literal(var, false));
}
}
if (!literals.isEmpty()) {
solver.add(this.f.or(literals));
}
}
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class SATTest method testPBC.
@Test
public void testPBC() {
for (final SATSolver s : this.solvers) {
final List<Literal> lits = new ArrayList<>();
final List<Integer> coeffs = new ArrayList<>();
for (int i = 0; i < 5; i++) {
lits.add(this.f.literal("x" + i, i % 2 == 0));
coeffs.add(i + 1);
}
s.add(this.f.pbc(CType.GE, 10, lits, coeffs));
assertSolverSat(s);
s.reset();
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PBEncoderTest method testCC0.
@Test
public void testCC0() {
for (final PBEncoder encoder : this.encoders) {
final int numLits = 100;
final List<Literal> lits = new ArrayList<>();
final List<Integer> coeffs = new ArrayList<>();
final Variable[] problemLits = new Variable[numLits];
for (int i = 0; i < numLits; i++) {
final Variable var = this.f.variable("v" + i);
lits.add(var);
problemLits[i] = var;
coeffs.add(1);
}
final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, 0, lits, coeffs));
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(clauses);
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(problemLits)).hasSize(1).allMatch(m -> m.positiveVariables().isEmpty());
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PBEncoderTest method testCCNormalized.
@Test
public void testCCNormalized() throws ParserException {
final List<Literal> lits = new ArrayList<>();
lits.add(this.f.literal("m", true));
lits.add(this.f.literal("n", true));
final List<Integer> coeffs2 = new ArrayList<>();
coeffs2.add(2);
coeffs2.add(2);
final PBConstraint normCC = (PBConstraint) this.f.pbc(CType.LE, 2, lits, coeffs2);
assertThat(this.encoders[0].encode(normCC)).containsExactly(this.f.parse("~m | ~n"));
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PBEncoderTest method testSpecialCases.
@Test
public void testSpecialCases() {
final List<Literal> lits = new ArrayList<>();
lits.add(this.f.literal("m", true));
lits.add(this.f.literal("n", true));
final List<Integer> coeffs = new ArrayList<>();
coeffs.add(2);
coeffs.add(1);
final PBConstraint truePBC = (PBConstraint) this.f.pbc(CType.GE, 0, lits, coeffs);
for (final PBEncoder encoder : this.encoders) {
assertThat(encoder.encode(truePBC)).isEmpty();
}
}
Aggregations