precompute lineq timeseries during boolean evaluation
This commit is contained in:
parent
8f5035a9e3
commit
00ec325589
3 changed files with 72 additions and 24 deletions
|
|
@ -8,23 +8,32 @@ import funcy as fn
|
|||
from lenses import lens
|
||||
|
||||
import stl.ast
|
||||
import stl
|
||||
|
||||
oo = float('inf')
|
||||
|
||||
def pointwise_sat(phi):
|
||||
ap_names = [z.id.name for z in stl.utils.AP_lens(phi).get_all()]
|
||||
def _eval_stl(x, t):
|
||||
evaluated = stl.utils.eval_lineqs(phi, x)
|
||||
evaluated.update(fn.project(x, ap_names))
|
||||
return eval_stl(phi)(evaluated, t)
|
||||
return _eval_stl
|
||||
|
||||
@singledispatch
|
||||
def pointwise_sat(stl):
|
||||
def eval_stl(stl):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.Or)
|
||||
@eval_stl.register(stl.Or)
|
||||
def _(stl):
|
||||
fs = [pointwise_sat(arg) for arg in stl.args]
|
||||
fs = [eval_stl(arg) for arg in stl.args]
|
||||
return lambda x, t: any(f(x, t) for f in fs)
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.And)
|
||||
@eval_stl.register(stl.And)
|
||||
def _(stl):
|
||||
fs = [pointwise_sat(arg) for arg in stl.args]
|
||||
fs = [eval_stl(arg) for arg in stl.args]
|
||||
return lambda x, t: all(f(x, t) for f in fs)
|
||||
|
||||
|
||||
|
|
@ -33,7 +42,10 @@ def get_times(x, tau, lo=None, hi=None):
|
|||
lo = min(v.first()[0] for v in x.values())
|
||||
if hi is None or hi is oo:
|
||||
hi = max(v.last()[0] for v in x.values())
|
||||
end = min(v.domain.end() for v in x.values())
|
||||
try:
|
||||
end = min(v.domain.end() for v in x.values())
|
||||
except:
|
||||
import pdb; pdb.set_trace()
|
||||
hi = hi + tau if hi + tau <= end else end
|
||||
lo = lo + tau if lo + tau <= end else end
|
||||
|
||||
|
|
@ -46,10 +58,10 @@ def get_times(x, tau, lo=None, hi=None):
|
|||
return sorted(set(fn.pluck(0, all_times)))
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.Until)
|
||||
@eval_stl.register(stl.Until)
|
||||
def _(stl):
|
||||
def _until(x, t):
|
||||
f1, f2 = pointwise_sat(stl.arg1), pointwise_sat(stl.arg2)
|
||||
f1, f2 = eval_stl(stl.arg1), eval_stl(stl.arg2)
|
||||
for tau in get_times(x, t):
|
||||
if not f1(x, tau):
|
||||
return f2(x, tau)
|
||||
|
|
@ -65,23 +77,23 @@ def eval_unary_temporal_op(phi, always=True):
|
|||
return lambda x, t: retval
|
||||
if hi == lo:
|
||||
return lambda x, t: f(x, t)
|
||||
f = pointwise_sat(phi.arg)
|
||||
f = eval_stl(phi.arg)
|
||||
return lambda x, t: fold(f(x, tau) for tau in get_times(x, t, lo, hi))
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.F)
|
||||
@eval_stl.register(stl.F)
|
||||
def _(phi):
|
||||
return eval_unary_temporal_op(phi, always=False)
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.G)
|
||||
@eval_stl.register(stl.G)
|
||||
def _(phi):
|
||||
return eval_unary_temporal_op(phi, always=True)
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.Neg)
|
||||
@eval_stl.register(stl.Neg)
|
||||
def _(stl):
|
||||
f = pointwise_sat(stl.arg)
|
||||
f = eval_stl(stl.arg)
|
||||
return lambda x, t: not f(x, t)
|
||||
|
||||
|
||||
|
|
@ -94,22 +106,20 @@ op_lookup = {
|
|||
}
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.AtomicPred)
|
||||
@eval_stl.register(stl.AtomicPred)
|
||||
def _(stl):
|
||||
return lambda x, t: x[str(stl.id)][t]
|
||||
|
||||
|
||||
@pointwise_sat.register(stl.LinEq)
|
||||
def _(stl):
|
||||
op = op_lookup[stl.op]
|
||||
return lambda x, t: op(eval_terms(stl, x, t), stl.const)
|
||||
@eval_stl.register(stl.LinEq)
|
||||
def _(lineq):
|
||||
return lambda x, t: x[lineq][t]
|
||||
|
||||
|
||||
def eval_terms(lineq, x, t):
|
||||
psi = lens(lineq).terms.each_().modify(eval_term(x, t))
|
||||
return sum(psi.terms)
|
||||
terms = lens(lineq).terms.each_().get_all()
|
||||
return sum(eval_term(term, x, t) for term in terms)
|
||||
|
||||
|
||||
def eval_term(x, t):
|
||||
# TODO(lift interpolation much higher)
|
||||
return lambda term: term.coeff*x[term.id.name][t]
|
||||
def eval_term(term, x, t):
|
||||
return float(term.coeff)*x[term.id.name][t]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue