Search in sources :

Example 16 with SATSolver

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

the class DRUPTest method testUnsatCoresFromDimacs.

@Test
public void testUnsatCoresFromDimacs() throws IOException {
    final List<List<Formula>> cnfs = new ArrayList<>(3);
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/simple_input.cnf", this.f));
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/pg4_input.cnf", this.f));
    cnfs.add(DimacsReader.readCNF("src/test/resources/drup/avg_input.cnf", this.f, "var"));
    for (final SATSolver solver : this.solvers) {
        for (final List<Formula> cnf : cnfs) {
            solver.add(cnf);
            assertSolverUnsat(solver);
            final UNSATCore<Proposition> unsatCore = solver.unsatCore();
            verifyCore(unsatCore, cnf);
            solver.reset();
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 17 with SATSolver

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

the class DRUPTest method testUnsatCoresAimTestset.

@Test
public void testUnsatCoresAimTestset() throws IOException {
    final File testFolder = new File("src/test/resources/sat/unsat");
    final File[] files = testFolder.listFiles();
    assert files != null;
    int count = 0;
    for (final SATSolver solver : this.solvers) {
        for (final File file : files) {
            final String fileName = file.getName();
            if (fileName.endsWith(".cnf")) {
                final List<Formula> cnf = DimacsReader.readCNF(file, this.f);
                solver.add(cnf);
                assertSolverUnsat(solver);
                final UNSATCore<Proposition> unsatCore = solver.unsatCore();
                verifyCore(unsatCore, cnf);
                solver.reset();
                count++;
            }
        }
        solver.reset();
    }
    assertThat(count).isEqualTo(36 * this.solvers.length);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) File(java.io.File) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 18 with SATSolver

use of org.logicng.solvers.SATSolver 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);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) FormulaFactory(org.logicng.formulas.FormulaFactory) StandardProposition(org.logicng.propositions.StandardProposition) Proposition(org.logicng.propositions.Proposition) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 19 with SATSolver

use of org.logicng.solvers.SATSolver 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);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) FormulaFactory(org.logicng.formulas.FormulaFactory) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 20 with SATSolver

use of org.logicng.solvers.SATSolver 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);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) FormulaFactory(org.logicng.formulas.FormulaFactory) ExtendedProposition(org.logicng.propositions.ExtendedProposition) LogicNGTest(org.logicng.LogicNGTest) 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