use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel in project LogicNG by logic-ng.
the class BDDReorderingTest method testExceptionalBehavior.
@Test
public void testExceptionalBehavior() {
assertThatThrownBy(() -> {
final BDDKernel kernel = new BDDKernel(this.f, Arrays.asList(this.A, this.B), 100, 100);
final BDDReordering reordering = new BDDReordering(kernel);
final Formula formula = this.f.parse("a | b");
BDDFactory.build(formula, kernel);
reordering.swapVariables(0, 2);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("Unknown variable number: " + 2);
assertThatThrownBy(() -> {
final BDDKernel kernel = new BDDKernel(this.f, Arrays.asList(this.A, this.B), 100, 100);
final BDDReordering reordering = new BDDReordering(kernel);
final Formula formula = this.f.parse("a | b");
BDDFactory.build(formula, kernel);
reordering.swapVariables(3, 0);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("Unknown variable number: " + 3);
}
use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel 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.jbuddy.BDDKernel in project LogicNG by logic-ng.
the class BDDReorderingTest method testReorderOnBuild.
private void testReorderOnBuild(final int minVars, final int maxVars, final boolean verbose) {
for (int vars = minVars; vars <= maxVars; vars++) {
for (int depth = 4; depth <= 6; depth++) {
final FormulaFactory f = new FormulaFactory();
final Formula formula = randomFormula(vars, depth, f);
if (verbose) {
System.out.println(String.format("vars = %2d, depth = %2d, nodes = %5d", vars, depth, formula.numberOfNodes()));
}
final BDDKernel kernel = new BDDKernel(f, new ArrayList<>(formula.variables()), 1000, 10000);
final BDD bdd = BDDFactory.build(formula, kernel);
final int nodeCount = new BDDOperations(kernel).nodeCount(bdd.index());
final BigInteger modelCount = bdd.modelCount();
for (final BDDReorderingMethod method : REORDER_METHODS) {
reorderOnBuild(f, formula, method, modelCount, nodeCount, true, verbose);
}
for (final BDDReorderingMethod method : REORDER_METHODS) {
reorderOnBuild(f, formula, method, modelCount, nodeCount, false, verbose);
}
}
}
}
use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel 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.jbuddy.BDDKernel in project LogicNG by logic-ng.
the class BDDReorderingTest method reorderOnBuild.
private void reorderOnBuild(final FormulaFactory f, final Formula formula, final BDDReorderingMethod method, final BigInteger originalCount, final int originalUsedNodes, final boolean withBlocks, final boolean verbose) {
final BDDKernel kernel = new BDDKernel(f, new ArrayList<>(formula.variables()), 1000, 10000);
addVariableBlocks(formula.variables().size(), withBlocks, kernel);
kernel.getReordering().setReorderDuringConstruction(method, 10000);
final long start = System.currentTimeMillis();
final BDD bdd = BDDFactory.build(formula, kernel);
final long duration = System.currentTimeMillis() - start;
final int usedAfter = new BDDOperations(kernel).nodeCount(bdd.index());
verifyVariableBlocks(f, formula, withBlocks, bdd);
verifyBddConsistency(f, formula, bdd, originalCount);
final double reduction = (originalUsedNodes - usedAfter) / (double) originalUsedNodes * 100;
if (verbose) {
System.out.println(String.format("%-20s: Built in %5d ms, reduction by %6.2f%% from %6d to %6d", method, duration, reduction, originalUsedNodes, usedAfter));
}
}
Aggregations