== Listing 8.1: LQR by dynamic programming ==
infinite-horizon gain K = [3.7106, 3.4867] after 92 Riccati iterations
max |P_iter - P_DARE| = 3.34e-13
closed-loop spectral radius = 0.8297
max eigenvalue of P - (Acl' P Acl + Q + K'RK) residual = 6.39e-14
horizon T   K_0 (first gain)           max|P_0 - P_inf|
       1   [0.0000, 0.0000]   3.359e+01
       2   [0.0000, 0.4762]   2.959e+01
       5   [0.9230, 1.7254]   1.786e+01
      10   [2.8795, 3.0078]   4.594e+00
      20   [3.6896, 3.4735]   1.048e-01
      40   [3.7106, 3.4867]   7.060e-05
T=20: x0' P_0 x0 = 37.516089; simulated DP cost = 37.516089
500 random perturbations of the gain sequence: best cost 37.5819, share below DP cost 0.000

== Listing 8.2: MPC against LQR ==
LQR first action -K x0 = -4.756634
H= 1: first MPC action with terminal cost P_inf -4.756634; with terminal cost Q -0.142857
H= 3: first MPC action with terminal cost P_inf -4.756634; with terminal cost Q -0.908920
H=10: first MPC action with terminal cost P_inf -4.756634; with terminal cost Q -4.046567
closed-loop cost over 40 steps (stage costs plus terminal x'Px) with |u| <= 0.3:
  LQR gain, clipped           173.640
  MPC horizon  3, box bounds  173.376
  MPC horizon 10, box bounds  171.188
  MPC horizon 25, box bounds  170.805
  open-loop optimum (one 40-step QP)  170.802

== Listing 8.3: compounding error, exactly ==
absorbing chain (one slip is final), per-step slip probability eps: cost J, bound eps T (T+1)/2, T^2 eps
eps=0.010 T= 10: J=   0.534  bound=   0.550  T^2 eps=   1.000
eps=0.010 T= 20: J=   1.973  bound=   2.100  T^2 eps=   4.000
eps=0.010 T= 40: J=   7.228  bound=   8.200  T^2 eps=  16.000
eps=0.010 T= 80: J=  25.305  bound=  32.400  T^2 eps=  64.000
eps=0.002 T= 10: J=   0.109  bound=   0.110  T^2 eps=   0.200
eps=0.002 T= 20: J=   0.415  bound=   0.420  T^2 eps=   0.800
eps=0.002 T= 40: J=   1.598  bound=   1.640  T^2 eps=   3.200
eps=0.002 T= 80: J=   6.152  bound=   6.480  T^2 eps=  12.800
recoverable chain (a slipped learner returns with probability 0.3 per step), eps=0.01:
  T= 10: J=  0.253   J/T=0.0253
  T= 20: J=  0.573   J/T=0.0287
  T= 40: J=  1.219   J/T=0.0305
  T= 80: J=  2.509   J/T=0.0314
  stationary share of time off the expert path = eps/(eps+recovery) = 0.0323
random MDP: 8 states, 3 actions, horizon 12; expert cost J* = 3.7104
learner = expert with probability 1-eps, uniform over the other actions otherwise
eps=0.01: gap J-J*=  0.0464  PDL right side=  0.0464  u T eps =   0.107  eps T(T+1)/2 =   0.780
eps=0.05: gap J-J*=  0.2315  PDL right side=  0.2315  u T eps =   0.535  eps T(T+1)/2 =   3.900
eps=0.20: gap J-J*=  0.9161  PDL right side=  0.9161  u T eps =   2.138  eps T(T+1)/2 =  15.600

== Listing 8.4: a sim-to-real gap bound ==
one axis, LQR gain from the nominal model, cost J = sum_t |position error|, horizon 60
real plant (drag, gain)   J_sim    |J_real - J_sim|   contraction bound   naive Lipschitz bound   L_P     L_2
(0.5, 1.0)                 18.921       2.566               54.977              1.219e+03        0.9099  1.0794
(0.0, 0.7)                 18.921       1.980              159.225              3.529e+03        0.9099  1.0794
(1.0, 0.8)                 18.921       6.586              123.831              2.745e+03        0.9099  1.0794
(2.0, 0.5)                 18.921      20.395              272.186              6.033e+03        0.9099  1.0794
horizon T:                 20       40       80      160
measured gap, (0.5, 1.0):    1.671    2.533    2.567    2.567
contraction bound:          12.582   33.174   76.964  165.035
total-variation form on a random MDP (uniform policy, costs in [0,1], T=20): bound = eta T (T-1)/2
  mixing weight 0.02: sup TV = 0.0178, |J - J_hat| = 0.0001, bound 3.3854
  mixing weight 0.10: sup TV = 0.0828, |J - J_hat| = 0.0006, bound 15.7252
  mixing weight 0.30: sup TV = 0.2231, |J - J_hat| = 0.0137, bound 42.3848

== Listing 8.5: the control-barrier-function filter ==
half-space projection: closed form against SLSQP on 40 random problems, max coordinate difference 1.61e-07
  largest constraint value after projection a.u - b = 4.44e-16; actions already feasible left unchanged: True
box plus half-space (bisection on the multiplier): max difference to SLSQP 8.33e-09, feasible problems 40 of 40
exact model, 500 episodes from random start states with h1 >= 0 and psi >= 0, 60 steps
start speed = fraction c of the braking limit sqrt(2 umax h1) (capped by the barrier's own limit g1 h1/dt)
c     g1    g2    policy   violations  min h1      min psi       episodes with an infeasible call  mean |u_safe - u|
0.5   0.2   0.5   push        0.000    8.56e-08   -5.42e-18                      0                    1.9976
0.5   0.2   0.5   random      0.000    5.21e-03    4.77e-06                      0                    0.0902
0.5   0.3   0.5   push        0.100   -8.73e-02   -4.98e-02                    199                    1.9977
0.5   0.3   0.5   random      0.000    7.81e-04   -5.07e-03                      5                    0.0904
0.5   0.5   0.5   push        0.660   -4.60e-01   -2.40e-01                    370                    1.9977
0.5   0.5   0.5   random      0.038   -8.68e-02   -5.34e-02                     64                    0.0934
1.0   0.2   0.5   push        0.008   -2.39e-02   -4.40e-02                     66                    1.9951
1.0   0.2   0.5   random      0.006   -2.39e-02   -4.40e-02                     26                    0.1104

== Listing 8.6: the task and the expert ==
configuration: time step 0.1, action bound 2.0, noise 0.01, obstacle radius 0.3, wall y <= 0.75, goal tolerance 0.2, 60 steps, waypoint (0.1, +-0.55), side read from the sign of y once |y| > 0.04, chunk length 8
400 expert episodes, plant noise 0.01: success 1.000, collisions 0.000, wall violations 0.000
minimum obstacle clearance 0.123, mean 0.176; wall at y=0.75, highest point reached 0.552
share of upper detours 0.477
at the symmetric start state the two expert modes have summed chunk y-actions +3.986 and -3.986; their mean is +0.000
60 demonstrations with action noise 0.0: 3600 (state, chunk) pairs, state spread (std of y) 0.256, std of x-velocity 0.312
60 demonstrations with action noise 0.5: 3600 (state, chunk) pairs, state spread (std of y) 0.266, std of x-velocity 0.314

== Listing 8.7: distribution shift, behaviour cloning and DAgger (5 seeds) ==
policy      error on expert states   error on own states   ratio   success        collisions     mean distance to nearest demo state
bc              0.0490                0.4420           8.97   0.143+-0.154  0.857+-0.154  0.156
dagger          0.0532                0.0869           1.64   0.957+-0.031  0.043+-0.031  0.030
DAgger rounds (own rollouts of the learner during data collection, 30 episodes per round, mean over seeds):
round  data size   success   collisions   wall violations
    0       3600     0.153        0.833             0.000
    1       5400     0.760        0.240             0.007
    2       7200     0.927        0.073             0.007
    3       9000     0.913        0.087             0.000
collisions incurred while collecting DAgger data, total over 5 seeds x 4 rounds x 30 episodes: 185

== Listing 8.8: diffusion policy over action chunks ==
network: input 33, hidden [96, 96], output 16; chunk length 8; 16 DDIM steps; data prediction; 2500 training steps, batch 128
symmetric start, 400 samples per seed: share of upward chunks 0.566 (per-seed range 0.30 to 0.81); mean |summed y action| 1.266
behaviour cloning (MSE) at the same state: summed y action +0.029 (mean over seeds), largest magnitude over seeds 0.746
chunk error on held-out committed states (|y| > 0.1): BC 0.1692, diffusion sample 0.2139
execution horizon k (actions executed before re-planning), success over 5 seeds, 60 episodes each:
  k=1: success 0.750+-0.053  collisions 0.250+-0.053
  k=2: success 0.573+-0.108  collisions 0.427+-0.108
  k=4: success 0.390+-0.094  collisions 0.610+-0.094
  k=8: success 0.243+-0.045  collisions 0.757+-0.045

== Listing 8.9: MPC baseline ==
horizon H  success         collisions     wall violations   gradient evaluations per control step
       5  0.994+-0.024  0.000+-0.000  0.000+-0.000   20
      10  1.000+-0.000  0.000+-0.000  0.000+-0.000   20
      15  1.000+-0.000  0.000+-0.000  0.000+-0.000   20
per control step: behaviour cloning 1 network forward pass, diffusion policy 16 forward passes (DDIM steps),
MPC 20 projected-gradient iterations, each a forward and a backward sweep over the horizon

== Listing 8.10: comparison on the nominal plant (60 episodes per seed, 5 seeds) ==
method                          success        collisions     wall violations   filter active
expert (privileged)             1.000+-0.000  0.000+-0.000  0.000+-0.000     0.0000
MPC (nominal model, H=15)       1.000+-0.000  0.000+-0.000  0.000+-0.000     0.0000
BC, clean demonstrations        0.143+-0.154  0.857+-0.154  0.000+-0.000     0.0000
BC, noise-injected demos        0.573+-0.161  0.427+-0.161  0.000+-0.000     0.0000
DAgger (4 rounds)               0.957+-0.031  0.043+-0.031  0.000+-0.000     0.0000
diffusion, noise-injected demos  0.750+-0.053  0.250+-0.053  0.003+-0.009     0.0000
diffusion, DAgger data          0.863+-0.112  0.083+-0.065  0.000+-0.000     0.0000
MPC + filter                    1.000+-0.000  0.000+-0.000  0.000+-0.000     0.0000
DAgger + filter                 0.957+-0.031  0.043+-0.031  0.000+-0.000     0.0000
diffusion (DAgger data) + filter  0.863+-0.112  0.083+-0.065  0.000+-0.000     0.0002

== Listing 8.11: the filter with a wrong model, and the margin ==
adversarial policy (always push toward the wall), 200 episodes, 60 steps; wall violation rate [infeasible filter calls]
plant              no filter   model-free    margin 0.03     margin 0.06     margin 0.09    knows plant, margin 0.03
exact (no noise)   1.000           0.000 [    0]    0.000 [    0]    0.000 [    0]    0.000 [    0]    0.000 [    0]
noise 0.01         1.000           0.980 [    0]    0.000 [    0]    0.000 [    0]    0.000 [    0]    0.000 [    0]
wind 0.5           1.000           1.000 [    0]    1.000 [    0]    0.000 [    0]    0.000 [    0]    0.000 [    0]
wind 0.8           1.000           1.000 [  536]    1.000 [  399]    1.000 [  287]    0.000 [  191]    0.000 [  136]
gain 0.6           1.000           0.995 [    0]    0.000 [    0]    0.000 [    0]    0.000 [    0]    0.000 [    4]
wind 1.5           1.000           1.000 [ 6513]    1.000 [ 6260]    1.000 [ 5986]    1.000 [ 5690]    1.000 [ 4781]
learned policy (DAgger, 3 seeds, 60 episodes) on the plant with wind 0.8 (a velocity increment of 0.08 per step the filter does not know about)
  no filter : violations 0.106+-0.322  success 0.661+-0.822  filter active 0.000
  margin 0.00: violations 0.106+-0.322  success 0.661+-0.822  filter active 0.004
  margin 0.06: violations 0.028+-0.120  success 0.650+-0.806  filter active 0.021
  margin 0.09: violations 0.000+-0.000  success 0.661+-0.729  filter active 0.038
  margin 0.12: violations 0.000+-0.000  success 0.672+-0.628  filter active 0.062

== Listing 8.12: observation-to-action delay (3 seeds, 40 episodes) ==
delay (steps of 0.1 s)       0      1      2      3      4
expert (feedback)           1.000  1.000  1.000  1.000  0.000
MPC                         1.000  1.000  1.000  1.000  0.867
DAgger                      0.967  0.942  0.850  0.342  0.083
diffusion, k=1              0.925  0.858  0.725  0.050  0.000
diffusion, k=4              0.733  0.708  0.600  0.108  0.000

== Listing 8.13: model mismatch (3 seeds, 40 episodes; success / collisions / mean final distance) ==
plant                                         expert      MPC nominal      MPC matched           DAgger        diffusion
nominal                               1.00/0.00/0.01   1.00/0.00/0.04   1.00/0.00/0.04   0.97/0.03/0.01   0.92/0.07/0.01
actuator gain 0.6                     1.00/0.00/0.02   0.85/0.00/0.17   1.00/0.00/0.06   0.95/0.05/0.02   0.90/0.10/0.03
drag 1.5 per second                   1.00/0.00/0.08   0.00/0.00/0.25   1.00/0.00/0.04   0.95/0.03/0.07   0.91/0.04/0.28
wind 0.5                              1.00/0.00/0.14   0.00/0.00/0.46   1.00/0.00/0.10   0.97/0.03/0.11   0.73/0.12/2.00
gain 0.6 + drag 1.5 + wind 0.5        0.00/0.51/0.30   0.00/0.00/0.79   0.00/0.00/0.47   0.17/0.03/0.94   0.00/0.12/1.97

== Listing 8.14: domain randomization on the two-link arm ==
real arm: masses 1.3 and 1.7 kg (nominal 1.0, 1.0), friction 0.25 (nominal 0.1), 5-step (50 ms) torque delay; reach cost = integral of |q - q_goal|^2
simulation: step 0.01 s, 250 steps; randomised arms: 30 draws per seed, masses in [0.7, 1.5] and [0.7, 2.0], friction in [0.05, 0.3], delay 0 to 6 steps
rho        nominal-sim cost   real cost
 1.0e-03          0.776          3.813
 3.2e-03          0.542          3.274
 1.0e-02          0.418          3.135
 3.2e-02          0.366          3.259
 1.0e-01          0.354          2.912
 3.2e-01          0.384          1.215
 1.0e+00          0.471          1.469
 3.2e+00          0.606          3.132
 1.0e+01          0.793          5.913
nominal-tuned rho = 1.00e-01: sim cost 0.354, real cost 2.912, gap 2.558
best possible rho on the real arm = 3.16e-01: real cost 1.215
DR (randomised masses and friction): chosen rho 1.00e-01 (geometric mean over 5 draws), cost in randomised sim 0.444, real cost 2.912+-0.000, gap 2.468
DR (randomised masses, friction and delay): chosen rho 1.26e+00 (geometric mean over 5 draws), cost in randomised sim 1.365, real cost 1.802+-0.923, gap 0.437
