-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
executable file
·362 lines (310 loc) · 12.3 KB
/
sensor.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import structures.domain
import re
import random
cache={}
class Sensor():
neg = "-"
lor = "v"
land = "^"
valid_ops = set([neg,lor,land])
def __init__(self,lhs, op = None, rhs = None):
# if(isinstance(lhs,tuple)):
# self.lhs = lhs[0]
# self.rhs = lhs[2]
# self.op = lhs[1]
# else:
# self.lhs = lhs
# self.rhs = rhs
# self.op = op
self.lhs = lhs
self.rhs = rhs
self.op = op
if(self.op != None and self.op not in self.valid_ops and not isinstance(self.op, int)):
print "***",self.op.func
raise Exception("Invalid op "+ self.op)
"""used to generate terminal sensors for GP"""
@classmethod
def generate_sensor(cls, domain, depth):
af=[d[0] for d in domain.all_facts]
if depth==0:
return Sensor(random.choice(af))
else:
op=random.choice([Sensor.neg,Sensor.lor,Sensor.land,"spath","terminal"])
if op==Sensor.neg:
return Sensor(Sensor.generate_sensor(domain, depth - 1), Sensor.neg)
if op==Sensor.lor or op==Sensor.land:
return Sensor(Sensor.generate_sensor(domain, depth - 1), op, Sensor.generate_sensor(domain, depth - 1))
if op=="spath":
return Sensor(Sensor.generate_sensor(domain, depth - 1), random.randint(1, 10), Sensor.generate_sensor(domain, depth - 1))
if op=="terminal":
return Sensor((random.choice(af),))
@property
def lhs(self):
return self.lhs
@property
def rhs(self):
return self.rhs
@property
def op(self):
# assert isinstance(self.op, object)
return self.op
def is_atom(self):
return self.op is None and self.rhs is None
def is_negated(self):
return self.op == self.neg
def is_path_formula(self):
return isinstance(self.op, int)
def is_model_of(self, trace, state):
#global cache
#if (str(self),trace,state) in cache:
# return cache[(str(self),trace,state)]
#cache[(str(self),trace,state)]=models(self,trace,state)
#return cache[(str(self),trace,state)]
return models(self, trace, state)
def __getitem__(self, item):
if item == 0:
return self.lhs
elif item == 1:
return self.op
elif item == 2:
return self.rhs
else:
raise IndexError("Sensor formulas have at most 3 components")
def print_op(self):
if self.op is not None and isinstance(self.op, int):
return "["+str(self.op)+"]"
return self.op
def __hash__(self):
return hash((self.lhs,self.op,self.rhs))
def __repr__(self):
s = "("
if self.is_negated():
s += self.print_op()
s += str(self.lhs)
else:
# s += str(self.lhs) if isinstance(self.lhs, Sensor) else str(self.lhs[0])
if isinstance(self.lhs, Sensor) or isinstance(self.lhs, bool):
s += str(self.lhs)
elif isinstance(self.lhs, tuple):
s += self.lhs[0]
else:
s += self.lhs
s += " "+str(self.print_op())+" " if self.op != None else ""
s += str(self.rhs) if self.rhs!= None else ""
s += ")"
return s
# return (str(self.lhs) if self.lhs != None else "") + (" "+str(self.print_op())+" " if self.op != None else "") + (str(self.rhs) if self.rhs!= None else "")
def __str__(self):
return repr(self)
def __eq__(self, other):
if isinstance(other, Sensor):
return repr(self) == repr(other)
else:
return False
true = Sensor(True)
#sensor, trace,state
def models(sigma, t, s):
# assert isinstance(sigma, Sensor)
# assert isinstance(t, structures.domain.Trace)
assert isinstance(s, structures.domain.State)
if sigma.is_atom():
return sigma.lhs is True or sigma.lhs in s
elif sigma.is_negated():
return not models(sigma.lhs, t, s)
else:
if sigma.op == sigma.land:
return models(sigma.lhs, t, s) and models(sigma.rhs, t, s)
elif sigma.op == sigma.lor:
return models(sigma.lhs, t, s) or models(sigma.rhs, t, s)
elif sigma.is_path_formula():
if sigma.op == 0 or len(t) == 0:
return models(sigma.lhs, t, s) and models(sigma.rhs, t, s)
else:
a = t[0]
assert isinstance(a, structures.domain.Action)
tp = t[1:]
sp = a.result(s)
if models(sigma.lhs, t, s):
if not models(sigma.rhs, t, s):
return models(Sensor(true, sigma.op - 1, sigma.rhs), tp, sp)
else:
return True
else:
return models(sigma, tp, sp)
else:
raise Exception
# def models(sigma, t, s):
# # assert isinstance(sigma, Sensor)
# # assert isinstance(t, structures.domain.Trace)
# global cache
# if (sigma,tuple(t),s) in cache:
# print "hitc"
# return cache[(sigma,t,s)]
#
# assert isinstance(s, structures.domain.State)
# if sigma.is_atom():
# cache[(sigma,t,s)]= sigma.lhs is True or sigma.lhs in s
# return cache[(sigma,t,s)]
# #return sigma.lhs is True or sigma.lhs in s
# elif sigma.is_negated():
# cache[(sigma,t,s)]=not models(sigma.lhs,t,s)
# return cache[(sigma,t,s)]
# #return not models(sigma.lhs, t, s)
# else:
# if sigma.op == sigma.land:
# cache[(sigma,t,s)]=models(sigma.lhs, t, s) and models(sigma.rhs, t, s)
# return cache[(sigma,t,s)]
# #return models(sigma.lhs, t, s) and models(sigma.rhs, t, s)
# elif sigma.op == sigma.lor:
# cache[(sigma,t,s)]=models(sigma.lhs, t, s) or models(sigma.rhs, t, s)
# return cache[(sigma,t,s)]
# #return models(sigma.lhs, t, s) or models(sigma.rhs, t, s)
# elif sigma.is_path_formula():
# if sigma.op == 0 or len(t) == 0:
# cache[(sigma,t,s)]=models(sigma.lhs, t, s) and models(sigma.rhs, t, s)
# return cache[(sigma,t,s)]
# #return models(sigma.lhs, t, s) and models(sigma.rhs, t, s)
# else:
# a = t[0]
# assert isinstance(a, structures.domain.Action)
# tp = t[1:]
# sp = a.result(s)
# if models(sigma.lhs, t, s):
# if not models(sigma.rhs, t, s):
# #return models(Sensor(true, sigma.op - 1, sigma.rhs), tp, sp)
# cache[(sigma,t,s)]=models(Sensor(true, sigma.op - 1, sigma.rhs), tp, sp)
# return cache[(sigma,t,s)]
# else:
# #cache[(sigma,t,s)]=True
# #return cache[(sigma,t,s)]
# return True
# else:
# cache[(sigma,t,s)]=models(sigma,tp,sp)
# return cache[(sigma,t,s)]
# #return models(sigma, tp, sp)
# else:
# raise Exception
# return cache[(sigma,t,s)]
class Sensor_Parser():
def __init__(self):
self.true = Sensor(True)
def read_file(self,filename):
str = ""
with open(filename, 'r') as f:
# Remove single line comments
str = re.sub(r';.*$', '', f.read(), flags=re.MULTILINE).lower()
return str
# ------------------------------------------
# Tokens
# ------------------------------------------
def scan_tokens(self, str):
# Tokenize
stack = []
list = []
for t in re.findall(r'[()]|[^\s()]+', str.lower()):
if t == '(':
stack.append(list)
list = []
elif t == ')':
if stack:
l = list
list = stack.pop()
list.append(l)
else:
raise Exception('Missing open parentheses')
else:
list.append(t)
if stack:
raise Exception('Missing close parentheses')
if len(list) != 1:
raise Exception('Malformed expression: '+str)
return list[0]
def parse_sensor_file(self, filename):
return self.parse_sensor(self.read_file(filename))
def parse_op(self,str):
if str[0]=="[" and str[-1]=="]":
return int(str[1:-1])
elif str == "&":
return Sensor.land
elif str == "|":
return Sensor.lor
elif str == "~":
return Sensor.neg
elif str in Sensor.valid_ops:
return str
else:
raise Exception("Invalid operator "+str)
def parse_sensor(self, str):
tokens = self.scan_tokens(str)
if type(tokens) is list:
return self.build_sensor(tokens)
else:
raise Exception('Expression ' + str + ' does not match a sensor')
def build_sensor(self,tokens):
# TODO Fix the nasty bit of code below...
if isinstance(tokens,list) and len(tokens) == 1: # This should be a symbol
# t = tokens.pop(0)
# if t is list:
# return self.build_sensor(t)
# else:
# return Sensor(self.parse_symbol(t))
return Sensor(self.parse_symbol(tokens))
elif isinstance(tokens,str):
return Sensor(self.parse_symbol(tokens))
elif len(tokens) == 2: # This should be a negation
op = tokens.pop(0)
lhs = tokens.pop(0)
return Sensor(lhs=self.build_sensor(lhs),op=self.parse_op(op))
elif len(tokens) == 3:
lhs = tokens.pop(0)
op = tokens.pop(0)
rhs = tokens.pop(0)
return Sensor(self.build_sensor(lhs),self.parse_op(op),self.build_sensor(rhs))
else:
raise 'Expression ' + str(tokens) + ' does not match a sensor'
def parse_symbol(self,str):
if str == "true":
return True
elif str == "false":
return False
else:
return tuple(str)
# TODO Move this into the parser class?
def sensor_for_action(action):
precond = ""
pos_precond = "{0}" if len(action.positive_preconditions) > 0 else "True"
neg_precond = "{0}" if len(action.negative_preconditions) > 0 else "True"
for i in range(0, len(action.positive_preconditions)):
if i == len(action.positive_preconditions) - 1:
pos_precond = pos_precond.format(str(action.positive_preconditions[i]))
else:
pos_precond = "({0} ^ {1})".format(pos_precond.format(str(action.positive_preconditions[i])),"{0}")
for i in range(0, len(action.negative_preconditions)):
if i == len(action.negative_preconditions) - 1:
neg_precond = neg_precond.format("(-"+str(action.negative_preconditions[i])+")")
else:
neg_precond = "({0} ^ {1})".format(neg_precond.format("(-"+str(action.negative_preconditions[i]))+")","{0}")
precond = "({0} ^ {1})".format(pos_precond,neg_precond)
pos_precond = "{0}" if len(action.add_effects) > 0 else "True"
neg_precond = "{0}" if len(action.del_effects) > 0 else "True"
for i in range(0, len(action.add_effects)):
if i == len(action.add_effects) - 1:
pos_precond = pos_precond.format(str(action.add_effects[i]))
else:
pos_precond = "({0} ^ {1})".format(pos_precond.format(str(action.add_effects[i])), "{0}")
for i in range(0, len(action.del_effects)):
if i == len(action.del_effects) - 1:
neg_precond = neg_precond.format("(-" + str(action.del_effects[i])+")")
else:
neg_precond = "({0} ^ {1})".format(neg_precond.format("(-" + str(action.del_effects[i]))+")", "{0}")
effect = "({0} ^ {1})".format(pos_precond,neg_precond)
return "({0} [1] {1})".format(precond,effect).replace(",","").replace("'","");
def sand(s1,s2):
return Sensor(s1,"^",s2)
def sor(s1,s2):
return Sensor(s1,"v",s2)
def snot(s1):
return Sensor(s1,"-")
def spath(s1,s2,i):
assert isinstance(i,int)
return Sensor(s1,i,s2)