Search in sources :

Example 16 with CardinalityConstraint

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

the class CCAMOTest method testAMO1.

@Test
public void testAMO1() {
    final FormulaFactory f = new FormulaFactory();
    final CardinalityConstraint cc = (CardinalityConstraint) f.amo(f.variable("v0"));
    for (final CCConfig config : this.configs) {
        assertThat(new CCEncoder(f, config).encode(cc)).isEmpty();
    }
    assertThat(f.newCCVariable().name()).endsWith("_0");
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 17 with CardinalityConstraint

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

the class CCIncrementalFormulaTest method testLargeModularTotalizerAMK.

@Test
@LongRunningTag
public void testLargeModularTotalizerAMK() {
    for (final SATSolver solver : this.solvers) {
        final CCEncoder encoder = this.encoders[2];
        final CCEncoder initialEncoder = 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();
        solver.reset();
        // >= 42
        solver.add(initialEncoder.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) LongRunningTag(org.logicng.LongRunningTag) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 18 with CardinalityConstraint

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

the class CCIncrementalFormulaTest method testSimpleIncrementalALK.

@Test
public void testSimpleIncrementalALK() {
    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.GE, 2, 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());
        // >=2
        assertSolverSat(solver);
        // >= 3
        solver.add(incData.newLowerBound(3));
        assertSolverSat(solver);
        // >= 4
        solver.add(incData.newLowerBound(4));
        assertSolverSat(solver);
        // >= 5
        solver.add(incData.newLowerBound(5));
        assertSolverSat(solver);
        // >= 6
        solver.add(incData.newLowerBound(6));
        assertSolverSat(solver);
        // >= 7
        solver.add(incData.newLowerBound(7));
        assertSolverSat(solver);
        final SolverState state = solver.saveState();
        // >= 8
        solver.add(incData.newLowerBound(8));
        assertSolverUnsat(solver);
        solver.loadState(state);
        assertSolverSat(solver);
        // <= 9
        solver.add(incData.newLowerBound(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) List(java.util.List) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 19 with CardinalityConstraint

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

the class CCIncrementalSolverTest method testVeryLargeModularTotalizerAMK.

@Test
@LongRunningTag
public void testVeryLargeModularTotalizerAMK() {
    this.f.putConfiguration(this.configs[2]);
    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 SATSolver solver = this.solvers[3];
    solver.reset();
    solver.add(this.f.cc(CType.GE, 234, vars));
    final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.LE, currentBound, vars));
    // search the lower bound
    while (solver.sat() == Tristate.TRUE) {
        incData.newUpperBoundForSolver(--currentBound);
    }
    assertThat(currentBound).isEqualTo(233);
}
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 20 with CardinalityConstraint

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

the class CCIncrementalSolverTest method testLargeTotalizerLowerBoundALK.

@Test
public void testLargeTotalizerLowerBoundALK() {
    this.f.putConfiguration(this.configs[2]);
    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 SATSolver solver = this.solvers[0];
    solver.reset();
    solver.add(this.f.cc(CType.LE, 87, vars));
    this.f.putConfiguration(this.configs[0]);
    final CCIncrementalData incData = solver.addIncrementalCC((CardinalityConstraint) this.f.cc(CType.GE, currentBound, vars));
    // search the lower bound
    while (solver.sat() == Tristate.TRUE) {
        // <= currentBound + 1
        incData.newLowerBoundForSolver(++currentBound);
    }
    assertThat(currentBound).isEqualTo(88);
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Aggregations

CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)24 Test (org.junit.jupiter.api.Test)20 Variable (org.logicng.formulas.Variable)19 LogicNGTest (org.logicng.LogicNGTest)18 SATSolver (org.logicng.solvers.SATSolver)14 List (java.util.List)7 FormulaFactory (org.logicng.formulas.FormulaFactory)7 Formula (org.logicng.formulas.Formula)5 LongRunningTag (org.logicng.LongRunningTag)4 Assignment (org.logicng.datastructures.Assignment)4 SolverState (org.logicng.solvers.SolverState)4 CCIncrementalData (org.logicng.cardinalityconstraints.CCIncrementalData)2 PBConstraint (org.logicng.formulas.PBConstraint)2 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 BEST (org.logicng.cardinalityconstraints.CCConfig.AMO_ENCODER.BEST)1