コード例 #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_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()
コード例 #3
0
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)
コード例 #4
0
if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("execution instructions [SAT|SMT|LP] [query string]")
        exit(1)

    q = sys.argv[2]
    if sys.argv[1].lower() == 'sat':
        sat = SatFormula.from_str(q)
        formula = preprocess_from_sat(sat)
        dpll = DPLL(formula)
        res = dpll.search()
        print(f"Got {'SAT' if res else 'UNSAT'} for query\n{q}")
        # optional to retrieve the assignment
        if res:
            assignment = dpll.get_full_assignment()
            all_variables = set(l.variable for l in sat.get_literals())
            actual_assignments = map(lambda k: (k, assignment[k]),
                                     all_variables)
            pprint(dict(actual_assignments))
    if sys.argv[1].lower() == 'smt':
        smt_query = Formula.from_str(q)
        res, model = smt_query.solve()
        print(f"Got {'SAT' if res else 'UNSAT'} for query\n{q}")

    if sys.argv[1].lower() == 'lp':
        if len(sys.argv) < 4:
            print("lp requires at least one equation and one objective")
            exit(1)
        equations = sys.argv[2:-1]
        objective = sys.argv[-1]