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 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 and also can build a neural network to solve a complex problem.
Problem Statement
Doctor Black has just been found dead in his mansion yesterday. Yesterday, there were only Three People in Doctor Black’s mansion.
They are the prime suspects:
Col. Mustard
Prof. Plum
Ms. Scarlet
Police found Three Weapons in the mansion:
Knife
Revolver
Wrench
The murder has happened in one of the Three Rooms of the mansion:
Ballroom
Kitchen
Library
We have to find who is the murderer of Dr. Black. We can start creating the knowledge base of our game engine by adding the above information.
We will make our game engine knowledgeable by implementing propositional logic. We will also receive further clues during the investigation. That will be also added to the knowledge base...
The Python Code to Develop The Clue Game
# we import termcolor to change the color in terminal window
import termcolor
# we have imported the logic file
from logic import *
#Now we are symboling all of the charecters,rooms,weapons
mustard=Symbol("col.mustard")
plum=Symbol("ProfPlum")
scarlet=Symbol("MsScarlet")
charecters=[mustard,plum,scarlet]
ballroom=Symbol("ballroom")
kitchen=Symbol("kitchen")
library=Symbol("library")
rooms=[ballroom,kitchen,library]
revolber=Symbol("revolber")
knife=Symbol("knife")
wrench=Symbol("wrench")
wreapons=[revolber,knife,wrench]
# Now we are concating characters, rooms, and weapons in symbols.
symbols=charecters+rooms+wreapons
# we are checking the model and get some truth value
def check_knowledge(knowledge_base):
for symbol in symbols:
if model_check(knowledge_base,symbol):
termcolor.cprint(f"{symbol}:YES","green")
elif not model_check(knowledge_base,Not(symbol)):
print(f"{symbol}:Maybe")
# Createing knowledge base
knowledge_base=And(
Or(mustard,plum,scarlet),
Or(ballroom,kitchen,library),
Or(knife,revolber,wrench)
)
# They are the clue
knowledge_base.add(And(
Not(mustard),Not(kitchen),Not(wrench)
))
knowledge_base.add(Or(
Not(scarlet),Not(library),Not(wrench)
))
knowledge_base.add(Not(plum))
knowledge_base.add(Not(ballroom))
knowledge_base.add(Not(revolber))
check_knowledge(knowledge_base)
Result
In investigation time we got a clue that col. mustard, kitchen, the wrench is not suspicious.
Further, step by step we have known that Plum, Ballroom, Revolver is not suspicious too…
Each Clue was added to the knowledge base and finally, the Game engine has detected the Murderer, the Room, and the weapon.
Conclusion
Firstly we explained the problem, then we implemented it in python code. Although the lesson was too easy, I hope you have learned a lot about propositional logic and how to do knowledge-based engineering in Artificial Intelligence.
However, you can develop a crime detection tool or similar board game by implementing the code snippet given above. In the previous lesson (2nd Article), we learned to generate new knowledge from the existing knowledge base without explicitly programming. You can also combine that idea in your project too.
This is a simple and easier implementation of the “ CSE 418: Artificial Intelligence Lab” course at City University, Dhaka, Bangladesh conducted by Nuruzzaman Faruqui Sir. Which is the best AI course in Bangladesh.
You are free to copy the code from here or some other concluding remarks.