Search in sources :

Example 6 with Not

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

the class FactorOutSimplifier method applyRec.

private Formula applyRec(final Formula formula, final boolean cache) {
    switch(formula.type()) {
        case OR:
        case AND:
            final List<Formula> newOps = new ArrayList<>();
            for (final Formula op : formula) {
                newOps.add(apply(op, cache));
            }
            final Formula newFormula = formula.factory().naryOperator(formula.type(), newOps);
            return newFormula instanceof NAryOperator ? simplify((NAryOperator) newFormula) : newFormula;
        case NOT:
            return apply(((Not) formula).operand(), cache).negate();
        case FALSE:
        case TRUE:
        case LITERAL:
        case IMPL:
        case EQUIV:
        case PBC:
            return formula;
        default:
            throw new IllegalStateException("Unknown formula type: " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) ArrayList(java.util.ArrayList) NAryOperator(org.logicng.formulas.NAryOperator)

Example 7 with Not

use of org.logicng.formulas.Not 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 8 with Not

use of org.logicng.formulas.Not 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 9 with Not

use of org.logicng.formulas.Not 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 10 with Not

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

Not (org.logicng.formulas.Not)13 Formula (org.logicng.formulas.Formula)9 PBConstraint (org.logicng.formulas.PBConstraint)8 BinaryOperator (org.logicng.formulas.BinaryOperator)7 Literal (org.logicng.formulas.Literal)7 NAryOperator (org.logicng.formulas.NAryOperator)7 FormulaFactory (org.logicng.formulas.FormulaFactory)4 Implication (org.logicng.formulas.Implication)4 ArrayList (java.util.ArrayList)3 Equivalence (org.logicng.formulas.Equivalence)3 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 And (org.logicng.formulas.And)2 Variable (org.logicng.formulas.Variable)2 Iterator (java.util.Iterator)1 EncodingResult (org.logicng.datastructures.EncodingResult)1 Tristate (org.logicng.datastructures.Tristate)1 FType (org.logicng.formulas.FType)1 Or (org.logicng.formulas.Or)1