Search in sources :

Example 46 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method testSelectionOrderSimple02.

@Test
public void testSelectionOrderSimple02() {
    for (final SATSolver solver : this.solvers) {
        final List<Variable> literals = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            final Variable lit = this.f.variable("x" + i);
            literals.add(lit);
        }
        solver.add(this.f.cc(CType.EQ, 2, literals));
        for (int i = 0; i < 10; ++i) {
            assertThat(solver.satWithSelectionOrder(literals)).isEqualTo(Tristate.TRUE);
            final Assignment assignment = solver.model();
            testLocalMinimum(solver, assignment, literals);
            testHighestLexicographicalAssignment(solver, assignment, literals);
            solver.add(assignment.blockingClause(this.f, literals));
        }
        solver.reset();
        solver.add(this.f.cc(CType.EQ, 2, literals));
        final List<Literal> selectionOrder02 = Arrays.asList(this.f.literal("x4", true), this.f.literal("x0", false), this.f.literal("x1", true), this.f.literal("x2", true), this.f.literal("x3", true));
        for (int i = 0; i < 10; ++i) {
            assertThat(solver.satWithSelectionOrder(selectionOrder02)).isEqualTo(Tristate.TRUE);
            final Assignment assignment = solver.model();
            testLocalMinimum(solver, assignment, selectionOrder02);
            testHighestLexicographicalAssignment(solver, assignment, selectionOrder02);
            solver.add(assignment.blockingClause(this.f, selectionOrder02));
        }
        solver.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 47 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method testUPZeroLiteralsDimacsFiles.

@Test
public void testUPZeroLiteralsDimacsFiles() throws IOException {
    final File testFolder = new File("src/test/resources/sat");
    final File[] files = testFolder.listFiles();
    assert files != null;
    for (final SATSolver solver : this.solvers) {
        for (final File file : files) {
            final String fileName = file.getName();
            if (fileName.endsWith(".cnf")) {
                readCNF(solver, file);
                final boolean res = solver.sat() == Tristate.TRUE;
                if (res) {
                    final SortedSet<Literal> upZeroLiterals = solver.execute(UpZeroLiteralsFunction.get());
                    final List<Literal> negations = new ArrayList<>(upZeroLiterals.size());
                    for (final Literal lit : upZeroLiterals) {
                        negations.add(lit.negate());
                    }
                    solver.add(this.f.or(negations));
                    // Test if CNF implies identified unit propagated literals on level zero, i.e., each literal is a backbone literal
                    assertThat(solver.sat()).isEqualTo(Tristate.FALSE);
                }
            }
        }
        solver.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) File(java.io.File) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 48 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method testSelectionOrderSimple01.

@Test
public void testSelectionOrderSimple01() throws ParserException {
    for (final SATSolver solver : this.solvers) {
        final Formula formula = this.parser.parse("~(x <=> y)");
        solver.add(formula);
        List<Literal> selectionOrder = Arrays.asList(this.X, this.Y);
        assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
        Assignment assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.setSolverToUndef();
        selectionOrder = Arrays.asList(this.Y, this.X);
        assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
        assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.setSolverToUndef();
        selectionOrder = Collections.singletonList(this.NX);
        assertThat(solver.sat(selectionOrder)).isEqualTo(Tristate.TRUE);
        assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.Y, this.NX);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.setSolverToUndef();
        selectionOrder = Arrays.asList(this.NY, this.NX);
        assertThat(solver.satWithSelectionOrder(selectionOrder)).isEqualTo(Tristate.TRUE);
        assignment = solver.model();
        assertThat(assignment.literals()).containsExactlyInAnyOrder(this.X, this.NY);
        testLocalMinimum(solver, assignment, selectionOrder);
        testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
        solver.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 49 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method testUPZeroLiterals.

@Test
public void testUPZeroLiterals() throws ParserException {
    // Note: The complete unit propagated set of literals on level 0 depends on each solver's added learned clauses during the solving process
    final Map<Formula, SortedSet<Literal>> expectedSubsets = new HashMap<>();
    expectedSubsets.put(this.f.verum(), new TreeSet<>());
    expectedSubsets.put(this.parser.parse("a"), new TreeSet<>(Collections.singletonList(this.f.literal("a", true))));
    expectedSubsets.put(this.parser.parse("a | b"), new TreeSet<>());
    expectedSubsets.put(this.parser.parse("a & b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", true))));
    expectedSubsets.put(this.parser.parse("a & ~b"), new TreeSet<>(Arrays.asList(this.f.literal("a", true), this.f.literal("b", false))));
    expectedSubsets.put(this.parser.parse("(a | c) & ~b"), new TreeSet<>(Collections.singletonList(this.f.literal("b", false))));
    expectedSubsets.put(this.parser.parse("(b | c) & ~b & (~c | d)"), new TreeSet<>(Arrays.asList(this.f.literal("b", false), this.f.literal("c", true), this.f.literal("d", true))));
    for (final SATSolver solver : this.solvers) {
        for (final Formula formula : expectedSubsets.keySet()) {
            solver.reset();
            solver.add(formula);
            final boolean res = solver.sat() == Tristate.TRUE;
            assertThat(res).isTrue();
            final SortedSet<Literal> upLiterals = solver.execute(UpZeroLiteralsFunction.get());
            assertThat(upLiterals).containsAll(expectedSubsets.get(formula));
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Formula(org.logicng.formulas.Formula) HashMap(java.util.HashMap) Literal(org.logicng.formulas.Literal) SortedSet(java.util.SortedSet) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 50 with Literal

use of org.logicng.formulas.Literal in project LogicNG by logic-ng.

the class SATTest method testDimacsFilesWithSelectionOrder.

@Test
@LongRunningTag
public void testDimacsFilesWithSelectionOrder() throws IOException {
    final Map<String, Boolean> expectedResults = new HashMap<>();
    final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/sat/results.txt"));
    while (reader.ready()) {
        final String[] tokens = reader.readLine().split(";");
        expectedResults.put(tokens[0], Boolean.valueOf(tokens[1]));
    }
    final File testFolder = new File("src/test/resources/sat");
    final File[] files = testFolder.listFiles();
    assert files != null;
    for (final SATSolver solver : this.solvers) {
        for (final File file : files) {
            final String fileName = file.getName();
            if (fileName.endsWith(".cnf")) {
                readCNF(solver, file);
                final List<Literal> selectionOrder = new ArrayList<>();
                for (final Variable var : FormulaHelper.variables(solver.execute(FormulaOnSolverFunction.get()))) {
                    if (selectionOrder.size() < 10) {
                        selectionOrder.add(var.negate());
                    }
                }
                final boolean res = solver.satWithSelectionOrder(selectionOrder) == Tristate.TRUE;
                assertThat(res).isEqualTo(expectedResults.get(fileName));
                if (expectedResults.get(fileName)) {
                    final Assignment assignment = solver.model();
                    testLocalMinimum(solver, assignment, selectionOrder);
                    testHighestLexicographicalAssignment(solver, assignment, selectionOrder);
                }
            }
        }
        solver.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Assignment(org.logicng.datastructures.Assignment) Literal(org.logicng.formulas.Literal) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) LongRunningTag(org.logicng.LongRunningTag) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Aggregations

Literal (org.logicng.formulas.Literal)115 Formula (org.logicng.formulas.Formula)51 ArrayList (java.util.ArrayList)38 Test (org.junit.jupiter.api.Test)32 Variable (org.logicng.formulas.Variable)29 FormulaFactory (org.logicng.formulas.FormulaFactory)25 PBConstraint (org.logicng.formulas.PBConstraint)21 TreeSet (java.util.TreeSet)17 SATSolver (org.logicng.solvers.SATSolver)14 Assignment (org.logicng.datastructures.Assignment)13 LNGVector (org.logicng.collections.LNGVector)12 HashMap (java.util.HashMap)11 LogicNGTest (org.logicng.LogicNGTest)11 LNGIntVector (org.logicng.collections.LNGIntVector)8 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)8 PropositionalParser (org.logicng.io.parsers.PropositionalParser)8 BufferedReader (java.io.BufferedReader)7 FileReader (java.io.FileReader)7 BinaryOperator (org.logicng.formulas.BinaryOperator)7 Not (org.logicng.formulas.Not)7