Professional Documents
Culture Documents
Ahsanullah University of Science and Technology (AUST)
Ahsanullah University of Science and Technology (AUST)
Submitted By-
Ashna Nawar Ahmed
I.D.: 16.02.04.024
Section-A1
Year- 4th
Semester-1st
Department-CSE
Reasoning with Propositional Logic
IDEA:
Propositional Logic (PL) is a language for representing Knowledge Bases. It is also used for
reasoning, inference and answering queries to knowledge bases. One of the most well-known
problems that are solved using Reasoning with Propositional Logic is the “Treasure Hunt:
Avoiding Monsters” problem. The main idea of this problem is: An Agent wants to hunt for
treasure in a certain environment, while avoiding the monsters that are lurking in the region
and letting off warning to the immediate-surroundings in the form of their smell. The Agent can
pick directions to move on the basis of the knowledge of this smell. The ultimate goal is to
reach gold before reaching a monster. This is the idea that we will try to implement in
algorithm and then in code form.
MAJOR STEPS:
Here we have formed a 4x4 grid as a representation of the environment, where each cell has
the information of whether it has a monster, a pile of gold or smell from a monster in some
surrounding cell. The Agent only has the ability to either move up or right. Each time, the
Agent has to choose whether to move up or right, based on the smell they are getting in the
current cell they are at. One of three situations may arise:
1) No smell is found in any of the options at hand, in which case the Agent may move
without hesitation to any of the options at hand
2) Smell is found at one of the options (up or left), in which case the Agent moves to the
cell that has no smell, to avoid contact with monsters.
3) Smell is found at both of the options at hand, in which case the Agent must make a
random choice blindly, and hope for the best not to get attacked by a monster.
These are the major steps of this Treasure Hunting algorithm.
INPUT:
In the Python code we have done on the basis of this algorithm, the Agent chooses randomly
(using built-in library module “Random”) from the options that are given to them (up or right).
They are also warned of any smell that is present in the cells that they are meant to choose
from. They are also congratulated when they reach gold and conversely, warned when they
get eaten by a monster. The game is over when the Agent wins gold or gets eaten.
EXPLANATION OF ALGORITHM:
The entire algorithm is based on the smell emanating from certain cells. This is the criteria
that determines all the choices made by the Agent. The only time the Agent has to choose an
option without complete logic is when faced with 2 choices that are equally bad, i.e when both
choices faced by them are cells that have smell. So we can say that even though most of the
problem is solvable by simple Propositional Logic, some of the times the Agent may have to
make a choice that is random, otherwise it will get stuck.
CODE IN PYTHON:
import random
#KNOWLEDGE BASE }
grid={ optionsForEachCell={
11:[False,False,False], 12:[22,13],
12:[False,False,True], 13:[23,14],
13:[False,False,True], 14:[24],
14:[True,True,True], 21:[31,22],
21:[False,False,True], 22:[32,23],
22:[False,True,True], 23:[33,24],
23:[True,False,True], 24:[34],
24:[False,False,True], 31:[41,32],
31:[False,False,False], 32:[42,33],
32:[False,False,True], 33:[43,34],
33:[False,False,False], 34:[44],
34:[False,True,False], 41:[42],
41:[False,False,False], 42:[43],
42:[False,False,False], 43:[44]
43:[False,False,False], }
44:[False,True,False]
#initial position
pos = 11
path=[]
path.append(pos)
positionChoice=-1
while 1:
print(optionsForEachCell[pos])
for x in optionsForEachCell[pos]:
if(grid[x][2]==True):
else:
countNonSmellyOptions=0
for x in optionsForEachCell[pos]:
if(grid[x][2]==False):
countNonSmellyOptions=countNonSmellyOptions+1
if(countNonSmellyOptions==0):
#2 CHOICES
if(len(optionsForEachCell[pos])>1):
r = random.randint(0, 1)
positionChoice=optionsForEachCell[pos][r]
#1 CHOICE ONLY
else:
positionChoice = optionsForEachCell[pos][0]
elif(countNonSmellyOptions==2):
r = random.randint(0, 1)
positionChoice = optionsForEachCell[pos][r]
pos=positionChoice
path.append(pos)
print("Path so far:")
print(path)
situation=grid[pos]
positionChoice = -1
countNonSmellyOptions=0
#MONSTER CHECKING
if(situation[0]==False):
else:
break
#GOLD CHECKING
if(situation[1]==False):
else:
print("Congratulations!!! ",pos," has some gold!!! You successfully acquired gold without getting
eaten!!!")
break
print("Game Over")
print(path)
Screenshots:
Two screenshots have been provided, showcasing what the output looks like when the two
possible outcomes arrive: getting eaten or getting the gold.