Skip to content

Commit c4ca823

Browse files
committed
debug: ineq. constraint with NonLinModel and MultipleShooting
I forgot that I need a new dispatch in `con_nonlinprog!`, since the terminal constraints are linear for `NonLinModel` thus they do not appear there.
1 parent b84d375 commit c4ca823

File tree

2 files changed

+85
-48
lines changed

2 files changed

+85
-48
lines changed

src/controller/nonlinmpc.jl

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
582582
Ŷ0, x̂0end = predict!(Ŷ0, x̂0end, X̂0, Û0, mpc, model, transcription, U0, Z̃)
583583
Ue, Ŷe = extended_vectors!(Ue, Ŷe, mpc, U0, Ŷ0)
584584
gc = con_custom!(gc, mpc, Ue, Ŷe, ϵ)
585-
g = con_nonlinprog!(g, mpc, model, x̂0end, Ŷ0, gc, ϵ)
585+
g = con_nonlinprog!(g, mpc, model, transcription, x̂0end, Ŷ0, gc, ϵ)
586586
geq = con_nonlinprogeq!(geq, X̂0, Û0, mpc, model, transcription, U0, Z̃)
587587
end
588588
return nothing
@@ -692,53 +692,6 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
692692
return Jfunc, ∇Jfunc!, gfuncs, ∇gfuncs!, geqfuncs, ∇geqfuncs!
693693
end
694694

695-
"""
696-
con_nonlinprog!(g, mpc::NonLinMPC, model::LinModel, _ , _ , gc, ϵ) -> g
697-
698-
Nonlinear constrains for [`NonLinMPC`](@ref) when `model` is a [`LinModel`](@ref).
699-
700-
The method mutates the `g` vectors in argument and returns it. Only the custom constraints
701-
are include in the `g` vector.
702-
"""
703-
function con_nonlinprog!(g, mpc::NonLinMPC, ::LinModel, _ , _ , gc, ϵ)
704-
for i in eachindex(g)
705-
g[i] = gc[i]
706-
end
707-
return g
708-
end
709-
710-
"""
711-
con_nonlinprog!(g, mpc::NonLinMPC, model::SimModel, x̂0end, Ŷ0, gc, ϵ) -> g
712-
713-
Nonlinear constrains for [`NonLinMPC`](@ref) when `model` is not a [`LinModel`](@ref).
714-
715-
The method mutates the `g` vectors in argument and returns it. The output prediction,
716-
the terminal state and the custom constraints are include in the `g` vector.
717-
"""
718-
function con_nonlinprog!(g, mpc::NonLinMPC, ::SimModel, x̂0end, Ŷ0, gc, ϵ)
719-
nx̂, nŶ = length(x̂0end), length(Ŷ0)
720-
for i in eachindex(g)
721-
mpc.con.i_g[i] || continue
722-
if i nŶ
723-
j = i
724-
g[i] = (mpc.con.Y0min[j] - Ŷ0[j]) - ϵ*mpc.con.C_ymin[j]
725-
elseif i 2nŶ
726-
j = i - nŶ
727-
g[i] = (Ŷ0[j] - mpc.con.Y0max[j]) - ϵ*mpc.con.C_ymax[j]
728-
elseif i 2nŶ + nx̂
729-
j = i - 2nŶ
730-
g[i] = (mpc.con.x̂0min[j] - x̂0end[j]) - ϵ*mpc.con.c_x̂min[j]
731-
elseif i 2nŶ + 2nx̂
732-
j = i - 2nŶ - nx̂
733-
g[i] = (x̂0end[j] - mpc.con.x̂0max[j]) - ϵ*mpc.con.c_x̂max[j]
734-
else
735-
j = i - 2nŶ - 2nx̂
736-
g[i] = gc[j]
737-
end
738-
end
739-
return g
740-
end
741-
742695
@doc raw"""
743696
con_custom!(gc, mpc::NonLinMPC, Ue, Ŷe, ϵ) -> gc
744697

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Ŷ - 2nx̂
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!(

0 commit comments

Comments
 (0)