Beispiel #1
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 solve(self) -> bool:
        dpll_algorithm = DPLL(self.sat_formula, propagate_helper=self.propagate, conflict_helper=self.conflict)

        # Solve sat formula
        is_sat = dpll_algorithm.search()

        # The search is infeasible
        if not is_sat:
            return False

        return True
    def solve(self) -> Tuple[bool, Optional[Dict[str, bool]]]:
        from sat_solver.preprocessor import preprocess_from_sat
        cnf_formula = preprocess_from_sat(self.sat_formula)
        dpll_algorithm = DPLL(cnf_formula,
                              propagate_helper=self.propagate,
                              conflict_helper=self.conflict)

        # Solve sat formula
        is_sat = dpll_algorithm.search()

        # Sat formula is unsat hence, smt formula is ussat
        if not is_sat:
            return False, None

        partial_assignment = dpll_algorithm.get_partial_assignment()
        assignment = self.transfer_assignment(partial_assignment)
        return self.satisfied(partial_assignment), assignment
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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
Beispiel #7
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
Beispiel #8
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
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)
def get_result(query: str):
    sat_formula = SatFormula.from_str(query)
    formula = preprocess_from_sat(sat_formula)
    dpll = DPLL(formula)
    res = dpll.search()
    return res
Beispiel #11
0
from lp_solver.lp_program import LpProgram
from lp_solver.unbounded_exception import UnboundedException, InfeasibleException
from lp_solver.lp_theory import LpTheory

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: