Search in sources :

Example 1 with DTreeLeaf

use of org.logicng.knowledgecompilation.dnnf.datastructures.dtree.DTreeLeaf 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)

Aggregations

BitSet (java.util.BitSet)1 TimeoutException (java.util.concurrent.TimeoutException)1 Formula (org.logicng.formulas.Formula)1 Literal (org.logicng.formulas.Literal)1 DTreeLeaf (org.logicng.knowledgecompilation.dnnf.datastructures.dtree.DTreeLeaf)1