|
| 1 | +using ModelPredictiveControl, Plots |
| 2 | +function f!(ẋ, x, u, _ , p) |
| 3 | + g, L, K, m = p # [m/s²], [m], [kg/s], [kg] |
| 4 | + θ, ω = x[1], x[2] # [rad], [rad/s] |
| 5 | + τ = u[1] # [Nm] |
| 6 | + ẋ[1] = ω |
| 7 | + ẋ[2] = -g/L*sin(θ) - K/m*ω + τ/m/L^2 |
| 8 | +end |
| 9 | +h!(y, x, _ , _ ) = (y[1] = 180/π*x[1]) # [°] |
| 10 | +p = [9.8, 0.4, 1.2, 0.3] |
| 11 | +nu = 1; nx = 2; ny = 1; Ts = 0.1 |
| 12 | +model = NonLinModel(f!, h!, Ts, nu, nx, ny; p) |
| 13 | +vu = ["\$τ\$ (Nm)"] |
| 14 | +vx = ["\$θ\$ (rad)", "\$ω\$ (rad/s)"] |
| 15 | +vy = ["\$θ\$ (°)"] |
| 16 | +model = setname!(model; u=vu, x=vx, y=vy) |
| 17 | + |
| 18 | +## ========================================= |
| 19 | +σQ = [0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1] |
| 20 | + |
| 21 | +## ========================================= |
| 22 | +p_plant = copy(p); p_plant[3] = 1.25*p[3] |
| 23 | +N = 35; u = [0.5]; |
| 24 | + |
| 25 | +## ========================================= |
| 26 | +Hp, Hc, Mwt, Nwt, Cwt = 20, 2, [0.5], [2.5], Inf |
| 27 | +umin, umax = [-1.5], [+1.5] |
| 28 | + |
| 29 | +h2!(y, x, _ , _ ) = (y[1] = 180/π*x[1]; y[2]=x[2]) |
| 30 | +nu, nx, ny = 1, 2, 2 |
| 31 | +model2 = NonLinModel(f!, h2!, Ts, nu, nx, ny; p) |
| 32 | +plant2 = NonLinModel(f!, h2!, Ts, nu, nx, ny; p=p_plant) |
| 33 | +model2 = setname!(model2, u=vu, x=vx, y=[vy; vx[2]]) |
| 34 | +plant2 = setname!(plant2, u=vu, x=vx, y=[vy; vx[2]]) |
| 35 | +estim2 = UnscentedKalmanFilter(model2; σQ, σR, |
| 36 | + nint_u, σQint_u, i_ym=[1]) |
| 37 | + |
| 38 | +function JE(UE, ŶE, _ , p) |
| 39 | + Ts = p |
| 40 | + τ, ω = UE[1:end-1], ŶE[2:2:end-1] |
| 41 | + return Ts*sum(τ.*ω) |
| 42 | +end |
| 43 | +p = Ts; Mwt2 = [Mwt; 0.0]; Ewt = 3.5e3 |
| 44 | +empc = NonLinMPC(estim2; Hp, Hc, |
| 45 | + Nwt, Mwt=Mwt2, Cwt, JE, Ewt, p, oracle=true, transcription=MultipleShooting(), hessian=true) |
| 46 | +empc = setconstraint!(empc; umin, umax) |
| 47 | + |
| 48 | +## ========================================= |
| 49 | +using JuMP; unset_time_limit_sec(empc.optim) |
| 50 | + |
| 51 | +## ========================================= |
| 52 | +x_0 = [0, 0]; x̂_0 = [0, 0, 0]; ry = [180; 0] |
| 53 | +res2_ry = sim!(empc, N, ry; plant=plant2, x_0, x̂_0) |
| 54 | +plot(res2_ry, ploty=[1]) |
| 55 | + |
| 56 | + |
| 57 | +## ========================================= |
| 58 | +## ========= Benchmark ===================== |
| 59 | +## ========================================= |
| 60 | +using BenchmarkTools |
| 61 | +using JuMP, Ipopt, KNITRO |
| 62 | + |
| 63 | +optim = JuMP.Model(Ipopt.Optimizer, add_bridges=false) |
| 64 | +empc_ipopt = NonLinMPC(estim2; Hp, Hc, Nwt, Mwt=Mwt2, Cwt, JE, Ewt, optim, p, oracle=true, transcription=MultipleShooting(), hessian=true) |
| 65 | +empc_ipopt = setconstraint!(empc_ipopt; umin, umax) |
| 66 | +JuMP.unset_time_limit_sec(empc_ipopt.optim) |
| 67 | +sim!(empc_ipopt, N, ry; plant=plant2, x_0=x_0, x̂_0=x̂_0) |
| 68 | +@profview sim!(empc_ipopt, N, ry; plant=plant2, x_0=x_0, x̂_0=x̂_0) |
| 69 | + |
| 70 | +y_step = [10.0, 0.0] |
| 71 | + |
| 72 | +bm = @benchmark( |
| 73 | + sim!($empc_ipopt, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0), |
| 74 | + samples=50, |
| 75 | + seconds=10*60 |
| 76 | + ) |
| 77 | +@show btime_EMPC_track_solver_IP = median(bm) |
| 78 | + |
| 79 | + |
| 80 | +bm = @benchmark( |
| 81 | + sim!($empc_ipopt, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, y_step=$y_step), |
| 82 | + samples=50, |
| 83 | + seconds=10*60 |
| 84 | + ) |
| 85 | +@show btime_EMPC_regul_solver_IP = median(bm) |
| 86 | + |
0 commit comments