Skip to content

Commit 4ce2a1d

Browse files
committed
adding: scripts to examples folder
1 parent 2890943 commit 4ce2a1d

19 files changed

+1694
-0
lines changed

examples/Knitrodebug.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using ModelPredictiveControl
2+
using JuMP, KNITRO, Ipopt
3+
4+
A = [ 0.800737 0.0
5+
0.0 0.606531 ]
6+
Bu = [ 0.378599 0.378599
7+
-0.291167 0.291167 ]
8+
C = [ 1.0 0.0
9+
0.0 1.0 ]
10+
f(x,u,_,_) = A*x + Bu*u
11+
h(x,_,_) = C*x
12+
model = NonLinModel(f, h, 4.0, 2, 2, 2, solver=nothing)
13+
14+
optim = Model(KNITRO.Optimizer) # Model(Ipopt.Optimizer) # Ipopt does work here
15+
oracle = true # true leads to `LOCALLY_INFEASIBLE` on KNITRO dev version, false works
16+
17+
nmpc = NonLinMPC(model; Hp=10, direct=false, Cwt=Inf, optim, oracle)
18+
nmpc = setconstraint!(nmpc, ymin=[-2,-2],ymax=[+2,+2])
19+
unset_time_limit_sec(nmpc.optim)
20+
unset_silent(nmpc.optim)
21+
set_optimizer_attribute(nmpc.optim, "outlev", 6)
22+
u = moveinput!(nmpc, [1, 1])
23+
#solution_summary(nmpc.optim)

examples/TestMPC.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module TestMPC
2+
3+
greet() = print("Hello World!")
4+
5+
end # module TestMPC

examples/Unodebug.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using ModelPredictiveControl, JuMP
2+
using UnoSolver
3+
4+
N = 35 # number of JuMP.optimize! calls
5+
6+
function f!(ẋ, x, u, _ , p)
7+
g, L, K, m = p # [m/s²], [m], [kg/s], [kg]
8+
θ, ω = x[1], x[2] # [rad], [rad/s]
9+
τ = u[1] # [Nm]
10+
ẋ[1] = ω
11+
ẋ[2] = -g/L*sin(θ) - K/m*ω + τ/m/L^2
12+
return nothing
13+
end
14+
h!(y, x, _ , _ ) = (y[1] = 180/π*x[1]; nothing) # [°]
15+
p = [9.8, 0.4, 1.2, 0.3]
16+
nu, nx, ny, Ts = 1, 2, 1, 0.1
17+
model = NonLinModel(f!, h!, Ts, nu, nx, ny; p)
18+
p_plant = copy(p); p_plant[3] = p[3]*1.25
19+
plant = NonLinModel(f!, h!, Ts, nu, nx, ny; p=p_plant)
20+
Hp, Hc, Mwt, Nwt = 20, 2, [0.5], [2.5]
21+
α=0.01; σQ=[0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1]
22+
σQint_ym = zeros(0)
23+
umin, umax = [-1.5], [+1.5]
24+
transcription = MultipleShooting()
25+
optim = Model(()->UnoSolver.Optimizer(preset="filtersqp"))
26+
oracle = true
27+
hessian = true
28+
nmpc = NonLinMPC(model;
29+
Hp, Hc, Mwt, Nwt, Cwt=Inf, transcription, oracle, hessian, optim,
30+
α, σQ, σR, nint_u, σQint_u, σQint_ym
31+
)
32+
nmpc = setconstraint!(nmpc; umin, umax)
33+
unset_time_limit_sec(nmpc.optim)
34+
sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])
35+
@time sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])
36+
@profview sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])

examples/bench_empc_hessian.jl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+

examples/bench_linearize.jl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# spell-checker: disable
2+
3+
using Revise
4+
5+
using ModelPredictiveControl
6+
7+
using LinearAlgebra
8+
using ControlSystemsBase
9+
using BenchmarkTools
10+
using DifferentiationInterface
11+
12+
#=
13+
using DifferentiationInterface
14+
using SparseConnectivityTracer
15+
using SparseMatrixColorings
16+
17+
f1(x::AbstractVector) = diff(x .^ 2) + diff(reverse(x .^ 2))
18+
f2(x::AbstractVector) = sum(f1(x) .^ 2)
19+
20+
dense_forward_backend = AutoForwardDiff()
21+
sparse_forward_backend = AutoSparse(
22+
dense_forward_backend; # any object from ADTypes
23+
sparsity_detector=TracerSparsityDetector(),
24+
coloring_algorithm=GreedyColoringAlgorithm(),
25+
)
26+
27+
x = rand(1000)
28+
jac_prep_sparse = prepare_jacobian(f1, sparse_forward_backend, zero(x))
29+
30+
J = similar(sparsity_pattern(jac_prep_sparse), eltype(x))
31+
jacobian!(f1, J, jac_prep_sparse, sparse_forward_backend, x)
32+
=#
33+
34+
35+
function f3!(dx, x, u, d, p)
36+
mul!(dx, p.A, x)
37+
mul!(dx, p.Bu, u, 1, 1)
38+
mul!(dx, p.Bd, d, 1, 1)
39+
return nothing
40+
end
41+
function h3!(y, x, d, p)
42+
mul!(y, p.C, x)
43+
mul!(y, p.Dd, d, 1, 1)
44+
return nothing
45+
end
46+
47+
A=[0 0.5; -0.2 -0.1]
48+
Bu=[0; 0.5]
49+
Bd=[0; 0.5]
50+
C=[0.4 0]
51+
Dd=[0]
52+
nonLinModel3 = NonLinModel(f3!, h3!, 1.0, 1, 2, 1, 1, solver=RungeKutta(4, supersample=2), p=(;A, Bu, Bd, C, Dd))
53+
54+
55+
linearizeModel = linearize(nonLinModel3, x=[1,1], u=[1], d=[1])
56+
57+
@benchmark linearize!($linearizeModel, $nonLinModel3, x=$[2,2], u=$[1], d=$[1])
58+
59+
linearize2!(linemodel, model) = (linearize!(linemodel, model); nothing)
60+
61+
#=
62+
linfunc! = nonLinModel3.linfunc!
63+
xnext = linearizeModel.buffer.x
64+
y = linearizeModel.buffer.y
65+
A = linearizeModel.A
66+
Bu = linearizeModel.Bu
67+
Bd = linearizeModel.Bd
68+
C = linearizeModel.C
69+
Dd = linearizeModel.Dd
70+
x = [2.0, 2.0]
71+
u = [1.0]
72+
d = [1.0]
73+
cx = Constant(x)
74+
cu = Constant(u)
75+
cd = Constant(d)
76+
backend = nonLinModel3.jacobian
77+
78+
79+
@code_warntype linfunc!(xnext, y, A, Bu, C, Bd, Dd, backend, x, u, d, cx, cu, cd)
80+
=#
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using ModelPredictiveControl, JuMP
2+
using Ipopt, UnoSolver
3+
4+
N = 35*10 # number of solves/optimize! calls
5+
6+
function f!(ẋ, x, u, _ , p)
7+
g, L, K, m = p # [m/s²], [m], [kg/s], [kg]
8+
θ, ω = x[1], x[2] # [rad], [rad/s]
9+
τ = u[1] # [Nm]
10+
ẋ[1] = ω
11+
ẋ[2] = -g/L*sin(θ) - K/m*ω + τ/m/L^2
12+
return nothing
13+
end
14+
p = [9.8, 0.4, 1.2, 0.3]
15+
nu, nx, ny, Ts = 1, 2, 1, 0.1
16+
17+
p_plant = copy(p)
18+
p_plant[3] = 1.25*p[3]
19+
20+
h(x, _ , _ ) = [180/π*x[1], x[2]]
21+
nu, nx, ny = 1, 2, 2
22+
model = NonLinModel(f!, h, Ts, nu, nx, ny; p=p)
23+
plant = NonLinModel(f!, h, Ts, nu, nx, ny; p=p_plant)
24+
25+
Hp, Hc, Mwt, Nwt = 20, 2, [0.5, 0], [2.5]
26+
α=0.01; σQ=[0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1]
27+
σQint_ym = zeros(0)
28+
29+
estim = UnscentedKalmanFilter(model; σQ, σR, nint_u, σQint_u, i_ym=[1])
30+
31+
umin, umax = [-1.5], [+1.5]
32+
transcription = MultipleShooting()
33+
oracle = true
34+
hessian = true
35+
optim = JuMP.Model(()->UnoSolver.Optimizer(preset="filtersqp"))
36+
37+
function gc!(LHS, Ue, Ŷe, _, p, ϵ)
38+
Pmax = p
39+
i_τ, i_ω = 1, 2
40+
for i in eachindex(LHS)
41+
τ, ω = Ue[i_τ], Ŷe[i_ω]
42+
P = τ*ω
43+
LHS[i] = P - Pmax - ϵ
44+
i_τ += 1
45+
i_ω += 2
46+
end
47+
return nothing
48+
end
49+
Cwt, Pmax, nc = 1e5, 3, Hp+1
50+
51+
nmpc = NonLinMPC(estim;
52+
Hp, Hc, Mwt, Nwt,
53+
#Cwt, gc!, nc, p=Pmax,
54+
transcription, oracle, hessian,
55+
optim
56+
)
57+
nmpc = setconstraint!(nmpc; umin, umax)
58+
unset_time_limit_sec(nmpc.optim)
59+
#unset_silent(nmpc.optim)
60+
sim!(nmpc, N, [180.0, 0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])
61+
@time sim!(nmpc, N, [180.0, 0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])
62+
@profview sim!(nmpc, N, [180.0, 0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])

examples/debug_SMC.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using ModelPredictiveControl
2+
using DifferentiationInterface, SparseConnectivityTracer, SparseMatrixColorings
3+
import ForwardDiff
4+
hessian = AutoSparse(
5+
AutoForwardDiff();
6+
sparsity_detector = TracerSparsityDetector(),
7+
coloring_algorithm = GreedyColoringAlgorithm(; postprocessing=true)
8+
)
9+
f(x,u,_,_) = 0.5*x + 0.5*u
10+
h(x,_,_) = x
11+
model = NonLinModel(f, h, 10.0, 1, 1, 1, solver=nothing)
12+
nmpc = NonLinMPC(model; Hp=5, direct=false, hessian)
13+
moveinput!(nmpc)

examples/di.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using ModelPredictiveControl, ControlSystemsBase
2+
3+
Ts = 4.0
4+
A = [ 0.800737 0.0 0.0 0.0
5+
0.0 0.606531 0.0 0.0
6+
0.0 0.0 0.8 0.0
7+
0.0 0.0 0.0 0.6 ]
8+
Bu = [ 0.378599 0.378599
9+
-0.291167 0.291167
10+
0.0 0.0
11+
0.0 0.0 ]
12+
Bd = [ 0; 0; 0.5; 0.5;; ]
13+
C = [ 1.0 0.0 0.684 0.0
14+
0.0 1.0 0.0 -0.4736 ]
15+
Dd = [ 0.19; -0.148;; ]
16+
Du = zeros(2,2)
17+
model = LinModel(ss(A,[Bu Bd],C,[Du Dd],Ts),Ts,i_d=[3])
18+
model = setop!(model, uop=[10,10], yop=[50,30], dop=[5])

examples/ekf.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using ModelPredictiveControl, ControlSystemsBase
2+
using JuMP, Ipopt, MadNLP
3+
using BenchmarkTools
4+
5+
Ts = 4.0
6+
A = [ 0.800737 0.0 0.0 0.0
7+
0.0 0.606531 0.0 0.0
8+
0.0 0.0 0.8 0.0
9+
0.0 0.0 0.0 0.6 ]
10+
Bu = [ 0.378599 0.378599
11+
-0.291167 0.291167
12+
0.0 0.0
13+
0.0 0.0 ]
14+
Bd = [ 0; 0; 0.5; 0.5;; ]
15+
C = [ 1.0 0.0 0.684 0.0
16+
0.0 1.0 0.0 -0.4736 ]
17+
Dd = [ 0.19; -0.148;; ]
18+
Du = zeros(2,2)
19+
#model = LinModel(ss(A,[Bu Bd],C,[Du Dd],Ts),Ts,i_d=[3])
20+
#model = setop!(model, uop=[10,10], yop=[50,30], dop=[5])
21+
#mpc = LinMPC(model, transcription=MultipleShooting())#, Cwt=Inf)
22+
#mpc = setconstraint!(mpc, ymax=[Inf,30.5])
23+
24+
using LinearAlgebra
25+
26+
function f!(xnext, x, u, d, p)
27+
mul!(xnext, p.A, x)
28+
mul!(xnext, p.Bu, u, 1, 1)
29+
mul!(xnext, p.Bd, d, 1, 1)
30+
return nothing
31+
end
32+
33+
function h!(y, x, d, p)
34+
mul!(y, p.C, x)
35+
mul!(y, p.Dd, d, 1, 1)
36+
return nothing
37+
end
38+
39+
model = NonLinModel(f!, h!, Ts, 2, 4, 2, 1, solver=nothing, p=(;A,Bu,Bd,C,Dd))
40+
model = setop!(model, uop=[10,10], yop=[50,30], dop=[5])
41+
42+
ekf = ExtendedKalmanFilter(model, nint_u=[1, 1])
43+
44+
y = [50.0, 30.0]
45+
u = [10.0, 10.0]
46+
d = [5.0]
47+
preparestate!(ekf, y, d)
48+
updatestate!(ekf, u, y, d)
49+
50+
@benchmark (preparestate!($ekf, $y, $d); updatestate!($ekf, $u, $y, $d))

0 commit comments

Comments
 (0)