Search in sources :

Example 1 with BDDInnerNode

use of org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode in project LogicNG by logic-ng.

the class BDDReorderingTest method testSwappingMultipleBdds.

@Test
public void testSwappingMultipleBdds() throws ParserException {
    final BDDKernel kernel = new BDDKernel(this.f, Arrays.asList(this.A, this.B, this.C), 100, 100);
    final Formula formula1 = this.f.parse("a | b | c");
    final Formula formula2 = this.f.parse("a & b");
    final BDD bdd1 = BDDFactory.build(formula1, kernel);
    final BDD bdd2 = BDDFactory.build(formula2, kernel);
    assertThat(bdd1.getVariableOrder()).containsExactly(this.A, this.B, this.C);
    assertThat(bdd2.getVariableOrder()).containsExactly(this.A, this.B, this.C);
    assertThat(bdd2.apply(new LngBDDFunction())).isEqualTo(new BDDInnerNode(this.A, BDDConstant.getFalsumNode(this.f), new BDDInnerNode(this.B, BDDConstant.getFalsumNode(this.f), BDDConstant.getVerumNode(this.f))));
    bdd1.swapVariables(this.A, this.B);
    assertThat(bdd1.getVariableOrder()).containsExactly(this.B, this.A, this.C);
    assertThat(bdd2.getVariableOrder()).containsExactly(this.B, this.A, this.C);
    assertThat(bdd2.apply(new LngBDDFunction())).isEqualTo(new BDDInnerNode(this.B, BDDConstant.getFalsumNode(this.f), new BDDInnerNode(this.A, BDDConstant.getFalsumNode(this.f), BDDConstant.getVerumNode(this.f))));
}
Also used : Formula(org.logicng.formulas.Formula) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDInnerNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode) LngBDDFunction(org.logicng.knowledgecompilation.bdds.functions.LngBDDFunction) Test(org.junit.jupiter.api.Test)

Example 2 with BDDInnerNode

use of org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode in project LogicNG by logic-ng.

the class BDDReorderingTest method testSwapping.

@Test
public void testSwapping() throws ParserException {
    final BDDKernel kernel = new BDDKernel(this.f, Arrays.asList(this.A, this.B, this.C), 100, 100);
    final Formula formula = this.f.parse("a | b | c");
    final BDD bdd = BDDFactory.build(formula, kernel);
    assertThat(bdd.getVariableOrder()).containsExactly(this.A, this.B, this.C);
    bdd.swapVariables(this.A, this.B);
    assertThat(bdd.getVariableOrder()).containsExactly(this.B, this.A, this.C);
    bdd.swapVariables(this.A, this.B);
    assertThat(bdd.getVariableOrder()).containsExactly(this.A, this.B, this.C);
    bdd.swapVariables(this.A, this.A);
    assertThat(bdd.getVariableOrder()).containsExactly(this.A, this.B, this.C);
    bdd.swapVariables(this.A, this.C);
    assertThat(bdd.getVariableOrder()).containsExactly(this.C, this.B, this.A);
    bdd.swapVariables(this.B, this.C);
    assertThat(bdd.getVariableOrder()).containsExactly(this.B, this.C, this.A);
    assertThat(this.f.equivalence(formula, bdd.cnf()).holds(new TautologyPredicate(this.f))).isTrue();
    assertThat(bdd.apply(new LngBDDFunction())).isEqualTo(new BDDInnerNode(this.B, new BDDInnerNode(this.C, new BDDInnerNode(this.A, BDDConstant.getFalsumNode(this.f), BDDConstant.getVerumNode(this.f)), BDDConstant.getVerumNode(this.f)), BDDConstant.getVerumNode(this.f)));
    assertThatThrownBy(() -> bdd.swapVariables(this.B, this.X)).isInstanceOf(IllegalArgumentException.class);
}
Also used : Formula(org.logicng.formulas.Formula) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) TautologyPredicate(org.logicng.predicates.satisfiability.TautologyPredicate) BDDInnerNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode) LngBDDFunction(org.logicng.knowledgecompilation.bdds.functions.LngBDDFunction) Test(org.junit.jupiter.api.Test)

Example 3 with BDDInnerNode

use of org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode in project LogicNG by logic-ng.

the class LngBDDFunction method buildBDDNode.

private BDDNode buildBDDNode(final int index, final BDDKernel kernel, final Map<Integer, int[]> kernelNodeMap, final Map<Integer, BDDNode> nodeMap) {
    BDDNode node = nodeMap.get(index);
    if (node != null) {
        return node;
    }
    if (index == BDDKernel.BDD_FALSE) {
        node = BDDConstant.getFalsumNode(kernel.factory());
    } else if (index == BDDKernel.BDD_TRUE) {
        node = BDDConstant.getVerumNode(kernel.factory());
    } else {
        final int[] kernelNode = kernelNodeMap.get(index);
        final Variable variable = kernel.getVariableForIndex(kernelNode[1]);
        final BDDNode lowNode = buildBDDNode(kernelNode[2], kernel, kernelNodeMap, nodeMap);
        final BDDNode highNode = buildBDDNode(kernelNode[3], kernel, kernelNodeMap, nodeMap);
        node = new BDDInnerNode(variable, lowNode, highNode);
    }
    nodeMap.put(index, node);
    return node;
}
Also used : Variable(org.logicng.formulas.Variable) BDDInnerNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode) BDDNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDNode)

Aggregations

BDDInnerNode (org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode)3 Test (org.junit.jupiter.api.Test)2 Formula (org.logicng.formulas.Formula)2 LngBDDFunction (org.logicng.knowledgecompilation.bdds.functions.LngBDDFunction)2 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)2 Variable (org.logicng.formulas.Variable)1 BDDNode (org.logicng.knowledgecompilation.bdds.datastructures.BDDNode)1 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)1