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