use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PseudoBooleanParserTest method testNumberLiterals.
@Test
public void testNumberLiterals() throws ParserException {
final FormulaFactory f = new FormulaFactory();
final PseudoBooleanParser parser = new PseudoBooleanParser(f);
assertThat(parser.parse("12 & A")).isEqualTo(f.and(f.variable("12"), f.variable("A")));
assertThat(parser.parse("~12 & A")).isEqualTo(f.and(f.literal("12", false), f.variable("A")));
assertThat(parser.parse("12 * 12 + 13 * A + 10 * B <= 25")).isEqualTo(f.pbc(CType.LE, 25, new Literal[] { f.variable("12"), f.variable("A"), f.variable("B") }, new int[] { 12, 13, 10 }));
assertThat(parser.parse("-12 * ~12 + 13 * A + 10 * B <= 25")).isEqualTo(f.pbc(CType.LE, 25, new Literal[] { f.literal("12", false), f.variable("A"), f.variable("B") }, new int[] { -12, 13, 10 }));
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PrimeCompilerTest method verifyImplicates.
private void verifyImplicates(final List<SortedSet<Literal>> implicateSets, final Formula formula) {
final FormulaFactory f = formula.factory();
final List<Formula> implicates = new ArrayList<>();
for (final SortedSet<Literal> implicate : implicateSets) {
implicates.add(f.or(implicate));
PrimeImplicateReductionTest.testPrimeImplicateProperty(formula, implicate);
}
assertThat(f.equivalence(f.and(implicates), formula).holds(new TautologyPredicate(f))).as("Conjunction of implicates should be equivalent to the original formula.").isTrue();
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PrimeCompilerTest method verifyImplicants.
private void verifyImplicants(final List<SortedSet<Literal>> implicantSets, final Formula formula) {
final FormulaFactory f = formula.factory();
final List<Formula> implicants = new ArrayList<>();
for (final SortedSet<Literal> implicant : implicantSets) {
implicants.add(f.and(implicant));
PrimeImplicantReductionTest.testPrimeImplicantProperty(formula, implicant);
}
assertThat(f.equivalence(f.or(implicants), formula).holds(new TautologyPredicate(f))).as("Disjunction of implicants should be equivalent to the original formula.").isTrue();
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PrimeImplicateReductionTest method testPrimeImplicateProperty.
public static void testPrimeImplicateProperty(final Formula formula, final SortedSet<Literal> primeImplicate) {
final FormulaFactory f = formula.factory();
final MiniSat solver = MiniSat.miniSat(f);
solver.add(formula);
final SortedSet<Literal> negatedLiterals = FormulaHelper.negateLiterals(primeImplicate, TreeSet::new);
assertThat(solver.sat(negatedLiterals)).isEqualTo(Tristate.FALSE);
for (final Literal lit : negatedLiterals) {
final SortedSet<Literal> reducedNegatedLiterals = new TreeSet<>(negatedLiterals);
reducedNegatedLiterals.remove(lit);
assertThat(solver.sat(reducedNegatedLiterals)).isEqualTo(Tristate.TRUE);
}
}
use of org.logicng.formulas.Literal in project LogicNG by logic-ng.
the class PrimeImplicateReductionTest method testFormula.
private void testFormula(final Formula formula, final SATHandler handler, final boolean expAborted) {
final FormulaFactory f = formula.factory();
final MiniSat solver = MiniSat.miniSat(f);
solver.add(formula.negate());
final boolean isSAT = solver.sat() == Tristate.TRUE;
if (!isSAT) {
return;
}
final SortedSet<Literal> falsifyingAssignment = FormulaHelper.negateLiterals(solver.model().literals(), TreeSet::new);
final NaivePrimeReduction naive = new NaivePrimeReduction(formula);
final SortedSet<Literal> primeImplicate = naive.reduceImplicate(falsifyingAssignment, handler);
if (expAborted) {
assertThat(handler.aborted()).isTrue();
assertThat(primeImplicate).isNull();
} else {
assertThat(falsifyingAssignment).containsAll(primeImplicate);
testPrimeImplicateProperty(formula, primeImplicate);
}
}
Aggregations