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);
}
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);
}
}
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();
}
}
}
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();
}
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);
}
Aggregations