Search in sources :

Example 1 with MiniCard

use of org.logicng.solvers.sat.MiniCard 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 MiniCard

use of org.logicng.solvers.sat.MiniCard in project LogicNG by logic-ng.

the class MiniSat method add.

@Override
public void add(final Formula formula, final Proposition proposition) {
    this.result = UNDEF;
    if (formula.type() == FType.PBC) {
        final PBConstraint constraint = (PBConstraint) formula;
        if (constraint.isCC()) {
            if (this.style == SolverStyle.MINICARD) {
                if (constraint.comparator() == CType.LE) {
                    ((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs());
                } else if (constraint.comparator() == CType.LT && constraint.rhs() > 3) {
                    ((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs() - 1);
                } else if (constraint.comparator() == CType.EQ && constraint.rhs() == 1) {
                    ((MiniCard) this.solver).addAtMost(generateClauseVector(Arrays.asList(constraint.operands())), constraint.rhs());
                    this.solver.addClause(generateClauseVector(Arrays.asList(constraint.operands())), proposition);
                } else {
                    addFormulaAsCNF(constraint, proposition);
                }
            } else {
                final EncodingResult result = EncodingResult.resultForMiniSat(this.f, this, proposition);
                this.ccEncoder.encode((CardinalityConstraint) constraint, result);
            }
        } else {
            addFormulaAsCNF(constraint, proposition);
        }
    } else {
        addFormulaAsCNF(formula, proposition);
    }
}
Also used : MiniCard(org.logicng.solvers.sat.MiniCard) EncodingResult(org.logicng.datastructures.EncodingResult) PBConstraint(org.logicng.formulas.PBConstraint)

Example 3 with MiniCard

use of org.logicng.solvers.sat.MiniCard in project LogicNG by logic-ng.

the class LNGHeapTest method test.

@Test
public void test() {
    final MiniSatStyleSolver solver = new MiniCard();
    solver.newVar(true, true);
    solver.newVar(true, true);
    solver.newVar(true, true);
    final LNGHeap heap = new LNGHeap(solver);
    assertThat(heap.empty()).isTrue();
    heap.insert(1);
    heap.insert(2);
    heap.insert(0);
    assertThat(heap.get(0)).isEqualTo(1);
    assertThat(heap.toString()).isEqualTo("LNGHeap{[1, 2], [2, 0], [0, 1]}");
    assertThat(heap.size()).isEqualTo(3);
    heap.clear();
    assertThat(heap.empty()).isTrue();
}
Also used : MiniCard(org.logicng.solvers.sat.MiniCard) MiniSatStyleSolver(org.logicng.solvers.sat.MiniSatStyleSolver) Test(org.junit.jupiter.api.Test)

Aggregations

MiniCard (org.logicng.solvers.sat.MiniCard)3 MiniSatStyleSolver (org.logicng.solvers.sat.MiniSatStyleSolver)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.jupiter.api.Test)1 LNGIntVector (org.logicng.collections.LNGIntVector)1 LNGVector (org.logicng.collections.LNGVector)1 EncodingResult (org.logicng.datastructures.EncodingResult)1 UNSATCore (org.logicng.explanations.UNSATCore)1 DRUPTrim (org.logicng.explanations.drup.DRUPTrim)1 Formula (org.logicng.formulas.Formula)1 PBConstraint (org.logicng.formulas.PBConstraint)1 Proposition (org.logicng.propositions.Proposition)1 StandardProposition (org.logicng.propositions.StandardProposition)1 GlucoseSyrup (org.logicng.solvers.sat.GlucoseSyrup)1