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
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)
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
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)
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))
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())
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()
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)