예제 #1
0
    def duplicate_knowledge_base(self,
                                 base: KnowledgeBase,
                                 step: int,
                                 use_dependency_graph=False):
        """ Deep copies a KB """

        clauses_ = self.personal_deepcopy(base.clauses)
        set_literals_ = self.duplicate_dict(base.current_set_literals)
        bookkeeping_ = self.duplicate_default_dict(base.bookkeeping,
                                                   self.duplicate_set, set)

        # dependency graph stuff
        if (use_dependency_graph):
            initial_ = self.duplicate_default_dict(
                base.dependency_graph.initial_coocurrence, self.duplicate_set,
                set)
            graph_ = self.duplicate_default_dict(base.dependency_graph.graph,
                                                 self.duplicate_set, set)
            dependency_graph_ = DependencyGraph(initial=initial_,
                                                graph=graph_,
                                                existing_literals=list(
                                                    set_literals_.keys()))
        else:
            dependency_graph_ = use_dependency_graph

        return KnowledgeBase(clauses=clauses_,
                             current_set_literals=set_literals_,
                             bookkeeping=bookkeeping_,
                             clause_counter=base.clause_counter,
                             literal_counter=base.literal_counter,
                             dependency_graph=dependency_graph_,
                             timestep=step)
예제 #2
0
def test_solver_case3():

    clauses = [Clause(1, [1, -2]), Clause(2, [1, -3, 2]), Clause(3, [3, 2, -1]), Clause(4, [-2, -1, 3])]
    clauses = {clause.id: clause for clause in clauses}
    kb = KnowledgeBase(clauses, clause_counter=3)
    s = CDCL_DPLL_Solver(kb)

    res, solved = s.solve_instance()
예제 #3
0
def test_solver_case5():
    ls = [[-1, -2, -3], [-1, -2], [-2,-3], [-1,-3]]
    clauses = [Clause(i, l) for i, l in enumerate(ls)]
    clauses = {clause.id: clause for clause in clauses}
    kb = KnowledgeBase(clauses, clause_counter=len(clauses))
    s = CDCL_DPLL_Solver(kb)

    res, solved, _ = s.solve_instance()
    print(res.current_set_literals)
예제 #4
0
def test_solver_diplomatic_puzzle():
    clauses = {1: Clause(1, [1, -2]), 2: Clause(2, [2, 3]), 3: Clause(3, [-3, -1])}
    kb = KnowledgeBase(clauses, clause_counter=3)
    s = CDCL_DPLL_Solver(kb)

    res, solved = s.solve_instance()

    assert res.current_set_literals == {1:True, 2: True, 3: False}
    assert solved is True
예제 #5
0
def test_solver_case4():
    ls = [[1,4], [1,-3,-8], [1,8,12], [2,11], [-7,-3,9], [-7,8,-9], [7,8,-10], [7,10,-12]]
    settings = get_settings(1)
    clauses = [Clause(i, l) for i, l in enumerate(ls)]
    clauses = {clause.id: clause for clause in clauses}
    kb = KnowledgeBase(clauses, clause_counter=len(clauses))
    s = CDCL_DPLL_Solver(kb, split_stats=[], heuristics=settings)

    res, solved, stats = s.solve_instance()
    print(res.current_set_literals, stats)
예제 #6
0
    def wrap_up_result(self, base: KnowledgeBase, solved: bool, stats: List , all_literals: List[int]) -> Tuple[KnowledgeBase, bool, List]:
        """
        Makes sure that literals that are not set yet because their value is irrelevant are set at random.

        :param base:
        :param solved:
        :param stats:
        :param all_literals:
        :return:
        """

        for literal in all_literals:
            if (not literal in base.current_set_literals):
                try:
                    rand_assign = random.choice([True, False])
                    base.set_literal(literal, rand_assign)
                except Exception:
                    pass # ignore

        return base, solved, stats
예제 #7
0
def test_solver_tautology():
    clauses = {1: Clause(1, [1, 2, 3, -1])}
    kb = KnowledgeBase(clauses, clause_counter=1)
    s = CDCL_DPLL_Solver(kb)

    assert s.solve_instance()[1] is True