Search in sources :

Example 16 with PBConstraint

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

the class EvaluatesToConstantPredicateTest method testPBCToTrue.

@Test
public void testPBCToTrue() {
    final PBConstraint pbc01 = (PBConstraint) this.f.pbc(CType.EQ, 2, new Literal[] { this.A, this.B }, new int[] { 2, -4 });
    assertThat(pbc01.holds(this.emptyToTrue)).isFalse();
    assertThat(pbc01.holds(this.aToTrue)).isFalse();
    assertThat(pbc01.holds(this.aNotBToTrue)).isTrue();
    final PBConstraint pbc02 = (PBConstraint) this.f.pbc(CType.GT, 2, new Literal[] { this.B, this.C }, new int[] { 2, 1 });
    assertThat(pbc02.holds(this.emptyToTrue)).isFalse();
    assertThat(pbc02.holds(this.aToTrue)).isFalse();
    assertThat(pbc02.holds(this.aNotBToTrue)).isFalse();
    assertThat(this.PBC1.holds(this.emptyToTrue)).isFalse();
    assertThat(this.PBC1.holds(this.aToTrue)).isFalse();
    assertThat(this.PBC1.holds(this.aNotBToTrue)).isFalse();
    assertThat(this.PBC2.holds(this.emptyToTrue)).isFalse();
    assertThat(this.PBC2.holds(this.aToTrue)).isFalse();
    assertThat(this.PBC2.holds(this.aNotBToTrue)).isFalse();
}
Also used : Literal(org.logicng.formulas.Literal) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test)

Example 17 with PBConstraint

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

the class EvaluatesToConstantPredicateTest method testPBCToFalse.

@Test
public void testPBCToFalse() {
    final PBConstraint pbc01 = (PBConstraint) this.f.pbc(CType.EQ, 2, new Literal[] { this.A, this.B }, new int[] { 2, -4 });
    assertThat(pbc01.holds(this.emptyToFalse)).isFalse();
    assertThat(pbc01.holds(this.aToFalse)).isFalse();
    assertThat(pbc01.holds(this.aNotBToFalse)).isFalse();
    final PBConstraint pbc02 = (PBConstraint) this.f.pbc(CType.GT, 2, new Literal[] { this.B, this.C }, new int[] { 2, 1 });
    assertThat(pbc02.holds(this.emptyToFalse)).isFalse();
    assertThat(pbc02.holds(this.aToFalse)).isFalse();
    assertThat(pbc02.holds(this.aNotBToFalse)).isTrue();
    assertThat(this.PBC1.holds(this.emptyToFalse)).isFalse();
    assertThat(this.PBC1.holds(this.aToFalse)).isFalse();
    assertThat(this.PBC1.holds(this.aNotBToFalse)).isFalse();
    assertThat(this.PBC2.holds(this.emptyToFalse)).isFalse();
    assertThat(this.PBC2.holds(this.aToFalse)).isFalse();
    assertThat(this.PBC2.holds(this.aNotBToFalse)).isFalse();
}
Also used : Literal(org.logicng.formulas.Literal) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test)

Example 18 with PBConstraint

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

the class FormulaRandomizerTest method testPbc.

@Test
public void testPbc() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().seed(4242).weightPbcCoeffPositive(3).weightPbcCoeffNegative(1).weightPositiveLiteral(3).weightNegativeLiteral(1).weightPbcTypeLe(5).weightPbcTypeLt(4).weightPbcTypeGe(3).weightPbcTypeGt(2).weightPbcTypeEq(1).maximumCoefficientPbc(10).build());
    int posCoeff = 0;
    int negCoeff = 0;
    int posLit = 0;
    int negLit = 0;
    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.pbc();
        assertThat(formula).isInstanceOf(PBConstraint.class);
        final PBConstraint pbc = (PBConstraint) formula;
        int posSum = 0;
        int negSum = 0;
        for (final int coefficient : pbc.coefficients()) {
            if (coefficient > 0) {
                posCoeff++;
                posSum += coefficient;
            } else {
                negCoeff++;
                negSum += coefficient;
            }
        }
        assertThat(pbc.numberOfOperands()).isLessThanOrEqualTo(10);
        assertThat(pbc.rhs()).isStrictlyBetween(negSum - 1, posSum + 1);
        for (final Literal literal : pbc.literals()) {
            if (literal.phase()) {
                posLit++;
            } else {
                negLit++;
            }
        }
        switch(pbc.comparator()) {
            case LE:
                le++;
                break;
            case LT:
                lt++;
                break;
            case GE:
                ge++;
                break;
            case GT:
                gt++;
                break;
            case EQ:
                eq++;
                break;
        }
    }
    assertThat(posCoeff).isStrictlyBetween((int) (.8 * 3 * negCoeff), (int) (1.2 * 3 * negCoeff));
    assertThat(posLit).isStrictlyBetween((int) (.8 * 3 * negLit), (int) (1.2 * 3 * negLit));
    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) Literal(org.logicng.formulas.Literal) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

Example 19 with PBConstraint

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

the class FormulaRandomizerTest method testMaximumOperandsPbc.

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

Example 20 with PBConstraint

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

the class FormulaRandomizerTest method testAmo.

@Test
public void testAmo() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().seed(4242).maximumCoefficientPbc(10).build());
    for (int i = 0; i < 100; i++) {
        final Formula formula = random.amo();
        assertThat(formula).isInstanceOf(PBConstraint.class);
        final PBConstraint amo = (PBConstraint) formula;
        assertThat(amo.isCC()).isTrue();
        assertThat(amo.rhs()).isEqualTo(1);
        assertThat(amo.comparator()).isEqualTo(CType.LE);
    }
}
Also used : Formula(org.logicng.formulas.Formula) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

Aggregations

PBConstraint (org.logicng.formulas.PBConstraint)35 Formula (org.logicng.formulas.Formula)22 Test (org.junit.jupiter.api.Test)21 Literal (org.logicng.formulas.Literal)15 Variable (org.logicng.formulas.Variable)11 ArrayList (java.util.ArrayList)9 LogicNGTest (org.logicng.LogicNGTest)9 FormulaFactory (org.logicng.formulas.FormulaFactory)9 BinaryOperator (org.logicng.formulas.BinaryOperator)8 Not (org.logicng.formulas.Not)8 SATSolver (org.logicng.solvers.SATSolver)8 NAryOperator (org.logicng.formulas.NAryOperator)6 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)5 List (java.util.List)3 Set (java.util.Set)3 CNFFactorization (org.logicng.transformations.cnf.CNFFactorization)3 BigInteger (java.math.BigInteger)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 EncodingResult (org.logicng.datastructures.EncodingResult)2