Exemplo n.º 1
0
    def test_collect_symbols(self):
        expression = "(A AND NOT B) => B <=> NOT C OR D => E"
        parser = PLParser()
        root_term = parser.parse(expression)
        sc = SymbolsCollector()

        sc.collect_symbols(root_term)
        result_symbols = sc.symbols
        expected_symbols = ["A", "B", "C", "D", "E"]

        self.assertSameElements(expected_symbols, result_symbols)
Exemplo n.º 2
0
    def test_collect_symbols(self):
        expression = "(A AND NOT B) => B <=> NOT C OR D => E"
        parser = PLParser()
        root_term = parser.parse(expression)
        sc = SymbolsCollector()

        sc.collect_symbols(root_term)
        result_symbols = sc.symbols
        expected_symbols = ["A", "B", "C", "D", "E"]

        self.assertSameElements(expected_symbols, result_symbols)
Exemplo n.º 3
0
    def tt_entails(self, knowledge_base, alpha):
        kb_sentence = knowledge_base.as_sentence()
        query_sentence = PLParser().parse(alpha)

        collector = SymbolsCollector()
        kb_symbols = collector.collect_symbols(kb_sentence)
        query_symbols = collector.collect_symbols(query_sentence)

        # symbols <- a list of proposition symbols in KB and alpha
        symbols_list = list(kb_symbols.union(query_symbols))
        # return TT-Check-All(KB, alpha, symbols, [])
        return self.tt_check_all(kb_sentence, query_sentence, symbols_list, Model())
Exemplo n.º 4
0
    def tt_entails(self, knowledge_base, alpha):
        kb_sentence = knowledge_base.as_sentence()
        query_sentence = PLParser().parse(alpha)

        collector = SymbolsCollector()
        kb_symbols = collector.collect_symbols(kb_sentence)
        query_symbols = collector.collect_symbols(query_sentence)

        # symbols <- a list of proposition symbols in KB and alpha
        symbols_list = list(kb_symbols.union(query_symbols))
        # return TT-Check-All(KB, alpha, symbols, [])
        return self.tt_check_all(kb_sentence, query_sentence, symbols_list,
                                 Model())
Exemplo n.º 5
0
        def __init__(self, root_term):
            self.head = None
            self.premise_symbols = None

            if root_term.type == TokenTypes.IDENTIFIER:
                self.head = root_term
                self.premise_symbols = []
            elif root_term.type != TokenTypes.IMPLICATION:
                raise ValueError("Sentence " + str(root_term) + " isn't a horn clause")
            else:
                self.head = root_term.children[1]

                collector = SymbolsCollector()
                premise_expression = root_term.children[0]
                self.premise_symbols = [SymbolTerm(name) for name in collector.collect_symbols(premise_expression)]
Exemplo n.º 6
0
    def find_model_for(self, sentence, number_of_flips,
                       probability_of_random_walk):
        model = Model()
        cnf_sentence = CNFTransformer().transform(sentence)
        clauses = CNFClauseGatherer().collect(cnf_sentence)

        symbols = SymbolsCollector().collect_symbols(sentence)
        # model <- a random assignment of true/false to the symbols in clauses
        for symbol in symbols:
            model = model.extend(symbol, randbool())

        # for i = 1 to max_flips do
        for i in range(number_of_flips):
            # if model satisfies clauses then return model
            if self._all_clauses_satisfied(clauses, model):
                return model

            # clause <- a randomly selected clause from clauses that is false in model
            symbols_list = list(
                self._get_symbols_of_randomly_selected_false_clause(
                    clauses, model))
            # with probability p flip the value in model of a randomly selected symbol from clause
            if random.random() >= probability_of_random_walk:
                symbol = select_randomly_from_list(symbols_list)
            # else flip whichever symbol in clause maximizes the number of satisfied clauses
            else:
                symbol = self._get_symbol_whose_flip_maximises_satisfied_clauses(
                    clauses, model, symbols_list)

            model.flip(symbol)

        return None
Exemplo n.º 7
0
    def _get_symbols_of_randomly_selected_false_clause(self, clauses, model):
        false_clauses = []
        for clause in clauses:
            if not model.is_true(clause):
                false_clauses.append(clause)

        random_false_clause = select_randomly_from_list(false_clauses)
        return SymbolsCollector().collect_symbols(random_false_clause)
Exemplo n.º 8
0
 def dpll_satisfiable(self, sentence, model=Model()):
     cnf_sentence = CNFTransformer().transform(sentence)
     # clauses <- the set of clauses in the CNF representation of s
     clauses = CNFClauseGatherer().collect(cnf_sentence)
     # symbols <- a list of the proposition symbols in s
     symbols = SymbolsCollector().collect_symbols(sentence)
     # return DPLL(clauses, symbols, [])
     return self._dpll(clauses, symbols, model)
Exemplo n.º 9
0
        def __init__(self, root_term):
            self.head = None
            self.premise_symbols = None

            if root_term.type == TokenTypes.IDENTIFIER:
                self.head = root_term
                self.premise_symbols = []
            elif root_term.type != TokenTypes.IMPLICATION:
                raise ValueError("Sentence " + str(root_term) +
                                 " isn't a horn clause")
            else:
                self.head = root_term.children[1]

                collector = SymbolsCollector()
                premise_expression = root_term.children[0]
                self.premise_symbols = [
                    SymbolTerm(name)
                    for name in collector.collect_symbols(premise_expression)
                ]