Example #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
Example #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
Example #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)
Example #4
0
    def test_evaluate_should_return_true_for_x1(self):
        robdd = Robdd.false()
        v = robdd.make_x(0)
        robdd = synth(robdd, Bdd.OR, v)

        self.assertTrue(robdd.evaluate([1]))
        self.assertTrue(robdd.evaluate([1, 0]))
        self.assertTrue(robdd.evaluate([1, 1, 0, 1]))

        self.assertFalse(robdd.evaluate([0]))
        self.assertFalse(robdd.evaluate([0, 1]))
        self.assertFalse(robdd.evaluate([0, 1, 1, 1]))
Example #5
0
def blah():
    v = [
        '1000000000000001', '1100000000000011', '1010000000000101',
        '1000000110000001', '1000100000010001', '1001000000001001',
        '1000010000100001', '1000001001000001', '1111000000001111',
        '1010010110100101', '1001100110011001', '1100110000110101',
        '1010101001010101', '1100001111000011', '1111111111111111'
    ]
    #
    # '''
    # 1000010000100001
    # 1000000110000001
    # 1010000000000101
    # 1010010110100101
    # '''

    # print v[2]

    solution = None
    for __v__ in v:
        if solution is None:
            solution = add_restriction(__v__)
        else:
            solution = synth(solution, Bdd.OR, add_restriction(__v__))

    # solution = add_restriction(v[0])

    # for i in range(len(v)):
    #     if i in [0, 1, 13]:
    #         continue
    #     solution = synth(solution, Bdd.AND, add_negated(v[i]))

    # solution = remove_interval_down(solution, v[6])
    # solution = remove_interval_down(solution, v[2])
    # solution = remove_interval_down(solution, v[4])
    # solution = remove_interval_down(solution, v[5])
    #
    # solution = remove_interval_up(solution, v[1])

    # solution = Robdd.make_x(0)
    # solution = synth(solution, Bdd.OR, Robdd.make_not_x(0))
    # solution = synth(solution, Bdd.OR, Robdd.make_x(1))

    solution = Robdd.false()
    solution = synth(solution, Bdd.OR, Robdd.make_not_x(0))

    with open('/tmp/graph.dot', 'w') as fh:
        fh.write(solution.graphviz())

    return solution
Example #6
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
    def test_get_minimal_element_should_work2(self):
        robdd = Robdd.false()

        a = (1, 1, 1, 1)
        pa = Partition(mask=(0, 0, 0))

        b = (1, 1, 0, 0)
        pb = Partition(mask=(0, 0, 1))

        c = (1, 0, 1, 0)
        pc = Partition(mask=(0, 1, 0))

        d = (1, 0, 0, 1)
        pd = Partition(mask=(0, 1, 1))

        e = (1, 0, 0, 0)
        pe = Partition(mask=(0, 1, 2))

        robdd = synth(robdd, Bdd.OR, add_restriction(a))
        robdd = synth(robdd, Bdd.OR, add_restriction(b))
        robdd = synth(robdd, Bdd.OR, add_restriction(c))
        robdd = synth(robdd, Bdd.OR, add_restriction(d))

        mapping_partition_to_boolean = dict()
        mapping_partition_to_boolean[pa.mask] = a
        mapping_partition_to_boolean[pb.mask] = b
        mapping_partition_to_boolean[pc.mask] = c
        mapping_partition_to_boolean[pd.mask] = d
        mapping_partition_to_boolean[pe.mask] = e

        mapping_boolean_to_partition = dict()
        mapping_boolean_to_partition[a] = pa
        mapping_boolean_to_partition[b] = pb
        mapping_boolean_to_partition[c] = pc
        mapping_boolean_to_partition[d] = pd
        mapping_boolean_to_partition[e] = pe

        minimal_element = get_minimal_element(robdd, mapping_partition_to_boolean, mapping_boolean_to_partition)

        self.assertTrue(minimal_element == (0, 0, 1) or minimal_element == (0, 1, 0) or minimal_element == (0, 1, 1))
    def test_get_minimal_element_should_return_none_if_empty(self):
        robdd = Robdd.false()

        self.assertIsNone(get_minimal_element(robdd, dict(), dict()))
Example #9
0
 def test_evaluate_should_always_return_false(self):
     robdd = Robdd.false()
     self.assertFalse(robdd.evaluate([0]))
     self.assertFalse(robdd.evaluate([1]))
     self.assertFalse(robdd.evaluate([0, 0]))
     self.assertFalse(robdd.evaluate([0, 1, 1, 0]))