Search in sources :

Example 6 with SATSolver

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

the class CCEXOTest method testEXO.

private void testEXO(final int numLits, 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.exo(problemLits));
    assertSolverSat(solver);
    assertThat(solver.enumerateAllModels(problemLits)).hasSize(numLits).allMatch(m -> m.positiveVariables().size() == 1);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint)

Example 7 with SATSolver

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

the class CCIncrementalFormulaTest method testSimpleIncrementalAMK.

@Test
public void testSimpleIncrementalAMK() {
    for (final CCEncoder encoder : this.encoders) {
        final CCEncoder initialEncoder = new CCEncoder(this.f);
        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 Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LE, 9, vars));
        final CCIncrementalData incData = cc.second();
        final SATSolver solver = MiniSat.miniSat(this.f);
        // >= 4
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.GE, 4, vars)));
        // <= 7
        solver.add(initialEncoder.encode((CardinalityConstraint) this.f.cc(CType.LE, 7, vars)));
        solver.add(cc.first());
        assertSolverSat(solver);
        // <= 9
        assertSolverSat(solver);
        // <= 8
        solver.add(incData.newUpperBound(8));
        assertSolverSat(solver);
        assertThat(incData.currentRHS()).isEqualTo(8);
        // <= 7
        solver.add(incData.newUpperBound(7));
        assertSolverSat(solver);
        // <= 6
        solver.add(incData.newUpperBound(6));
        assertSolverSat(solver);
        // <= 5
        solver.add(incData.newUpperBound(5));
        assertSolverSat(solver);
        // <= 4
        solver.add(incData.newUpperBound(4));
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // <= 3
        solver.add(incData.newUpperBound(3));
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 2
        solver.add(incData.newUpperBound(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) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 8 with SATSolver

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

the class CCIncrementalFormulaTest method testLargeTotalizerLowerBoundALK.

@Test
public void testLargeTotalizerLowerBoundALK() {
    final CCEncoder encoder = this.encoders[0];
    final CCEncoder initivalEncoder = new CCEncoder(this.f);
    final int numLits = 100;
    int currentBound = 2;
    final Variable[] vars = new Variable[numLits];
    for (int i = 0; i < numLits; i++) {
        vars[i] = this.f.variable("v" + i);
    }
    final Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.GE, currentBound, vars));
    final CCIncrementalData incData = cc.second();
    final SATSolver solver = this.solvers[3];
    solver.reset();
    // <= 42
    solver.add(initivalEncoder.encode((CardinalityConstraint) this.f.cc(CType.LE, 87, vars)));
    solver.add(cc.first());
    // search the lower bound
    while (solver.sat() == Tristate.TRUE) {
        // <= currentBound + 1
        solver.add(incData.newLowerBound(++currentBound));
    }
    assertThat(currentBound).isEqualTo(88);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 9 with SATSolver

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

the class CCIncrementalFormulaTest method testLargeTotalizerUpperBoundAMK.

@Test
public void testLargeTotalizerUpperBoundAMK() {
    final CCEncoder encoder = this.encoders[0];
    final CCEncoder initivalEncoder = new CCEncoder(this.f);
    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);
    }
    final Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LE, currentBound, vars));
    final CCIncrementalData incData = cc.second();
    final SATSolver solver = this.solvers[3];
    solver.reset();
    // >= 42
    solver.add(initivalEncoder.encode((CardinalityConstraint) this.f.cc(CType.GE, 42, vars)));
    solver.add(cc.first());
    // search the lower bound
    while (solver.sat() == Tristate.TRUE) {
        // <= currentBound - 1
        solver.add(incData.newUpperBound(--currentBound));
    }
    assertThat(currentBound).isEqualTo(41);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 10 with SATSolver

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

the class CCIncrementalFormulaTest method testVeryLargeModularTotalizerAMK.

@Test
@LongRunningTag
public void testVeryLargeModularTotalizerAMK() {
    final CCEncoder encoder = this.encoders[2];
    final CCEncoder initivalEncoder = new CCEncoder(this.f);
    final int numLits = 300;
    int currentBound = numLits - 1;
    final Variable[] vars = new Variable[numLits];
    for (int i = 0; i < numLits; i++) {
        vars[i] = this.f.variable("v" + i);
    }
    final Pair<List<Formula>, CCIncrementalData> cc = encoder.encodeIncremental((CardinalityConstraint) this.f.cc(CType.LE, currentBound, vars));
    final CCIncrementalData incData = cc.second();
    final SATSolver solver = this.solvers[3];
    solver.reset();
    solver.add(initivalEncoder.encode((CardinalityConstraint) this.f.cc(CType.GE, 234, vars)));
    solver.add(cc.first());
    // search the lower bound
    while (solver.sat() == Tristate.TRUE) {
        solver.add(incData.newUpperBound(--currentBound));
    }
    assertThat(currentBound).isEqualTo(233);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) LongRunningTag(org.logicng.LongRunningTag) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

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