-
Notifications
You must be signed in to change notification settings - Fork 3
/
WumpusWorldAI.py
305 lines (264 loc) · 9.89 KB
/
WumpusWorldAI.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
from Agent import * # See the Agent.py file
import copy
numberOfCalls=0
class KnowledgeBase:
#clause[i]=-1 represents the presence of negative literal represented by i
#clause[i]=1 represents the presence of positive literal represented by i
#values 0 to 15 represents W(1,1) to W(4,4)
#values 16 to 31 represents S(1,1) to S(4,4)
#values 33 to 47 represents P(1,1) to P(4,4)
#values 48 to 63 represents B(1,1) to B(4,4)
def __init__(self):
self.clauses= []
#clauses for atleast 1 Wumpus and 1 Pit
atleast1Wumpus= {}
atleast1Pit = {}
for i in range (16):
atleast1Wumpus[i]=1
atleast1Pit[i+32]=1
self.clauses.append(atleast1Wumpus)
self.clauses.append(atleast1Pit)
#clauses for atmost 1 Wumpus and 1 Pit
for i in range(16):
for j in range(i+1, 16):
atmost1Wumpus={}
atmost1Pit={}
atmost1Wumpus[i]=-1
atmost1Wumpus[j]=-1
atmost1Pit[i+32]=-1
atmost1Pit[j+32]=-1
self.clauses.append(atmost1Wumpus)
self.clauses.append(atmost1Pit)
#Stench-Wumpus bijection clauses
for i in range(16):
stenchWumpusClause={}
stenchWumpusClause[i+16]=-1
if (i+4)//4 < 4:
stenchWumpusClause[i+4]=1
stenchClause={}
stenchClause[i+16]=1
stenchClause[i+4]=-1
self.clauses.append(stenchClause)
if(i-4)//4 >= 0:
stenchWumpusClause[i-4]=1
stenchClause={}
stenchClause[i+16]=1
stenchClause[i-4]=-1
self.clauses.append(stenchClause)
if i//4 == (i+1)//4:
stenchWumpusClause[i+1]=1
stenchClause={}
stenchClause[i+16]=1
stenchClause[i+1]=-1
self.clauses.append(stenchClause)
if i//4 == (i-1)//4:
stenchWumpusClause[i-1]=1
stenchClause={}
stenchClause[i+16]=1
stenchClause[i-1]=-1
self.clauses.append(stenchClause)
self.clauses.append(stenchWumpusClause)
#Breeze-Pit Bijection Clauses
for i in range(16):
breezePitClause={}
breezePitClause[i+48]=-1
if(i+4)//4 < 4:
breezePitClause[i+4+32]=1
pitClause={}
pitClause[i+48]=1
pitClause[i+4+32]=-1
self.clauses.append(pitClause)
if(i-4)//4 >= 0:
breezePitClause[i-4+32]=1
pitClause={}
pitClause[i+48]=1
pitClause[i-4+32]=-1
self.clauses.append(pitClause)
if i//4 == (i+1)//4:
breezePitClause[i+1+32]=1
pitClause={}
pitClause[i+48]=1
pitClause[i+1+32]=-1
self.clauses.append(pitClause)
if i//4 == (i-1)//4:
breezePitClause[i-1+32]=1
pitClause={}
pitClause[i+48]=1
pitClause[i-1+32]=-1
self.clauses.append(pitClause)
self.clauses.append(breezePitClause)
#No wumpus and pit at [1, 1]
noWumpusStart={0:-1}
noPitStart={32:-1}
self.clauses.append(noWumpusStart)
self.clauses.append(noPitStart)
def AddClause(self, clause): #adding a clause to knowledge base
self.clauses.append(clause)
def getclauses(self): #return Wumpus clauses
return copy.deepcopy(self.clauses)
def FindPureSymbol(clauses, symbols):
for symbol in symbols:
positive=0
negative=0
for clause in clauses:
if symbol in clause:
if clause[symbol]==1:
positive= positive+1
else:
negative= negative+1
if negative==0:
return symbol, 1
elif positive==0:
return symbol, -1
return -1, 0
def FindUnitClause(clauses):
for clause in clauses:
if len(clause)==1:
for symbol in clause:
return symbol, clause[symbol]
return -1, 0
def selectSymbol(clauses, symbols):
count={}
positive={}
negative={}
for clause in clauses:
for literal in clause:
if literal not in count:
count[literal]=0
positive[literal]=0
negative[literal]=0
count[literal]= count[literal]+1
if clause[literal]==1:
positive[literal]=positive[literal]+1
else:
negative[literal]=negative[literal]+1
maxLiteral= list(symbols.keys())[0]
maxCount=0
for literal in count:
if count[literal]>maxCount:
maxLiteral= literal
maxCount= count[literal]
if positive[maxLiteral]>negative[maxLiteral]:
return maxLiteral, 1
return maxLiteral, -1
def DPLL(clauses, symbols, model):
global numberOfCalls
numberOfCalls= numberOfCalls+1
removeClauses=[]
for clause in clauses:
valueUnknown=True
deleteLiterals=[]
for literal in clause.keys():
if literal in model.keys():
if model[literal]==clause[literal]: #clause is true
removeClauses.append(clause)
valueUnknown=False
break
else:
deleteLiterals.append(literal)
for literal in deleteLiterals:
del clause[literal]
if valueUnknown==True and not bool(clause): #clause is false
return False
clauses= [ x for x in clauses if x not in removeClauses]
if len(clauses)==0: #all clauses are true
return True
pureSymbol, value = FindPureSymbol(clauses, symbols)
if value!=0:
del symbols[pureSymbol]
model[pureSymbol]=value
return DPLL(clauses, symbols, model)
unitSymbol, value = FindUnitClause(clauses)
if value!=0:
del symbols[unitSymbol]
model[unitSymbol]=value
return DPLL(clauses, symbols, model)
symbol, value= selectSymbol(clauses, symbols)
del symbols[symbol]
model[symbol]= value
if DPLL(copy.deepcopy(clauses), copy.deepcopy(symbols), copy.deepcopy(model)):
return True
model[symbol]= -value
return DPLL(clauses, symbols, model)
def DPLLSatisfiable(clauses):
symbols={}
for clause in clauses:
for literal in clause:
symbols[literal]=True
model={}
return DPLL(clauses, symbols, model)
def MoveToUnvisited(ag, visited, goalLoc, dfsVisited): #dfs to new safe room
curPos=ag.FindCurrentLocation()
curLoc= 4*(curPos[0]-1)+curPos[1]-1
if(curLoc==goalLoc):
return True
dfsVisited[curLoc]=True
if curPos[1]+1 <=4 and (visited[curLoc+1]==True or (curLoc+1)==goalLoc) and dfsVisited[curLoc+1]==False:
ag.TakeAction('Up')
roomReachable= MoveToUnvisited(ag, visited, goalLoc, dfsVisited)
if roomReachable:
return True
ag.TakeAction('Down')
if curPos[0]+1 <=4 and (visited[curLoc+4]==True or (curLoc+4)==goalLoc) and dfsVisited[curLoc+4]==False:
ag.TakeAction('Right')
roomReachable= MoveToUnvisited(ag, visited, goalLoc, dfsVisited)
if roomReachable:
return True
ag.TakeAction('Left')
if curPos[0]-1 >0 and (visited[curLoc-4]==True or (curLoc-4)==goalLoc) and dfsVisited[curLoc-4]==False:
ag.TakeAction('Left')
roomReachable= MoveToUnvisited(ag, visited, goalLoc, dfsVisited)
if roomReachable:
return True
ag.TakeAction('Right')
if curPos[1]-1 >0 and (visited[curLoc-1]==True or (curLoc-1)==goalLoc) and dfsVisited[curLoc-1]==False:
ag.TakeAction('Down')
roomReachable= MoveToUnvisited(ag, visited, goalLoc, dfsVisited)
if roomReachable:
return True
ag.TakeAction('Up')
return False
def ExitWumpusWorld(ag, kb):
visited = [False for i in range(16)] #Rooms Visited till now
while(ag.FindCurrentLocation()!=[4, 4]):
percept= ag.PerceiveCurrentLocation()
curPos = ag.FindCurrentLocation()
curLocIndex= 4*(curPos[0]-1)+ curPos[1]-1
visited[curLocIndex]=True
breezeClause={}
stenchClause={}
if percept[0]==True: #breeze
breezeClause[curLocIndex+48]=1
else:
breezeClause[curLocIndex+48]=-1
kb.AddClause(breezeClause) #presence/absence of breeze
if percept[1]==True: #stench
stenchClause[curLocIndex+16]=1
else:
stenchClause[curLocIndex+16]=-1
kb.AddClause(stenchClause) #presence/absence of stench
for newLoc in range(16):
if visited[newLoc]==False:
tempclauses= kb.getclauses()
checkClause={newLoc:1, newLoc+32:1}
tempclauses.append(checkClause)
if DPLLSatisfiable(tempclauses)==False:
#Room is safe
noWumpus={newLoc:-1}
noPit={newLoc+32:-1}
kb.AddClause(noWumpus)
kb.AddClause(noPit)
dfsVisited = [False for i in range(16)]
roomReachable=MoveToUnvisited(ag, visited, newLoc, dfsVisited) #dfs to new safe Room
if roomReachable:
break
def main():
ag = Agent()
kb= KnowledgeBase()
print('Start Location: {0}'.format(ag.FindCurrentLocation()))
ExitWumpusWorld(ag, kb)
print('{0} reached. Exiting the Wumpus World.'.format(ag.FindCurrentLocation()))
print('Total number of times DPLL function is called: {0}'.format(numberOfCalls))
if __name__=='__main__':
main()