use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class BackboneGenerationTest method verifyBackbone.
private boolean verifyBackbone(final Backbone backbone, final Formula formula, final Collection<Variable> variables) {
final SATSolver solver = MiniSat.miniSat(formula.factory());
solver.add(formula);
for (final Variable bbVar : backbone.getPositiveBackbone()) {
if (solver.sat(bbVar.negate()) == Tristate.TRUE) {
return false;
}
}
for (final Variable bbVar : backbone.getNegativeBackbone()) {
if (solver.sat(bbVar) == Tristate.TRUE) {
return false;
}
}
for (final Variable variable : variables) {
if (!backbone.getPositiveBackbone().contains(variable) && !backbone.getNegativeBackbone().contains(variable)) {
if (solver.sat(variable) == Tristate.FALSE) {
return false;
}
if (solver.sat(variable.negate()) == Tristate.FALSE) {
return false;
}
}
}
return true;
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class CCALKTest method testCC.
private void testCC(final int numLits, final int rhs, final int expected, final FormulaFactory f) {
final Variable[] problemLits = new Variable[numLits];
for (int i = 0; i < numLits; i++) {
problemLits[i] = f.variable("v" + i);
}
final SATSolver solver = MiniSat.miniSat(f);
solver.add(f.cc(CType.GE, rhs, problemLits));
if (expected != 0) {
assertSolverSat(solver);
} else {
assertSolverUnsat(solver);
}
assertThat(solver.execute(ModelEnumerationFunction.builder().variables(problemLits).handler(new NumberOfModelsHandler(12000)).build())).hasSize(expected).allMatch(m -> m.positiveVariables().size() >= rhs);
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class CCAMKTest method testCC.
private void testCC(final int numLits, final int rhs, final int expected, final FormulaFactory f, final boolean miniCard) {
final Variable[] problemLits = new Variable[numLits];
for (int i = 0; i < numLits; i++) {
problemLits[i] = f.variable("v" + i);
}
final SATSolver solver = miniCard ? MiniSat.miniCard(f) : MiniSat.miniSat(f);
solver.add(f.cc(CType.LE, rhs, problemLits));
assertSolverSat(solver);
assertThat(solver.enumerateAllModels(problemLits)).hasSize(expected).allMatch(m -> m.positiveVariables().size() <= rhs);
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class FormulaFactoryWithoutContradictionCheckTest method testSatSolverWithContradictions.
@Test
public void testSatSolverWithContradictions() throws ParserException {
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(this.f.parse("A"));
solver.add(this.f.parse("A => B"));
solver.add(this.f.parse("C | ~C"));
final List<Assignment> models = solver.enumerateAllModels();
assertThat(models).hasSize(2);
models.forEach(m -> assertThat(m.literals()).containsAnyOf(this.f.literal("C", true), this.f.literal("C", false)));
solver.add(this.f.parse("D & ~D"));
assertThat(solver.sat()).isEqualTo(Tristate.FALSE);
}
use of org.logicng.solvers.SATSolver in project LogicNG by logic-ng.
the class FormulaFactoryWithoutContradictionCheckTest method testSatSolverWithTautologies.
@Test
public void testSatSolverWithTautologies() throws ParserException {
final SATSolver solver = MiniSat.miniSat(this.f);
solver.add(this.f.parse("A"));
solver.add(this.f.parse("A => B"));
solver.add(this.f.parse("C | ~C"));
List<Assignment> models = solver.enumerateAllModels();
assertThat(models).hasSize(2);
models.forEach(m -> assertThat(m.literals()).containsAnyOf(this.f.literal("C", true), this.f.literal("C", false)));
solver.add(this.f.parse("D | ~D"));
models = solver.enumerateAllModels();
assertThat(models).hasSize(4);
models.forEach(m -> assertThat(m.literals()).containsAnyOf(this.f.literal("C", true), this.f.literal("C", false), this.f.literal("D", true), this.f.literal("D", false)));
}
Aggregations