Search in sources :

Example 26 with SATSolver

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

the class IncDecTest method testIncDecDeep.

@Test
public void testIncDecDeep() {
    for (final SATSolver s : this.solvers) {
        s.add(this.f.variable("a"));
        final SolverState state1 = s.saveState();
        s.add(this.f.variable("b"));
        assertSolverSat(s);
        final SolverState state2 = s.saveState();
        s.add(this.f.literal("a", false));
        assertSolverUnsat(s);
        s.loadState(state1);
        try {
            s.loadState(state2);
            assert false;
        } catch (final IllegalArgumentException e) {
        // fine
        }
        s.add(this.f.literal("b", false));
        assertSolverSat(s);
        final SolverState state3 = s.saveState();
        s.add(this.f.literal("a", false));
        assertSolverUnsat(s);
        s.loadState(state3);
        s.add(this.f.variable("c"));
        final SolverState state4 = s.saveState();
        final SolverState state5 = s.saveState();
        s.loadState(state4);
        try {
            s.loadState(state5);
            assert false;
        } catch (final IllegalArgumentException e) {
        // fine
        }
        assertSolverSat(s);
        s.loadState(state1);
        assertSolverSat(s);
        try {
            s.loadState(state3);
            assert false;
        } catch (final IllegalArgumentException e) {
        // fine
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) SolverState(org.logicng.solvers.SolverState) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 27 with SATSolver

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

the class SATTest method testModelEnumerationWithHandler02.

@Test
public void testModelEnumerationWithHandler02() {
    for (int i = 0; i < this.solvers.length - 1; i++) {
        final SATSolver s = this.solvers[i];
        final SortedSet<Variable> lits = new TreeSet<>();
        final SortedSet<Variable> firstFive = new TreeSet<>();
        for (int j = 0; j < 20; j++) {
            final Variable lit = this.f.variable("x" + j);
            lits.add(lit);
            if (j < 5) {
                firstFive.add(lit);
            }
        }
        s.add(this.f.cc(CType.GE, 1, lits));
        final NumberOfModelsHandler handler = new NumberOfModelsHandler(29);
        final List<Assignment> modelsWithHandler = s.execute(ModelEnumerationFunction.builder().additionalVariables(Collections.singletonList(firstFive.first())).handler(handler).build());
        assertThat(handler.aborted()).isTrue();
        assertThat(modelsWithHandler.size()).isEqualTo(29);
        for (final Assignment model : modelsWithHandler) {
            for (final Variable lit : lits) {
                assertThat(model.positiveVariables().contains(lit) || model.negativeVariables().contains(lit)).isTrue();
            }
        }
        s.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet) NumberOfModelsHandler(org.logicng.handlers.NumberOfModelsHandler) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 28 with SATSolver

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

the class SATTest method testPigeonHole7.

@Test
public void testPigeonHole7() {
    for (final SATSolver s : this.solvers) {
        s.add(this.pg.generate(7));
        assertSolverUnsat(s);
        assertThat(s.model()).isNull();
        s.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) LogicNGTest(org.logicng.LogicNGTest) Test(org.junit.jupiter.api.Test)

Example 29 with SATSolver

use of org.logicng.solvers.SATSolver 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 30 with SATSolver

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

the class SATTest method testFormula1.

@Test
public void testFormula1() throws ParserException {
    for (final SATSolver s : this.solvers) {
        s.add(this.parser.parse("(x => y) & (~x => y) & (y => z) & (z => ~x)"));
        assertSolverSat(s);
        assertThat(s.model().size()).isEqualTo(3);
        assertThat(s.model().evaluateLit(this.f.variable("x"))).isFalse();
        assertThat(s.model().evaluateLit(this.f.variable("y"))).isTrue();
        assertThat(s.model().evaluateLit(this.f.variable("z"))).isTrue();
        s.add(this.f.variable("x"));
        assertSolverUnsat(s);
        s.reset();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) 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