Search in sources :

Example 86 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class LiteralProfileTest method testNot.

@Test
public void testNot() throws ParserException {
    final Map<Literal, Integer> expected = new HashMap<>();
    expected.put(this.f2.variable("a"), 2);
    expected.put(this.f2.variable("b"), 1);
    expected.put(this.f2.variable("c"), 1);
    expected.put(this.f2.literal("b", false), 1);
    expected.put(this.f2.literal("c", false), 2);
    final PropositionalParser p = new PropositionalParser(this.f);
    final Formula formula = p.parse("~(a & (b | c) & ((~b | ~c) => (~c & a)))");
    assertThat(formula.apply(this.litProfile, true)).isEqualTo(expected);
    assertThat(formula.apply(this.litProfile, false)).isEqualTo(expected);
}
Also used : Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 87 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class MinimumPrimeImplicantTest method isPrimeImplicant.

private void isPrimeImplicant(final Formula formula, final SortedSet<Literal> pi) {
    assertThat(this.f.implication(this.f.and(pi), formula).holds(new TautologyPredicate(this.f))).isTrue();
    for (final Literal literal : pi) {
        final TreeSet<Literal> newSet = new TreeSet<>(pi);
        newSet.remove(literal);
        if (!newSet.isEmpty()) {
            assertThat(this.f.implication(this.f.and(newSet), formula).holds(new TautologyPredicate(this.f))).isFalse();
        }
    }
}
Also used : TautologyPredicate(org.logicng.predicates.satisfiability.TautologyPredicate) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal)

Example 88 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class MinimumPrimeImplicantTest method testSimpleCases.

@Test
public void testSimpleCases() throws ParserException {
    Formula formula = this.f.parse("a");
    SortedSet<Literal> pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(1);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("a | b | c");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(1);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("a & b & (~a|~b)");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).isNull();
    formula = this.f.parse("a & b & c");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(3);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("a | b | ~c => e & d & f");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(3);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("a | b | ~c <=> e & d & f");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(4);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("(a | b | ~c <=> e & d & f) | (a | b | ~c => e & d & f)");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(3);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("(a | b | ~c <=> e & d & f) | (a | b | ~c => e & d & f) | (a & b)");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(2);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("(a | b | ~c <=> e & d & f) | (a | b | ~c => e & d & f) | (a & b) | (f => g)");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(1);
    isPrimeImplicant(formula, pi);
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) Test(org.junit.jupiter.api.Test)

Example 89 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class QuineMcCluskeyTest method getTerm.

static Term getTerm(final String string, final FormulaFactory f) throws ParserException {
    final List<Literal> literals = new ArrayList<>();
    final PropositionalParser p = new PropositionalParser(f);
    for (final String var : string.split(" ")) {
        literals.add((Literal) p.parse(var));
    }
    return convertToTerm(literals, f);
}
Also used : Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) PropositionalParser(org.logicng.io.parsers.PropositionalParser)

Example 90 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method testLocalMinimum.

/**
 * Tests if the given satisfying assignment is a local minimal model regarding the given relevant literals, i.e.
 * there is no variable in the assignment, contained in the relevant literals, that can be flipped without
 * resulting in an unsatisfying assignment.
 * @param solver           the solver with the loaded formulas
 * @param assignment       the satisfying assignment
 * @param relevantLiterals the relevant literals.
 */
private void testLocalMinimum(final SATSolver solver, final Assignment assignment, final Collection<? extends Literal> relevantLiterals) {
    final SortedSet<Literal> literals = assignment.literals();
    for (final Literal lit : relevantLiterals) {
        if (!literals.contains(lit)) {
            final SortedSet<Literal> literalsWithFlip = new TreeSet<>(literals);
            literalsWithFlip.remove(lit.negate());
            literalsWithFlip.add(lit);
            assertThat(solver.sat(literalsWithFlip)).isEqualTo(Tristate.FALSE);
        }
    }
}
Also used : 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