Knowledge Representation Using Propositional Logic In Python (Part-1)

Knowledge Representation Using Propositional Logic In Python (Part-1)

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 be 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.

No alt text provided for this image

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 tell us anything about whether it is raining today. Here is how we can go about it: looking at sentence 3, we know Harry visited Dumbledore. Looking at sentence 2, we know 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 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

No alt text provided for this image

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

First, 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 generate new knowledge without being 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. This report is a part of the “CSE 418: Artificial Intelligence Lab” course at City University, Dhaka, Bangladesh. You are free to copy the code from here or some other concluding remarks.

gebrhans weldegebriel

Lecturer at ADIGRAT UNIVERSITY

1mo

it is very helpful this simple document....and also you have to share other components for representing the KRR mechanism

To view or add a comment, sign in

More articles by Md. Taufiqul Haque Khan Tusar

Insights from the community

Others also viewed

Explore topics