Search in sources :

Example 1 with Implication

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

the class NNFTransformation method applyRec.

private Formula applyRec(final Formula formula, final boolean polarity) {
    final FormulaFactory f = formula.factory();
    Formula nnf;
    if (polarity) {
        nnf = formula.transformationCacheEntry(NNF);
        if (nnf != null) {
            return nnf;
        }
    }
    final FType type = formula.type();
    switch(type) {
        case TRUE:
        case FALSE:
        case LITERAL:
            nnf = polarity ? formula : formula.negate();
            break;
        case NOT:
            nnf = applyRec(((Not) formula).operand(), !polarity);
            break;
        case OR:
        case AND:
            nnf = applyRec(formula.iterator(), formula.type(), polarity, f);
            break;
        case EQUIV:
            final Equivalence equiv = (Equivalence) formula;
            if (polarity) {
                nnf = f.and(f.or(applyRec(equiv.left(), false), applyRec(equiv.right(), true)), f.or(applyRec(equiv.left(), true), applyRec(equiv.right(), false)));
            } else {
                nnf = f.and(f.or(applyRec(equiv.left(), false), applyRec(equiv.right(), false)), f.or(applyRec(equiv.left(), true), applyRec(equiv.right(), true)));
            }
            break;
        case IMPL:
            final Implication impl = (Implication) formula;
            if (polarity) {
                nnf = f.or(applyRec(impl.left(), false), applyRec(impl.right(), true));
            } else {
                nnf = f.and(applyRec(impl.left(), true), applyRec(impl.right(), false));
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            if (polarity) {
                final List<Formula> encoding = pbc.getEncoding();
                nnf = applyRec(encoding.iterator(), FType.AND, true, f);
            } else {
                nnf = applyRec(pbc.negate(), true);
            }
            break;
        default:
            throw new IllegalStateException("Unknown formula type = " + type);
    }
    if (polarity) {
        formula.setTransformationCacheEntry(NNF, nnf);
    }
    return nnf;
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) FType(org.logicng.formulas.FType) Not(org.logicng.formulas.Not) Equivalence(org.logicng.formulas.Equivalence) Implication(org.logicng.formulas.Implication) PBConstraint(org.logicng.formulas.PBConstraint)

Example 2 with Implication

use of org.logicng.formulas.Implication 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 Implication

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

the class DistributiveSimplifier method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    final FormulaFactory f = formula.factory();
    final Formula result;
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
        case PBC:
            result = formula;
            break;
        case EQUIV:
            final Equivalence equiv = (Equivalence) formula;
            result = f.equivalence(this.apply(equiv.left(), cache), this.apply(equiv.right(), cache));
            break;
        case IMPL:
            final Implication impl = (Implication) formula;
            result = f.implication(this.apply(impl.left(), cache), this.apply(impl.right(), cache));
            break;
        case NOT:
            final Not not = (Not) formula;
            result = f.not(this.apply(not.operand(), cache));
            break;
        case OR:
        case AND:
            result = distributeNAry(formula, cache, f);
            break;
        default:
            throw new IllegalStateException("Unknown formula type: " + formula.type());
    }
    if (cache) {
        formula.setTransformationCacheEntry(TransformationCacheEntry.DISTRIBUTIVE_SIMPLIFICATION, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Not(org.logicng.formulas.Not) Equivalence(org.logicng.formulas.Equivalence) Implication(org.logicng.formulas.Implication)

Example 4 with Implication

use of org.logicng.formulas.Implication 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

Implication (org.logicng.formulas.Implication)4 Not (org.logicng.formulas.Not)4 Equivalence (org.logicng.formulas.Equivalence)3 And (org.logicng.formulas.And)2 Formula (org.logicng.formulas.Formula)2 FormulaFactory (org.logicng.formulas.FormulaFactory)2 Literal (org.logicng.formulas.Literal)2 PBConstraint (org.logicng.formulas.PBConstraint)2 Iterator (java.util.Iterator)1 BinaryOperator (org.logicng.formulas.BinaryOperator)1 FType (org.logicng.formulas.FType)1 Or (org.logicng.formulas.Or)1