我正在使用https://github.com/tulip-control/dd/blob/master/doc.md#multi-valued-decision-diagrams-mdd
Ioannis Filippidis 设计的包用于多值决策图上的以下两个功能,top
以及successors
.
我需要帮助编写第三个函数,,level
(概述如下)。对于每个节点,都有一个唯一级别,如图所示。
from dd.bdd import BDD
from dd import mdd as _mdd
bits = dict(x0=0, x1=1, y0=2, y1=3)
bdd = BDD(bits)
u = bdd.add_expr('(x0 /\ y1) \/ (~ y0 /\ x1)')
bdd.incref(u)
# convert BDD to MDD
ints = dict(
x=dict(level=1, len=4, bitnames=['x0', 'x1']),
y=dict(level=0, len=4, bitnames=['y0', 'y1']))
mdd, umap = _mdd.bdd_to_mdd(bdd, ints)
# plot MDD with graphviz
pd = _mdd.to_pydot(mdd)
pd.write_pdf('mdd_example_3.pdf')
def successors(n,m): # returns immediate successors; n = node, m = mdd
return m._succ[n][1:]
def top(m): # returns top variable of m = mdd
for m in mdd._succ:
if mdd._succ[m][0]==0: return(m)
# def level(n,m):
# returns level l of node n in mdd m
top(mdd)
5
successors(3,mdd)
(1, -1, -1, -1)