Search in sources :

Example 11 with SATSolver

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

the class CCIncrementalSolverTest method testSimpleIncrementalAMK.

@Test
public void testSimpleIncrementalAMK() {
    for (final CCConfig config : this.configs) {
        this.f.putConfiguration(this.configs[2]);
        final int numLits = 10;
        final Variable[] vars = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            vars[i] = this.f.variable("v" + i);
        }
        final SATSolver solver = MiniSat.miniSat(this.f);
        // >= 4
        solver.add(this.f.cc(CType.GE, 4, vars));
        // <= 7
        solver.add(this.f.cc(CType.LE, 7, vars));
        this.f.putConfiguration(config);
        final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.LE, 9, vars));
        // <= 9
        assertSolverSat(solver);
        // <= 8
        incData.newUpperBoundForSolver(8);
        assertSolverSat(solver);
        // <= 7
        incData.newUpperBoundForSolver(7);
        assertSolverSat(solver);
        // <= 6
        incData.newUpperBoundForSolver(6);
        assertSolverSat(solver);
        // <= 5
        incData.newUpperBoundForSolver(5);
        assertSolverSat(solver);
        // <= 4
        incData.newUpperBoundForSolver(4);
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // <= 3
        incData.newUpperBoundForSolver(3);
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 2
        incData.newUpperBoundForSolver(2);
        assertSolverUnsat(solver);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 12 with SATSolver

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

the class CCIncrementalSolverTest method testLargeModularTotalizerAMK.

@Test
@LongRunningTag
public void testLargeModularTotalizerAMK() {
    for (final SATSolver solver : this.solvers) {
        if (solver != null) {
            this.f.putConfiguration(this.configs[2]);
            final int numLits = 100;
            int currentBound = numLits - 1;
            final Variable[] vars = new Variable[numLits];
            for (int i = 0; i < numLits; i++) {
                vars[i] = this.f.variable("v" + i);
            }
            solver.reset();
            // >= 42
            solver.add(this.f.cc(CType.GE, 42, vars));
            final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.LE, currentBound, vars));
            // search the lower bound
            while (solver.sat() == Tristate.TRUE) {
                // <= currentBound - 1
                incData.newUpperBoundForSolver(--currentBound);
            }
            assertThat(currentBound).isEqualTo(41);
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) LongRunningTag(org.logicng.LongRunningTag) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 13 with SATSolver

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

the class CCIncrementalSolverTest method testSimpleIncrementalALK.

@Test
public void testSimpleIncrementalALK() {
    for (final CCConfig config : this.configs) {
        this.f.putConfiguration(this.configs[2]);
        final int numLits = 10;
        final Variable[] vars = new Variable[numLits];
        for (int i = 0; i < numLits; i++) {
            vars[i] = this.f.variable("v" + i);
        }
        final SATSolver solver = this.solvers[2];
        solver.reset();
        // >= 4
        solver.add(this.f.cc(CType.GE, 4, vars));
        // <= 7
        solver.add(this.f.cc(CType.LE, 7, vars));
        this.f.putConfiguration(config);
        final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.GE, 2, vars));
        // >=2
        assertSolverSat(solver);
        // >= 3
        incData.newLowerBoundForSolver(3);
        assertSolverSat(solver);
        // >= 4
        incData.newLowerBoundForSolver(4);
        assertSolverSat(solver);
        // >= 5
        incData.newLowerBoundForSolver(5);
        assertSolverSat(solver);
        // >= 6
        incData.newLowerBoundForSolver(6);
        assertSolverSat(solver);
        // >= 7
        incData.newLowerBoundForSolver(7);
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // >= 8
        incData.newLowerBoundForSolver(8);
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 9
        incData.newLowerBoundForSolver(9);
        assertSolverUnsat(solver);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) SolverState(org.logicng.solvers.SolverState) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 14 with SATSolver

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

the class CCPerformanceTest method buildAMK.

private void buildAMK(final int numLits, 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);
    for (int i = 10; i < 100; i = i + 10) {
        final CardinalityConstraint cc = (CardinalityConstraint) f.cc(CType.LE, i, problemLits);
        solver.reset();
        solver.add(cc);
        assertSolverSat(solver);
        final Assignment model = solver.model();
        assertThat(cc.evaluate(model)).isTrue();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Assignment(org.logicng.datastructures.Assignment) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint)

Example 15 with SATSolver

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

the class DRUPTest method testTrivialCasesPropositions.

@Test
public void testTrivialCasesPropositions() throws ParserException {
    for (final SATSolver solver : this.solvers) {
        assertSolverSat(solver);
        final StandardProposition p1 = new StandardProposition("P1", this.f.parse("$false"));
        solver.add(p1);
        assertSolverUnsat(solver);
        UNSATCore<Proposition> unsatCore = solver.unsatCore();
        assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p1);
        solver.reset();
        assertSolverSat(solver);
        final StandardProposition p2 = new StandardProposition("P2", this.f.parse("a"));
        solver.add(p2);
        assertSolverSat(solver);
        final StandardProposition p3 = new StandardProposition("P3", this.f.parse("~a"));
        solver.add(p3);
        assertSolverUnsat(solver);
        unsatCore = solver.unsatCore();
        assertThat(unsatCore.propositions()).containsExactlyInAnyOrder(p2, p3);
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) StandardProposition(org.logicng.propositions.StandardProposition) 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)

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