Search in sources :

Example 11 with CardinalityConstraint

use of org.logicng.formulas.CardinalityConstraint 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 12 with CardinalityConstraint

use of org.logicng.formulas.CardinalityConstraint 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 13 with CardinalityConstraint

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

the class OptimizationFunctionTest method testExoModel.

@ParameterizedTest
@MethodSource("solvers")
public void testExoModel(final SATSolver solver) {
    final FormulaFactory f = solver.factory();
    final CardinalityConstraint exo = (CardinalityConstraint) f.exo(f.variable("a"), f.variable("b"), f.variable("c"));
    final Assignment minimumModel = optimize(Collections.singleton(exo), exo.variables(), Collections.emptyList(), false, solver, null);
    testMinimumModel(exo, minimumModel, exo.variables());
    final Assignment maximumModel = optimize(Collections.singleton(exo), exo.variables(), Collections.emptyList(), true, solver, null);
    testMaximumModel(exo, maximumModel, exo.variables());
}
Also used : Assignment(org.logicng.datastructures.Assignment) FormulaFactory(org.logicng.formulas.FormulaFactory) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 14 with CardinalityConstraint

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

the class FormulaRandomizerTest method testCc.

@Test
public void testCc() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().seed(4242).weightPbcTypeLe(5).weightPbcTypeLt(4).weightPbcTypeGe(3).weightPbcTypeGt(2).weightPbcTypeEq(1).maximumCoefficientPbc(10).build());
    int le = 0;
    int lt = 0;
    int ge = 0;
    int gt = 0;
    int eq = 0;
    for (int i = 0; i < 500; i++) {
        final Formula formula = random.cc();
        assertThat(formula).isInstanceOf(CardinalityConstraint.class);
        final CardinalityConstraint cc = (CardinalityConstraint) formula;
        assertThat(cc.isCC()).isTrue();
        assertThat(cc.numberOfOperands()).isLessThanOrEqualTo(10);
        if (cc.comparator() == CType.GT) {
            assertThat(cc.rhs()).isStrictlyBetween(-2, 11);
        } else if (cc.comparator() == CType.LT) {
            assertThat(cc.rhs()).isStrictlyBetween(0, 11);
        } else {
            assertThat(cc.rhs()).isStrictlyBetween(-1, 11);
        }
        switch(cc.comparator()) {
            case LE:
                le++;
                break;
            case LT:
                lt++;
                break;
            case GE:
                ge++;
                break;
            case GT:
                gt++;
                break;
            case EQ:
                eq++;
                break;
        }
    }
    assertThat(le).isStrictlyBetween((int) (.7 * 5 / 4 * lt), (int) (1.3 * 5 / 4 * lt));
    assertThat(lt).isStrictlyBetween((int) (.7 * 4 / 3 * ge), (int) (1.3 * 4 / 3 * ge));
    assertThat(ge).isStrictlyBetween((int) (.7 * 3 / 2 * gt), (int) (1.3 * 3 / 2 * gt));
    assertThat(gt).isStrictlyBetween((int) (.7 * 2 / 1 * eq), (int) (1.3 * 2 / 1 * eq));
}
Also used : Formula(org.logicng.formulas.Formula) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

Example 15 with CardinalityConstraint

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

the class FormulaRandomizerTest method testMaximumOperandsCc.

@Test
public void testMaximumOperandsCc() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().maximumOperandsOr(10).seed(42).build());
    for (int i = 0; i < 100; i++) {
        final Formula formula = random.cc();
        assertThat(formula.type()).isEqualTo(FType.PBC);
        final CardinalityConstraint cc = (CardinalityConstraint) formula;
        assertThat(cc.literals().size()).isLessThanOrEqualTo(10);
    }
}
Also used : Formula(org.logicng.formulas.Formula) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

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