Search in sources :

Example 81 with Literal

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 }));
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Literal(org.logicng.formulas.Literal) Test(org.junit.jupiter.api.Test)

Example 82 with Literal

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();
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) TautologyPredicate(org.logicng.predicates.satisfiability.TautologyPredicate) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList)

Example 83 with Literal

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();
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) TautologyPredicate(org.logicng.predicates.satisfiability.TautologyPredicate) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList)

Example 84 with Literal

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);
    }
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) MiniSat(org.logicng.solvers.MiniSat) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal)

Example 85 with Literal

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);
    }
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) MiniSat(org.logicng.solvers.MiniSat) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal)

Aggregations

Literal (org.logicng.formulas.Literal)115 Formula (org.logicng.formulas.Formula)51 ArrayList (java.util.ArrayList)38 Test (org.junit.jupiter.api.Test)32 Variable (org.logicng.formulas.Variable)29 FormulaFactory (org.logicng.formulas.FormulaFactory)25 PBConstraint (org.logicng.formulas.PBConstraint)21 TreeSet (java.util.TreeSet)17 SATSolver (org.logicng.solvers.SATSolver)14 Assignment (org.logicng.datastructures.Assignment)13 LNGVector (org.logicng.collections.LNGVector)12 HashMap (java.util.HashMap)11 LogicNGTest (org.logicng.LogicNGTest)11 LNGIntVector (org.logicng.collections.LNGIntVector)8 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)8 PropositionalParser (org.logicng.io.parsers.PropositionalParser)8 BufferedReader (java.io.BufferedReader)7 FileReader (java.io.FileReader)7 BinaryOperator (org.logicng.formulas.BinaryOperator)7 Not (org.logicng.formulas.Not)7