def findAllWays(f): v = bdd.expr("0") v = bdd.expr2bdd(v) for i in f.satisfy_all(): s = bdd.expr("1") s = bdd.expr2bdd(s) for a in i.items(): if (a[1] == 1): s = s & a[0] else: s = s & ~a[0] v = v | s return v
def generate_bdd(comparison: list, expressions: list) -> list: ''' Method that converts given expressions into BDD for a given R. Parameter list: comparison list and expression list. returns: BDD as list for a given R. ''' R = comparison for expr in expressions: R = R or expr2bdd(expr(expr)) return R
def EquivalentCheckFromFile(path1, path2): answ1 = exe.Execution(path1) answ2 = exe.Execution(path2) prs = parser.Parser(path1) for name in prs.varNames("output"): r1 = bdd.expr2bdd(answ1[name][0]) r3 = bdd.expr2bdd(answ2[name][0]) f1 = findAllWays(~r1) & findAllWays(r3) if (~f1.is_zero()): return f1 r2 = bdd.expr2bdd(answ1[name][1]) r4 = bdd.expr2bdd(answ2[name][1]) f2 = findAllWays(~r1) & findAllWays(r2) & findAllWays( ~r3) & findAllWays(~r4) if (~f2.is_zero()): return f2 f3 = findAllWays(~r1) & findAllWays(~r2) & findAllWays( ~r3) & findAllWays(r4) if (~f3.is_zero()): return f3 f = bdd.expr("0") return f
def EquivalentCheck(out1, out2, inV): r1 = out1[0] r2 = out1[1] r3 = out2[0] r4 = out2[1] f1 = findAllWays(~r1) & findAllWays(r3) f2 = findAllWays(~r1) & findAllWays(r2) & findAllWays(~r3) & findAllWays( ~r4) f3 = findAllWays(~r1) & findAllWays(~r2) & findAllWays(~r3) & findAllWays( r4) f = f1 | f2 | f3 f = bdd.expr2bdd(f) return f
def text2bddExpr(text): formula = expr2bdd(expr(text)) print(expr(text)) gv = Source(formula.to_dot()) file_path = "selfGeneratedDot.dot" gv.render(filename=file_path, format='png', view=False) return file_path # text2bddExpr("Or(a,b,c)") # get_dot_from_json("delete_this.json", "delete_this.dot") #text2bddExpr("a|b&c") # render("dot", "png", "delete_this.dot") # render("delete_this.dot", format='png')
def main(): edgeFormulaList = [] x0, x1, x2, x3, x4 = pyeda.bddvars('x', 5) y0, y1, y2, y3, y4 = pyeda.bddvars('y', 5) print("Generating Graph...") for i in range(0, 32): for j in range(0,32): if (((i+3) % 32) == (j % 32)) | (((i+7) % 32) == (j % 32)): newFormula = conjure_formula(i, j) edgeFormulaList.append(newFormula) F = connect_to_edges(edgeFormulaList) R = pyeda.expr2bdd(F) print("Generating transitive closure, R*") RStar = transitive_closure(R) print("Negating the transitive closure, R*") negRStar = ~RStar result = negRStar.smoothing((x0, x1, x2, x3, x4, y0, y1, y2, y3, y4)) result = ~result print("Let x,y be in the set S. Node x can reach node y in one+ steps in a graph G. . .") print(f"\n{result.equivalent(True)}\n")
#!/usr/bin/env python3 from pyeda.inter import expr, expr2bdd, bddvar # type # expr.Variable # expr.OrOp # # bdd.BDDVariable # bdd.BinaryDecisionDiagram # # expr() => expr.* # expr2bdd() => from expr to bdd # bddvar() => bdd.BDDVariable # # both expr and bdd can use ~|&^ operator # f = expr('a & b | b & c | c & a') f = expr('a1_1 & a2_1 | a2_1 & a2_2 | a2_2 & a1_1') g = f | expr('d') # type(f) => expr # type(g) => expr f2 = expr2bdd(f) # type(f2) = bdd print(f2.satisfy_one()) print(f2.satisfy_all()) # generator print(list(f2.satisfy_all())) print(f2.satisfy_count()) print(f2.to_dot())
bdds_together = join_disjunct(bdds) print("bdds_together: long string bdd for entire equation") pprint(bdds_together) # step 4' printHeader("Step 4'") x_names = names[0:5] p_bdds = join_disjunct( map(lambda x: transformEdge(x, x_names), map(lambda x: getBin(x, 5), primes))) p = expr2bdd(expr(p_bdds)) y_names = names[5:-1] e_bdds = join_disjunct( map(lambda y: transformEdge(y, y_names), map(lambda y: getBin(y, 5), evens))) e = expr2bdd(expr(e_bdds)) print("p_bdds: like bdds_together") pprint(p_bdds) print("e_bdds: like bdds_together") pprint(e_bdds) # step 5 printHeader("Step 5")
def boolX(): f = bdd.expr("1") f = bdd.expr2bdd(f) g = bdd.expr("0") g = bdd.expr2bdd(g) return (f, g)
def boolZero(): f = bdd.expr("0") f = bdd.expr2bdd(f) g = bdd.expr("0") g = bdd.expr2bdd(g) return (f, g)
print("Joining edges for boolean expressions...") rForms = joinEdgeList(rList) pForms = joinEdgeList(pList) eForms = joinEdgeList(eList) debug(rForms) if (render_graph): print("Attempting to render graph from R formula...") try: renderGraph(rForms) except: print("Failed to render graph :(") print("Converting boolean functions into BDDs RR, PP, and EE") RR = pyeda.expr2bdd(rForms) PP = pyeda.expr2bdd(pForms) EE = pyeda.expr2bdd(eForms) debug(RR) print("Computing BDD RR2 from BDD RR") RR2 = RR.compose({ j0: k0, j1: k1, j2: k2, j3: k3, j4: k4 }) & RR.compose({ i0: k0, i1: k1, i2: k2,
# send the edge to to formula creation function newFormula = edgeToBooleanFormula(i, j) # add the formula to the list edgeFormulaList.append(newFormula) print("Done") # Create a big boolean expression, F, for the entire graph G print("Building the boolean expression, F, from the graph G..") F = joinEdgeFormulaList(edgeFormulaList) print("Done") # Convert F into BDD: R print("Converting F to a BDD, R..") R = pyeda.expr2bdd(F) print("Done") # Compute the transitive closure R* print("Computing the transitive closure, R*..") RStar = computeTransitiveClosure(R) print("Done") # for all i, j ∈ S, node i can reach node j in one or more steps in G # first we negate R* print("Negating the transitive closure, R*..") negRStar = ~RStar print("Done") # Then apply smoothing over all BDD vars print("Smoothing over all x[0]..x[4] and y[0]..y[4]")