-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl.py
292 lines (232 loc) · 8.02 KB
/
stl.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
"""
Module with STL definitions
Author: Francisco Penedo ([email protected])
"""
import operator
import copy
import numpy as np
from pyparsing import Word, Suppress, Optional, Combine, nums, \
Literal, Forward, delimitedList, alphanums, Keyword, Group
# Operator constants
EXPR = 0
NOT = 1
AND = 2
OR = 3
NEXT = 4
ALWAYS = 5
EVENTUALLY = 6
LE = operator.le
GT = operator.gt
class Signal(object):
"""
Class for an observed signal.
Example: let y(t) = x1(t) + x2(t) be an observed (or secondary) signal for a
model with primary signals x1 and x2. We can define this signal as follows:
>>> x = [[1,2,3], [3,4,5]]
>>> class Model():
>>> def getVarByName(self, i):
>>> return x[i[0]][i[1]]
>>> labels = [lambda t: (0, t), lambda t: (1, t)]
>>> f = lambda x, y: x + y
>>> y = Signal(labels, f)
>>> signal(Model(), 1)
6
"""
def __init__(self, labels, f):
"""
labels : array of functions
Functions that return the name of the primary signals at any
given time needed for this observed signal.
f : function
Function of the primary signals. Arity should be equal to the length
of labels.
"""
self._labels = labels
self._f = f
def signal(self, model, t):
"""
Obtain the observed signal at time t for the given model.
model : object with a getVarByName(self, signal_t) method
The model containing the time series for the primary signals.
The method getVarByName should accept objects returned by the
functions in the labels parameter to __init__ and return the
value of the signal at the given time
t : numeric
The time
"""
vs = [model.getVarByName(l(t)) for l in self._labels]
# TODO Get rid of any
if any(var is None for var in vs):
return None
else:
return self._f(vs)
class Formula(object):
"""
An STL formula.
"""
def __init__(self, operator, args, bounds=None):
"""
operator : one of EXPR, AND, OR, NOT, ALWAYS, EVENTUALLY, NEXT
args : either a list of Formulas or a Signal
If operator is EXPR, this is the signal corresponding to the
atomic formula g(t) > 0. Otherwise, the list of arguments of the
operator (1 for NOT, and the temporal operators, 2 or more for
AND and OR).
bounds : list of two numerics, optional
The bounds of the temporal operator. Defaults to [0, 0]
"""
self._op = operator
self._args = args
if bounds is None:
bounds = [0, 0]
self._bounds = bounds
def _hexpr(self):
return 0
def _hnot(self):
return 0
def _hand(self):
return max(map(lambda f: f.horizon(), self.args))
def _hor(self):
return self._hand()
def _halways(self):
return self.bounds[1] + self.args[0].horizon()
def _hnext(self):
return 1 + self.args[0].horizon()
def _heventually(self):
return self._halways()
def horizon(self):
"""
Computes the time horizon of the formula
"""
return {
EXPR: self._hexpr,
NOT: self._hnot,
AND: self._hand,
OR: self._hor,
NEXT: self._hnext,
ALWAYS: self._halways,
EVENTUALLY: self._heventually
}[self.op]()
def copy(self):
return copy.deepcopy(self)
@property
def op(self):
return self._op
@op.setter
def op(self, value):
self._op = value
@property
def args(self):
return self._args
@args.setter
def args(self, value):
self._args = value
@property
def bounds(self):
return self._bounds
@bounds.setter
def bounds(self, value):
self._bounds = value
def __str__(self):
return {
EXPR: "(%s)" % str(self.args[0]),
NOT: "~ %s" % str(self.args[0]),
AND: "(%s)" % " ^ ".join([str(arg) for arg in self.args]),
OR: "(%s)" % " v ".join([str(arg) for arg in self.args]),
NEXT: "O%s" % str(self.args[0]),
ALWAYS: "G_[%.2f, %.2f] %s" % \
(self.bounds[0], self.bounds[1], str(self.args[0])),
EVENTUALLY: "F_[%.2f, %.2f] %s" % \
(self.bounds[0], self.bounds[1], str(self.args[0]))
}[self.op]
def __repr__(self):
return self.__str__()
# FIXME used fixed time intervals
def robustness(formula, model, t=0):
"""
Computes the robustness of a formula with respect to a model at time t.
The computation is recursive.
formula : Formula
model : object as defined in Signal
The model containing the values of the primary signal.
time : numeric
The time at which to compute the robustness.
"""
return {
EXPR: lambda: formula.args[0].signal(model, t),
NOT: lambda: -robustness(formula.args[0], model, t),
AND: lambda: min(robustness(f, model, t) for f in formula.args),
OR: lambda: max(robustness(f, model, t) for f in formula.args),
NEXT: lambda: robustness(formula.args[0], model, t + model.tinter),
ALWAYS: lambda: min(robustness(formula.args[0], model, t + j) for j in
np.arange(formula.bounds[0],
formula.bounds[1] + model.tinter,
model.tinter)),
EVENTUALLY: lambda: max(robustness(formula.args[0], model, t + j) for j in
np.arange(formula.bounds[0],
formula.bounds[1] + model.tinter,
model.tinter))
}[formula.op]()
def satisfies(formula, model, t=0):
"""
Checks if a model satisfies a formula at some time.
Satisfaction is defined in this function as robustness >= 0.
formula : Formula
model : a model as defined in Signal
t : numeric
The time
"""
return robustness(formula, model, t) >= 0
# parser
def num_parser():
"""
A floating point number parser
"""
T_DOT = Literal(".")
T_MIN = Literal("-")
num = Combine(Optional(T_MIN) + Word(nums) +
Optional(T_DOT + Word(nums))).setParseAction(lambda t: float(t[0]))
return num
def stl_parser(expr=None):
"""
Builds an stl parser using the given expression parser.
The STL grammar used is the following:
form ::= ( expr )
| "~" form
| ( and_list )
| ( or_list )
| op "_" interval form
and_list ::= form "^" form
| form "^" and_list
or_list ::= form "v" form
| form "v" or_list
op ::= "G" | "F"
interval ::= [ num "," num ]
where num is a floating point number
expr : a parser, optional, defaults to r'\w+'
An expression parser.
"""
if not expr:
expr = Word(alphanums)
T_GLOB = Keyword("G", alphanums)
T_FUT = Keyword("F", alphanums)
T_LPAR, T_RPAR, T_LBRK, T_RBRK, T_UND, T_COM, T_TILD = map(Suppress, "()[]_,~")
num = num_parser()
interval = Group(T_LBRK + num + T_COM + num + T_RBRK)
form = Forward()
form_not = T_TILD + form
form_and = T_LPAR + delimitedList(form, "^") + T_RPAR
form_or = T_LPAR + delimitedList(form, "v") + T_RPAR
form_expr = T_LPAR + expr + T_RPAR
form_alw = T_GLOB + T_UND + interval + form
form_fut = T_FUT + T_UND + interval + form
form << (form_expr ^ form_not ^ form_and ^ form_or ^ form_alw ^ form_fut)
form_expr.setParseAction(lambda t: Formula(EXPR, [t[0]]))
form_not.setParseAction(lambda t: Formula(NOT, [t[0]]))
form_and.setParseAction(lambda t: Formula(AND, list(t)))
form_or.setParseAction(lambda t: Formula(OR, list(t)))
form_alw.setParseAction(
lambda t: Formula(ALWAYS, [t[2]], bounds=list(t[1])))
form_fut.setParseAction(
lambda t: Formula(EVENTUALLY, [t[2]], bounds=list(t[1])))
return form