Пример #1
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
Пример #2
0
    def test_dpll_when_all_clauses_true(self):
        model = Model()
        model = model.extend("A", True).extend("B", True)

        sentence = PLParser().parse("(A OR B) AND (A OR B)")
        result = DPLL().dpll_satisfiable(sentence, model)
        self.assertTrue(result)
Пример #3
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
Пример #4
0
    def test_dpll_when_all_clauses_true(self):
        model = Model()
        model = model.extend("A", True).extend("B", True)

        sentence = PLParser().parse("(A OR B) AND (A OR B)")
        result = DPLL().dpll_satisfiable(sentence, model)
        self.assertTrue(result)
Пример #5
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)
Пример #6
0
    def test_not(self):
        expression = "NOT A"
        parser = PLParser()
        root_term = parser.parse(expression)

        m = Model()
        m = m.extend("A", False)

        self.assertTrue(m.is_true(root_term))
        m.clear()

        m = m.extend("A", True)

        self.assertFalse(m.is_true(root_term))
Пример #7
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())
Пример #8
0
    def test_not(self):
        expression = "NOT A"
        parser = PLParser()
        root_term = parser.parse(expression)

        m = Model()
        m = m.extend("A", False)

        self.assertTrue(m.is_true(root_term))
        m.clear()

        m = m.extend("A", True)

        self.assertFalse(m.is_true(root_term))
Пример #9
0
    def test_biconditional(self):
        expression = "A <=> B"
        parser = PLParser()
        root_term = parser.parse(expression)

        m = Model()
        m = m.extend("A", True).extend("B", True)

        self.assertTrue(m.is_true(root_term))
        m.clear()

        m = m.extend("A", True).extend("B", False)

        self.assertFalse(m.is_true(root_term))
        m.clear()

        m = m.extend("A", False).extend("B", True)

        self.assertFalse(m.is_true(root_term))
        m.clear()

        m = m.extend("A", False).extend("B", False)

        self.assertTrue(m.is_true(root_term))
        m.clear()
Пример #10
0
    def test_biconditional(self):
        expression = "A <=> B"
        parser = PLParser()
        root_term = parser.parse(expression)

        m = Model()
        m = m.extend("A", True).extend("B", True)

        self.assertTrue(m.is_true(root_term))
        m.clear()

        m = m.extend("A", True).extend("B", False)

        self.assertFalse(m.is_true(root_term))
        m.clear()

        m = m.extend("A", False).extend("B", True)

        self.assertFalse(m.is_true(root_term))
        m.clear()

        m = m.extend("A", False).extend("B", False)

        self.assertTrue(m.is_true(root_term))
        m.clear()
Пример #11
0
    def test_dpll_return_false_with_one_false_in_model(self):
        model = Model().extend("A", True).extend("B", False)

        sentence = PLParser().parse("(A OR B) AND (A => B)")
        result = DPLL().dpll_satisfiable(sentence, model)
        self.assertFalse(result)