Skip to content

Commit 9268403

Browse files
committed
debug: do not call @constraint when no nonlinear constraints
1 parent 82b70f9 commit 9268403

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

src/controller/nonlinmpc.jl

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -729,69 +729,69 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
729729
end
730730

731731
# TODO: move docstring of method above here an re-work it
732-
function get_nonlinops(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where JNT<:Real
732+
function get_nonlinops(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT<:Real
733733
# ----------- common cache for all functions ----------------------------------------
734734
model = mpc.estim.model
735735
transcription = mpc.transcription
736736
grad, jac = mpc.gradient, mpc.jacobian
737737
nu, ny, nx̂, nϵ = model.nu, model.ny, mpc.estim.nx̂, mpc.
738738
nk = get_nk(model, transcription)
739739
Hp, Hc = mpc.Hp, mpc.Hc
740-
ng, ng_i_g = length(mpc.con.i_g), sum(mpc.con.i_g)
740+
i_g = findall(mpc.con.i_g) # convert to non-logical indices for non-allocating @views
741+
ng, ngi = length(mpc.con.i_g), sum(mpc.con.i_g)
741742
nc, neq = mpc.con.nc, mpc.con.neq
742743
nZ̃, nU, nŶ, nX̂, nK = length(mpc.Z̃), Hp*nu, Hp*ny, Hp*nx̂, Hp*nk
743744
nΔŨ, nUe, nŶe = nu*Hc + nϵ, nU + nu, nŶ + ny
744745
strict = Val(true)
745-
myNaN, myInf = convert(JNT, NaN), convert(JNT, Inf)
746-
J::Vector{JNT} = zeros(JNT, 1)
747-
ΔŨ::Vector{JNT} = zeros(JNT, nΔŨ)
748-
x̂0end::Vector{JNT} = zeros(JNT, nx̂)
749-
K0::Vector{JNT} = zeros(JNT, nK)
750-
Ue::Vector{JNT}, Ŷe::Vector{JNT} = zeros(JNT, nUe), zeros(JNT, nŶe)
751-
U0::Vector{JNT}, Ŷ0::Vector{JNT} = zeros(JNT, nU), zeros(JNT, nŶ)
752-
Û0::Vector{JNT}, X̂0::Vector{JNT} = zeros(JNT, nU), zeros(JNT, nX̂)
753-
gc::Vector{JNT}, g::Vector{JNT} = zeros(JNT, nc), zeros(JNT, ng)
754-
g_i_g::Vector{JNT} = zeros(JNT, ng_i_g)
755-
geq::Vector{JNT} = zeros(JNT, neq)
746+
myNaN, myInf = convert(JNT, NaN), convert(JNT, Inf)
747+
J::Vector{JNT} = zeros(JNT, 1)
748+
ΔŨ::Vector{JNT} = zeros(JNT, nΔŨ)
749+
x̂0end::Vector{JNT} = zeros(JNT, nx̂)
750+
K0::Vector{JNT} = zeros(JNT, nK)
751+
Ue::Vector{JNT}, Ŷe::Vector{JNT} = zeros(JNT, nUe), zeros(JNT, nŶe)
752+
U0::Vector{JNT}, Ŷ0::Vector{JNT} = zeros(JNT, nU), zeros(JNT, nŶ)
753+
Û0::Vector{JNT}, X̂0::Vector{JNT} = zeros(JNT, nU), zeros(JNT, nX̂)
754+
gc::Vector{JNT}, g::Vector{JNT} = zeros(JNT, nc), zeros(JNT, ng)
755+
gi::Vector{JNT}, geq::Vector{JNT} = zeros(JNT, ngi), zeros(JNT, neq)
756756
# -------------- inequality constraint: nonlinear oracle -----------------------------
757-
function g_i_g!(g_i_g, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, geq, g)
757+
function gi!(gi, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, geq, g)
758758
update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
759-
g_i_g .= @views g[mpc.con.i_g]
759+
gi .= @views g[i_g]
760760
return nothing
761761
end
762-
Z̃_∇g = fill(myNaN, nZ̃) # NaN to force update_predictions! at first call
763-
g_context = (
762+
Z̃_∇gi = fill(myNaN, nZ̃) # NaN to force update_predictions! at first call
763+
gi_context = (
764764
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
765765
Cache(Û0), Cache(K0), Cache(X̂0),
766766
Cache(gc), Cache(geq), Cache(g)
767767
)
768-
g_prep = prepare_jacobian(g_i_g!, g_i_g, jac, Z̃_∇g, ∇g_context...; strict)
769-
g_i_g = init_diffmat(JNT, jac, ∇g_prep, nZ̃, ng)
770-
function update_con!(g_i_g, ∇g_i_g, Z̃_∇g, Z̃_arg)
771-
if isdifferent(Z̃_arg, Z̃_∇g)
772-
Z̃_∇g .= Z̃_arg
773-
value_and_jacobian!(g_i_g!, g_i_g, ∇g_i_g, ∇g_prep, jac, Z̃_∇g, ∇g_context...)
768+
gi_prep = prepare_jacobian(gi!, gi, jac, Z̃_∇gi, ∇gi_context...; strict)
769+
gi = init_diffmat(JNT, jac, ∇gi_prep, nZ̃, ng)
770+
function update_con!(gi, ∇gi, Z̃_∇gi, Z̃_arg)
771+
if isdifferent(Z̃_arg, Z̃_∇gi)
772+
Z̃_∇gi .= Z̃_arg
773+
value_and_jacobian!(gi!, gi, ∇gi, ∇gi_prep, jac, Z̃_∇gi, ∇gi_context...)
774774
end
775775
return nothing
776776
end
777-
function gfunc_oracle!(g_vec, Z̃_arg)
778-
update_con!(g_i_g, ∇g_i_g, Z̃_∇g, Z̃_arg)
779-
return g_vec .= g_i_g
777+
function gi_func!(gi_vec, Z̃_arg)
778+
update_con!(gi, ∇gi, Z̃_∇gi, Z̃_arg)
779+
return gi_vec .= gi
780780
end
781-
function gfunc_oracle!(∇g_vec, Z̃_arg)
782-
update_con!(g_i_g, ∇g_i_g, Z̃_∇g, Z̃_arg)
783-
return diffmat2vec!(∇g_vec, ∇g_i_g)
781+
function gi_func!(∇gi_vec, Z̃_arg)
782+
update_con!(gi, ∇gi, Z̃_∇gi, Z̃_arg)
783+
return diffmat2vec!(∇gi_vec, ∇gi)
784784
end
785-
g_min = fill(-myInf, ng_i_g)
786-
g_max = zeros(JNT, ng_i_g)
787-
g_structure = init_diffstructure(∇g_i_g)
785+
gi_min = fill(-myInf, ngi)
786+
gi_max = zeros(JNT, ngi)
787+
gi_structure = init_diffstructure(∇gi)
788788
g_oracle = Ipopt._VectorNonlinearOracle(;
789789
dimension = nZ̃,
790-
l = g_min,
791-
u = g_max,
792-
eval_f = gfunc_oracle!,
793-
jacobian_structure =g_structure,
794-
eval_jacobian =gfunc_oracle!
790+
l = gi_min,
791+
u = gi_max,
792+
eval_f = gi_func!,
793+
jacobian_structure =gi_structure,
794+
eval_jacobian =gi_func!
795795
)
796796
# ------------- equality constraints : nonlinear oracle ------------------------------
797797
function geq!(geq, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g)
@@ -813,11 +813,11 @@ function get_nonlinops(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where JNT<
813813
end
814814
return nothing
815815
end
816-
function geq_oracle!(geq_vec, Z̃_arg)
816+
function geq_func!(geq_vec, Z̃_arg)
817817
update_con_eq!(geq, ∇geq, Z̃_∇geq, Z̃_arg)
818818
return geq_vec .= geq
819819
end
820-
function geq_oracle!(∇geq_vec, Z̃_arg)
820+
function geq_func!(∇geq_vec, Z̃_arg)
821821
update_con_eq!(geq, ∇geq, Z̃_∇geq, Z̃_arg)
822822
return diffmat2vec!(∇geq_vec, ∇geq)
823823
end
@@ -827,9 +827,9 @@ function get_nonlinops(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where JNT<
827827
dimension = nZ̃,
828828
l = geq_min,
829829
u = geq_max,
830-
eval_f = geq_oracle!,
830+
eval_f = geq_func!,
831831
jacobian_structure = ∇geq_structure,
832-
eval_jacobian =geq_oracle!
832+
eval_jacobian =geq_func!
833833
)
834834
# ------------- objective function: splatting syntax ---------------------------------
835835
function J!(Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq)
@@ -903,8 +903,8 @@ function set_nonlincon_exp!(
903903
optim, JuMP.Vector{JuMP.VariableRef}, Ipopt._VectorNonlinearOracle
904904
)
905905
map(con_ref -> JuMP.delete(optim, con_ref), nonlin_constraints)
906-
@constraint(optim, Z̃var in g_oracle)
907-
mpc.con.neq > 0 && @constraint(optim, Z̃var in geq_oracle)
906+
any(mpc.con.i_g) && @constraint(optim, Z̃var in g_oracle)
907+
mpc.con.neq > 0 && @constraint(optim, Z̃var in geq_oracle)
908908
return nothing
909909
end
910910

0 commit comments

Comments
 (0)