use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class SATTest method testFormulaOnSolver.
@Test
public void testFormulaOnSolver() throws ParserException {
for (final SATSolver solver : this.solvers) {
if (solver instanceof MiniSat) {
final PseudoBooleanParser p = new PseudoBooleanParser(this.f);
final Set<Formula> formulas = new LinkedHashSet<>();
formulas.add(p.parse("A | B | C"));
formulas.add(p.parse("~A | ~B | ~C"));
formulas.add(p.parse("A | ~B"));
formulas.add(p.parse("A"));
solver.add(formulas);
compareFormulas(formulas, solver.execute(FormulaOnSolverFunction.get()));
formulas.add(p.parse("~A | C"));
solver.reset();
solver.add(formulas);
compareFormulas(formulas, solver.execute(FormulaOnSolverFunction.get()));
final Formula formula = p.parse("C + D + E <= 2");
formulas.add(formula);
solver.add(formula);
compareFormulas(formulas, solver.execute(FormulaOnSolverFunction.get()));
}
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class SATTest method testUPZeroLiterals.
@Test
public void testUPZeroLiterals() throws ParserException {
// Note: The complete unit propagated set of literals on level 0 depends on each solver's added learned clauses during the solving process
final Map<Formula, SortedSet<Literal>> expectedSubsets = new HashMap<>();
expectedSubsets.put(this.f.verum(), new TreeSet<>());
expectedSubsets.put(this.parser.parse("a"), new TreeSet<>(Collections.singletonList(this.f.literal("a", true))));
expectedSubsets.put(this.parser.parse("a | b"), new TreeSet<>());
expectedSubsets.put(this.parser.parse("a & b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", true))));
expectedSubsets.put(this.parser.parse("a & ~b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", false))));
expectedSubsets.put(this.parser.parse("(a | c) & ~b"), new TreeSet<>(Collections.singletonList(this.f.literal("b", false))));
expectedSubsets.put(this.parser.parse("(b | c) & ~b & (~c | d)"), new TreeSet<>(Arrays.asList(this.f.literal("b", false), this.f.literal("c", true), this.f.literal("d", true))));
for (final SATSolver solver : this.solvers) {
for (final Formula formula : expectedSubsets.keySet()) {
solver.reset();
solver.add(formula);
final boolean res = solver.sat() == Tristate.TRUE;
assertThat(res).isTrue();
final SortedSet<Literal> upLiterals = solver.execute(UpZeroLiteralsFunction.get());
assertThat(upLiterals).containsAll(expectedSubsets.get(formula));
}
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class SATTest method testAnd3.
@Test
public void testAnd3() {
for (final SATSolver s : this.solvers) {
final List<Formula> formulas = new ArrayList<>(3);
formulas.add(this.f.literal("a", true));
formulas.add(this.f.literal("b", false));
formulas.add(this.f.literal("a", false));
formulas.add(this.f.literal("d", false));
s.add(formulas);
assertSolverUnsat(s);
s.reset();
}
}
use of org.logicng.formulas.Formula 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.Formula in project LogicNG by logic-ng.
the class PBEncoderTest method testCC.
private void testCC(final int numLits, final int rhs, final int expected, final PBEncoder encoder) {
final Variable[] problemLits = new Variable[numLits];
final int[] coeffs = new int[numLits];
for (int i = 0; i < numLits; i++) {
problemLits[i] = this.f.variable("v" + i);
coeffs[i] = 1;
}
final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, rhs, problemLits, coeffs));
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(clauses);
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(problemLits)).hasSize(expected).allMatch(m -> m.positiveVariables().size() <= rhs);
}
Aggregations