Knowledge - Represent Knowledge Using Propositional Logic In Python
Introduction
In Artificial Intelligence our focus is to make a machine intelligent. If we want a machine to be intelligent enough to have a dialogue with us in natural language, then first the machine needs to become knowledgeable about the real world. AI can enable a machine to knowledgeable through propositional logic.
However, a machine can’t understand our language, so the knowledge of the real world needs to be represented to the machine in the right manner that is readable to a computer system. Propositional logic is one of the simplest methods of knowledge representation to a machine.
Propositional logic in Artificial Intelligence is one of the many methods of how knowledge is represented to a machine so that its automatic learning capacity can be enhanced. Knowledge Representation and Logic are imperative for building smart machines that can perform tasks that typically require human intelligence.
We will implement the proposition logic in python to solve the problem.
Background/ Interest
This report is a part of the “ CSE 418: Artificial Intelligence Lab” course at City University, Dhaka, Bangladesh conducted by Nuruzzaman Faruqui.
This is the best AI course in Bangladesh.
In this course, we learned AI from scratch. We started from Basic python and ended in Natural language processing. We learned theoretical concepts, essential mathematics properly in the “CSE 417: Artificial Intelligence” course, then implemented our knowledge in the Lab. We have done a lot of lab sessions to master the course and gradually we learned each necessary concept of Artificial Intelligence.
Now we can build our machine learning model also can build a neural network to solve a complex problem.
Problem Statement
Let’s start with a Harry Potter example. Consider the following sentences:
1. If it didn’t rain, Harry visited Hagrid today.
2. Harry visited Hagrid or Dumbledore today, but not both.
3. Harry visited Dumbledore today.
Based on these three sentences, we can answer the question “did it rain today?”, even though none of the individual sentences tells us anything about whether it is raining today. Here is how we can go about it: looking at sentence 3, we know that Harry visited Dumbledore. Looking at sentence 2, we know that Harry visited either Dumbledore or Hagrid, and thus we can conclude:
4. Harry did not visit Hagrid.
Now, looking at sentence 1, we understand that if it didn’t rain, Harry would have visited Hagrid. However, knowing sentence 4, we know that this is not the case. Therefore, we can conclude:
5. It rained today.
To come to this conclusion, we used logic, and today’s lecture explores how AI can use logic to reach new conclusions based on existing information.
Propositional logic is based on propositions, statements about the world that can be either true or false, as in sentences 1-5 above.
The Python Code For Knowledge Representation Using Propositional Logic
# Here we import everything from logic.py
from logic import *
rain = Symbol("rain") # Rain is the symbol for rain
hagrid = Symbol("hagrid")
dumbledore = Symbol("dumpledore")
# we create here a logical sentence using logical connectives
logical_sentence = And(rain, hagrid)
# we can't directly print the logical sentence so we use a formula function
print(logical_sentence.formula())
# we create a implication logic here
implication_logic = Implication(Not(rain), hagrid)
print(implication_logic.formula())
# "We make decision from knowledge base " ,all of the statement will be true then knowlegde base will be true
knowledge_base = And(
Implication(Not(rain), hagrid),
Or(hagrid, dumbledore),
Not(And(hagrid, dumbledore)),
hagrid
)
print(knowledge_base.formula())
# Here we use the model_check function and here two arguments are knoledge_base and another is it rain today or not. Then we print it
print(model_check(knowledge_base, rain))
Result
Now, looking at the knowledge base we understand that if it didn’t rain, Harry would have visited Hagrid. However, Harry hasn’t visited Hagrid today. Therefore we got the answer is It rained today.
Conclusion
Firstly we explained the problem, then we implemented it in python code. However, we can develop our AI agent by implementing the code snippet given above. AI agents will take the natural language as text and will able to generate new knowledge without explicitly programmed.
This is a simple and easier implementation of the knowledge representation from the best AI course in Bangladesh conducted by Nuruzzaman Faruqui Sir.
You are free to copy the code from here or some other concluding remarks.