Пример #1
0
def test_up_unsat():
    # (x1|~x2|x3)&x2&(~x1|x3)&(x2|~x3) --> UNSAT
    x1_var = Variable('x1')
    x2_var = Variable('x2')
    x3_var = Variable('x3')

    x1 = Literal(x1_var, negated=False)
    not_x1 = Literal(x1_var, negated=True)
    x2 = Literal(x2_var, negated=False)
    not_x2 = Literal(x2_var, negated=True)
    x3 = Literal(x3_var, negated=False)
    not_x3 = Literal(x3_var, negated=True)

    clauses = [[x1, not_x2, x3], [x2], [not_x1, x3], [not_x2, not_x3]]
    # literal_to_clauses = {x1: {0}, not_x1: {2}, not_x2: {0, 3}, x2: {1}, x3: {0, 2}, not_x3: {3}}

    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)

    dpll = DPLL(cnf)
    actual_cnf = dpll.unit_propagation()

    assert actual_cnf is None
    assert not dpll.get_full_assignment()[x3_var]
    assert dpll.get_full_assignment()[x2_var]
Пример #2
0
def test_search_complex():
    # (~x|z) & (~x|~z|~y) & (~z|w) & (~w|~y) (lec3, slide 18)
    # (~x1 | x2) & (~x1 | ~x2 | ~x3) & (~x2 | x4) & (~x4| ~x3)
    x1_var = Variable('x1')
    x2_var = Variable('x2')
    x3_var = Variable('x3')
    x4_var = Variable('x4')
    x5_var = Variable('x5')
    x6_var = Variable('x6')

    x1 = Literal(x1_var, negated=False)
    not_x1 = Literal(x1_var, negated=True)
    x2 = Literal(x2_var, negated=False)
    not_x2 = Literal(x2_var, negated=True)
    x3 = Literal(x3_var, negated=False)
    not_x3 = Literal(x3_var, negated=True)
    x4 = Literal(x4_var, negated=False)
    not_x4 = Literal(x4_var, negated=True)

    x5 = Literal(x5_var, negated=False)
    not_x5 = Literal(x5_var, negated=True)
    x6 = Literal(x6_var, negated=False)
    not_x6 = Literal(x6_var, negated=True)
    # (~x1 | x2) & (~x1 | ~x2 | ~x3) & (~x2 | x4) & (~x4| ~x3)
    clauses = [[not_x1, x2], [not_x1, not_x2, not_x3], [not_x2, x4],
               [not_x4, not_x3], [x5, x6], [not_x5, not_x6]]
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    search_result = dpll.search()
    assert search_result
def test_find_all_paths():
    vars = [Variable('x{}'.format(i + 1)) for i in range(8)]
    pos_l = [None] + [Literal(v, negated=False) for v in vars]
    neg_l = [None] + [Literal(v, negated=True) for v in vars]

    clauses = [[pos_l[1]], [neg_l[1], pos_l[5]], [neg_l[1], pos_l[3]],
               [neg_l[2], neg_l[3], pos_l[4]], [neg_l[5], pos_l[2]]]
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    g = ImplicationGraph(cnf)

    g.add_decide_node(1, pos_l[1])
    g.add_node(1, pos_l[3], None, 2)
    g.add_node(1, pos_l[5], None, 1)
    g.add_node(1, pos_l[2], None, 4)
    # g.add_node(1, pos_l[2], None, 3)
    g.add_node(1, pos_l[4], None, 3)
    source_node = g._nodes[pos_l[1]]
    target_node = g._nodes[pos_l[4]]
    actual_paths = g._find_all_paths(source_node, target_node)
    expected_paths = [[
        pos_l[1].variable, pos_l[5].variable, pos_l[2].variable,
        pos_l[4].variable
    ], [pos_l[1].variable, pos_l[3].variable, pos_l[4].variable]]

    assert {(frozenset(item))
            for item in actual_paths} == {(frozenset(item))
                                          for item in expected_paths}
def get_complicated_graph() -> (ImplicationGraph, int, List[Variable]):
    variables = [Variable('TEMP')
                 ] + [Variable('x{}'.format(i + 1)) for i in range(10)]
    pos_l = [Literal(v, negated=False) for v in variables]
    neg_l = [Literal(v, negated=True) for v in variables]

    c0 = [pos_l[10]
          ]  # unused clause to keep the numbering aligned to the slides
    c1 = [pos_l[2], pos_l[3]]
    c2 = [pos_l[9]
          ]  # unused clause to keep the numbering aligned to the slides
    c3 = [neg_l[3], neg_l[4]]
    c4 = [neg_l[4], neg_l[2], neg_l[1]]
    c5 = [neg_l[6], neg_l[5], pos_l[4]]
    c6 = [pos_l[7], pos_l[5]]
    c7 = [neg_l[8], pos_l[7], pos_l[6]]
    conflict = [c0, c1, c2, c3, c4, c5, c6, c7]

    clauses = conflict
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)

    g = ImplicationGraph(cnf)

    g.add_decide_node(1, pos_l[1])
    g.add_decide_node(2, pos_l[8])
    g.add_decide_node(3, pos_l[7])

    g.add_node(3, pos_l[6], None, 7)
    g.add_node(3, pos_l[5], None, 6)
    g.add_node(3, pos_l[4], None, 5)
    g.add_node(3, neg_l[3], None, 3)
    g.add_node(3, neg_l[2], None, 4)

    return g, 1, variables
Пример #5
0
def test_search_complex_unsat():
    variables = [Variable('TEMP')
                 ] + [Variable('x{}'.format(i + 1)) for i in range(10)]
    pos_l = [Literal(v, negated=False) for v in variables]
    neg_l = [Literal(v, negated=True) for v in variables]

    # x1 = T, x2 = F, x3 =
    # x3 != x2 /\ x1 != x2 /\ (!x1 \/ x2 \/ !x3) /\ (x1 \/ !x2 \/ x3)
    clauses = [[pos_l[1], pos_l[2]], [neg_l[1], neg_l[2]],
               [pos_l[3], pos_l[2]], [neg_l[3], neg_l[2]],
               [neg_l[1], pos_l[2], neg_l[3]], [pos_l[3], neg_l[2], pos_l[1]]]

    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    search_result = dpll.search()
    # print(dpll.get_assignment())
    assert not search_result
Пример #6
0
def test_search_simple_unsat():
    # (x1|~x2) | (~x1 | x2)
    x1_var = Variable('x1')
    x2_var = Variable('x2')
    # x3_var = Variable('x3')

    x1 = Literal(x1_var, negated=False)
    not_x1 = Literal(x1_var, negated=True)
    x2 = Literal(x2_var, negated=False)
    not_x2 = Literal(x2_var, negated=True)

    clauses = [[x1], [x2], [not_x1, not_x2]]
    # literal_to_clauses = {x1: {0}, x2: {0}, x3: {0}} #not_x1: {2}, not_x2: {0}
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    search_result = dpll.search()

    assert not search_result
Пример #7
0
def test_search_simple():
    # (x1|x2|~x3) | (x3 | ~x2)
    x1_var = Variable('x1')
    x2_var = Variable('x2')
    x3_var = Variable('x3')

    x1 = Literal(x1_var, negated=False)
    not_x1 = Literal(x1_var, negated=True)
    x2 = Literal(x2_var, negated=False)
    not_x2 = Literal(x2_var, negated=True)
    x3 = Literal(x3_var, negated=False)
    not_x3 = Literal(x3_var, negated=True)

    clauses = [[x1, x2, not_x3], [x3, not_x2]]
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    search_result = dpll.search()
    assert search_result
Пример #8
0
def test_multi_level_deduction_sat():
    x1_var = Variable('x1')
    x2_var = Variable('x2')
    x3_var = Variable('x3')

    x1 = Literal(x1_var, negated=False)
    not_x1 = Literal(x1_var, negated=True)
    x2 = Literal(x2_var, negated=False)
    not_x2 = Literal(x2_var, negated=True)
    x3 = Literal(x3_var, negated=False)
    not_x3 = Literal(x3_var, negated=True)

    clauses = [[x1], [x3, x2], [not_x2, not_x3, not_x1]]
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    search_result = dpll.search()

    assert search_result
def get_simple_graph() -> (ImplicationGraph, int, List[Variable]):
    variables = [Variable('TEMP')
                 ] + [Variable('x{}'.format(i + 1)) for i in range(8)]
    pos_l = [Literal(v, negated=False) for v in variables]
    neg_l = [Literal(v, negated=True) for v in variables]

    # x1 =True --> x3=True, x5=True  --> x2 = True,
    clauses = [[pos_l[1]], [neg_l[1], pos_l[5]], [neg_l[1], pos_l[3]],
               [neg_l[2], neg_l[3]], [neg_l[5], pos_l[2]]]
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    g = ImplicationGraph(cnf)

    g.add_decide_node(1, pos_l[1])
    g.add_node(1, pos_l[3], None, 2)
    g.add_node(1, pos_l[5], None, 1)
    g.add_node(1, neg_l[2], None, 4)
    # g.add_node(1, pos_l[2], None, 3)
    # g.add_node(1, pos_l[4], None, 3)

    return g, 3, variables
Пример #10
0
def test_multi_level_conflict_sat():
    vars = [Variable('x{}'.format(i + 1)) for i in range(8)]
    pos_l = [None] + [Literal(v, negated=False) for v in vars]
    neg_l = [None] + [Literal(v, negated=True) for v in vars]

    c1 = [pos_l[2], pos_l[3]]
    # c2 = [pos_l[1], pos_l[4], neg_l[8]]
    c3 = [neg_l[3], neg_l[4]]
    c4 = [neg_l[4], neg_l[2], neg_l[1]]
    c5 = [neg_l[6], neg_l[5], pos_l[4]]
    c6 = [pos_l[7], pos_l[5]]
    c7 = [neg_l[8], pos_l[7], pos_l[6]]
    conflict = [c3, c4, c5, c6, c7, c1]  # c2,
    # If we implement pure_literal will need to change this
    # this is just to make sure the order decisions will be: x1=True, x2=True, x3=True, the conflict is because x1
    n_temps = 4
    temp_literals = [
        Literal(Variable('x1_temp{}'.format(idx)), negated=False)
        for idx in range(n_temps)
    ]
    x1_clauses = [[pos_l[1], l] for l in temp_literals]
    temp_literals = [
        Literal(Variable('x8_temp{}'.format(idx)), negated=False)
        for idx in range(n_temps)
    ]
    x8_clauses = [[pos_l[8], l] for l in temp_literals[:-1]]
    temp_literals = [
        Literal(Variable('x7_temp{}'.format(idx)), negated=False)
        for idx in range(n_temps)
    ]
    x7_clauses = [[neg_l[7], l] for l in temp_literals[:-2]]

    clauses = x1_clauses + x8_clauses + x7_clauses + conflict
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    search_result = dpll.search()

    assert search_result
Пример #11
0
def test_up_simple():
    # (x1|~x2|x3)&x2&(~x1|x3) --> (x1|x3) & (~x1|x3)
    x1_var = Variable('x1')
    x2_var = Variable('x2')
    x3_var = Variable('x3')

    x1 = Literal(x1_var, negated=False)
    not_x1 = Literal(x1_var, negated=True)
    x2 = Literal(x2_var, negated=False)
    not_x2 = Literal(x2_var, negated=True)
    x3 = Literal(x3_var, negated=False)

    clauses = [[x1, not_x2, x3], [x2], [not_x1, x3]]

    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    actual_cnf = dpll.unit_propagation()
    expected_cnf = [[x1, not_x2, x3], [not_x1, x3]]

    assert dpll.get_full_assignment()[x2_var]
    actual_cnf_real = [cl for cl in actual_cnf.clauses if cl != []]
    assert actual_cnf_real == expected_cnf, dpll.get_full_assignment()
def perform_test(clauses: List[List[Literal]], debug=False):
    z3_time_start = timer()
    z3_res = get_z3_result(clauses, debug)
    z3_time_end = timer()

    our_time_start = timer()
    cnf = CnfFormula(clauses)
    cnf = preprocess(cnf)
    dpll = DPLL(cnf)
    search_result = dpll.search()
    if debug:
        print(dpll.get_full_assignment())
    our_time_end = timer()

    assert search_result == z3_res, "Our: {}, Z3: {}".format(
        search_result, z3_res)
    res_str = 'Sat ' if search_result else 'UNSAT '
    all_vars = set([lit.variable for clause in clauses for lit in clause])
    res_str += "#var: {}, #clauses: {} #per_clause: {} ".format(
        len(all_vars), len(clauses), len(clauses[0]))
    res_str += "Time(sec): Our {:0.2f}, z3: {:0.2f}".format(
        our_time_end - our_time_start, z3_time_end - z3_time_start)
    print(res_str)