Skip to content

Commit cff734a

Browse files
committed
Merge branch 'debug_ms_terminal_2' into diff_interface_final
2 parents 708456b + 1a3992a commit cff734a

File tree

4 files changed

+105
-53
lines changed

4 files changed

+105
-53
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
33
authors = ["Francis Gagnon"]
4-
version = "1.4.3"
4+
version = "1.4.4"
55

66
[deps]
77
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"

src/controller/nonlinmpc.jl

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ function get_optim_functions(
595595
Ue, Ŷe = extended_vectors!(Ue, Ŷe, mpc, U0, Ŷ0)
596596
ϵ = getϵ(mpc, Z̃)
597597
gc = con_custom!(gc, mpc, Ue, Ŷe, ϵ)
598-
g = con_nonlinprog!(g, mpc, model, x̂0end, Ŷ0, gc, ϵ)
598+
g = con_nonlinprog!(g, mpc, model, transcription, x̂0end, Ŷ0, gc, ϵ)
599599
geq = con_nonlinprogeq!(geq, X̂0, Û0, mpc, model, transcription, U0, Z̃)
600600
end
601601
return nothing
@@ -739,53 +739,6 @@ function get_optim_functions(
739739
return Jfunc, ∇Jfunc!, gfuncs, ∇gfuncs!, geqfuncs, ∇geqfuncs!
740740
end
741741

742-
"""
743-
con_nonlinprog!(g, mpc::NonLinMPC, model::LinModel, _ , _ , gc, ϵ) -> g
744-
745-
Nonlinear constrains for [`NonLinMPC`](@ref) when `model` is a [`LinModel`](@ref).
746-
747-
The method mutates the `g` vectors in argument and returns it. Only the custom constraints
748-
are include in the `g` vector.
749-
"""
750-
function con_nonlinprog!(g, mpc::NonLinMPC, ::LinModel, _ , _ , gc, ϵ)
751-
for i in eachindex(g)
752-
g[i] = gc[i]
753-
end
754-
return g
755-
end
756-
757-
"""
758-
con_nonlinprog!(g, mpc::NonLinMPC, model::SimModel, x̂0end, Ŷ0, gc, ϵ) -> g
759-
760-
Nonlinear constrains for [`NonLinMPC`](@ref) when `model` is not a [`LinModel`](@ref).
761-
762-
The method mutates the `g` vectors in argument and returns it. The output prediction,
763-
the terminal state and the custom constraints are include in the `g` vector.
764-
"""
765-
function con_nonlinprog!(g, mpc::NonLinMPC, ::SimModel, x̂0end, Ŷ0, gc, ϵ)
766-
nx̂, nŶ = length(x̂0end), length(Ŷ0)
767-
for i in eachindex(g)
768-
mpc.con.i_g[i] || continue
769-
if i nŶ
770-
j = i
771-
g[i] = (mpc.con.Y0min[j] - Ŷ0[j]) - ϵ*mpc.con.C_ymin[j]
772-
elseif i 2nŶ
773-
j = i - nŶ
774-
g[i] = (Ŷ0[j] - mpc.con.Y0max[j]) - ϵ*mpc.con.C_ymax[j]
775-
elseif i 2nŶ + nx̂
776-
j = i - 2nŶ
777-
g[i] = (mpc.con.x̂0min[j] - x̂0end[j]) - ϵ*mpc.con.c_x̂min[j]
778-
elseif i 2nŶ + 2nx̂
779-
j = i - 2nŶ - nx̂
780-
g[i] = (x̂0end[j] - mpc.con.x̂0max[j]) - ϵ*mpc.con.c_x̂max[j]
781-
else
782-
j = i - 2nŶ - 2nx̂
783-
g[i] = gc[j]
784-
end
785-
end
786-
return g
787-
end
788-
789742
@doc raw"""
790743
con_custom!(gc, mpc::NonLinMPC, Ue, Ŷe, ϵ) -> gc
791744

src/controller/transcription.jl

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,90 @@ function predict!(
11161116
return Ŷ0, x̂0end
11171117
end
11181118

1119+
"""
1120+
con_nonlinprog!(
1121+
g, mpc::PredictiveController, model::LinModel, ::TranscriptionMethod, _ , _ , gc, ϵ
1122+
) -> g
1123+
1124+
Nonlinear constrains when `model` is a [`LinModel`](@ref).
1125+
1126+
The method mutates the `g` vectors in argument and returns it. Only the custom constraints
1127+
are include in the `g` vector.
1128+
"""
1129+
function con_nonlinprog!(
1130+
g, mpc::PredictiveController, ::LinModel, ::TranscriptionMethod, _ , _ , gc, ϵ
1131+
)
1132+
for i in eachindex(g)
1133+
g[i] = gc[i]
1134+
end
1135+
return g
1136+
end
1137+
1138+
"""
1139+
con_nonlinprog!(
1140+
g, mpc::PredictiveController, model::NonLinModel, ::TranscriptionMethod, x̂0end, Ŷ0, gc, ϵ
1141+
) -> g
1142+
1143+
Nonlinear constrains when `model` is a [`NonLinModel`](@ref) with non-[`SingleShooting`](@ref).
1144+
1145+
The method mutates the `g` vectors in argument and returns it. The output prediction and the
1146+
custom constraints are include in the `g` vector.
1147+
"""
1148+
function con_nonlinprog!(
1149+
g, mpc::PredictiveController, ::NonLinModel, ::TranscriptionMethod, x̂0end, Ŷ0, gc, ϵ
1150+
)
1151+
nx̂, nŶ = length(x̂0end), length(Ŷ0)
1152+
for i in eachindex(g)
1153+
mpc.con.i_g[i] || continue
1154+
if i nŶ
1155+
j = i
1156+
g[i] = (mpc.con.Y0min[j] - Ŷ0[j]) - ϵ*mpc.con.C_ymin[j]
1157+
elseif i 2nŶ
1158+
j = i - nŶ
1159+
g[i] = (Ŷ0[j] - mpc.con.Y0max[j]) - ϵ*mpc.con.C_ymax[j]
1160+
else
1161+
j = i - 2nŶ
1162+
g[i] = gc[j]
1163+
end
1164+
end
1165+
return g
1166+
end
1167+
1168+
"""
1169+
con_nonlinprog!(
1170+
g, mpc::PredictiveController, model::NonLinModel, ::SingleShooting, x̂0end, Ŷ0, gc, ϵ
1171+
) -> g
1172+
1173+
Nonlinear constrains when `model` is [`NonLinModel`](@ref) with [`SingleShooting`](@ref).
1174+
1175+
The method mutates the `g` vectors in argument and returns it. The output prediction,
1176+
the terminal state and the custom constraints are include in the `g` vector.
1177+
"""
1178+
function con_nonlinprog!(
1179+
g, mpc::PredictiveController, ::NonLinModel, ::SingleShooting, x̂0end, Ŷ0, gc, ϵ
1180+
)
1181+
nx̂, nŶ = length(x̂0end), length(Ŷ0)
1182+
for i in eachindex(g)
1183+
mpc.con.i_g[i] || continue
1184+
if i nŶ
1185+
j = i
1186+
g[i] = (mpc.con.Y0min[j] - Ŷ0[j]) - ϵ*mpc.con.C_ymin[j]
1187+
elseif i 2nŶ
1188+
j = i - nŶ
1189+
g[i] = (Ŷ0[j] - mpc.con.Y0max[j]) - ϵ*mpc.con.C_ymax[j]
1190+
elseif i 2nŶ + nx̂
1191+
j = i - 2nŶ
1192+
g[i] = (mpc.con.x̂0min[j] - x̂0end[j]) - ϵ*mpc.con.c_x̂min[j]
1193+
elseif i 2nŶ + 2nx̂
1194+
j = i - 2nŶ - nx̂
1195+
g[i] = (x̂0end[j] - mpc.con.x̂0max[j]) - ϵ*mpc.con.c_x̂max[j]
1196+
else
1197+
j = i - 2nŶ - 2nx̂
1198+
g[i] = gc[j]
1199+
end
1200+
end
1201+
return g
1202+
end
11191203

11201204
"""
11211205
con_nonlinprogeq!(

test/3_test_predictive_control.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,8 @@ end
865865
Hp=50
866866

867867
linmodel = LinModel(tf([2], [10000, 1]), 3000.0)
868-
nmpc_lin = NonLinMPC(linmodel; Hp, Hc=5, gc=gc, nc=2Hp, p=[0; 0])
869-
868+
nmpc_lin = NonLinMPC(linmodel; Hp, Hc=5, gc, nc=2Hp, p=[0; 0])
869+
870870
setconstraint!(nmpc_lin, x̂min=[-1e6,-Inf], x̂max=[1e6,+Inf])
871871
setconstraint!(nmpc_lin, umin=[-10], umax=[10])
872872
setconstraint!(nmpc_lin, Δumin=[-1e6], Δumax=[1e6])
@@ -935,7 +935,7 @@ end
935935
f = (x,u,_,p) -> p.A*x + p.Bu*u
936936
h = (x,_,p) -> p.C*x
937937
nonlinmodel = NonLinModel(f, h, linmodel.Ts, 1, 1, 1, solver=nothing, p=linmodel)
938-
nmpc = NonLinMPC(nonlinmodel; Hp, Hc=5, gc=gc, nc=2Hp, p=[0; 0])
938+
nmpc = NonLinMPC(nonlinmodel; Hp, Hc=5, gc, nc=2Hp, p=[0; 0])
939939

940940
setconstraint!(nmpc, x̂min=[-1e6,-Inf], x̂max=[+1e6,+Inf])
941941
setconstraint!(nmpc, umin=[-1e6], umax=[+1e6])
@@ -1002,7 +1002,9 @@ end
10021002
@test all(isapprox.(info[:Ŷ], 3.14; atol=1e-1))
10031003
@test all(isapprox.(info[:gc][Hp+1:end], 0.0; atol=1e-1))
10041004

1005-
nmpc_ms = NonLinMPC(nonlinmodel; Hp, Hc=5, transcription=MultipleShooting())
1005+
nmpc_ms = NonLinMPC(
1006+
nonlinmodel; Hp, Hc=5, transcription=MultipleShooting(), gc, nc=2Hp, p=[0; 0]
1007+
)
10061008

10071009
preparestate!(nmpc_ms, [0])
10081010

@@ -1013,6 +1015,19 @@ end
10131015
moveinput!(nmpc_ms, [10])
10141016
info = getinfo(nmpc_ms)
10151017
@test info[:x̂end][1] 0 atol=1e-1
1018+
setconstraint!(nmpc_ms, x̂min=[-1e6,-Inf], x̂max=[1e6,+Inf])
1019+
1020+
nmpc_ms.p .= [1; 0]
1021+
moveinput!(nmpc_ms, [100])
1022+
info = getinfo(nmpc_ms)
1023+
@test all(isapprox.(info[:U], 4.2; atol=1e-1))
1024+
@test all(isapprox.(info[:gc][1:Hp], 0.0; atol=1e-1))
1025+
1026+
nmpc_ms.p .= [0; 1]
1027+
moveinput!(nmpc_ms, [100])
1028+
info = getinfo(nmpc_ms)
1029+
@test all(isapprox.(info[:Ŷ], 3.14; atol=1e-1))
1030+
@test all(isapprox.(info[:gc][Hp+1:end], 0.0; atol=1e-1))
10161031

10171032
end
10181033

0 commit comments

Comments
 (0)