def queen_conditions(not_x, i, j, n): queen = Robdd.make_x(index_of(i, j)) # creates the rule "none in the same column" a = Robdd.true() for y in range(1, n + 1): if y == j: continue a_ = synth(queen, Bdd.IMPL, not_x[(i, y)]) a = synth(a, Bdd.AND, a_) # creates the rule "none in the same line" b = Robdd.true() for x in range(1, n + 1): if x == i: continue b_ = synth(queen, Bdd.IMPL, not_x[(x, j)]) b = synth(b, Bdd.AND, b_) # creates the rule "none in the diagonals" c = Robdd.true() x = 1 while (i - x) > 0 and (j - x) > 0: c_ = synth(queen, Bdd.IMPL, not_x[(i - x, j - x)]) c = synth(c, Bdd.AND, c_) x += 1 x = 1 while (i + x) <= n and (j + x) <= n: c_ = synth(queen, Bdd.IMPL, not_x[(i + x, j + x)]) c = synth(c, Bdd.AND, c_) x += 1 d = Robdd.true() x = 1 while (i - x) > 0 and (j + x) <= n: d_ = synth(queen, Bdd.IMPL, not_x[(i - x, j + x)]) d = synth(d, Bdd.AND, d_) x += 1 x = 1 while (i + x) <= n and (j - x) > 0: d_ = synth(queen, Bdd.IMPL, not_x[(i + x, j - x)]) d = synth(d, Bdd.AND, d_) x += 1 return synth(synth(a, Bdd.AND, b), Bdd.AND, synth(c, Bdd.AND, d))
def queen_conditions(not_x, i, j, n) : queen = Robdd.make_x(index_of(i, j)) # creates the rule "none in the same column" a = Robdd.true() for y in range(1, n + 1) : if y == j : continue a_ = synth(queen, Bdd.IMPL, not_x[(i, y)]) a = synth(a, Bdd.AND, a_) # creates the rule "none in the same line" b = Robdd.true() for x in range(1, n + 1) : if x == i : continue b_ = synth(queen, Bdd.IMPL, not_x[(x, j)]) b = synth(b, Bdd.AND, b_) # creates the rule "none in the diagonals" c = Robdd.true() x = 1 while (i - x) > 0 and (j - x) > 0 : c_ = synth(queen, Bdd.IMPL, not_x[(i - x, j - x)]) c = synth(c, Bdd.AND, c_) x += 1 x = 1 while (i + x) <= n and (j + x) <= n : c_ = synth(queen, Bdd.IMPL, not_x[(i + x, j + x)]) c = synth(c, Bdd.AND, c_) x += 1 d = Robdd.true() x = 1 while (i - x) > 0 and (j + x) <= n : d_ = synth(queen, Bdd.IMPL, not_x[(i - x, j + x)]) d = synth(d, Bdd.AND, d_) x += 1 x = 1 while (i + x) <= n and (j - x) > 0 : d_ = synth(queen, Bdd.IMPL, not_x[(i + x, j - x)]) d = synth(d, Bdd.AND, d_) x += 1 return synth(synth(a, Bdd.AND, b), Bdd.AND, synth(c, Bdd.AND, d))
def queens(n): solution = Robdd.true() # create the rule "there must be at least one queen at each line" for j in range(1, n + 1): line = Robdd.false() for i in range(1, n + 1): queen = Robdd.make_x(index_of(i, j)) line = synth(line, Bdd.OR, queen) solution = synth(solution, Bdd.AND, line) # create a list of "NOT" expressions not_expressions = {} for j in range(1, n + 1): for i in range(1, n + 1): not_expressions[(i, j)] = Robdd.make_not_x(index_of(i, j)) # create conditions for each position for j in range(1, n + 1): for i in range(1, n + 1): queen = queen_conditions(not_expressions, i, j, n) solution = synth(solution, Bdd.AND, queen) return solution
def negate_bdd(bdd): neg_bdd = synthesize(bdd, Bdd.AND, Robdd.true()) for i in xrange(len(neg_bdd.items)): neg_bdd.items[i] = tuple((neg_bdd.items[i][0], not neg_bdd.items[i][1] if neg_bdd.items[i][1] in (True, False) else neg_bdd.items[i][ 1], not neg_bdd.items[i][2] if neg_bdd.items[i][2] in (True, False) else neg_bdd.items[i][ 2])) # invert root, if root is a leaf if neg_bdd.root in (0, 1): neg_bdd.root ^= 1 return neg_bdd
def negate_bdd(bdd): neg_bdd = synthesize(bdd, Bdd.AND, Robdd.true()) for i in xrange(len(neg_bdd.items)): neg_bdd.items[i] = tuple( (neg_bdd.items[i][0], not neg_bdd.items[i][1] if neg_bdd.items[i][1] in (True, False) else neg_bdd.items[i][1], not neg_bdd.items[i][2] if neg_bdd.items[i][2] in (True, False) else neg_bdd.items[i][2])) # invert root, if root is a leaf if neg_bdd.root in (0, 1): neg_bdd.root ^= 1 return neg_bdd
def add_restriction(s): r = Robdd.true() for i, b in enumerate(s): # if i == 0: # continue # if i >= 8 : # continue if b == '1': current_var = Robdd.make_x(i) else: current_var = Robdd.make_not_x(i) r = synth(r, Bdd.AND, current_var) return r
def test_evaluate_should_always_return_true(self): robdd = Robdd.true() self.assertTrue(robdd.evaluate([0])) self.assertTrue(robdd.evaluate([1])) self.assertTrue(robdd.evaluate([0, 0])) self.assertTrue(robdd.evaluate([0, 1, 1, 0]))