From 7282ab91ae087e3b3b6767f312dac2ef63051195 Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 02:37:49 +1000 Subject: [PATCH 01/14] forward chaining first implementation --- inference_engine_algorithms/forward_chaining | 76 ++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 inference_engine_algorithms/forward_chaining diff --git a/inference_engine_algorithms/forward_chaining b/inference_engine_algorithms/forward_chaining new file mode 100644 index 000000000000..a69af7d8005e --- /dev/null +++ b/inference_engine_algorithms/forward_chaining @@ -0,0 +1,76 @@ +import re + + +def find_symbols_in_kb(knowledge_base: list[str]) -> dict: + ''' + ''' + inferred = {} + + for i in range(len(knowledge_base)): + symbols = re.findall(r'[a-zA-Z]', knowledge_base[i]) + for symbol in symbols: + if symbol not in inferred: + inferred[symbol] = False + + return inferred + + +def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: + + ''' + ''' + + count = {} + for clause in knowledge_base: + if(len(clause) != 1): + index = clause.find("=>") + premise = clause[:index] + letters = ''.join(e for e in premise if e.isalpha()) + count[premise] = len(letters) + + return count + + +def get_known_facts(knowledge_base: list[str]) -> list[str]: + + ''' + ''' + facts = [] + for clause in knowledge_base: + if len(clause) == 1: + facts.append(clause) + + return facts + + + + +def forward_chianing(knowledge_base: list[str], query:str) -> bool: + + count = number_of_symbols_in_premise(knowledge_base) + inferred = find_symbols_in_kb(knowledge_base) + queue = get_known_facts(knowledge_base) + + while(len(queue) > 0): + p = queue.pop() + if p == query: return True + if inferred[p] == False: + inferred[p] = True + for clause in knowledge_base: + index = clause.find("=>") + premise = clause[:index] + if p in premise: + count[premise] -= 1 + if count[premise] == 0: + queue.append(clause[-1]) + + return False + + + +if __name__ == "__main__": + + kb = ["p => q", "q => r", "r => p", "s => t", "u => v", "v => w", "w => u", "a", "b", "c" , "w" , "u&q=>r", "q"] + + result = forward_chianing(kb, "p") + print(result) \ No newline at end of file From 98a810e818cc13ffac7d9f0620b56a425f2a299d Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 02:38:28 +1000 Subject: [PATCH 02/14] forward chaining first implementaion --- .../forward_chaining | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 propositional_logic_inference_algorithms/forward_chaining diff --git a/propositional_logic_inference_algorithms/forward_chaining b/propositional_logic_inference_algorithms/forward_chaining new file mode 100644 index 000000000000..8344ac57eb82 --- /dev/null +++ b/propositional_logic_inference_algorithms/forward_chaining @@ -0,0 +1,76 @@ +import re + + +def find_symbols_in_kb(knowledge_base: list[str]) -> dict: + ''' + ''' + inferred = {} + + for i in range(len(knowledge_base)): + symbols = re.findall(r'[a-zA-Z]', knowledge_base[i]) + for symbol in symbols: + if symbol not in inferred: + inferred[symbol] = False + + return inferred + + +def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: + + ''' + ''' + + count = {} + for clause in knowledge_base: + if(len(clause) != 1): + index = clause.find("=>") + premise = clause[:index] + letters = ''.join(e for e in premise if e.isalpha()) + count[premise] = len(letters) + + return count + + +def get_known_facts(knowledge_base: list[str]) -> list[str]: + + ''' + ''' + facts = [] + for clause in knowledge_base: + if len(clause) == 1: + facts.append(clause) + + return facts + + + + +def forward_chianing(knowledge_base: list[str], query:str) -> bool: + + count = number_of_symbols_in_premise(knowledge_base) + inferred = find_symbols_in_kb(knowledge_base) + queue = get_known_facts(knowledge_base) + + while(len(queue) > 0): + p = queue.pop() + if p == query: return True + if inferred[p] == False: + inferred[p] = True + for clause in knowledge_base: + index = clause.find("=>") + premise = clause[:index] + if p in premise: + count[premise] -= 1 + if count[premise] == 0: + queue.append(clause[-1]) + + return False + + + +if __name__ == "__main__": + + kb = ["p => q", "q => r", "r => p", "s => t", "u => v", "v => w", "w => u", "a", "b", "c" , "w" , "u&q=>r", "q"] + + result = forward_chianing(kb, "t") + print(result) \ No newline at end of file From 9d0f32678c6ede8fd2ce5bd774ecdac187e49ea0 Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 14:12:52 +1000 Subject: [PATCH 03/14] forward chaining algorithm complete --- .../forward_chaining | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/propositional_logic_inference_algorithms/forward_chaining b/propositional_logic_inference_algorithms/forward_chaining index 8344ac57eb82..2ab33fc7925a 100644 --- a/propositional_logic_inference_algorithms/forward_chaining +++ b/propositional_logic_inference_algorithms/forward_chaining @@ -3,7 +3,11 @@ import re def find_symbols_in_kb(knowledge_base: list[str]) -> dict: ''' + Find all unique symbols in the Knowledge_base + :param knowledge_base: a list of string of definite clauses + :returns: a dictionary with symbols as the keys their values are False ''' + inferred = {} for i in range(len(knowledge_base)): @@ -18,6 +22,9 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: ''' + Count the number of prposiotion symbols in each premise of KB clause + :param knowledge_base: a list of string of definite clauses + :returns: a dictionary with key as the premise of KB clause and vlaue of count of symbols in the premise ''' count = {} @@ -32,9 +39,13 @@ def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: def get_known_facts(knowledge_base: list[str]) -> list[str]: - ''' + Get the known facts in KB + :param knowledge_base: a list of string of definite clauses + :returns: list of facts + ''' + facts = [] for clause in knowledge_base: if len(clause) == 1: @@ -45,7 +56,17 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]: -def forward_chianing(knowledge_base: list[str], query:str) -> bool: +def forward_chaining(knowledge_base: list[str], query:str) -> bool: + ''' Forward chaining on Knowledge Base(KB) of definite clauses + :param knowledge_base: a list of string of definite clauses + :param query: a single proposition sysmbol that you are checking if it is entailed by the KB + :returns: If the query entailed by the KB or not? + >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q") + True + >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C") + False + + ''' count = number_of_symbols_in_premise(knowledge_base) inferred = find_symbols_in_kb(knowledge_base) @@ -68,9 +89,35 @@ def forward_chianing(knowledge_base: list[str], query:str) -> bool: + +KB = [ + "P => Q", + "L & M => P", + "B&L=> M", + "A&P=>L", + "A&B=>L", + "A", + "B" ] + +''' +1)- KB must be written in horn form. +2)- It must be written as an implcaion whoose +its premise(head) must be conjuction of positive literals and its conclusion(body) +3)- It must contains facts about the world which are written as a single proposition symbol +''' + +QUERY = "Q" + +''' +Query is a signe proposition symbol that you check if it is entailed by the KB +''' + if __name__ == "__main__": + - kb = ["p => q", "q => r", "r => p", "s => t", "u => v", "v => w", "w => u", "a", "b", "c" , "w" , "u&q=>r", "q"] + import doctest - result = forward_chianing(kb, "t") + doctest.testmod(verbose=True) + + result = forward_chaining(KB, QUERY) print(result) \ No newline at end of file From 08e84bd106daa1ca41d6e3b0fd7ba1342b82a648 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 May 2024 04:16:56 +0000 Subject: [PATCH 04/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- inference_engine_algorithms/forward_chaining | 2 +- .../forward_chaining | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/inference_engine_algorithms/forward_chaining b/inference_engine_algorithms/forward_chaining index a69af7d8005e..3b5db5781981 100644 --- a/inference_engine_algorithms/forward_chaining +++ b/inference_engine_algorithms/forward_chaining @@ -69,7 +69,7 @@ def forward_chianing(knowledge_base: list[str], query:str) -> bool: if __name__ == "__main__": - + kb = ["p => q", "q => r", "r => p", "s => t", "u => v", "v => w", "w => u", "a", "b", "c" , "w" , "u&q=>r", "q"] result = forward_chianing(kb, "p") diff --git a/propositional_logic_inference_algorithms/forward_chaining b/propositional_logic_inference_algorithms/forward_chaining index 2ab33fc7925a..de262c785f46 100644 --- a/propositional_logic_inference_algorithms/forward_chaining +++ b/propositional_logic_inference_algorithms/forward_chaining @@ -90,18 +90,18 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool: -KB = [ - "P => Q", - "L & M => P", +KB = [ + "P => Q", + "L & M => P", "B&L=> M", - "A&P=>L", - "A&B=>L", - "A", + "A&P=>L", + "A&B=>L", + "A", "B" ] ''' 1)- KB must be written in horn form. -2)- It must be written as an implcaion whoose +2)- It must be written as an implcaion whoose its premise(head) must be conjuction of positive literals and its conclusion(body) 3)- It must contains facts about the world which are written as a single proposition symbol ''' @@ -114,10 +114,10 @@ Query is a signe proposition symbol that you check if it is entailed by the KB if __name__ == "__main__": - + import doctest doctest.testmod(verbose=True) - + result = forward_chaining(KB, QUERY) print(result) \ No newline at end of file From d947265a0eb5dc9bc793c08667d15d5f84d418b6 Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 14:27:08 +1000 Subject: [PATCH 05/14] Added a reference and fixed some typos --- .../forward_chaining | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/propositional_logic_inference_algorithms/forward_chaining b/propositional_logic_inference_algorithms/forward_chaining index de262c785f46..48eb4ed2ae0b 100644 --- a/propositional_logic_inference_algorithms/forward_chaining +++ b/propositional_logic_inference_algorithms/forward_chaining @@ -1,5 +1,12 @@ import re +''' +The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition +symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from +known facts (positive literals) in the knowledge base. + +Refrence: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf +''' def find_symbols_in_kb(knowledge_base: list[str]) -> dict: ''' @@ -24,7 +31,7 @@ def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: ''' Count the number of prposiotion symbols in each premise of KB clause :param knowledge_base: a list of string of definite clauses - :returns: a dictionary with key as the premise of KB clause and vlaue of count of symbols in the premise + :returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise ''' count = {} @@ -59,7 +66,7 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]: def forward_chaining(knowledge_base: list[str], query:str) -> bool: ''' Forward chaining on Knowledge Base(KB) of definite clauses :param knowledge_base: a list of string of definite clauses - :param query: a single proposition sysmbol that you are checking if it is entailed by the KB + :param query: a single proposition symbol that you are checking if it is entailed by the KB :returns: If the query entailed by the KB or not? >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q") True @@ -90,19 +97,19 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool: -KB = [ - "P => Q", - "L & M => P", +KB = [ + "P => Q", + "L & M => P", "B&L=> M", - "A&P=>L", - "A&B=>L", - "A", + "A&P=>L", + "A&B=>L", + "A", "B" ] ''' 1)- KB must be written in horn form. -2)- It must be written as an implcaion whoose -its premise(head) must be conjuction of positive literals and its conclusion(body) +2)- It must be written as an implcaion whose +its premise(head) must be conjunction of positive literals and its conclusion(body) 3)- It must contains facts about the world which are written as a single proposition symbol ''' @@ -114,10 +121,10 @@ Query is a signe proposition symbol that you check if it is entailed by the KB if __name__ == "__main__": - + import doctest doctest.testmod(verbose=True) - + result = forward_chaining(KB, QUERY) print(result) \ No newline at end of file From 3a5361c4873712c40a14f3969896bb9b64f2ca93 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 May 2024 04:27:33 +0000 Subject: [PATCH 06/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../forward_chaining | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/propositional_logic_inference_algorithms/forward_chaining b/propositional_logic_inference_algorithms/forward_chaining index 48eb4ed2ae0b..4564a7e53c4f 100644 --- a/propositional_logic_inference_algorithms/forward_chaining +++ b/propositional_logic_inference_algorithms/forward_chaining @@ -2,7 +2,7 @@ import re ''' The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from -known facts (positive literals) in the knowledge base. +known facts (positive literals) in the knowledge base. Refrence: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf @@ -97,18 +97,18 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool: -KB = [ - "P => Q", - "L & M => P", +KB = [ + "P => Q", + "L & M => P", "B&L=> M", - "A&P=>L", - "A&B=>L", - "A", + "A&P=>L", + "A&B=>L", + "A", "B" ] ''' 1)- KB must be written in horn form. -2)- It must be written as an implcaion whose +2)- It must be written as an implcaion whose its premise(head) must be conjunction of positive literals and its conclusion(body) 3)- It must contains facts about the world which are written as a single proposition symbol ''' @@ -121,10 +121,10 @@ Query is a signe proposition symbol that you check if it is entailed by the KB if __name__ == "__main__": - + import doctest doctest.testmod(verbose=True) - + result = forward_chaining(KB, QUERY) print(result) \ No newline at end of file From 4ee23ffcba505116c341f198565486ba2f3792e4 Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 14:28:48 +1000 Subject: [PATCH 07/14] fixed more typos --- propositional_logic_inference_algorithms/forward_chaining | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/propositional_logic_inference_algorithms/forward_chaining b/propositional_logic_inference_algorithms/forward_chaining index 48eb4ed2ae0b..60869c904fa1 100644 --- a/propositional_logic_inference_algorithms/forward_chaining +++ b/propositional_logic_inference_algorithms/forward_chaining @@ -4,7 +4,7 @@ The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single pro symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from known facts (positive literals) in the knowledge base. -Refrence: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf +Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf ''' From c7cd58f100c625ee947867385bda01dc576c0edd Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 16:09:37 +1000 Subject: [PATCH 08/14] changing the file name --- .../{forward_chaining => forward_chaining.py} | 0 .../forward_chaining | 130 ------------------ 2 files changed, 130 deletions(-) rename inference_engine_algorithms/{forward_chaining => forward_chaining.py} (100%) delete mode 100644 propositional_logic_inference_algorithms/forward_chaining diff --git a/inference_engine_algorithms/forward_chaining b/inference_engine_algorithms/forward_chaining.py similarity index 100% rename from inference_engine_algorithms/forward_chaining rename to inference_engine_algorithms/forward_chaining.py diff --git a/propositional_logic_inference_algorithms/forward_chaining b/propositional_logic_inference_algorithms/forward_chaining deleted file mode 100644 index d7db0584e592..000000000000 --- a/propositional_logic_inference_algorithms/forward_chaining +++ /dev/null @@ -1,130 +0,0 @@ -import re -''' -The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition -symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from -known facts (positive literals) in the knowledge base. - -Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf - -''' - -def find_symbols_in_kb(knowledge_base: list[str]) -> dict: - ''' - Find all unique symbols in the Knowledge_base - :param knowledge_base: a list of string of definite clauses - :returns: a dictionary with symbols as the keys their values are False - ''' - - inferred = {} - - for i in range(len(knowledge_base)): - symbols = re.findall(r'[a-zA-Z]', knowledge_base[i]) - for symbol in symbols: - if symbol not in inferred: - inferred[symbol] = False - - return inferred - - -def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: - - ''' - Count the number of prposiotion symbols in each premise of KB clause - :param knowledge_base: a list of string of definite clauses - :returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise - ''' - - count = {} - for clause in knowledge_base: - if(len(clause) != 1): - index = clause.find("=>") - premise = clause[:index] - letters = ''.join(e for e in premise if e.isalpha()) - count[premise] = len(letters) - - return count - - -def get_known_facts(knowledge_base: list[str]) -> list[str]: - ''' - Get the known facts in KB - :param knowledge_base: a list of string of definite clauses - :returns: list of facts - - ''' - - facts = [] - for clause in knowledge_base: - if len(clause) == 1: - facts.append(clause) - - return facts - - - - -def forward_chaining(knowledge_base: list[str], query:str) -> bool: - ''' Forward chaining on Knowledge Base(KB) of definite clauses - :param knowledge_base: a list of string of definite clauses - :param query: a single proposition symbol that you are checking if it is entailed by the KB - :returns: If the query entailed by the KB or not? - >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q") - True - >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C") - False - - ''' - - count = number_of_symbols_in_premise(knowledge_base) - inferred = find_symbols_in_kb(knowledge_base) - queue = get_known_facts(knowledge_base) - - while(len(queue) > 0): - p = queue.pop() - if p == query: return True - if inferred[p] == False: - inferred[p] = True - for clause in knowledge_base: - index = clause.find("=>") - premise = clause[:index] - if p in premise: - count[premise] -= 1 - if count[premise] == 0: - queue.append(clause[-1]) - - return False - - - - -KB = [ - "P => Q", - "L & M => P", - "B&L=> M", - "A&P=>L", - "A&B=>L", - "A", - "B" ] - -''' -1)- KB must be written in horn form. -2)- It must be written as an implcaion whose -its premise(head) must be conjunction of positive literals and its conclusion(body) -3)- It must contains facts about the world which are written as a single proposition symbol -''' - -QUERY = "Q" - -''' -Query is a signe proposition symbol that you check if it is entailed by the KB -''' - -if __name__ == "__main__": - - - import doctest - - doctest.testmod(verbose=True) - - result = forward_chaining(KB, QUERY) - print(result) \ No newline at end of file From 664653ebfc4e2b50fe256f71e93fe90d6bef6c1f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 May 2024 06:10:01 +0000 Subject: [PATCH 09/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../forward_chaining.py | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/inference_engine_algorithms/forward_chaining.py b/inference_engine_algorithms/forward_chaining.py index 3b5db5781981..6bddbd610c99 100644 --- a/inference_engine_algorithms/forward_chaining.py +++ b/inference_engine_algorithms/forward_chaining.py @@ -2,12 +2,11 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: - ''' - ''' + """ """ inferred = {} for i in range(len(knowledge_base)): - symbols = re.findall(r'[a-zA-Z]', knowledge_base[i]) + symbols = re.findall(r"[a-zA-Z]", knowledge_base[i]) for symbol in symbols: if symbol not in inferred: inferred[symbol] = False @@ -16,25 +15,21 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: - - ''' - ''' + """ """ count = {} for clause in knowledge_base: - if(len(clause) != 1): + if len(clause) != 1: index = clause.find("=>") premise = clause[:index] - letters = ''.join(e for e in premise if e.isalpha()) + letters = "".join(e for e in premise if e.isalpha()) count[premise] = len(letters) return count def get_known_facts(knowledge_base: list[str]) -> list[str]: - - ''' - ''' + """ """ facts = [] for clause in knowledge_base: if len(clause) == 1: @@ -43,17 +38,15 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]: return facts - - -def forward_chianing(knowledge_base: list[str], query:str) -> bool: - +def forward_chianing(knowledge_base: list[str], query: str) -> bool: count = number_of_symbols_in_premise(knowledge_base) inferred = find_symbols_in_kb(knowledge_base) queue = get_known_facts(knowledge_base) - while(len(queue) > 0): + while len(queue) > 0: p = queue.pop() - if p == query: return True + if p == query: + return True if inferred[p] == False: inferred[p] = True for clause in knowledge_base: @@ -67,10 +60,22 @@ def forward_chianing(knowledge_base: list[str], query:str) -> bool: return False - if __name__ == "__main__": - - kb = ["p => q", "q => r", "r => p", "s => t", "u => v", "v => w", "w => u", "a", "b", "c" , "w" , "u&q=>r", "q"] + kb = [ + "p => q", + "q => r", + "r => p", + "s => t", + "u => v", + "v => w", + "w => u", + "a", + "b", + "c", + "w", + "u&q=>r", + "q", + ] result = forward_chianing(kb, "p") - print(result) \ No newline at end of file + print(result) From 205fb53953b3b76d28b382ca4be08720f967be43 Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 16:20:12 +1000 Subject: [PATCH 10/14] fixed pushing issue --- .../forward_chaining.py | 114 +++++++++++++----- 1 file changed, 84 insertions(+), 30 deletions(-) diff --git a/inference_engine_algorithms/forward_chaining.py b/inference_engine_algorithms/forward_chaining.py index 6bddbd610c99..a51597301828 100644 --- a/inference_engine_algorithms/forward_chaining.py +++ b/inference_engine_algorithms/forward_chaining.py @@ -1,12 +1,26 @@ -import re +''' +The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition +symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from +known facts (positive literals) in the knowledge base. +known facts (positive literals) in the knowledge base. + +Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf + +''' +import re def find_symbols_in_kb(knowledge_base: list[str]) -> dict: - """ """ + ''' + Find all unique symbols in the Knowledge_base + :param knowledge_base: a list of string of definite clauses + :returns: a dictionary with symbols as the keys their values are False + ''' + inferred = {} for i in range(len(knowledge_base)): - symbols = re.findall(r"[a-zA-Z]", knowledge_base[i]) + symbols = re.findall(r'[a-zA-Z]', knowledge_base[i]) for symbol in symbols: if symbol not in inferred: inferred[symbol] = False @@ -15,21 +29,32 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: - """ """ + + ''' + Count the number of prposiotion symbols in each premise of KB clause + :param knowledge_base: a list of string of definite clauses + :returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise + ''' count = {} for clause in knowledge_base: - if len(clause) != 1: + if(len(clause) != 1): index = clause.find("=>") premise = clause[:index] - letters = "".join(e for e in premise if e.isalpha()) + letters = ''.join(e for e in premise if e.isalpha()) count[premise] = len(letters) return count def get_known_facts(knowledge_base: list[str]) -> list[str]: - """ """ + ''' + Get the known facts in KB + :param knowledge_base: a list of string of definite clauses + :returns: list of facts + + ''' + facts = [] for clause in knowledge_base: if len(clause) == 1: @@ -38,16 +63,28 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]: return facts -def forward_chianing(knowledge_base: list[str], query: str) -> bool: + + +def forward_chaining(knowledge_base: list[str], query:str) -> bool: + ''' Forward chaining on Knowledge Base(KB) of definite clauses + :param knowledge_base: a list of string of definite clauses + :param query: a single proposition symbol that you are checking if it is entailed by the KB + :returns: If the query entailed by the KB or not? + >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q") + True + >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C") + False + + ''' + count = number_of_symbols_in_premise(knowledge_base) inferred = find_symbols_in_kb(knowledge_base) queue = get_known_facts(knowledge_base) - while len(queue) > 0: + while(len(queue) > 0): p = queue.pop() - if p == query: - return True - if inferred[p] == False: + if p == query: return True + if not inferred[p]: inferred[p] = True for clause in knowledge_base: index = clause.find("=>") @@ -60,22 +97,39 @@ def forward_chianing(knowledge_base: list[str], query: str) -> bool: return False + + +KB = [ + "P => Q", + "L & M => P", + "B&L=> M", + "A&P=>L", + "A&B=>L", + "A", + "B" ] + +''' +1)- KB must be written in horn form. +2)- It must be written as an implcaion whose +2)- It must be written as an implcaion whose +its premise(head) must be conjunction of positive literals and its conclusion(body) +3)- It must contains facts about the world which are written as a single proposition symbol +''' +QUERY = "Q" + +''' +Query is a signe proposition symbol that you check if it is entailed by the KB + +''' + if __name__ == "__main__": - kb = [ - "p => q", - "q => r", - "r => p", - "s => t", - "u => v", - "v => w", - "w => u", - "a", - "b", - "c", - "w", - "u&q=>r", - "q", - ] - - result = forward_chianing(kb, "p") - print(result) + + + + import doctest + + doctest.testmod(verbose=True) + + + result = forward_chaining(KB, QUERY) + print(result) \ No newline at end of file From 715e3e394fe2a6bd1368045341344a82e9d8c70e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 May 2024 06:20:36 +0000 Subject: [PATCH 11/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../forward_chaining.py | 66 ++++++++----------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/inference_engine_algorithms/forward_chaining.py b/inference_engine_algorithms/forward_chaining.py index a51597301828..bb03c07f66fc 100644 --- a/inference_engine_algorithms/forward_chaining.py +++ b/inference_engine_algorithms/forward_chaining.py @@ -1,26 +1,27 @@ -''' +""" The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from -known facts (positive literals) in the knowledge base. +known facts (positive literals) in the knowledge base. known facts (positive literals) in the knowledge base. Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf -''' +""" import re + def find_symbols_in_kb(knowledge_base: list[str]) -> dict: - ''' + """ Find all unique symbols in the Knowledge_base :param knowledge_base: a list of string of definite clauses :returns: a dictionary with symbols as the keys their values are False - ''' + """ inferred = {} for i in range(len(knowledge_base)): - symbols = re.findall(r'[a-zA-Z]', knowledge_base[i]) + symbols = re.findall(r"[a-zA-Z]", knowledge_base[i]) for symbol in symbols: if symbol not in inferred: inferred[symbol] = False @@ -29,31 +30,30 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: - - ''' + """ Count the number of prposiotion symbols in each premise of KB clause :param knowledge_base: a list of string of definite clauses :returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise - ''' + """ count = {} for clause in knowledge_base: - if(len(clause) != 1): + if len(clause) != 1: index = clause.find("=>") premise = clause[:index] - letters = ''.join(e for e in premise if e.isalpha()) + letters = "".join(e for e in premise if e.isalpha()) count[premise] = len(letters) return count def get_known_facts(knowledge_base: list[str]) -> list[str]: - ''' + """ Get the known facts in KB :param knowledge_base: a list of string of definite clauses :returns: list of facts - ''' + """ facts = [] for clause in knowledge_base: @@ -63,10 +63,8 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]: return facts - - -def forward_chaining(knowledge_base: list[str], query:str) -> bool: - ''' Forward chaining on Knowledge Base(KB) of definite clauses +def forward_chaining(knowledge_base: list[str], query: str) -> bool: + """Forward chaining on Knowledge Base(KB) of definite clauses :param knowledge_base: a list of string of definite clauses :param query: a single proposition symbol that you are checking if it is entailed by the KB :returns: If the query entailed by the KB or not? @@ -75,15 +73,16 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool: >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C") False - ''' + """ count = number_of_symbols_in_premise(knowledge_base) inferred = find_symbols_in_kb(knowledge_base) queue = get_known_facts(knowledge_base) - while(len(queue) > 0): + while len(queue) > 0: p = queue.pop() - if p == query: return True + if p == query: + return True if not inferred[p]: inferred[p] = True for clause in knowledge_base: @@ -97,39 +96,26 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool: return False +KB = ["P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B"] - -KB = [ - "P => Q", - "L & M => P", - "B&L=> M", - "A&P=>L", - "A&B=>L", - "A", - "B" ] - -''' +""" 1)- KB must be written in horn form. -2)- It must be written as an implcaion whose +2)- It must be written as an implcaion whose 2)- It must be written as an implcaion whose its premise(head) must be conjunction of positive literals and its conclusion(body) 3)- It must contains facts about the world which are written as a single proposition symbol -''' +""" QUERY = "Q" -''' +""" Query is a signe proposition symbol that you check if it is entailed by the KB -''' +""" if __name__ == "__main__": - - - import doctest doctest.testmod(verbose=True) - result = forward_chaining(KB, QUERY) - print(result) \ No newline at end of file + print(result) From 18dfd30c66be44b327a00af8bc4dc1caa57b9738 Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 16:32:41 +1000 Subject: [PATCH 12/14] fixed some Github test issue --- inference_engine_algorithms/__init__.py | 0 .../forward_chaining.py | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 inference_engine_algorithms/__init__.py diff --git a/inference_engine_algorithms/__init__.py b/inference_engine_algorithms/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/inference_engine_algorithms/forward_chaining.py b/inference_engine_algorithms/forward_chaining.py index a51597301828..33a50d4ade99 100644 --- a/inference_engine_algorithms/forward_chaining.py +++ b/inference_engine_algorithms/forward_chaining.py @@ -12,7 +12,7 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: ''' - Find all unique symbols in the Knowledge_base + Find all unique symbols in the Knowledge_base. :param knowledge_base: a list of string of definite clauses :returns: a dictionary with symbols as the keys their values are False ''' @@ -31,9 +31,9 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: ''' - Count the number of prposiotion symbols in each premise of KB clause + Count the number of prposiotion symbols in each premise of KB clause. :param knowledge_base: a list of string of definite clauses - :returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise + :returns: a dict with keys as the premise and value is count of symbols in premise ''' count = {} @@ -49,7 +49,7 @@ def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: def get_known_facts(knowledge_base: list[str]) -> list[str]: ''' - Get the known facts in KB + Get the known facts in KB. :param knowledge_base: a list of string of definite clauses :returns: list of facts @@ -66,13 +66,17 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]: def forward_chaining(knowledge_base: list[str], query:str) -> bool: - ''' Forward chaining on Knowledge Base(KB) of definite clauses + ''' Forward chaining on Knowledge Base(KB) of definite clauses. :param knowledge_base: a list of string of definite clauses - :param query: a single proposition symbol that you are checking if it is entailed by the KB + :param query: a single proposition symbol :returns: If the query entailed by the KB or not? - >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q") + >>> input_kb = [ "P => Q", "L & M => P", + ... "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ] + >>> forward_chaining(input_kb, "Q") True - >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C") + >>> input_kb = [ "P => Q", "L & M => P", + ... "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ] + >>> forward_chaining(input_kb, "C") False ''' @@ -110,15 +114,14 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool: ''' 1)- KB must be written in horn form. -2)- It must be written as an implcaion whose 2)- It must be written as an implcaion whose -its premise(head) must be conjunction of positive literals and its conclusion(body) -3)- It must contains facts about the world which are written as a single proposition symbol +its premise(head) must be conjunction of positive literals and its conclusion(body). +3)- Contains facts about the world as single proposition symbol. ''' QUERY = "Q" ''' -Query is a signe proposition symbol that you check if it is entailed by the KB +Query is a signe proposition symbol that you check if it is entailed by the KB. ''' From 2fa24dfe78f6f8904688e32f27fde553591e47e2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 May 2024 06:38:52 +0000 Subject: [PATCH 13/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- inference_engine_algorithms/forward_chaining.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/inference_engine_algorithms/forward_chaining.py b/inference_engine_algorithms/forward_chaining.py index 323e8383bd8f..91d5d679e490 100644 --- a/inference_engine_algorithms/forward_chaining.py +++ b/inference_engine_algorithms/forward_chaining.py @@ -30,7 +30,6 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict: def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict: - """ Count the number of prposiotion symbols in each premise of KB clause. :param knowledge_base: a list of string of definite clauses @@ -66,13 +65,13 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]: def forward_chaining(knowledge_base: list[str], query: str) -> bool: """Forward chaining on Knowledge Base(KB) of definite clauses :param knowledge_base: a list of string of definite clauses - :param query: a single proposition symbol + :param query: a single proposition symbol :returns: If the query entailed by the KB or not? - >>> input_kb = [ "P => Q", "L & M => P", + >>> input_kb = [ "P => Q", "L & M => P", ... "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ] >>> forward_chaining(input_kb, "Q") True - >>> input_kb = [ "P => Q", "L & M => P", + >>> input_kb = [ "P => Q", "L & M => P", ... "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ] >>> forward_chaining(input_kb, "C") False From e717439c7fa643bf3d50a54ce607db785b56bf01 Mon Sep 17 00:00:00 2001 From: KarimAly <88921421+KarimAly12@users.noreply.github.com> Date: Sat, 11 May 2024 16:42:30 +1000 Subject: [PATCH 14/14] fixed a comment --- inference_engine_algorithms/forward_chaining.py | 1 - 1 file changed, 1 deletion(-) diff --git a/inference_engine_algorithms/forward_chaining.py b/inference_engine_algorithms/forward_chaining.py index 323e8383bd8f..5737e497df00 100644 --- a/inference_engine_algorithms/forward_chaining.py +++ b/inference_engine_algorithms/forward_chaining.py @@ -2,7 +2,6 @@ The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from known facts (positive literals) in the knowledge base. -known facts (positive literals) in the knowledge base. Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf