Search in sources :

Example 1 with BDDOperations

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations in project LogicNG by logic-ng.

the class BDDCNFFunction method apply.

@Override
public Formula apply(final BDD bdd) {
    final BDDKernel kernel = bdd.underlyingKernel();
    final List<byte[]> unsatPaths = new BDDOperations(kernel).allUnsat(bdd.index());
    final List<Formula> clauses = new ArrayList<>();
    List<Formula> literals;
    for (final byte[] path : unsatPaths) {
        literals = new ArrayList<>();
        for (int i = 0; i < path.length; i++) {
            if (path[i] == 0) {
                literals.add(kernel.getVariableForIndex(i));
            } else if (path[i] == 1) {
                literals.add(kernel.getVariableForIndex(i).negate());
            }
        }
        clauses.add(kernel.factory().or(literals));
    }
    return kernel.factory().and(clauses);
}
Also used : Formula(org.logicng.formulas.Formula) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) ArrayList(java.util.ArrayList)

Example 2 with BDDOperations

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations in project LogicNG by logic-ng.

the class LngBDDFunction method apply.

@Override
public BDDNode apply(final BDD bdd) {
    final BDDKernel kernel = bdd.underlyingKernel();
    final int index = bdd.index();
    final Map<Integer, int[]> kernelNodeMap = new BDDOperations(kernel).allNodes(index).stream().collect(Collectors.toMap(node -> node[0], node -> node));
    return buildBDDNode(index, kernel, kernelNodeMap, new HashMap<>());
}
Also used : BDDNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDNode) BDD(org.logicng.knowledgecompilation.bdds.BDD) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) Variable(org.logicng.formulas.Variable) Map(java.util.Map) BDDInnerNode(org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode) BDDConstant(org.logicng.knowledgecompilation.bdds.datastructures.BDDConstant) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations)

Example 3 with BDDOperations

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations in project LogicNG by logic-ng.

the class BDDDotFileWriter method write.

/**
 * Writes a given formula's internal data structure as a dot file.
 * @param file the file of the dot file to write
 * @param bdd  the BDD
 * @throws IOException if there was a problem writing the file
 */
public static void write(final File file, final BDD bdd) throws IOException {
    final StringBuilder sb = new StringBuilder(String.format("digraph G {%n"));
    if (!bdd.isContradiction()) {
        sb.append(String.format("  %s [shape=box, label=\"$true\", style = bold, color = darkgreen];%n", CONST_TRUE));
    }
    if (!bdd.isTautology()) {
        sb.append(String.format("  %s [shape=box, label=\"$false\", style = bold, color = red];%n", CONST_FALSE));
    }
    for (final int[] internalNode : new BDDOperations(bdd.underlyingKernel()).allNodes(bdd.index())) {
        sb.append(String.format("  %s%d [shape=ellipse, label=\"%s\"];%n", NODE_PREFIX, internalNode[0], bdd.underlyingKernel().getVariableForIndex(internalNode[1]).name()));
        sb.append(String.format("  %s%d -> %s [style = dotted, color = red];%n", NODE_PREFIX, internalNode[0], getNodeString(internalNode[2])));
        sb.append(String.format("  %s%d -> %s [color = darkgreen];%n", NODE_PREFIX, internalNode[0], getNodeString(internalNode[3])));
    }
    sb.append("}").append(System.lineSeparator());
    try (final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
        writer.append(sb);
        writer.flush();
    }
}
Also used : BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 4 with BDDOperations

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations in project LogicNG by logic-ng.

the class BDDReorderingTest method performReorder.

private void performReorder(final FormulaFactory f, final Formula formula, final BDDReorderingMethod reorderMethod, final boolean withBlocks, final boolean verbose) {
    final BDDKernel kernel = new BDDKernel(f, new ArrayList<>(formula.variables()), 1000, 10000);
    final BDD bdd = BDDFactory.build(formula, kernel);
    final BigInteger count = bdd.modelCount();
    final int usedBefore = new BDDOperations(kernel).nodeCount(bdd.index());
    final long start = System.currentTimeMillis();
    addVariableBlocks(formula.variables().size(), withBlocks, kernel);
    kernel.getReordering().reorder(reorderMethod);
    final long duration = System.currentTimeMillis() - start;
    final int usedAfter = new BDDOperations(kernel).nodeCount(bdd.index());
    assertThat(verifyBddConsistency(f, formula, bdd, count)).isTrue();
    verifyVariableBlocks(f, formula, withBlocks, bdd);
    if (reorderMethod != BDDReorderingMethod.BDD_REORDER_RANDOM) {
        assertThat(usedAfter).isLessThanOrEqualTo(usedBefore);
    }
    final double reduction = (usedBefore - usedAfter) / (double) usedBefore * 100;
    if (verbose) {
        System.out.println(String.format("%-20s: Reduced %7s blocks in %5dms by %.2f%% from %d to %d", reorderMethod, withBlocks ? "with" : "without", duration, reduction, usedBefore, usedAfter));
    }
}
Also used : BDDKernel(org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel) BDDOperations(org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations) BigInteger(java.math.BigInteger)

Example 5 with BDDOperations

use of org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations 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)

Aggregations

BDDOperations (org.logicng.knowledgecompilation.bdds.jbuddy.BDDOperations)7 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)6 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Formula (org.logicng.formulas.Formula)2 Variable (org.logicng.formulas.Variable)2 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Collectors (java.util.stream.Collectors)1 Assignment (org.logicng.datastructures.Assignment)1 FormulaFactory (org.logicng.formulas.FormulaFactory)1 BDD (org.logicng.knowledgecompilation.bdds.BDD)1 BDDConstant (org.logicng.knowledgecompilation.bdds.datastructures.BDDConstant)1 BDDInnerNode (org.logicng.knowledgecompilation.bdds.datastructures.BDDInnerNode)1 BDDNode (org.logicng.knowledgecompilation.bdds.datastructures.BDDNode)1 BDDReorderingMethod (org.logicng.knowledgecompilation.bdds.jbuddy.BDDReorderingMethod)1