Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def remove_interval_up(original, s):
    r = Robdd.false()
    for i, b in enumerate(s):
        if b == '0':
            current_var = Robdd.make_x(i)
            r = synth(r, Bdd.OR, current_var)

    return synth(original, Bdd.AND, r)
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def add_negated(s):
    r = Robdd.false()
    for i, b in enumerate(s):
        # if i == 0:
        #     continue
        # if i >= 8:
        #     continue

        if b == '0':
            current_var = Robdd.make_x(i)
        else:
            current_var = Robdd.make_not_x(i)

        r = synth(r, Bdd.OR, current_var)
    return r
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
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))