Search in sources :

Example 21 with SATSolver

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;
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable)

Example 22 with SATSolver

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);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) NumberOfModelsHandler(org.logicng.handlers.NumberOfModelsHandler) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint)

Example 23 with SATSolver

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);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint)

Example 24 with SATSolver

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);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Test(org.junit.jupiter.api.Test)

Example 25 with SATSolver

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)));
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Test(org.junit.jupiter.api.Test)

Aggregations

SATSolver (org.logicng.solvers.SATSolver)130 Test (org.junit.jupiter.api.Test)104 LogicNGTest (org.logicng.LogicNGTest)83 Variable (org.logicng.formulas.Variable)44 Assignment (org.logicng.datastructures.Assignment)41 Formula (org.logicng.formulas.Formula)36 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)19 FormulaFactory (org.logicng.formulas.FormulaFactory)19 ArrayList (java.util.ArrayList)16 Literal (org.logicng.formulas.Literal)14 StandardProposition (org.logicng.propositions.StandardProposition)12 SolverState (org.logicng.solvers.SolverState)10 List (java.util.List)9 TreeSet (java.util.TreeSet)8 PBConstraint (org.logicng.formulas.PBConstraint)8 ExtendedProposition (org.logicng.propositions.ExtendedProposition)8 Proposition (org.logicng.propositions.Proposition)7 LongRunningTag (org.logicng.LongRunningTag)6 Tristate (org.logicng.datastructures.Tristate)6 File (java.io.File)5