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