Search in sources :

Example 16 with Formula

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

the class VariablesFunction method apply.

@Override
public SortedSet<Variable> apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(VARIABLES);
    if (cached != null) {
        return (SortedSet<Variable>) cached;
    }
    SortedSet<Variable> result = new TreeSet<>();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
            result = new TreeSet<>();
            break;
        case LITERAL:
            final Literal lit = (Literal) formula;
            result.add(lit.variable());
            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.variables(pbc.literals());
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    result = Collections.unmodifiableSortedSet(result);
    if (cache) {
        formula.setFunctionCacheEntry(VARIABLES, result);
    }
    return result;
}
Also used : Variable(org.logicng.formulas.Variable) SortedSet(java.util.SortedSet) Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 17 with Formula

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

the class DnnfCompiler method conjoin.

protected Formula conjoin(final Formula implied, final DTreeNode tree, final int currentShannons) throws TimeoutException {
    final Formula left;
    final Formula right;
    if (implied == this.f.falsum() || (left = cnfAux(tree.left(), currentShannons)) == this.f.falsum() || (right = cnfAux(tree.right(), currentShannons)) == this.f.falsum()) {
        return this.f.falsum();
    } else {
        return this.f.and(implied, left, right);
    }
}
Also used : Formula(org.logicng.formulas.Formula)

Example 18 with Formula

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

the class DnnfCompiler method cnf2Ddnnf.

protected Formula cnf2Ddnnf(final DTree tree, final int currentShannons) throws TimeoutException {
    final BitSet separator = tree.dynamicSeparator();
    final Formula implied = this.newlyImpliedLiterals(tree.staticVarSet());
    if (separator.isEmpty()) {
        if (tree instanceof DTreeLeaf) {
            return this.f.and(implied, leaf2Ddnnf((DTreeLeaf) tree));
        } else {
            return conjoin(implied, (DTreeNode) tree, currentShannons);
        }
    } else {
        final int var = chooseShannonVariable(tree, separator, currentShannons);
        if (this.handler != null && !this.handler.shannonExpansion()) {
            throw new TimeoutException();
        }
        /* Positive branch */
        Formula positiveDnnf = this.f.falsum();
        if (this.solver.decide(var, true)) {
            positiveDnnf = cnf2Ddnnf(tree, currentShannons + 1);
        }
        this.solver.undoDecide(var);
        if (positiveDnnf == this.f.falsum()) {
            if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
                return cnf2Ddnnf(tree);
            } else {
                return this.f.falsum();
            }
        }
        /* Negative branch */
        Formula negativeDnnf = this.f.falsum();
        if (this.solver.decide(var, false)) {
            negativeDnnf = cnf2Ddnnf(tree, currentShannons + 1);
        }
        this.solver.undoDecide(var);
        if (negativeDnnf == this.f.falsum()) {
            if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
                return cnf2Ddnnf(tree);
            } else {
                return this.f.falsum();
            }
        }
        final Literal lit = this.solver.litForIdx(var);
        final Formula positiveBranch = this.f.and(lit, positiveDnnf);
        final Formula negativeBranch = this.f.and(lit.negate(), negativeDnnf);
        return this.f.and(implied, this.f.or(positiveBranch, negativeBranch));
    }
}
Also used : Formula(org.logicng.formulas.Formula) DTreeLeaf(org.logicng.knowledgecompilation.dnnf.datastructures.dtree.DTreeLeaf) Literal(org.logicng.formulas.Literal) BitSet(java.util.BitSet) TimeoutException(java.util.concurrent.TimeoutException)

Example 19 with Formula

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

the class DnnfCompiler method compile.

protected Formula compile(final DTree dTree, final DnnfCompilationHandler handler) {
    if (this.nonUnitClauses.isAtomicFormula()) {
        return this.cnf;
    }
    if (!this.solver.start()) {
        return this.f.falsum();
    }
    initializeCaches(dTree);
    this.handler = handler;
    start(handler);
    Formula result;
    try {
        result = cnf2Ddnnf(dTree);
    } catch (final TimeoutException e) {
        result = null;
    }
    this.handler = null;
    return result == null ? null : this.f.and(this.unitClauses, result);
}
Also used : Formula(org.logicng.formulas.Formula) TimeoutException(java.util.concurrent.TimeoutException)

Example 20 with Formula

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

the class DnnfFactory method compile.

/**
 * Compiles the given formula to a DNNF instance.
 * @param formula the formula
 * @return the compiled DNNF
 */
public Dnnf compile(final Formula formula) {
    final SortedSet<Variable> originalVariables = new TreeSet<>(formula.variables());
    final Formula cnf = formula.cnf();
    originalVariables.addAll(cnf.variables());
    final Formula simplifedFormula = simplifyFormula(cnf);
    final DnnfCompiler compiler = new DnnfCompiler(simplifedFormula);
    final Formula dnnf = compiler.compile(new MinFillDTreeGenerator());
    return new Dnnf(originalVariables, dnnf);
}
Also used : Formula(org.logicng.formulas.Formula) MinFillDTreeGenerator(org.logicng.knowledgecompilation.dnnf.datastructures.dtree.MinFillDTreeGenerator) Dnnf(org.logicng.knowledgecompilation.dnnf.datastructures.Dnnf) Variable(org.logicng.formulas.Variable) TreeSet(java.util.TreeSet)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16