-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
250 lines (211 loc) · 6.81 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
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
import sys
sys.setrecursionlimit(10000)
from pyPL import *
from helper import *
from a_search import aStarSearch
class State(Enum):
W = 1
P = 2
S = 3
B = 4
OK = 5
PW = 6
PP = 7
class Agent:
def __init__(self, w, h):
self.size = w, h
self.kb = initRules(w,h) # knowledge bas
"""
akb represents at least knowledge base, helping agent using the rule
that if the agent smells stench, there is at least one wumpus nearby.
The same rulle is applied to pit and breeze.
"""
self.akb = atLeastRules(w,h)
self.score = 0 # score
self.p = 1, 1 # position
self.moveCnt = 150 # allowance for move cnt
self.worldState = {}
self.unvisited = set()
self.ok = set()
for x in range (1, w+1):
for y in range(1, h+1):
self.worldState[(x,y)] = set()
self.unvisited.add((x,y))
self.plan = []
self.adjList = {}
self.steps = []
print("self.kb", self.kb)
print("self.akb", self.akb)
print("self.unvisited", self.unvisited)
def pCNF(self, kb):
for clause in kb:
print(str([str(u) for u in list(clause)]))
def updateKB(self, world):
# mark this place as safe
self.ok.add(self.p)
percepts = world.perceptsAt(self.p)
pSentences = AtomicSentence(True, "True")
# sentences for those are in current percepts
for p in percepts:
if p != "G" and p != "A":
pSentences = pSentences.cAnd(makePerceptSentence(p, self.p[0], self.p[1]))
self.worldState[self.p].add(p)
# sentences for those are not in current percepts
notPercepts = [p for p in world.percepts if p not in percepts]
for p in notPercepts:
neis = neighbors(*self.p, *self.size)
if p == "B":
[self.worldState[nei].remove("PP") for nei in neis if "PP" in self.worldState[nei]]
if p == "S":
[self.worldState[nei].remove("PW") for nei in neis if "PW" in self.worldState[nei]]
pSentences = pSentences.cAnd(~makePerceptSentence(p, self.p[0], self.p[1]))
# add the percepts to the current knowledge base
cnf = pSentences.cnf()
self.kb.extend(cnf)
def constructGraph(self, allowed):
"""
construct adjajency list for each allowed squares
"""
adjList = {}
for u in allowed:
adjList[u] = []
neis = neighbors(u[0], u[1], self.size[0], self.size[1])
for nei in neis:
if nei in allowed:
adjList[u].append(nei)
return adjList
def makePlan(self, world):
if "G" in world.perceptsAt(self.p):
self.plan.append(Act.GRAB_GOLD)
if len(self.plan) == 0:
# visits the closest safe unvisited square
goals = set([u for u in self.unvisited if u in self.ok])
print("goals", goals)
# print("safe unvisited from self.p", self.p , goals)
if len(goals) > 0:
adjList = self.constructGraph(self.ok)
result = aStarSearch(self.p, goals, adjList)
self.p = result['steps'][-1]
self.plan.extend(result['plan'])
# agent steps toward the future self.p
self.steps = result['steps']
if len(self.plan) == 0:
# go where there is a possible wumpus but not a possible pit
goals = set([u for u in self.unvisited if "PW" in self.worldState[u]])
if len(goals) > 0:
allowed = copy.deepcopy(self.ok).union(goals)
adjList = self.constructGraph(allowed)
result = aStarSearch(self.p, goals, adjList)
self.p = result['steps'][-1]
self.plan.extend(result['plan'])
self.plan.append(Act.SHOOT_ARROW)
# agent steps toward the future self.p
self.steps = result['steps']
# remove the possible wumpus tag from the shoot place
self.worldState[self.p].remove("PW")
# update the knowledge base of current place
notWumpus = ~makePerceptSentence("W", self.p[0], self.p[1])
self.kb.extend(notWumpus.cnf())
if len(self.plan) == 0:
# go where it is not safe, taking risk
for u in self.unvisited:
goals = set()
notOKSentence = (makePerceptSentence("W", u[0], u[1])).cOr(makePerceptSentence("P", u[0], u[1]))
kb = copy.deepcopy(self.kb)
kb.extend(self.akb.c)
notOK = resolution(kb, notOKSentence)
if not notOK:
goals.add(u)
if len(goals) > 0:
allowed = copy.deepcopy(self.ok).union(goals)
adjList = self.constructGraph(allowed)
result = aStarSearch(self.p, goals, adjList)
self.p = result['steps'][-1]
self.plan.extend(result['plan'])
# agent steps toward the future self.p
self.steps = result['steps']
# update the knowledge base of current place
# ok = (makePerceptSentence("OK", self.p[0], self.p[1]))
# self.kb.extend(ok.cnf())
if len(self.plan) == 0:
adjList = self.constructGraph(self.ok)
result = aStarSearch(self.p, {(1,1)}, adjList)
self.p = result['steps'][-1]
# agent steps toward the future self.p
self.steps = result['steps']
self.plan.extend(result['plan'])
self.plan.append(Act.EXIT_CAVE)
def updateWorldState(self):
neis = neighbors(self.p[0], self.p[1], self.size[0], self.size[1])
neis.append(self.p)
for pos in neis:
notPitSentence = ~(makePerceptSentence("P", pos[0], pos[1]))
notPit = resolution(self.kb, notPitSentence)
notWumpusSentence = ~(makePerceptSentence("W", pos[0], pos[1]))
notWumpus = resolution(self.kb, notWumpusSentence)
if notWumpus and notPit:
self.kb.extend(notWumpusSentence.cnf())
self.kb.extend(notPitSentence.cnf())
self.ok.add(pos)
else:
if not notPit:
# there is a possible pit
self.worldState[pos].add("PP")
if not notWumpus:
# there is a possible wumpus
self.worldState[pos].add("PW")
# mark the current position as visited
if self.p in self.unvisited:
self.unvisited.remove(self.p)
def planRoute(self, current, goals):
actions = []
return actions
def act(self):
result = {}
perceptsToRemove = { }
result['steps'] = self.steps
# tell if the agent wants to exit
result['exit'] = self.plan[-1] == Act.EXIT_CAVE
while len(self.plan) > 0:
if self.plan[0] == Act.STEP:
self.move()
elif self.plan[0] == Act.GRAB_GOLD:
perceptsToRemove["G"] = self.p
self.pickGold()
result['steps'].append(self.plan[0])
elif self.plan[0] == Act.SHOOT_ARROW:
perceptsToRemove["W"] = self.p
self.shootArrow()
result['steps'].append(self.plan[0])
self.plan.pop(0)
result['perceptsToRemove'] = perceptsToRemove
return result
def do(self, world):
self.steps = []
self.updateKB(world)
self.updateWorldState()
self.makePlan(world)
return self.act()
# def moveUp(self):
# self.p[1] += 1
# def moveDown(self):
# self.p[1] -= 1
# def moveLeft(self):
# self.p[0] -= 1
# def moveRight(self):
# self.p[0] += 1
def pickGold(self):
self.score += Act.GRAB_GOLD.value
def shootArrow(self):
self.score += Act.SHOOT_ARROW.value
def die(self):
self.score += Act.DIE.value
def move(self):
self.moveCnt -= Act.STEP.value
# self.p[1] += 1
# elif act == Act.DOWN:
# self.p[1] -= 1
# elif act == Act.LEFT:
# self.p[0] -= 1
# else:
# self.p[0] += 1