Skip to main content

2. Knowledge - Represent our Knowledge Using Propositional Logic

 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.


Figure from educba.com


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.


Popular posts from this blog

7. Optimization - Hill-Climbing Algorithm with "Random Restart" variant

Optimizing Cost Function Through Hill Climbing Algorithm & Random Restart Variant Introduction Hill climbing is a mathematical optimization algorithm, which means its purpose is to find the best solution to a problem that has a (large) number of possible solutions. Explaining the algorithm (and optimization in general) is best done using an example. In the Travelling salesman problem, we have a salesman who needs to visit a number of cities exactly once, after which he returns to the first city. The distances between each pair of cities are known, and we need to find the shortest route. As you can imagine, there is (often) a large number of possible solutions (routes) to a specific Travelling salesman problem; the goal is to find the best (i.e. the shortest) solution. Hill Climbing Variants Due to the limitations of Hill Climbing, multiple variants have been thought of to overcome the problem of being stuck in local minima and maxima. What all variations of the algorithm have in co...

9. Machine Learning - Banknote Authentication

  Building Machine Learning Model to Classify Fraud Bank Note Introduction Machine Learning!!!🤪 The highly interesting technology in today's world 😉. I know that you have heard a lot about it. Today we are going to build a machine learning model to authenticate banknote where it's authentic or not 💵.  Whenever you go to the bank to deposit some cash, the cashier places banknotes in a machine that tells whether a banknote is genuine or counterfeit.  The machine uses some classification techniques to do it. There are many machine learning algorithms for classification. Classification is a type of supervised machine learning. There are multiple machine learning algorithms in the classification. We understand it's hard to know the theoretical concept of each algorithm as a beginner. If it's true for you,  there is nothing to panic about.🤪  We will implement the 'K nearest neighbor, Support vector machine, Perceptron learning & Gaussian naive Bayes' algorithm...

3. Knowledge- The Clue Game

Knowledge - Represent Knowledge Using Propositional Logic In Python Introduction Representing knowledge is the key issue in Artificial Intelligence. We can develop a knowledgeable AI agent that can analyze like humans. In this lesson, we will develop a game engine that will detect a murder based on its knowledge base.  If we want a machine to be intelligent enough to think like a human, then first the machine needs some information about the real-world situation/problem. 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. In this lesson, we will implement propositional logic to make our game engine knowledgeable, and then we will make able the engine to detect the murderer. Background/ Interest This report is a part of the “ CSE 418: Artificial Intelligence Lab” course at City University, Dhaka, Bangladesh conduct...