Search in sources :

Example 1 with Proposition

use of org.logicng.propositions.Proposition 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 Proposition

use of org.logicng.propositions.Proposition 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 Proposition

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

the class DRUPTest method testUnsatCoresFromDimacs.

@Test
public void testUnsatCoresFromDimacs() throws IOException {
    final List<List<Formula>> cnfs = new ArrayList<>(3);
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/simple_input.cnf", this.f));
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/pg4_input.cnf", this.f));
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/avg_input.cnf", this.f, "var"));
    for (final SATSolver solver : this.solvers) {
        for (final List<Formula> cnf : cnfs) {
            solver.add(cnf);
            assertSolverUnsat(solver);
            final UNSATCore<Proposition> unsatCore = solver.unsatCore();
            verifyCore(unsatCore, cnf);
            solver.reset();
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) 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 Proposition

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

the class DRUPTest method verifyCore.

/**
 * Checks that each formula of the core is part of the original problem and that the core is really unsat.
 * @param originalCore the original core
 * @param cnf          the original problem
 */
private void verifyCore(final UNSATCore<Proposition> originalCore, final List<Formula> cnf) {
    final List<Formula> core = new ArrayList<>(originalCore.propositions().size());
    for (final Proposition prop : originalCore.propositions()) {
        core.add(prop.formula());
    }
    final SoftAssertions softly = new SoftAssertions();
    softly.assertThat(cnf).as("Core contains only original clauses").containsAll(core);
    final MiniSat solver = MiniSat.glucose(this.f, MiniSatConfig.builder().proofGeneration(true).incremental(false).build(), GlucoseConfig.builder().build());
    solver.add(core);
    softly.assertThat(solver.sat()).as("Core is unsatisfiable").isEqualTo(Tristate.FALSE);
    softly.assertAll();
}
Also used : Formula(org.logicng.formulas.Formula) MiniSat(org.logicng.solvers.MiniSat) SoftAssertions(org.assertj.core.api.SoftAssertions) ArrayList(java.util.ArrayList) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition)

Example 5 with Proposition

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

the class DRUPTest method testUnsatCoresAimTestset.

@Test
public void testUnsatCoresAimTestset() throws IOException {
    final File testFolder = new File("src/test/resources/sat/unsat");
    final File[] files = testFolder.listFiles();
    assert files != null;
    int count = 0;
    for (final SATSolver solver : this.solvers) {
        for (final File file : files) {
            final String fileName = file.getName();
            if (fileName.endsWith(".cnf")) {
                final List<Formula> cnf = DimacsReader.readCNF(file, this.f);
                solver.add(cnf);
                assertSolverUnsat(solver);
                final UNSATCore<Proposition> unsatCore = solver.unsatCore();
                verifyCore(unsatCore, cnf);
                solver.reset();
                count++;
            }
        }
        solver.reset();
    }
    assertThat(count).isEqualTo(36 * this.solvers.length);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) File(java.io.File) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Aggregations

Proposition (org.logicng.propositions.Proposition)12 StandardProposition (org.logicng.propositions.StandardProposition)10 ExtendedProposition (org.logicng.propositions.ExtendedProposition)8 Test (org.junit.jupiter.api.Test)7 LogicNGTest (org.logicng.LogicNGTest)7 SATSolver (org.logicng.solvers.SATSolver)7 ArrayList (java.util.ArrayList)5 Formula (org.logicng.formulas.Formula)5 UNSATCore (org.logicng.explanations.UNSATCore)4 MiniSat (org.logicng.solvers.MiniSat)3 File (java.io.File)2 LinkedHashSet (java.util.LinkedHashSet)2 SolverState (org.logicng.solvers.SolverState)2 HashMap (java.util.HashMap)1 List (java.util.List)1 SoftAssertions (org.assertj.core.api.SoftAssertions)1 LongRunningTag (org.logicng.LongRunningTag)1 LNGIntVector (org.logicng.collections.LNGIntVector)1 LNGVector (org.logicng.collections.LNGVector)1 DRUPTrim (org.logicng.explanations.drup.DRUPTrim)1