Search in sources :

Example 21 with BDDKernel

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);
}
Also used : Formula(org.logicng.formulas.Formula) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDReordering(org.logicng.knowledgecompilation.bdds.jbuddy.BDDReordering) Test(org.junit.jupiter.api.Test)

Example 22 with BDDKernel

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))));
}
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 23 with BDDKernel

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);
            }
        }
    }
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) BDDReorderingMethod(org.logicng.knowledgecompilation.bdds.jbuddy.BDDReorderingMethod) BigInteger(java.math.BigInteger)

Example 24 with BDDKernel

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);
}
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 25 with BDDKernel

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));
    }
}
Also used : BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations)

Aggregations

BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)36 Test (org.junit.jupiter.api.Test)23 Formula (org.logicng.formulas.Formula)20 FormulaFactory (org.logicng.formulas.FormulaFactory)17 PropositionalParser (org.logicng.io.parsers.PropositionalParser)6 BDDOperations (org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations)6 Variable (org.logicng.formulas.Variable)5 BDD (org.logicng.knowledgecompilation.bdds.BDD)5 VariableOrderingProvider (org.logicng.knowledgecompilation.bdds.orderings.VariableOrderingProvider)5 Assignment (org.logicng.datastructures.Assignment)4 NQueensGenerator (org.logicng.testutils.NQueensGenerator)4 NumberOfNodesBDDHandler (org.logicng.handlers.NumberOfNodesBDDHandler)3 BDDInnerNode (org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode)3 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 TimeoutBDDHandler (org.logicng.handlers.TimeoutBDDHandler)2 LngBDDFunction (org.logicng.knowledgecompilation.bdds.functions.LngBDDFunction)2