Search in sources :

Example 91 with Literal

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

the class SATTest method testUPZeroLiteralsUNSAT.

@Test
public void testUPZeroLiteralsUNSAT() throws ParserException {
    final Formula formula = this.parser.parse("a & (a => b) & (b => c) & (c => ~a)");
    for (final SATSolver solver : this.solvers) {
        solver.reset();
        solver.add(formula);
        solver.sat();
        final SortedSet<Literal> upLiterals = solver.execute(UpZeroLiteralsFunction.get());
        assertThat(upLiterals).isNull();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 92 with Literal

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

the class SATTest method testHighestLexicographicalAssignment.

/**
 * Tests if the given satisfying assignment is the highest assignment in the lexicographical order based on the given
 * literals order.
 * @param solver     the solver with the loaded formulas
 * @param assignment the satisfying assignment
 * @param order      the literals order
 */
private void testHighestLexicographicalAssignment(final SATSolver solver, final Assignment assignment, final List<? extends Literal> order) {
    final SortedSet<Literal> literals = assignment.literals();
    final List<Literal> orderSublist = new ArrayList<>();
    for (final Literal lit : order) {
        final boolean containsLit = literals.contains(lit);
        if (!containsLit) {
            final SortedSet<Literal> orderSubsetWithFlip = new TreeSet<>(orderSublist);
            orderSubsetWithFlip.remove(lit.negate());
            orderSubsetWithFlip.add(lit);
            assertThat(solver.sat(orderSubsetWithFlip)).isEqualTo(Tristate.FALSE);
        }
        orderSublist.add(containsLit ? lit : lit.negate());
    }
}
Also used : TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList)

Example 93 with Literal

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

the class FormulaRandomizerTest method testLiteral.

@Test
public void testLiteral() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().weightPositiveLiteral(40).weightNegativeLiteral(60).seed(42).build());
    int numPos = 0;
    for (int i = 0; i < 100; i++) {
        final Literal literal = random.literal();
        if (literal.phase()) {
            numPos++;
        }
    }
    assertThat(numPos).isStrictlyBetween(30, 50);
}
Also used : Literal(org.logicng.formulas.Literal) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

Example 94 with Literal

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

the class CCModularTotalizer method finalAdder.

private void finalAdder(final int mod, final LNGVector<Literal> upper, final LNGVector<Literal> lupper, final LNGVector<Literal> rupper, final Variable carry) {
    for (int i = 0; i <= lupper.size(); i++) {
        for (int j = 0; j <= rupper.size(); j++) {
            Literal a = this.varError;
            Literal b = this.varError;
            Literal c = this.varError;
            Literal d = this.varError;
            int closeMod = this.currentCardinalityRhs / mod;
            if (this.currentCardinalityRhs % mod != 0) {
                closeMod++;
            }
            if (mod * (i + j) > closeMod * mod) {
                continue;
            }
            if (i != 0) {
                a = lupper.get(i - 1);
            }
            if (j != 0) {
                b = rupper.get(j - 1);
            }
            if (i + j != 0 && i + j - 1 < upper.size()) {
                c = upper.get(i + j - 1);
            }
            if (i + j < upper.size()) {
                d = upper.get(i + j);
            }
            if (c != this.varUndef && c != this.varError) {
                final LNGVector<Literal> clause = new LNGVector<>();
                if (a != this.varUndef && a != this.varError) {
                    clause.push(a.negate());
                }
                if (b != this.varUndef && b != this.varError) {
                    clause.push(b.negate());
                }
                clause.push(c);
                if (clause.size() > 1) {
                    this.result.addClause(clause);
                }
            }
            final LNGVector<Literal> clause = new LNGVector<>();
            clause.push(carry.negate());
            if (a != this.varUndef && a != this.varError) {
                clause.push(a.negate());
            }
            if (b != this.varUndef && b != this.varError) {
                clause.push(b.negate());
            }
            if (d != this.varError && d != this.varUndef) {
                clause.push(d);
            }
            if (clause.size() > 1) {
                this.result.addClause(clause);
            }
        }
    }
}
Also used : Literal(org.logicng.formulas.Literal) LNGVector(org.logicng.collections.LNGVector)

Example 95 with Literal

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

the class CCSorting method recursiveMerger.

private void recursiveMerger(final int c, final LNGVector<Literal> inputA, final int a, final LNGVector<Literal> inputB, final int b, final EncodingResult formula, final LNGVector<Literal> output, final ImplicationDirection direction) {
    assert inputA.size() > 0;
    assert inputB.size() > 0;
    assert c > 0;
    output.clear();
    int a2 = a;
    int b2 = b;
    if (a2 > c) {
        a2 = c;
    }
    if (b2 > c) {
        b2 = c;
    }
    if (c == 1) {
        final Variable y = formula.newVariable();
        comparator(inputA.get(0), inputB.get(0), y, formula, direction);
        output.push(y);
        return;
    }
    if (a2 == 1 && b2 == 1) {
        assert c == 2;
        final Variable y1 = formula.newVariable();
        final Variable y2 = formula.newVariable();
        comparator(inputA.get(0), inputB.get(0), y1, y2, formula, direction);
        output.push(y1);
        output.push(y2);
        return;
    }
    final LNGVector<Literal> oddMerge = new LNGVector<>();
    final LNGVector<Literal> evenMerge = new LNGVector<>();
    final LNGVector<Literal> tmpLitsOddA = new LNGVector<>();
    final LNGVector<Literal> tmpLitsOddB = new LNGVector<>();
    final LNGVector<Literal> tmpLitsEvenA = new LNGVector<>();
    final LNGVector<Literal> tmpLitsEvenB = new LNGVector<>();
    for (int i = 0; i < a2; i = i + 2) {
        tmpLitsOddA.push(inputA.get(i));
    }
    for (int i = 0; i < b2; i = i + 2) {
        tmpLitsOddB.push(inputB.get(i));
    }
    for (int i = 1; i < a2; i = i + 2) {
        tmpLitsEvenA.push(inputA.get(i));
    }
    for (int i = 1; i < b2; i = i + 2) {
        tmpLitsEvenB.push(inputB.get(i));
    }
    merge(c / 2 + 1, tmpLitsOddA, tmpLitsOddB, formula, oddMerge, direction);
    merge(c / 2, tmpLitsEvenA, tmpLitsEvenB, formula, evenMerge, direction);
    assert oddMerge.size() > 0;
    output.push(oddMerge.get(0));
    int i = 1;
    int j = 0;
    while (true) {
        if (i < oddMerge.size() && j < evenMerge.size()) {
            if (output.size() + 2 <= c) {
                final Variable z0 = formula.newVariable();
                final Variable z1 = formula.newVariable();
                comparator(oddMerge.get(i), evenMerge.get(j), z0, z1, formula, direction);
                output.push(z0);
                output.push(z1);
                if (output.size() == c) {
                    break;
                }
            } else if (output.size() + 1 == c) {
                final Variable z0 = formula.newVariable();
                comparator(oddMerge.get(i), evenMerge.get(j), z0, formula, direction);
                output.push(z0);
                break;
            }
        } else if (i >= oddMerge.size() && j >= evenMerge.size()) {
            break;
        } else if (i >= oddMerge.size()) {
            assert j == evenMerge.size() - 1;
            output.push(evenMerge.back());
            break;
        } else {
            assert i == oddMerge.size() - 1;
            output.push(oddMerge.back());
            break;
        }
        i++;
        j++;
    }
    assert output.size() == a2 + b2 || output.size() == c;
}
Also used : Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) LNGVector(org.logicng.collections.LNGVector)

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