Search in sources :

Example 36 with Literal

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

the class MUSGenerationTest method readDimacs.

private List<StandardProposition> readDimacs(final String fileName) throws IOException {
    final List<StandardProposition> result = new ArrayList<>();
    final BufferedReader reader = new BufferedReader(new FileReader(fileName));
    while (reader.ready()) {
        final String line = reader.readLine();
        if (!line.startsWith("p") && !line.startsWith("c")) {
            final String[] tokens = line.split("\\s");
            final List<Literal> clause = new ArrayList<>();
            for (int i = 0; i < tokens.length - 1; i++) {
                final int lit = Integer.parseInt(tokens[i]);
                clause.add(lit < 0 ? this.f.literal("v" + (-lit), false) : this.f.literal("v" + lit, true));
            }
            result.add(new StandardProposition(this.f.clause(clause)));
        }
    }
    return result;
}
Also used : StandardProposition(org.logicng.propositions.StandardProposition) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 37 with Literal

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

the class LiteralProfileTest method testBinaryOperator.

@Test
public void testBinaryOperator() 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 impl = p.parse("(a & (b | c) & (~b | ~c)) => (~c & a)");
    final Formula equiv = p.parse("(a & (b | c) & (~b | ~c)) <=> (~c & a)");
    assertThat(impl.apply(this.litProfile, true)).isEqualTo(expected);
    assertThat(impl.apply(this.litProfile, false)).isEqualTo(expected);
    assertThat(equiv.apply(this.litProfile, true)).isEqualTo(expected);
    assertThat(equiv.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 38 with Literal

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

the class LiteralProfileTest method testNAryOperator.

@Test
public void testNAryOperator() 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 39 with Literal

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

the class MinimumPrimeImplicantTest method testSmallExamples.

@Test
public void testSmallExamples() throws ParserException {
    Formula formula = this.f.parse("(~(v17 | v18) | ~v1494 & (v17 | v18)) & ~v687 => v686");
    SortedSet<Literal> pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(1);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("(~(v17 | v18) | ~v1494 & (v17 | v18)) & v687 => ~v686");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(1);
    isPrimeImplicant(formula, pi);
    formula = this.f.parse("v173 + v174 + v451 + v258 + v317 + v259 + v452 + v453 + v175 + v176 + v177 + v178 + v179 + v180 + v181 + v182 + v183 + v102 + v103 + v104 + v105 = 1");
    pi = formula.apply(MinimumPrimeImplicantFunction.get());
    assertThat(pi).hasSize(21);
    isPrimeImplicant(formula, pi);
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) Test(org.junit.jupiter.api.Test)

Example 40 with Literal

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

the class VariableProfileTest method testNAryOperator.

@Test
public void testNAryOperator() throws ParserException {
    final Map<Literal, Integer> expected = new HashMap<>();
    expected.put(this.f2.variable("a"), 1);
    expected.put(this.f2.variable("b"), 2);
    expected.put(this.f2.variable("c"), 3);
    final PropositionalParser p = new PropositionalParser(this.f);
    final Formula formula = p.parse("a & (b | c) & (~b | ~c) & c");
    assertThat(formula.apply(this.varProfile, true)).isEqualTo(expected);
    assertThat(formula.apply(this.varProfile, 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)

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