Search in sources :

Example 1 with StandardProposition

use of org.logicng.propositions.StandardProposition in project LogicNG by logic-ng.

the class UnsatCoreFunction method apply.

@Override
public UNSATCore<Proposition> apply(final MiniSat solver, final Consumer<Tristate> resultSetter) {
    if (!solver.getConfig().proofGeneration()) {
        throw new IllegalStateException("Cannot generate an unsat core if proof generation is not turned on");
    }
    if (solver.getResult() == TRUE) {
        throw new IllegalStateException("An unsat core can only be generated if the formula is solved and is UNSAT");
    }
    if (solver.getResult() == Tristate.UNDEF) {
        throw new IllegalStateException("Cannot generate an unsat core before the formula was solved.");
    }
    if (solver.underlyingSolver() instanceof MiniCard) {
        throw new IllegalStateException("Cannot compute an unsat core with MiniCard.");
    }
    if (solver.underlyingSolver() instanceof GlucoseSyrup && solver.getConfig().incremental()) {
        throw new IllegalStateException("Cannot compute an unsat core with Glucose in incremental mode.");
    }
    if (solver.isLastComputationWithAssumptions()) {
        throw new IllegalStateException("Cannot compute an unsat core for a computation with assumptions.");
    }
    final DRUPTrim trimmer = new DRUPTrim();
    final Map<Formula, Proposition> clause2proposition = new HashMap<>();
    final LNGVector<LNGIntVector> clauses = new LNGVector<>(solver.underlyingSolver().pgOriginalClauses().size());
    for (final MiniSatStyleSolver.ProofInformation pi : solver.underlyingSolver().pgOriginalClauses()) {
        clauses.push(pi.clause());
        final Formula clause = getFormulaForVector(solver, pi.clause());
        Proposition proposition = pi.proposition();
        if (proposition == null) {
            proposition = new StandardProposition(clause);
        }
        clause2proposition.put(clause, proposition);
    }
    if (containsEmptyClause(clauses)) {
        final Proposition emptyClause = clause2proposition.get(solver.factory().falsum());
        return new UNSATCore<>(Collections.singletonList(emptyClause), true);
    }
    final DRUPTrim.DRUPResult result = trimmer.compute(clauses, solver.underlyingSolver().pgProof());
    if (result.trivialUnsat()) {
        return handleTrivialCase(solver);
    }
    final LinkedHashSet<Proposition> propositions = new LinkedHashSet<>();
    for (final LNGIntVector vector : result.unsatCore()) {
        propositions.add(clause2proposition.get(getFormulaForVector(solver, vector)));
    }
    return new UNSATCore<>(new ArrayList<>(propositions), false);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DRUPTrim(org.logicng.explanations.drup.DRUPTrim) MiniCard(org.logicng.solvers.sat.MiniCard) GlucoseSyrup(org.logicng.solvers.sat.GlucoseSyrup) MiniSatStyleSolver(org.logicng.solvers.sat.MiniSatStyleSolver) HashMap(java.util.HashMap) UNSATCore(org.logicng.explanations.UNSATCore) LNGIntVector(org.logicng.collections.LNGIntVector) LNGVector(org.logicng.collections.LNGVector) StandardProposition(org.logicng.propositions.StandardProposition) Formula(org.logicng.formulas.Formula) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition)

Example 2 with StandardProposition

use of org.logicng.propositions.StandardProposition in project LogicNG by logic-ng.

the class DRUPTest method testTrivialCasesPropositions.

@Test
public void testTrivialCasesPropositions() throws ParserException {
    for (final SATSolver solver : this.solvers) {
        assertSolverSat(solver);
        final StandardProposition p1 = new StandardProposition("P1", this.f.parse("$false"));
        solver.add(p1);
        assertSolverUnsat(solver);
        UNSATCore<Proposition> unsatCore = solver.unsatCore();
        assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1);
        solver.reset();
        assertSolverSat(solver);
        final StandardProposition p2 = new StandardProposition("P2", this.f.parse("a"));
        solver.add(p2);
        assertSolverSat(solver);
        final StandardProposition p3 = new StandardProposition("P3", this.f.parse("~a"));
        solver.add(p3);
        assertSolverUnsat(solver);
        unsatCore = solver.unsatCore();
        assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p2, p3);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 3 with StandardProposition

use of org.logicng.propositions.StandardProposition in project LogicNG by logic-ng.

the class DRUPTest method testCoreAndAssumptions.

@Test
public void testCoreAndAssumptions() throws ParserException {
    final FormulaFactory f = new FormulaFactory();
    final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().proofGeneration(true).cnfMethod(MiniSatConfig.CNFMethod.PG_ON_SOLVER).build());
    final StandardProposition p1 = new StandardProposition(f.parse("A => B"));
    final StandardProposition p2 = new StandardProposition(f.parse("A & B => G"));
    final StandardProposition p3 = new StandardProposition(f.or(f.literal("X", false), f.literal("A", true)));
    final StandardProposition p4 = new StandardProposition(f.or(f.literal("X", false), f.literal("G", false)));
    final StandardProposition p5 = new StandardProposition(f.literal("G", false));
    final StandardProposition p6 = new StandardProposition(f.literal("A", true));
    solver.add(p1);
    solver.add(p2);
    solver.add(p3);
    solver.add(p4);
    // Assumption call
    solver.sat(f.variable("X"));
    solver.add(p5);
    solver.add(p6);
    solver.sat();
    final UNSATCore<Proposition> unsatCore = solver.unsatCore();
    assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1, p2, p5, p6);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) FormulaFactory(org.logicng.formulas.FormulaFactory) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 4 with StandardProposition

use of org.logicng.propositions.StandardProposition in project LogicNG by logic-ng.

the class DRUPTest method testWithSpecialUnitCaseGlucose.

@Test
public void testWithSpecialUnitCaseGlucose() throws ParserException {
    final FormulaFactory f = new FormulaFactory();
    final SATSolver solver = MiniSat.glucose(f, MiniSatConfig.builder().proofGeneration(true).incremental(false).build(), GlucoseConfig.builder().build());
    final StandardProposition p1 = new StandardProposition(f.parse("a => b"));
    final StandardProposition p2 = new StandardProposition(f.parse("a => c | d"));
    final StandardProposition p3 = new StandardProposition(f.parse("b => c | d"));
    final StandardProposition p4 = new StandardProposition(f.parse("e | f | g | h => i"));
    final StandardProposition p5 = new StandardProposition(f.parse("~j => k | j"));
    final StandardProposition p6 = new StandardProposition(f.parse("b => ~(e | f)"));
    final StandardProposition p7 = new StandardProposition(f.parse("c => ~j"));
    final StandardProposition p8 = new StandardProposition(f.parse("l | m => ~i"));
    final StandardProposition p9 = new StandardProposition(f.parse("j => (f + g + h = 1)"));
    final StandardProposition p10 = new StandardProposition(f.parse("d => (l + m + e + f = 1)"));
    final StandardProposition p11 = new StandardProposition(f.parse("~k"));
    solver.addPropositions(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
    assertThat(solver.sat()).isEqualTo(TRUE);
    solver.add(f.variable("a"));
    assertThat(solver.sat()).isEqualTo(FALSE);
    assertThat(solver.unsatCore().propositions()).contains(p1, p2, p4, p5, p6, p7, p8, p9, p10, p11);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) FormulaFactory(org.logicng.formulas.FormulaFactory) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 5 with StandardProposition

use of org.logicng.propositions.StandardProposition in project LogicNG by logic-ng.

the class DRUPTest method testWithCcPropositions.

@Test
public void testWithCcPropositions() throws ParserException {
    final FormulaFactory f = new FormulaFactory();
    final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().proofGeneration(true).cnfMethod(MiniSatConfig.CNFMethod.PG_ON_SOLVER).build());
    final ExtendedProposition<StringBackpack> p1 = new ExtendedProposition<>(new StringBackpack("CC"), f.parse("A + B + C <= 1"));
    final StandardProposition p2 = new StandardProposition(f.parse("A"));
    final StandardProposition p3 = new StandardProposition(f.parse("B"));
    final StandardProposition p4 = new StandardProposition(f.parse("X & Y"));
    solver.add(p1);
    solver.add(p2);
    solver.add(p3);
    solver.add(p4);
    assertThat(solver.sat()).isEqualTo(FALSE);
    assertThat(solver.unsatCore().propositions()).containsExactlyInAnyOrder(p1, p2, p3);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) FormulaFactory(org.logicng.formulas.FormulaFactory) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Aggregations

StandardProposition (org.logicng.propositions.StandardProposition)18 Test (org.junit.jupiter.api.Test)13 LogicNGTest (org.logicng.LogicNGTest)9 SATSolver (org.logicng.solvers.SATSolver)9 Proposition (org.logicng.propositions.Proposition)6 ExtendedProposition (org.logicng.propositions.ExtendedProposition)5 FormulaFactory (org.logicng.formulas.FormulaFactory)4 ArrayList (java.util.ArrayList)3 Formula (org.logicng.formulas.Formula)3 LinkedHashSet (java.util.LinkedHashSet)2 UNSATCore (org.logicng.explanations.UNSATCore)2 BoundedSatHandler (org.logicng.handlers.BoundedSatHandler)2 SATHandler (org.logicng.handlers.SATHandler)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LNGIntVector (org.logicng.collections.LNGIntVector)1 LNGVector (org.logicng.collections.LNGVector)1 DRUPTrim (org.logicng.explanations.drup.DRUPTrim)1