Skip to main content

Python abstraction around Binary Decision Diagrams to implement Multivalued Decision Diagrams.

Project description

Py-MDD

Python abstraction around Binary Decision Diagrams to implement Multi-valued Decision Diagrams.

Build Status docs badge codecov PyPI version License: MIT

Table of Contents

About

Multi-valued Decision Diagrams (MDD) are a way to represent discrete function:

f : A₁ × A₂ × … × Aₙ → B.

Conceptually, a MDD for f can be thought of as a compressed decision tree (in the form of a directed acyclc graph).

For example, if we have a function over two variables,

x ∈ {1,2,3,4,5}, y ∈ {'a','b'}

with possible outputs f(x, y) ∈ {-1, 0, 1}, then the following diagram represents the function:

f(x, y) = 1 if (x ≡ 1 and y ≡ 'a') else 0

This library provides abstractions to easily create and manipulate MDDs.

Installation

If you just need to use py-mdd, you can just run:

$ pip install mdd

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

For the impatient, here is a basic usage example:

import mdd

interface = mdd.Interface(
    inputs={
        "x": [1, 2, 3],
        "y": [6, 'w'], 
        "z": [7, True, 8],
    }, 
    output=[-1, 0, 1],
)
func = interface.constantly(-1)
assert func({'x': 1, 'y': 'w', 'z': 8}) == -1

# Can access underlying BDD from `dd` library.
# Note: This BDD encodes both the function's output
#       *and* domain (valid inputs).
assert func.bdd.dag_size == 33

If 33 seems very large to you, this is just a constant function after all, note that as the following sections illustrate, its easy to implement alternative encodings which can be much more compact. [0]

Variables, Interfaces, and Encodings

The mdd api centers around three objects:

  1. Variable: Representation of a named variable taking on values in from a finite set described by an aiger circuit.
  2. Interface: Description of inputs and outputs of a Multi-valued Decision Diagram.
  3. DecisionDiagram: Representation of a Multi-valued Decision Diagram that conforms to an interface. This object is a wrapper around a Binary Decision Diagram object (from dd).

By default, variables use one-hot encoding, but all input variables can use arbitrary encodings by defining a bit-vector expression describing valid inputs and a encode/decoder pair from ints to the variable's domain.

# One hot encoded by default.
var1 = mdd.to_var(domain=["x", "y", "z"], name="myvar1")

# Hand crafted encoding using `py-aiger`.

import aiger_bv

# Named 2-length bitvector circuit.
bvexpr = aiger_bv.uatom(2, 'myvar3')

domain = ('a', 'b', 'c')
var2 = mdd.Variable(
     encode=domain.index,        # Any -> int
     decode=domain.__getitem__,  # int -> Any
     valid=bvexpr < 4,           # 0b11 is invalid!
)

# Can create new variable using same encoding, but different name.
var3 = var2.with_name("myvar3")

var4 = mdd.to_var(domain=[-1, 0, 1], name='output')

A useful feature of variables is that they can generate an aiger_bv BitVector object to describe circuits in terms of a variable.

a_int = var2.encode('a')
y_int = var1.encode('y')

# BitVector Expression testing if var2 is 'a' and var1 is 'y'.
expr = (var2.expr() == a_int) & (var1.expr() == y_int)

Given these variables, we can define an input/output interface.

# All variables must have distinct names.
interface = mdd.Interface(inputs=[var1, var2, var3], output=var4)

Further, as the first example showed, if the default encoding is fine, then we can simply pass a dictionary inplace of inputs and/or a iterable in place of the output. In this case, a 1-hot encoding will be created using the order of the variables.

interface = mdd.Interface(
    inputs={
        "x": [1, 2, 3],      # These are
        "y": [6, 'w'],       # 1-hot
        "z": [7, True, 8],   # Encoded.
    }, 
    output=[-1, 0, 1],       # uuid based output name.
)

Finally, given an interface we can create a Multi-valued Decision Diagram. There are five main ways to create a DecisionDiagram:

  1. Given an interface, create a constant function:

    func = interface.constantly(1)
    
  2. Wrap an py-aiger compatible object:

    import aiger_bv as BV
     
    x = interface.var('x')  # Access 'x' variable.
    out = interface.output  # Access output variable.
    
    expr = BV.ite(
        x.expr() == x.encode(2),       # Test.
        out.expr() == out.encode(0),   # True branch.
        out.expr() == out.encode(-1),  # False branch.
    )
    func = interface.lift(expr)
    assert func({'x': 2, 'y': 6, 'z': True}) == 0
    assert func({'x': 1, 'y': 6, 'z': True}) == -1
    
  3. Wrap an existing Binary Decision Diagram:

    bdd = mdd.to_bdd(expr)      # Convert `aiger` expression to bdd.
    func = interface.lift(bdd)  # bdd type comes from `dd` library.
    assert func({'x': 2, 'y': 6, 'z': True}) == 0
    assert func({'x': 1, 'y': 6, 'z': True}) == -1
    
  4. Partially Evaluate an existing DecisionDiagram:

    constantly_0 = func.let({'x': 2})
    assert func({'y': 6, 'z': True}) == 0
    
  5. Override an existing DecisionDiagram given a predicate:

    # Can be a BDD or and py-aiger compatible object.
    test = x.expr() == x.encode(1)
    # If x = 1, then return 1, otherwise return using func.
    func2 = func.override(test=test, value=1)
    assert func2({'x': 1, 'y': 6, 'z': True}) == 1
    

BDD Encoding Details

The py-mdd library uses a Binary Decision Diagram to represent a multi-valued function. The encoding slighly differs from the standard reduction [1] from mdds to bdds by assuming the following:

  1. If a variable encoding is invalid, then the bdd maps it to 0.
  2. The output is 1-hot encoded, i.e., there is a variable for each outcome.
  3. If a bdd has all input variables fixed to a valid assignment, the resulting bdd depends on exactly one output varible, which then determines the output.

Any bdd that conforms to this encoding can be wrapped up by an approriate Interface.

Ordering DecisionDiagrams

The underlying BDD can be reordered to respect variable ordering by providing a complete list of variable names to the order method.

func.order(['x', 'y', 'z', func.output.name])

Converting to Directed Graph (networkx)

If the networkx python package is installed:

$ pip install networkx

or the nx option is added when installing py-mdd:

$ pip install mdd[nx]

then one can export a DecisionDiagram as a directed graph.

note: for now, this graph is only partially reduced. In the future, the plan is to guarantee that the exported DAG is fulled reduced.

from mdd.nx import to_nx

graph = to_nx(func)  # Has BitVector expressions on edges to represent guards.

graph2 = to_nx(func, symbolic_edges=False)  # Has explicit sets of values on edges to represent guards.

[0]: To get a sense for how much overhead is introduced, consider the corresponding Zero Suppressed Decision Diagram (ZDD) of a 1-hot encoding. A classic result (see Art of Computer Programming vol 4a) is the ZDD size bounds the BDD size via O(#variables*|size of ZDD|).

[1]: Srinivasan, Arvind, et al. "Algorithms for discrete function manipulation." 1990 IEEE international conference on computer-aided design. IEEE Computer Society, 1990.

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

mdd-0.3.3.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

mdd-0.3.3-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file mdd-0.3.3.tar.gz.

File metadata

  • Download URL: mdd-0.3.3.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.4.0-52-generic

File hashes

Hashes for mdd-0.3.3.tar.gz
Algorithm Hash digest
SHA256 ba98fed5fc5d53f580229eb7f0100e1db54cfd04fc5311cf0887ea9ea2bd0cc1
MD5 e35f92b24845a32351fbd619f6d2c796
BLAKE2b-256 4b0f8befff567857be836920ab2644f8614bc2d607c87734ed71fd77c4b86909

See more details on using hashes here.

File details

Details for the file mdd-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: mdd-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.4.0-52-generic

File hashes

Hashes for mdd-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6a9d5e6ace9d97399d2b3d1846f99ad9852fe440331b8b9bfa2c0c5f5dbd6d8a
MD5 d1597f69d1e11ed9cc20e834e4f4c973
BLAKE2b-256 5b7c4606a689d1b6dfdd75781cc7f010e7d9cc88e20d74a509a546d8a372b975

See more details on using hashes here.

Supported by

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