Example #1
0
 def test_four_ors(self):
     self._test_gatherer("A OR B OR C OR D", {
         SymbolTerm("A"),
         SymbolTerm("B"),
         SymbolTerm("C"),
         SymbolTerm("D")
     })
Example #2
0
 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")
     ])
Example #3
0
 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")))
Example #5
0
 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")))))
Example #6
0
    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"))
        ])
Example #7
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)
                ]
    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)
Example #9
0
 def test_or(self):
     self._test_transformer("A OR B",
                            OrTerm(SymbolTerm("A"), SymbolTerm("B")))
Example #10
0
 def test_simple_and_clause(self):
     self._test_gatherer_str("A AND B", [SymbolTerm("A"), SymbolTerm("B")])
Example #11
0
 def test_biconditional_transformation(self):
     self._test_transformer(
         "A <=> B",
         AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")),
                 OrTerm(NotTerm(SymbolTerm("B")), SymbolTerm("A"))))
Example #12
0
 def test_or_and_not(self):
     self._test_gatherer("A OR (NOT B)", {SymbolTerm("A")},
                         {SymbolTerm("B")})
Example #13
0
 def test_and(self):
     self._test_transformer("A AND B",
                            AndTerm(SymbolTerm("A"), SymbolTerm("B")))
Example #14
0
 def test_one_or(self):
     self._test_gatherer("A OR B", {SymbolTerm("A"), SymbolTerm("B")})
Example #15
0
 def test_or_distribution1(self):
     self._test_transformer(
         "(A AND B) OR C",
         AndTerm(OrTerm(SymbolTerm("A"), SymbolTerm("C")),
                 OrTerm(SymbolTerm("B"), SymbolTerm("C"))))
Example #16
0
 def test_de_morgan2(self):
     self._test_transformer(
         "NOT (A OR B)",
         AndTerm(NotTerm(SymbolTerm("A")), NotTerm(SymbolTerm("B"))))
Example #17
0
 def test_de_morgan1(self):
     self._test_transformer(
         "NOT (A AND B)",
         OrTerm(NotTerm(SymbolTerm("A")), NotTerm(SymbolTerm("B"))))
Example #18
0
 def test_four_successive_nots_transformation(self):
     self._test_transformer("NOT (NOT (NOT (NOT A)))", SymbolTerm("A"))
Example #19
0
 def test_triple_negatiation_transformation(self):
     self._test_transformer("NOT (NOT (NOT A))", NotTerm(SymbolTerm("A")))
Example #20
0
 def test_double_negatiation_transformation(self):
     self._test_transformer("NOT (NOT A)", SymbolTerm("A"))
Example #21
0
 def test_single_symbol(self):
     self._test_gatherer("A", {SymbolTerm("A")})
Example #22
0
 def test_or_distribution2(self):
     self._test_transformer(
         "A OR (B AND C)",
         AndTerm(OrTerm(SymbolTerm("B"), SymbolTerm("A")),
                 OrTerm(SymbolTerm("C"), SymbolTerm("A"))))
Example #23
0
 def test_two_ors(self):
     self._test_gatherer(
         "A OR B OR C", {SymbolTerm("A"),
                         SymbolTerm("B"),
                         SymbolTerm("C")})
Example #24
0
 def test_root_and_term(self):
     self._test_transformer(
         "((A => B) AND C)",
         AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")),
                 SymbolTerm("C")))
Example #25
0
 def test_one_not(self):
     self._test_gatherer("NOT A", set(), {SymbolTerm("A")})
Example #26
0
 def test_symbol(self):
     self._test_gatherer_str("A", [SymbolTerm("A")])
Example #27
0
 def test_or_and_not2(self):
     self._test_gatherer("(NOT A) OR (NOT B) OR C", {SymbolTerm("C")},
                         {SymbolTerm("A"), SymbolTerm("B")})
Example #28
0
 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))
Example #30
0
 def test_implication_transfromation(self):
     self._test_transformer(
         "A => B", OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")))