Search in sources :

Example 6 with BinaryOperator

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

Example 7 with BinaryOperator

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

the class NumberOfAtomsFunction method apply.

@Override
public Long apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(NUMBER_OF_ATOMS);
    if (cached != null) {
        return (Long) cached;
    }
    long result = 0L;
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
        case PBC:
            result = 1L;
            break;
        case NOT:
            result = apply(((Not) formula).operand(), cache);
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            result = apply(binary.left(), cache) + apply(binary.right(), cache);
            break;
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            for (final Formula op : nary) {
                result += apply(op, cache);
            }
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    if (cache) {
        formula.setFunctionCacheEntry(NUMBER_OF_ATOMS, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator)

Example 8 with BinaryOperator

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

the class NumberOfNodesFunction method apply.

@Override
public Long apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(NUMBER_OF_NODES);
    if (cached != null) {
        return (Long) cached;
    }
    long result;
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
            result = 1L;
            break;
        case NOT:
            result = apply(((Not) formula).operand(), cache) + 1L;
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            result = apply(binary.left(), cache) + apply(binary.right(), cache) + 1L;
            break;
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            result = 1L;
            for (final Formula op : nary) {
                result += apply(op, cache);
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            result = 1L + pbc.operands().length;
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    if (cache) {
        formula.setFunctionCacheEntry(NUMBER_OF_NODES, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 9 with BinaryOperator

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

the class LiteralsFunction method apply.

@Override
public SortedSet<Literal> apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(LITERALS);
    if (cached != null) {
        return (SortedSet<Literal>) cached;
    }
    SortedSet<Literal> result = new TreeSet<>();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
            result = new TreeSet<>();
            break;
        case LITERAL:
            final Literal lit = (Literal) formula;
            result.add(lit);
            break;
        case NOT:
            final Not not = (Not) formula;
            result = apply(not.operand(), cache);
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            result.addAll(apply(binary.left(), cache));
            result.addAll(apply(binary.right(), cache));
            break;
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            for (final Formula op : nary) {
                result.addAll(apply(op, cache));
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            result = FormulaHelper.literals(pbc.operands());
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    result = Collections.unmodifiableSortedSet(result);
    if (cache) {
        formula.setFunctionCacheEntry(LITERALS, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) SortedSet(java.util.SortedSet) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 10 with BinaryOperator

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

the class BFSOrdering method bfs.

private LinkedHashSet<Variable> bfs(final Formula formula) {
    final LinkedHashSet<Variable> variables = new LinkedHashSet<>();
    final Queue<Formula> queue = new LinkedList<>();
    queue.add(formula);
    while (!queue.isEmpty()) {
        final Formula current = queue.remove();
        switch(current.type()) {
            case LITERAL:
                final Literal lit = (Literal) current;
                if (lit.phase()) {
                    variables.add(lit.variable());
                } else {
                    queue.add(lit.variable());
                }
                break;
            case NOT:
                queue.add(((Not) current).operand());
                break;
            case IMPL:
            case EQUIV:
                final BinaryOperator op = (BinaryOperator) current;
                queue.add(op.left());
                queue.add(op.right());
                break;
            case AND:
            case OR:
                for (final Formula operand : current) {
                    queue.add(operand);
                }
                break;
            case PBC:
                final PBConstraint pbc = (PBConstraint) current;
                for (final Literal literal : pbc.operands()) {
                    variables.add(literal.variable());
                }
                break;
        }
    }
    return variables;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Formula(org.logicng.formulas.Formula) Variable(org.logicng.formulas.Variable) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) PBConstraint(org.logicng.formulas.PBConstraint) LinkedList(java.util.LinkedList)

Aggregations

BinaryOperator (org.logicng.formulas.BinaryOperator)10 Formula (org.logicng.formulas.Formula)8 PBConstraint (org.logicng.formulas.PBConstraint)8 Literal (org.logicng.formulas.Literal)7 Not (org.logicng.formulas.Not)7 NAryOperator (org.logicng.formulas.NAryOperator)6 Variable (org.logicng.formulas.Variable)3 ArrayList (java.util.ArrayList)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 FormulaFactory (org.logicng.formulas.FormulaFactory)2 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 EncodingResult (org.logicng.datastructures.EncodingResult)1 And (org.logicng.formulas.And)1 Implication (org.logicng.formulas.Implication)1