def reorder(dvars, fol): """Shift integers up in the variable order of `fol.bdd`.""" assert dvars, dvars bdd = fol.bdd for var, d in dvars.items(): level = d['level'] dv = fol.vars[var] var_type = dv['type'] if var_type == 'bool': bitnames = [var] else: assert var_type in TYPE_HINTS, var_type bitnames = dv['bitnames'] # print('Shifting bits {bitnames} to ' # 'level {level}'.format( # level=level, # bitnames=bitnames)) # assert each bit goes up in level for bit in bitnames: old_level = bdd.level_of_var(bit) assert old_level >= level, (old_level, level) # shift to levels wanted order = [bdd.var_at_level(i) for i in range(len(bdd.vars))] below = order[:level] above = order[level:] above = [v for v in above if v not in bitnames] new_order = below + bitnames + above dorder = {var: i for i, var in enumerate(new_order)} _bdd.reorder(bdd, dorder) order = [bdd.var_at_level(i) for i in range(len(bdd.vars))] assert order == new_order, (order, new_order)
def test_count_nodes(): bdd = cudd.BDD() [bdd.add_var(var) for var in ['x', 'y', 'z']] u = bdd.add_expr('x /\ y') v = bdd.add_expr('x /\ z') assert len(u) == 3, len(u) assert len(v) == 3, len(v) cudd.reorder(bdd, dict(x=0, y=1, z=2)) n = cudd.count_nodes([u, v]) assert n == 5, n cudd.reorder(bdd, dict(z=0, y=1, x=2)) n = cudd.count_nodes([u, v]) assert n == 4, n
def test_reorder(): bdd = cudd.BDD() dvars = ['x', 'y', 'z'] for var in dvars: bdd.add_var(var) for i, var in enumerate(dvars): level = bdd.level_of_var(var) assert level == i, (var, level, i) order = dict(y=0, z=1, x=2) cudd.reorder(bdd, order) for var, level_ in order.iteritems(): level = bdd.level_of_var(var) assert level == level_, (var, level, level_)
def test_reorder(): bdd = cudd.BDD() dvars = ['x', 'y', 'z'] for var in dvars: bdd.add_var(var) for i, var in enumerate(dvars): level = bdd.level_of_var(var) assert level == i, (var, level, i) order = dict(y=0, z=1, x=2) cudd.reorder(bdd, order) for var in order: level_ = order[var] level = bdd.level_of_var(var) assert level == level_, (var, level, level_)
def test_copy_bdd_different_order(): bdd = cudd.BDD() other = cudd.BDD() assert bdd != other dvars = ['x', 'y', 'z', 'w'] for index, var in enumerate(dvars): bdd.add_var(var, index=index) other.add_var(var, index=index) # reorder order = dict(w=0, x=1, y=2, z=3) cudd.reorder(other, order) # confirm resultant order for var in order: level_ = order[var] level = other.level_of_var(var) assert level == level_, (var, level, level_) # same indices for var in dvars: i = bdd._index_of_var[var] j = other._index_of_var[var] assert i == j, (i, j) # but different levels for var in dvars: i = bdd.level_of_var(var) j = other.level_of_var(var) assert i != j, (i, j) # copy s = '(x | !y) & w & (z | !w)' u0 = bdd.add_expr(s) u1 = cudd.copy_bdd(u0, bdd, other) u2 = cudd.copy_bdd(u1, other, bdd) assert u0 == u2, (u0, u2) u3 = cudd.copy_bdd(other.false, other, bdd) assert u3 != u2, (u3, u2) # verify w = other.add_expr(s) assert w == u1, (w, u1) del u0, u1, u2, u3, w
def test_copy_bdd_different_order(): bdd = cudd.BDD() other = cudd.BDD() assert bdd != other dvars = ['x', 'y', 'z', 'w'] for index, var in enumerate(dvars): bdd.add_var(var, index=index) other.add_var(var, index=index) # reorder order = dict(w=0, x=1, y=2, z=3) cudd.reorder(other, order) # confirm resultant order for var in order: level_ = order[var] level = other.level_of_var(var) assert level == level_, (var, level, level_) # same indices for var in dvars: i = bdd._index_of_var[var] j = other._index_of_var[var] assert i == j, (i, j) # but different levels for var in dvars: i = bdd.level_of_var(var) j = other.level_of_var(var) assert i != j, (i, j) # copy s = '(x \/ ~ y) /\ w /\ (z \/ ~ w)' u0 = bdd.add_expr(s) u1 = cudd.copy_bdd(u0, other) u2 = cudd.copy_bdd(u1, bdd) assert u0 == u2, (u0, u2) u3 = cudd.copy_bdd(other.false, bdd) assert u3 != u2, (u3, u2) # verify w = other.add_expr(s) assert w == u1, (w, u1)
"""How to configure reordering in CUDD via `dd.cudd`.""" import pprint from dd import cudd bdd = cudd.BDD() vrs = ['x', 'y', 'z'] bdd.declare(*vrs) # get the variable order levels = {var: bdd.level_of_var(var) for var in vrs} print(levels) # change the levels desired_levels = dict(x=2, y=0, z=1) cudd.reorder(bdd, desired_levels) # confirm that variables are now where desired new_levels = {var: bdd.level_of_var(var) for var in vrs} print(new_levels) # dynamic reordering is initially turned on config = bdd.configure() pprint.pprint(config) # turn off dynamic reordering bdd.configure(reordering=False) # confirm dynamic reordering is now off config = bdd.configure() pprint.pprint(config)