ISS-CLF/RCBF Neural Policy Shield / math_sanity.py

Failed on benchmark

Raw ⬇ ZIP
 1import numpy as np
 2
 3def check_margin():
 4    # q(x)=x^2/2, xdot=u+w, |w|<=wbar: max_w grad(q)w=wbar|x|.
 5    x=.6; ws=np.linspace(0,.8,9)
 6    predicted=ws*abs(x)
 7    observed=np.array([max(x*w for w in (-w, w)) for w in ws])
 8    slope=np.polyfit(ws,observed,1)[0]
 9    return {'predicted_slope':float(abs(x)), 'observed_slope':float(slope),
10            'max_abs_error':float(np.max(abs(predicted-observed))),
11            'confirmed':bool(np.max(abs(predicted-observed))<1e-10)}
12
13def check_projection():
14    # Scalar QP min .5(u-u0)^2 subject to u<=b, u in [-1,1].
15    u0=.8; b=.25; u=max(-1.,min(1.,min(u0,b)))
16    return {'u0':u0,'bound':b,'projected':u,'constraint_residual':float(u-b),
17            'confirmed':bool(u<=b+1e-12)}
18
19if __name__=='__main__':
20    import json
21    out={'margin_scaling':check_margin(),'scalar_qp_projection':check_projection()}
22    print(json.dumps(out,indent=2))