Search in sources :

Example 1 with And

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

the class FormulaRandomizerTest method testMaximumOperandsAnd.

@Test
public void testMaximumOperandsAnd() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().maximumOperandsAnd(10).seed(42).build());
    for (int i = 0; i < 100; i++) {
        final Formula formula = random.and(1);
        assertThat(formula.type()).isEqualTo(FType.AND);
        final And and = (And) formula;
        assertThat(and.numberOfOperands()).isLessThanOrEqualTo(10);
    }
}
Also used : Formula(org.logicng.formulas.Formula) And(org.logicng.formulas.And) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

Example 2 with And

use of org.logicng.formulas.And 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 3 with And

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

the class TseitinTransformation method computeTseitin.

/**
 * Computes the Tseitin transformation for a given formula and stores it in the formula cache.
 * @param formula the formula
 */
private void computeTseitin(final Formula formula) {
    if (formula.transformationCacheEntry(TSEITIN) != null) {
        return;
    }
    final FormulaFactory f = formula.factory();
    switch(formula.type()) {
        case LITERAL:
            formula.setTransformationCacheEntry(TSEITIN, formula);
            formula.setTransformationCacheEntry(TSEITIN_VARIABLE, formula);
            break;
        case AND:
        case OR:
            final boolean isConjunction = formula instanceof And;
            final Literal tsLiteral = f.newCNFVariable();
            final List<Formula> nops = new ArrayList<>();
            final List<Formula> operands = new ArrayList<>(formula.numberOfOperands());
            final List<Formula> negOperands = new ArrayList<>(formula.numberOfOperands());
            if (isConjunction) {
                negOperands.add(tsLiteral);
                handleNary(formula, nops, operands, negOperands);
                for (final Formula operand : operands) {
                    nops.add(f.or(tsLiteral.negate(), operand));
                }
                nops.add(f.or(negOperands));
            } else {
                operands.add(tsLiteral.negate());
                handleNary(formula, nops, operands, negOperands);
                for (final Formula operand : negOperands) {
                    nops.add(f.or(tsLiteral, operand));
                }
                nops.add(f.or(operands));
            }
            formula.setTransformationCacheEntry(TSEITIN_VARIABLE, tsLiteral);
            formula.setTransformationCacheEntry(TSEITIN, f.and(nops));
            break;
        default:
            throw new IllegalArgumentException("Could not process the formula type " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) And(org.logicng.formulas.And) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList)

Example 4 with And

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

the class BDDFactory method buildRec.

/**
 * Recursive build procedure for the BDD.
 * <p>
 * If a BDD handler is given and the BDD generation is aborted due to the handler, the method will return
 * {@link BDDKernel#BDD_ABORT} as result. If {@code null} is passed as handler, the generation will continue without
 * interruption.
 * @param formula      the formula
 * @param kernel       the BDD kernel
 * @param construction the BDD construction instance
 * @param handler      the BDD handler
 * @return the BDD index or {@link BDDKernel#BDD_ABORT} if the computation was aborted
 */
private static int buildRec(final Formula formula, final BDDKernel kernel, final BDDConstruction construction, final BDDHandler handler) {
    switch(formula.type()) {
        case FALSE:
            return BDDKernel.BDD_FALSE;
        case TRUE:
            return BDDKernel.BDD_TRUE;
        case LITERAL:
            final Literal lit = (Literal) formula;
            final int idx = kernel.getOrAddVarIndex(lit.variable());
            return lit.phase() ? construction.ithVar(idx) : construction.nithVar(idx);
        case NOT:
            {
                final Not not = (Not) formula;
                final int operand = buildRec(not.operand(), kernel, construction, handler);
                if (operand == BDDKernel.BDD_ABORT) {
                    return BDDKernel.BDD_ABORT;
                }
                final int res = kernel.addRef(construction.not(operand), handler);
                kernel.delRef(operand);
                return res;
            }
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            final int left = buildRec(binary.left(), kernel, construction, handler);
            if (left == BDDKernel.BDD_ABORT) {
                return BDDKernel.BDD_ABORT;
            }
            final int right = buildRec(binary.right(), kernel, construction, handler);
            if (right == BDDKernel.BDD_ABORT) {
                return BDDKernel.BDD_ABORT;
            }
            int res = kernel.addRef(binary instanceof Implication ? construction.implication(left, right) : construction.equivalence(left, right), handler);
            kernel.delRef(left);
            kernel.delRef(right);
            return res;
        case AND:
        case OR:
            {
                final Iterator<Formula> it = formula.iterator();
                res = buildRec(it.next(), kernel, construction, handler);
                if (res == BDDKernel.BDD_ABORT) {
                    return BDDKernel.BDD_ABORT;
                }
                while (it.hasNext()) {
                    final int operand = buildRec(it.next(), kernel, construction, handler);
                    if (operand == BDDKernel.BDD_ABORT) {
                        return BDDKernel.BDD_ABORT;
                    }
                    final int previous = res;
                    res = formula instanceof And ? kernel.addRef(construction.and(res, operand), handler) : kernel.addRef(construction.or(res, operand), handler);
                    kernel.delRef(previous);
                    kernel.delRef(operand);
                }
                return res;
            }
        case PBC:
            return buildRec(formula.nnf(), kernel, construction, handler);
        default:
            throw new IllegalArgumentException("Unsupported operator for BDD generation: " + formula.type());
    }
}
Also used : Not(org.logicng.formulas.Not) And(org.logicng.formulas.And) Literal(org.logicng.formulas.Literal) Iterator(java.util.Iterator) BinaryOperator(org.logicng.formulas.BinaryOperator) Implication(org.logicng.formulas.Implication)

Aggregations

And (org.logicng.formulas.And)4 Literal (org.logicng.formulas.Literal)3 Formula (org.logicng.formulas.Formula)2 Implication (org.logicng.formulas.Implication)2 Not (org.logicng.formulas.Not)2 PBConstraint (org.logicng.formulas.PBConstraint)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Test (org.junit.jupiter.api.Test)1 BinaryOperator (org.logicng.formulas.BinaryOperator)1 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)1 Equivalence (org.logicng.formulas.Equivalence)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 Or (org.logicng.formulas.Or)1