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))));
}
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);
}
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;
}
Aggregations