def test_four_ors(self): self._test_gatherer("A OR B OR C OR D", { SymbolTerm("A"), SymbolTerm("B"), SymbolTerm("C"), SymbolTerm("D") })
def test_simple_and_multi_clause2(self): self._test_gatherer_str("D AND (A AND B) AND C", [ SymbolTerm("A"), SymbolTerm("B"), SymbolTerm("C"), SymbolTerm("D") ])
def test_and(self): # Here comes my LISP nostalgia self._test_parser( "Java AND Haskell => Scala", AndTerm( SymbolTerm("Java"), ImplicationTerm(SymbolTerm("Haskell"), SymbolTerm("Scala"))))
def test_dpll1(self): kb = KnowledgeBase() kb.tell_str("(B12 <=> (P11 OR (P13 OR (P22 OR P02))))") kb.tell_str("(B21 <=> (P20 OR (P22 OR (P31 OR P11))))") kb.tell_str("(B01 <=> (P00 OR (P02 OR P11)))") kb.tell_str("(B10 <=> (P11 OR (P20 OR P00)))") kb.tell_str("(NOT B21)") kb.tell_str("(NOT B12)") kb.tell_str("(B10)") kb.tell_str("(B01)") kb.ask_with_dpll(SymbolTerm("P00")) kb.ask_with_dpll(NotTerm(SymbolTerm("P00")))
def test_aima_example(self): self._test_transformer( "B11 <=> (P12 OR P21)", AndTerm( OrTerm(NotTerm(SymbolTerm("B11")), OrTerm(SymbolTerm("P12"), SymbolTerm("P21"))), AndTerm(OrTerm(NotTerm(SymbolTerm("P12")), SymbolTerm("B11")), OrTerm(NotTerm(SymbolTerm("P21")), SymbolTerm("B11")))))
def test_aima_example(self): parser = PLParser() sentence = parser.parse("B11 <=> (P12 OR P21)") transformer = CNFTransformer() cnf = transformer.transform(sentence) self._test_gatherer(cnf, [ OrTerm(NotTerm(SymbolTerm("B11")), OrTerm(SymbolTerm("P12"), SymbolTerm("P21"))), OrTerm(NotTerm(SymbolTerm("P12")), SymbolTerm("B11")), OrTerm(NotTerm(SymbolTerm("P21")), SymbolTerm("B11")) ])
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) ]
def test_as_sentence(self): kb = KnowledgeBase() kb.tell_str("A") kb.tell_str("A OR B") kb.tell_str("C <=> D") kb.tell_str("E => NOT F") sentence = kb.as_sentence() expected_sentence = AndTerm( SymbolTerm("A"), AndTerm( OrTerm(SymbolTerm("A"), SymbolTerm("B")), AndTerm( BiconditionalTerm(SymbolTerm("C"), SymbolTerm("D")), ImplicationTerm(SymbolTerm("E"), NotTerm(SymbolTerm("F")))))) self.assertEqual(expected_sentence, sentence)
def test_or(self): self._test_transformer("A OR B", OrTerm(SymbolTerm("A"), SymbolTerm("B")))
def test_simple_and_clause(self): self._test_gatherer_str("A AND B", [SymbolTerm("A"), SymbolTerm("B")])
def test_biconditional_transformation(self): self._test_transformer( "A <=> B", AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")), OrTerm(NotTerm(SymbolTerm("B")), SymbolTerm("A"))))
def test_or_and_not(self): self._test_gatherer("A OR (NOT B)", {SymbolTerm("A")}, {SymbolTerm("B")})
def test_and(self): self._test_transformer("A AND B", AndTerm(SymbolTerm("A"), SymbolTerm("B")))
def test_one_or(self): self._test_gatherer("A OR B", {SymbolTerm("A"), SymbolTerm("B")})
def test_or_distribution1(self): self._test_transformer( "(A AND B) OR C", AndTerm(OrTerm(SymbolTerm("A"), SymbolTerm("C")), OrTerm(SymbolTerm("B"), SymbolTerm("C"))))
def test_de_morgan2(self): self._test_transformer( "NOT (A OR B)", AndTerm(NotTerm(SymbolTerm("A")), NotTerm(SymbolTerm("B"))))
def test_de_morgan1(self): self._test_transformer( "NOT (A AND B)", OrTerm(NotTerm(SymbolTerm("A")), NotTerm(SymbolTerm("B"))))
def test_four_successive_nots_transformation(self): self._test_transformer("NOT (NOT (NOT (NOT A)))", SymbolTerm("A"))
def test_triple_negatiation_transformation(self): self._test_transformer("NOT (NOT (NOT A))", NotTerm(SymbolTerm("A")))
def test_double_negatiation_transformation(self): self._test_transformer("NOT (NOT A)", SymbolTerm("A"))
def test_single_symbol(self): self._test_gatherer("A", {SymbolTerm("A")})
def test_or_distribution2(self): self._test_transformer( "A OR (B AND C)", AndTerm(OrTerm(SymbolTerm("B"), SymbolTerm("A")), OrTerm(SymbolTerm("C"), SymbolTerm("A"))))
def test_two_ors(self): self._test_gatherer( "A OR B OR C", {SymbolTerm("A"), SymbolTerm("B"), SymbolTerm("C")})
def test_root_and_term(self): self._test_transformer( "((A => B) AND C)", AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")), SymbolTerm("C")))
def test_one_not(self): self._test_gatherer("NOT A", set(), {SymbolTerm("A")})
def test_symbol(self): self._test_gatherer_str("A", [SymbolTerm("A")])
def test_or_and_not2(self): self._test_gatherer("(NOT A) OR (NOT B) OR C", {SymbolTerm("C")}, {SymbolTerm("A"), SymbolTerm("B")})
def test_not_sentence(self): self._test_gatherer_str("NOT A", [NotTerm(SymbolTerm("A"))])
# Example of using WalkSAT algorithm # # Number of flips in a single WalkSAT algorithm NUMBER_OF_FLIPS = 10000 # Probability of flipping symbol's value in a clause WALK_SAT_PROBABILITY = 0.02 # Number of tries to solve a problem NUMBER_OF_TRIES = 10 # Create a simple knowledge base from AIMA example kb = KnowledgeBase() kb.tell_str("NOT P11") kb.tell_str("B11 <=> (P12 OR P21)") kb.tell_str("B21 <=> (P11 OR P22 OR P31)") kb.tell_str("NOT B11") kb.tell_str("B21") # Create sentence with all statements from knowledge base sentence = kb.as_sentence() # Create WalkSAT algorithm class ws = WalkSat() for i in range(NUMBER_OF_TRIES): # Get result model. If solution was found it return Model object, or None otherwise result_model = ws.find_model_for(AndTerm(sentence, SymbolTerm("P31")), NUMBER_OF_FLIPS, WALK_SAT_PROBABILITY) print("Model: " + str(result_model))
def test_implication_transfromation(self): self._test_transformer( "A => B", OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")))