-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAgent.py
114 lines (102 loc) · 4.21 KB
/
Agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Agent:
def __init__(self):
self.__wumpusWorld = [
['','','P',''], # Rooms [1,1] to [4,1]
['','','',''], # Rooms [1,2] to [4,2]
['W','','',''], # Rooms [1,3] to [4,3]
['','','',''], # Rooms [1,4] to [4,4]
] # This is the wumpus world shown in the assignment question.
# A different instance of the wumpus world will be used for evaluation.
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): # The function takes an action and returns whether the Agent is alive
# after taking the 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 #increment location index
z = 4 if z>4 else 1 if z<1 else z #Ensure that index is between 1 and 4
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): #This function perceives the current location.
#It tells whether breeze and stench are present in the current location.
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()