-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpnSimple.py
254 lines (194 loc) · 5.6 KB
/
hpnSimple.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
import random
from collections import deque
class FluentA(object):
def __init__(self, value):
self.value = value
def holdsIn(self, b):
if 'A' in [f for f in b]:
return b['A'] == self.value
else:
return False
class FluentB(object):
def __init__(self, value):
self.value = value
def holdsIn(self, b):
if 'B' in [f for f in b]:
return b['B'] == self.value
else:
return False
class FluentC(object):
def __init__(self, value):
self.value = value
def holdsIn(self, b):
if 'C' in [f for f in b]:
return b['C'] == self.value
else:
return False
class FluentD(object):
def __init__(self, value):
self.value = value
def holdsIn(self, b):
if 'D' in [f for f in b]:
return b['D'] == self.value
else:
return False
class FluentE(object):
def __init__(self, value):
self.value = value
def holdsIn(self, b):
if 'E' in [f for f in b]:
return b['E'] == self.value
else:
return False
class Pre(object):
def __init__(self, fluent, abstLevel):
self.fluent = fluent
self.abstLevel = abstLevel
class Op1(object):
def __init__(self):
self.effect = [FluentD(True)]
self.pre = [Pre(FluentA(True), 1)]
self._hasPrim = True
def isPrim(self, abstLevel):
return self._hasPrim and all([p.abstLevel <= abstLevel for p in self.pre])
class Op2(object):
def __init__(self):
self.effect = [FluentE(True)]
self.pre = [Pre(FluentC(True), 0)]
self._hasPrim = True
def isPrim(self, abstLevel):
return self._hasPrim and all([p.abstLevel <= abstLevel for p in self.pre])
class Op3(object):
def __init__(self):
self.effect = [FluentA(True)]
self.pre = [Pre(FluentB(True), 0)]
self._hasPrim = True
def isPrim(self, abstLevel):
return self._hasPrim and all([p.abstLevel <= abstLevel for p in self.pre])
#class Op4(object):
# def __init__(self):
# self.effect = [FluentA(False)]
# self.pre = [Pre(FluentB(True), 0)]
#
class Agent(object):
def __init__(self, world, goal):
self._operators = [Op1(), Op2(), Op3()]
self._goal = goal
self._world = world
def _visit(self, preImage, searchedLeaf, abstLevel):
childrenPI = list()
# print '\n\nFATHER\n', preImage, '\n\n'
random.shuffle(self._operators)
for op in self._operators:
childPreImage = list()
# print '\n\nOP\n', op, '\n\n'
# print '\n\nOP EFFECT\n', op.effect, '\n\n'
# print '\n\nOP PRECOND\n', [p.fluent for p in op.pre], '\n\n'
for f1 in preImage:
takeF1Out = False
for f2 in op.effect:
if type(f1) == type(f2):
takeF1Out = True
break
if not takeF1Out:
childPreImage.append(f1)
for precond in op.pre:
doNotAdd = False
if precond.abstLevel <= abstLevel:
for f3 in childPreImage:
if type(f3) == type(precond.fluent):
doNotAdd = True
break
if not doNotAdd:
childPreImage.append(precond.fluent)
childrenPI.append([op, childPreImage])
# print '\n\n CHILD \n', childrenPI ,'\n\n'
return childrenPI
def _plan(self, bnow, goal, abstLevel):
root = goal
searchedLeaf = bnow
if all([f.holdsIn(searchedLeaf) for f in root]):
return []
tree = list()
tree.append([[None, root], None])
cntr = 0
findSearchedLeaf = False
while True:
preImage = tree[cntr][0]
pImages = self._visit(preImage[1], searchedLeaf, abstLevel)
for preImage in pImages:
tree.append([preImage, cntr])
if all([f.holdsIn(searchedLeaf) for f in preImage[1]]):
findSearchedLeaf = True
break
if findSearchedLeaf:
break
cntr += 1
# get solution path from tree
plan = list()
idx = len(tree)-1
while idx > 0:
plan.append(tree[idx][0])
idx = tree[idx][1]
return plan
def _nextLevel(self, abstLevel, operator):
return abstLevel + 1;
def _bHPN(self, bnow, goal, abstLevel):
#print bnow
print '\nGOAL:\n', goal, '\n'
#print abstLevel
p = self._plan(bnow, goal, abstLevel) # updates
print '\n\nPLAN ( abs=', abstLevel, '):\n', p, '\n\n'
if len(p) == 0:
return goal
while True:
currBHoldingList = [all([f.holdsIn(bnow) for f in wg[1]]) for wg in p] # while bnow is in union_{i = 0}^{n-1} g_i(p)
if any(currBHoldingList) != True:
break
index = (len(currBHoldingList) - 1) - currBHoldingList[::-1].index(True)
if p[index][0].isPrim(abstLevel):
print '\nExecute:\n', p[index][0], '\n'
#obs = world.execute(p[index][0])
#bnow.update(p[index][0], obs)
# SIMPLIFING:
bnow = self._world.execute(p[index][0])
else:
newGoal = goal if index+1 >= len(p) else p[index+1][1]
bnow = self._bHPN(bnow, newGoal, self._nextLevel(abstLevel, p[index][0]))
if all([f.holdsIn(bnow) for f in goal]):
break
return bnow
def _bHPNTOp(self):
bnow = self._world.bnow
while not all([f.holdsIn(bnow) for f in self._goal]):
bnow = self._bHPN(bnow, self._goal, 0)
def go(self):
self._bHPNTOp()
class WorldSim(object):
""" This class is a container of all simulation elements and also the
interface for running the simulation.
"""
def __init__(self, binit):
self.bnow = binit
def execute(self, operator):
for eff in operator.effect:
#TODO
if not eff.holdsIn(self.bnow):
if type(eff) == FluentA:
self.bnow['A'] = eff.value
elif type(eff) == FluentB:
self.bnow['B'] = eff.value
elif type(eff) == FluentC:
self.bnow['C'] = eff.value
elif type(eff) == FluentD:
self.bnow['D'] = eff.value
elif type(eff) == FluentE:
self.bnow['E'] = eff.value
return self.bnow
# MAIN ########################################################################
if __name__ == '__main__':
binit = {'B': True, 'C': True, 'D': False}
goal = [FluentD(True), FluentE(True)]
w = WorldSim(binit)
a = Agent(w, goal)
a.go()