예제 #1
0
파일: test_clause.py 프로젝트: S-Ercan/GA
    def test_valid_valuation_satisfies(self):
        variable = Variable('a')
        mapping = {variable: True}
        v = Valuation(mapping)

        c = Clause([Literal(variable)])
        self.assertTrue(c.is_satisfied(v))
예제 #2
0
    def __init__(self):
        self.clauses = []
        self.truth_assignment = []
        self.truth_values = []

        for x in range(300):
            #for x in range(18):
            self.clauses.append(Clause())
예제 #3
0
파일: test_clause.py 프로젝트: S-Ercan/GA
    def test_invalid_valuation_does_not_satisfy(self):
        variable = Variable('a')
        v = Valuation({variable: False})

        c = Clause([Literal(variable)])
        self.assertFalse(c.is_satisfied(v))
예제 #4
0
파일: test_clause.py 프로젝트: S-Ercan/GA
 def test_is_satisfied_with_invalid_argument_type_fails(self):
     c = Clause([Literal(Variable('a'))])
     self.assertRaises(TypeError, lambda l: c.is_satisfied({}))
예제 #5
0
def run_dpll(formula_so_far, truth_so_far):
    # begin dpll by calling unit prop
    new_formula, new_assignment = unit_propagation(formula_so_far[:], truth_so_far[:])

    print(new_formula)
    print(new_assignment)

    # satisfiable instance
    if len(new_formula) == 0:
        # end timer for individual run
        end = time.time()

        # go to next sat instance
        return True

    # # unsatisfiable instance
    # if len(new_formula) > 0:
    #
    #     # end timer for individual run
    #     end = time.time()
    #
    #     # add totals and running times to arrays
    #     totals.append(1)
    #     running_times.append((end - start))
    #
    #     # go to next sat instance
    #     return False

    # find first variable unassigned in new_assignment
    first_unassigned_literal = 0
    for q in range(len(new_assignment)):
        if new_assignment[q] is None:
            first_unassigned_literal = q + 1
            break

    # create unit clause and add to formula
    unit_clause = Clause()
    unit_clause.create_unit_clause(first_unassigned_literal)
    new_formula.insert(0, unit_clause)

    # add to truth assignment
    new_assignment[first_unassigned_literal - 1] = True
    new_assignment[first_unassigned_literal + 99] = False

    # # add to truth assignment
    # if first_unassigned_literal <= 100:
    #     new_assignment[first_unassigned_literal-1] = True
    #     new_assignment[first_unassigned_literal+99] = False
    # else:
    #     new_assignment[first_unassigned_literal-1] = False
    #     new_assignment[first_unassigned_literal-101] = True

    print(new_assignment)
    print(new_formula)

    # recursively run dpll
    result = run_dpll(new_formula[:], new_assignment[:])

    if result:
        return True
    else:
        # create unit clause and add to formula
        unit_clause = Clause()
        unit_clause.create_unit_clause(first_unassigned_literal + 100)
        new_formula.insert(0, unit_clause)

        # add to truth assignment
        new_assignment[first_unassigned_literal - 1] = False
        new_assignment[first_unassigned_literal + 99] = True

        return run_dpll(new_formula[:], new_assignment[:])