Search in sources :

Example 21 with PBConstraint

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

the class FormulaRandomizerTest method testAtom.

@Test
public void testAtom() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().weightConstant(1).weightPositiveLiteral(2).weightNegativeLiteral(3).weightPbc(4).weightCc(5).weightAmo(6).weightExo(7).seed(42).build());
    int numConst = 0;
    int numPos = 0;
    int numNeg = 0;
    int numPbc = 0;
    int numCc = 0;
    int numAmo = 0;
    int numExo = 0;
    for (int i = 0; i < 1000; i++) {
        final Formula formula = random.atom();
        assertThat(formula.isAtomicFormula()).isTrue();
        if (formula.isConstantFormula()) {
            numConst++;
        } else if (formula.type() == FType.LITERAL) {
            if (((Literal) formula).phase()) {
                numPos++;
            } else {
                numNeg++;
            }
        } else {
            final PBConstraint pbc = (PBConstraint) formula;
            if (!pbc.isCC()) {
                numPbc++;
            } else if (pbc.rhs() == 1 && pbc.comparator() == CType.LE) {
                numAmo++;
            } else if (pbc.rhs() == 1 && pbc.comparator() == CType.EQ) {
                numExo++;
            } else {
                numCc++;
            }
        }
    }
    assertThat(numExo).isStrictlyBetween((int) (.8 * 7 / 6 * numAmo), (int) (1.2 * 7 / 6 * numAmo));
    assertThat(numAmo).isStrictlyBetween((int) (.8 * 6 / 5 * numCc), (int) (1.2 * 6 / 5 * numCc));
    assertThat(numCc).isStrictlyBetween((int) (.8 * 5 / 4 * numPbc), (int) (1.2 * 5 / 4 * numPbc));
    assertThat(numPbc).isStrictlyBetween((int) (.8 * 4 / 3 * numNeg), (int) (1.2 * 4 / 3 * numNeg));
    assertThat(numNeg).isStrictlyBetween((int) (.8 * 3 / 2 * numPos), (int) (1.2 * 3 / 2 * numPos));
    assertThat(numPos).isStrictlyBetween((int) (.8 * 2 / 1 * numConst), (int) (1.2 * 2 / 1 * numConst));
    final FormulaRandomizer randomOnlyLiterals = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().weightConstant(0).weightPositiveLiteral(3).weightNegativeLiteral(6).seed(42).build());
    for (int i = 0; i < 100; i++) {
        assertThat(randomOnlyLiterals.atom().type()).isEqualTo(FType.LITERAL);
    }
}
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 22 with PBConstraint

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

the class FormulaFactoryImporter method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    if (formula.factory() == this.newFormulaFactory) {
        return formula;
    }
    switch(formula.type()) {
        case TRUE:
            return this.newFormulaFactory.verum();
        case FALSE:
            return this.newFormulaFactory.falsum();
        case LITERAL:
            final Literal literal = (Literal) formula;
            return this.newFormulaFactory.literal(literal.name(), literal.phase());
        case NOT:
            final Not not = (Not) formula;
            return this.newFormulaFactory.not(apply(not.operand(), cache));
        case IMPL:
            final Implication implication = (Implication) formula;
            return this.newFormulaFactory.implication(apply(implication.left(), cache), apply(implication.right(), cache));
        case EQUIV:
            final Equivalence equivalence = (Equivalence) formula;
            return this.newFormulaFactory.equivalence(apply(equivalence.left(), cache), apply(equivalence.right(), cache));
        case OR:
            final Or or = (Or) formula;
            return this.newFormulaFactory.or(gatherAppliedOperands(or));
        case AND:
            final And and = (And) formula;
            return this.newFormulaFactory.and(gatherAppliedOperands(and));
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            final Literal[] literals = new Literal[pbc.operands().length];
            for (int i = 0; i < pbc.operands().length; i++) {
                literals[i] = (Literal) apply(pbc.operands()[i], cache);
            }
            return this.newFormulaFactory.pbc(pbc.comparator(), pbc.rhs(), literals, pbc.coefficients());
        default:
            throw new IllegalArgumentException("Unknown LogicNG formula type: " + formula.type());
    }
}
Also used : Not(org.logicng.formulas.Not) Or(org.logicng.formulas.Or) Equivalence(org.logicng.formulas.Equivalence) And(org.logicng.formulas.And) Literal(org.logicng.formulas.Literal) Implication(org.logicng.formulas.Implication) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint)

Example 23 with PBConstraint

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

the class LiteralSubstitution method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    final FormulaFactory f = formula.factory();
    switch(formula.type()) {
        case TRUE:
        case FALSE:
            return formula;
        case LITERAL:
            final Literal literal = (Literal) formula;
            Literal lit = this.substitution.get(literal);
            if (lit != null) {
                return lit;
            }
            if (!literal.phase()) {
                lit = this.substitution.get(literal.variable());
                return lit != null ? lit.negate() : formula;
            }
            return formula;
        case NOT:
            return f.not(apply(((Not) formula).operand(), cache));
        case EQUIV:
        case IMPL:
            final BinaryOperator binOp = (BinaryOperator) formula;
            return f.binaryOperator(formula.type(), apply(binOp.left(), cache), apply(binOp.right(), cache));
        case OR:
        case AND:
            final List<Formula> operands = new ArrayList<>();
            for (final Formula op : formula) {
                operands.add(apply(op, cache));
            }
            return f.naryOperator(formula.type(), operands);
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            final Literal[] originalOperands = pbc.operands();
            final Literal[] literals = new Literal[originalOperands.length];
            for (int i = 0; i < literals.length; i++) {
                literals[i] = (Literal) apply(originalOperands[i], false);
            }
            return f.pbc(pbc.comparator(), pbc.rhs(), literals, pbc.coefficients());
        default:
            throw new IllegalArgumentException("Unknown formula type: " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Not(org.logicng.formulas.Not) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) BinaryOperator(org.logicng.formulas.BinaryOperator) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint)

Example 24 with PBConstraint

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

the class PBSolvingTest method testLargePBs.

@Test
public void testLargePBs() {
    for (final PBEncoder encoder : this.encoders) {
        final SATSolver solver = this.solvers[0];
        solver.reset();
        final int numLits = 100;
        final Variable[] lits = new Variable[numLits];
        final int[] coeffs = new int[numLits];
        for (int i = 0; i < numLits; i++) {
            lits[i] = this.f.variable("v" + i);
            coeffs[i] = i + 1;
        }
        final PBConstraint pbc = (PBConstraint) this.f.pbc(CType.GE, 5000, lits, coeffs);
        solver.add(encoder.encode(pbc));
        assertSolverSat(solver);
        assertThat(pbc.evaluate(solver.model())).isTrue();
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

Example 25 with PBConstraint

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

the class PBSolvingTest method testPBLess.

@Test
public void testPBLess() {
    for (final PBEncoder encoder : this.encoders) {
        for (final SATSolver solver : this.solvers) {
            solver.reset();
            final int[] coeffs10 = new int[] { 3, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 6, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(140).allMatch(model -> model.positiveVariables().size() <= 3);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 7, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(140).allMatch(model -> model.positiveVariables().size() <= 3);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 0, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LE, 1, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 2, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
            solver.reset();
            solver.add(encoder.encode((PBConstraint) this.f.pbc(CType.LT, 1, this.literals10, coeffs10)));
            assertSolverSat(solver);
            assertThat(solver.enumerateAllModels(this.literals10)).hasSize(1);
        }
    }
}
Also used : SATSolver(org.logicng.solvers.SATSolver) PBConstraint(org.logicng.formulas.PBConstraint) Test(org.junit.jupiter.api.Test) LogicNGTest(org.logicng.LogicNGTest)

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