Skip to main content

Library for folding (or reducing) over a Reduced Ordered Binary Decision Diagram.

Project description

Fold-BDD

Library for folding (or reducing) over a Reduced Ordered Binary Decision Diagram.

Build Status codecov PyPI version License: MIT

Table of Contents

Installation

If you just need to use fold_bdd, you can just run:

$ pip install fold-bdd

For developers, note that this project uses the poetry python package/dependency management tool. Please familarize yourself with it and then run:

$ poetry install

Usage

The fold-bdd library supports two types of folds:

  1. Folding over the DAG of a BDD starting at the root and then recursively merging the low and high branches until the True/False leaves. This is simply a compressed variant of a post-order traversal.

  2. Folding over a path in the DAG, starting at the root and moving the the corresponding leaf (left fold).

In both cases, local context such as the levels of the parent and child nodes are passed in.

As input, each of these take in a bdd, from the dd library and function for accumulating or merging.

The following example illustrates how to use fold_bdd to count the number of solutions to a predicate using post_order and evaluate a path using fold_path.

Create ROBDD

# Create BDD.
from dd.cudd import BDD

manager = BDD()
manager.declare('x', 'y')
manager.reorder({'x': 1, 'y': 0})
manger.configure(reordering=False)

bexpr = manager.add_expr('x | y')

Post-Order Examples

from fold_bdd import post_order

Count Number of Nodes in BDD

def merge1(ctx, low=None, high=None):
    return 1 if low is None else low + high

def dag_size(bexpr):
    return post_order(bexpr, merge1)

assert bexpr.dag_size == dag_size(bexpr)

Count Number of Solutions to bexpr.

def merge2(ctx, low, high):
    if ctx.is_leaf:
        return ctx.skipped_paths if ctx.node_val else 0
    return (low + high) * ctx.skipped_paths

def count_solutions(bexpr):
    return post_order(bexpr, merge2)

assert count_solutions(bexpr) == 3

Fold Path Examples

Count nodes along path.

def merge(ctx, val, acc):
    return acc + 1

def count_nodes(bexpr, vals):
    return fold_path(merge, bexpr, vals, initial=0)

assert count_nodes(bexpr, (False, False)) == 3
assert count_nodes(bexpr, (True, False)) == 2

Count paths corresponding to BDD path

def merge(ctx, val, acc):
    return acc * ctx.skipped_paths

def count_paths(bexpr, vals):
    return fold_path(merge, bexpr, vals, initial=1)

assert count_paths(bexpr, (False, True)) == 1
assert count_paths(bexpr, (True, False)) == 2

Context Object Attributes

The Context object contains exposes attributes

  • node: Hashable # Reference to Node in ROBDD.
  • node_val: Union[str, bool] # Node name or leaf value.
  • negated: bool # Is the edge to prev node negated.
  • max_lvl: int # How many decision variables are there.
  • curr_lvl: int # Which decision is this.
  • prev_lvl: Optional[int] # Which decision was the parent. None if root.
  • low_lvl: Optional[int] # Which decision does the False edge point to. None if leaf.
  • high_lvl: Optional[int] # Which decision does the True edge point to. None if leaf.
  • is_leaf: bool # Is the current node a leaf.
  • skipped: int # How many decisions were skipped on edge to this node.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fold-bdd-0.4.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fold_bdd-0.4.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file fold-bdd-0.4.0.tar.gz.

File metadata

  • Download URL: fold-bdd-0.4.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.3 Linux/5.0.0-36-generic

File hashes

Hashes for fold-bdd-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ff44988ec41a509afe6f7f0192eae8ce25c7945cae40d0cd4e3dbf6d939c6ce2
MD5 581e8ca502c691e675b66137d7d7e738
BLAKE2b-256 0d141561c8e4845840102631c2b957d6c8c2f0713001e618ff2eea16181cddea

See more details on using hashes here.

File details

Details for the file fold_bdd-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: fold_bdd-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.3 Linux/5.0.0-36-generic

File hashes

Hashes for fold_bdd-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0b9bdac897f7698417cf9ff5bb5e0621892a2b3828409cb5d0bccc0fbe2bc34
MD5 c42c9403c4308cefc96f307284c55299
BLAKE2b-256 e4966f5b22532fab268ceddce08dac17a857a650d8f501c17a93fd2e6df548ec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page