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