Search in sources :

Example 91 with Formula

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

the class SATTest method testFormulaOnSolver.

@Test
public void testFormulaOnSolver() throws ParserException {
    for (final SATSolver solver : this.solvers) {
        if (solver instanceof MiniSat) {
            final PseudoBooleanParser p = new PseudoBooleanParser(this.f);
            final Set<Formula> formulas = new LinkedHashSet<>();
            formulas.add(p.parse("A | B | C"));
            formulas.add(p.parse("~A | ~B | ~C"));
            formulas.add(p.parse("A | ~B"));
            formulas.add(p.parse("A"));
            solver.add(formulas);
            compareFormulas(formulas, solver.execute(FormulaOnSolverFunction.get()));
            formulas.add(p.parse("~A | C"));
            solver.reset();
            solver.add(formulas);
            compareFormulas(formulas, solver.execute(FormulaOnSolverFunction.get()));
            final Formula formula = p.parse("C + D + E <= 2");
            formulas.add(formula);
            solver.add(formula);
            compareFormulas(formulas, solver.execute(FormulaOnSolverFunction.get()));
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) PseudoBooleanParser(org.logicng.io.parsers.PseudoBooleanParser) LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula) MiniSat(org.logicng.solvers.MiniSat) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 92 with Formula

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

the class SATTest method testUPZeroLiterals.

@Test
public void testUPZeroLiterals() throws ParserException {
    // Note: The complete unit propagated set of literals on level 0 depends on each solver's added learned clauses during the solving process
    final Map<Formula, SortedSet<Literal>> expectedSubsets = new HashMap<>();
    expectedSubsets.put(this.f.verum(), new TreeSet<>());
    expectedSubsets.put(this.parser.parse("a"), new TreeSet<>(Collections.singletonList(this.f.literal("a", true))));
    expectedSubsets.put(this.parser.parse("a | b"), new TreeSet<>());
    expectedSubsets.put(this.parser.parse("a & b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", true))));
    expectedSubsets.put(this.parser.parse("a & ~b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", false))));
    expectedSubsets.put(this.parser.parse("(a | c) & ~b"), new TreeSet<>(Collections.singletonList(this.f.literal("b", false))));
    expectedSubsets.put(this.parser.parse("(b | c) & ~b & (~c | d)"), new TreeSet<>(Arrays.asList(this.f.literal("b", false), this.f.literal("c", true), this.f.literal("d", true))));
    for (final SATSolver solver : this.solvers) {
        for (final Formula formula : expectedSubsets.keySet()) {
            solver.reset();
            solver.add(formula);
            final boolean res = solver.sat() == Tristate.TRUE;
            assertThat(res).isTrue();
            final SortedSet<Literal> upLiterals = solver.execute(UpZeroLiteralsFunction.get());
            assertThat(upLiterals).containsAll(expectedSubsets.get(formula));
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) SortedSet(java.util.SortedSet) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 93 with Formula

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

the class SATTest method testAnd3.

@Test
public void testAnd3() {
    for (final SATSolver s : this.solvers) {
        final List<Formula> formulas = new ArrayList<>(3);
        formulas.add(this.f.literal("a", true));
        formulas.add(this.f.literal("b", false));
        formulas.add(this.f.literal("a", false));
        formulas.add(this.f.literal("d", false));
        s.add(formulas);
        assertSolverUnsat(s);
        s.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) ArrayList(java.util.ArrayList) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 94 with Formula

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

the class PBEncoderTest method testCC0.

@Test
public void testCC0() {
    for (final PBEncoder encoder : this.encoders) {
        final int numLits = 100;
        final List<Literal> lits = new ArrayList<>();
        final List<Integer> coeffs = new ArrayList<>();
        final Variable[] problemLits = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            final Variable var = this.f.variable("v" + i);
            lits.add(var);
            problemLits[i] = var;
            coeffs.add(1);
        }
        final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, 0, lits, coeffs));
        final SATSolver solver = MiniSat.miniSat(this.f);
        solver.add(clauses);
        assertSolverSat(solver);
        assertThat(solver.enumerateAllModels(problemLits)).hasSize(1).allMatch(m -> m.positiveVariables().isEmpty());
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 95 with Formula

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

the class PBEncoderTest method testCC.

private void testCC(final int numLits, final int rhs, final int expected, final PBEncoder encoder) {
    final Variable[] problemLits = new Variable[numLits];
    final int[] coeffs = new int[numLits];
    for (int i = 0; i < numLits; i++) {
        problemLits[i] = this.f.variable("v" + i);
        coeffs[i] = 1;
    }
    final List<Formula> clauses = encoder.encode((PBConstraint) this.f.pbc(CType.LE, rhs, problemLits, coeffs));
    final SATSolver solver = MiniSat.miniSat(this.f);
    solver.add(clauses);
    assertSolverSat(solver);
    assertThat(solver.enumerateAllModels(problemLits)).hasSize(expected).allMatch(m -> m.positiveVariables().size() <= rhs);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) PBConstraint(org.logicng.formulas.PBConstraint)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16