0% found this document useful (0 votes)
23 views4 pages

Class Agent

class agent

Uploaded by

2021csm.r350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Class Agent

class agent

Uploaded by

2021csm.r350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

class Agent:

def __init__(self):

self._wumpusWorld = [

['','','',''],

['','W','P',''],

['','','',''],

['','','',''],

self._curLoc = [1,1]

self._isAlive = True

self._hasExited = False

def _FindIndicesForLocation(self,loc):

x,y = loc

i,j = y-1, x-1

return i,j

def _CheckForPitWumpus(self):

ww = self._wumpusWorld

i,j = self._FindIndicesForLocation(self._curLoc)

if 'P' in ww[i][j] or 'W' in ww[i][j]:

print(ww[i][j])

self._isAlive = False

print('Agent is DEAD.')

return self._isAlive

def TakeAction(self,action):

validActions = ['Up','Down','Left','Right']

assert action in validActions, 'Invalid Action.'


if self._isAlive == False:

print('Action cannot be performed. Agent is DEAD. Location:{0}'.format(self._curLoc))

return False

if self._hasExited == True:

print('Action cannot be performed. Agent has exited the Wumpus world.'.format(self._curLoc))

return False

index = validActions.index(action)

validMoves = [[0,1],[0,-1],[-1,0],[1,0]]

move = validMoves[index]

newLoc = []

for v, inc in zip(self._curLoc,move):

z = v + inc

z = 4 if z>4 else 1 if z<1 else z

newLoc.append(z)

self._curLoc = newLoc

print('Action Taken: {0}, Current Location {1}'.format(action,self._curLoc))

if self._curLoc[0]==4 and self._curLoc[1]==4:

self._hasExited=True

return self._CheckForPitWumpus()

def _FindAdjacentRooms(self):

cLoc = self._curLoc

validMoves = [[0,1],[0,-1],[-1,0],[1,0]]

adjRooms = []

for vM in validMoves:

room = []

valid = True

for v, inc in zip(cLoc,vM):

z = v + inc

if z<1 or z>4:
valid = False

break

else:

room.append(z)

if valid==True:

adjRooms.append(room)

return adjRooms

def PerceiveCurrentLocation(self):

breeze, stench = False, False

ww = self._wumpusWorld

if self._isAlive == False:

print('Agent cannot perceive. Agent is DEAD. Location:{0}'.format(self._curLoc))

return [None,None]

if self._hasExited == True:

print('Agent cannot perceive. Agent has exited the Wumpus World.'.format(self._curLoc))

return [None,None]

adjRooms = self._FindAdjacentRooms()

for room in adjRooms:

i,j = self._FindIndicesForLocation(room)

if 'P' in ww[i][j]:

breeze = True

if 'W' in ww[i][j]:

stench = True

return [breeze,stench]

def FindCurrentLocation(self):

return self._curLoc
def main():

ag = Agent()

print('curLoc',ag.FindCurrentLocation())

print('Percept [breeze, stench] :',ag.PerceiveCurrentLocation())

ag.TakeAction('Right')

print('Percept',ag.PerceiveCurrentLocation())

ag.TakeAction('Right')

print('Percept',ag.PerceiveCurrentLocation())

ag.TakeAction('Right')

print('Percept',ag.PerceiveCurrentLocation())

ag.TakeAction('Up')

print('Percept',ag.PerceiveCurrentLocation())

ag.TakeAction('Up')

print('Percept',ag.PerceiveCurrentLocation())

ag.TakeAction('Up')

print('Percept',ag.PerceiveCurrentLocation())

if __name__=='__main__':

main()

def TowerOfHanoi(n , source, destination, auxiliary):

if n==1:

print("Move disk 1 from source",source,"todestination",destination)

return

TowerOfHanoi(n-1, source, auxiliary, destination)

print("Move disk",n,"fromsource",source,"todestination",destination)

TowerOfHanoi(n-1, auxiliary, destination, source)

n=4

TowerOfHanoi(n,'A','B','C')

You might also like