Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Ahsanullah University of Science and Technology (AUST)

Department of Computer Science and Engineering

TERM ASSIGNMENT #01


Course No.: CSE4108
Course Title: Artificial Intelligence Lab

Date of Submission: 27-08-2020

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:

1,4 2,4 3,4 4,4


MONSTER NO MONSTER NO MONSTER NO MONSTER
GOLD NO GOLD GOLD GOLD
SMELL SMELL NO SMELL NO SMELL
1,3 2,3 3,3 4,3
NO MONSTER NO MONSTER NO MONSTER NO MONSTER
NO GOLD NO GOLD NO GOLD NO GOLD
SMELL SMELL NO SMELL NO SMELL
1,2 2,2 3,2 4,2
NO MONSTER MONSTER NO MONSTER NO MONSTER
NO GOLD GOLD NO GOLD NO GOLD
SMELL SMELL SMELL NO SMELL
1,1 2,1 3,1 4,1
NO MONSTER NO MONSTER NO MONSTER NO MONSTER
NO GOLD NO GOLD NO GOLD NO GOLD
NO SMELL SMELL NO SMELL NO SMELL

The input grid we have formed has the following information:

• Gold is at (1,4), (2,2), (3,4) and (4,4).


• Monsters are at (1,4), (2,2),
OUTPUT:
In this particular input, we have left several paths towards a pile of gold, as well as a couple of
monsters along the way. Some of the success-paths are:

(1, 1) (2, 1) (3, 1) (3, 2) (3, 3) (3, 4)

(1, 1) (2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (4, 4)

If these paths are taken by the agent, gold will be reached.


However, there are also some paths that will lead to getting eaten by a monster. Some such
paths are:

(1, 1) (2, 1) (2, 2)

(1, 1) (1, 2) (1, 3) (1, 4)

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={

#cell number: [monster, gold, smell] 11:[21,12],

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=[]

print("FIND GOLD AVOIDING MONSTER\n")


print("Current Position: 11")

path.append(pos)

positionChoice=-1

while 1:

print("You can move to positions:")

print(optionsForEachCell[pos])

for x in optionsForEachCell[pos]:

if(grid[x][2]==True):

print(x," has a smell. There may be monsters nearby.")

else:

print(x," has no smell.")

countNonSmellyOptions=0

for x in optionsForEachCell[pos]:

if(grid[x][2]==False):

countNonSmellyOptions=countNonSmellyOptions+1

#BOTH CHOICES HAVE SMELL/ONLY HAVE ONE CHOICE WHICH IS SMELLY

if(countNonSmellyOptions==0):

#2 CHOICES

if(len(optionsForEachCell[pos])>1):

print("Neither of the choices is perfect. Let's pick a direction at random.")

r = random.randint(0, 1)

positionChoice=optionsForEachCell[pos][r]

print(positionChoice, " has been picked.")

#1 CHOICE ONLY

else:

print("Only one option is available.")

positionChoice = optionsForEachCell[pos][0]

print(positionChoice, " has to be picked.")


#BOTH CHOICES HAVE NO SMELL

elif(countNonSmellyOptions==2):

print("Both of the choices are suitable. Let's pick a direction at random.")

r = random.randint(0, 1)

positionChoice = optionsForEachCell[pos][r]

print(positionChoice, " has been picked.")

pos=positionChoice

path.append(pos)

print("Path so far:")

print(path)

situation=grid[pos]

positionChoice = -1

countNonSmellyOptions=0

#MONSTER CHECKING

if(situation[0]==False):

print(pos," has no monster.")

else:

print(pos," has a monster! You just got eaten!")

break

#GOLD CHECKING

if(situation[1]==False):

print(pos," has no gold.")

else:

print("Congratulations!!! ",pos," has some gold!!! You successfully acquired gold without getting
eaten!!!")

break

print("Game Over")

print("Path was: ")

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.

Case 1: Getting Caught By Monster:


Case 2: Getting The Gold:

You might also like