Skip to content

Commit 5820512

Browse files
committed
improved code reuse in NonLinMP optim functions
1 parent b19fd99 commit 5820512

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/controller/nonlinmpc.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -513,42 +513,38 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
513513
û0_cache::DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache(zeros(JNT, nu), Ncache)
514514
g_cache::DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache(zeros(JNT, ng), Ncache)
515515
gc_cache::DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache(zeros(JNT, nc), Ncache)
516-
function Jfunc(ΔŨtup::T...) where T<:Real
517-
ΔŨ1 = ΔŨtup[begin]
518-
ΔŨ, g = get_tmp(ΔŨ_cache, ΔŨ1), get_tmp(g_cache, ΔŨ1)
519-
for i in eachindex(ΔŨtup)
520-
ΔŨ[i] = ΔŨtup[i] # ΔŨ .= ΔŨtup seems to produce a type instability
521-
end
522-
Ŷe, Ue = get_tmp(Ŷe_cache, ΔŨ1), get_tmp(Ue_cache, ΔŨ1)
523-
Ȳ, Ū = get_tmp(Ȳ_cache, ΔŨ1), get_tmp(Ū_cache, ΔŨ1)
524-
x̂0, x̂0next = get_tmp(x̂0_cache, ΔŨ1), get_tmp(x̂0next_cache, ΔŨ1)
525-
u0, û0 = get_tmp(u0_cache, ΔŨ1), get_tmp(û0_cache, ΔŨ1)
526-
gc = get_tmp(gc_cache, ΔŨ1)
527-
Ŷ0, x̂0end = predict!(Ȳ, x̂0, x̂0next, u0, û0, mpc, model, ΔŨ)
528-
Ue, Ŷe = extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
529-
ϵ = (nϵ 0) ? ΔŨ[end] : zero(T) # ϵ = 0 if nϵ == 0 (meaning no relaxation)
530-
gc = con_custom!(gc, mpc, Ue, Ŷe, ϵ)
531-
g = con_nonlinprog!(g, mpc, model, x̂0end, Ŷ0, gc, ϵ)
532-
return obj_nonlinprog!(Ȳ, Ū, mpc, model, Ue, Ŷe, ΔŨ)::T
533-
end
534-
function gfunc_i(i, ΔŨtup::NTuple{N, T}) where {N, T<:Real}
535-
ΔŨ1 = ΔŨtup[begin]
536-
ΔŨ, g = get_tmp(ΔŨ_cache, ΔŨ1), get_tmp(g_cache, ΔŨ1)
516+
function update_simulations!(ΔŨ, ΔŨtup::NTuple{N, T}) where {N, T<:Real}
537517
if any(new !== old for (new, old) in zip(ΔŨtup, ΔŨ)) # new ΔŨtup, update predictions:
518+
ΔŨ1 = ΔŨtup[begin]
538519
for i in eachindex(ΔŨtup)
539520
ΔŨ[i] = ΔŨtup[i] # ΔŨ .= ΔŨtup seems to produce a type instability
540521
end
541522
Ŷe, Ue = get_tmp(Ŷe_cache, ΔŨ1), get_tmp(Ue_cache, ΔŨ1)
542523
Ȳ, Ū = get_tmp(Ȳ_cache, ΔŨ1), get_tmp(Ū_cache, ΔŨ1)
543524
x̂0, x̂0next = get_tmp(x̂0_cache, ΔŨ1), get_tmp(x̂0next_cache, ΔŨ1)
544525
u0, û0 = get_tmp(u0_cache, ΔŨ1), get_tmp(û0_cache, ΔŨ1)
545-
gc = get_tmp(gc_cache, ΔŨ1)
526+
gc, g = get_tmp(gc_cache, ΔŨ1), get_tmp(g_cache, ΔŨ1)
546527
Ŷ0, x̂0end = predict!(Ȳ, x̂0, x̂0next, u0, û0, mpc, model, ΔŨ)
547528
Ue, Ŷe = extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
548529
ϵ = (nϵ 0) ? ΔŨ[end] : zero(T) # ϵ = 0 if nϵ == 0 (meaning no relaxation)
549530
gc = con_custom!(gc, mpc, Ue, Ŷe, ϵ)
550531
g = con_nonlinprog!(g, mpc, model, x̂0end, Ŷ0, gc, ϵ)
551532
end
533+
return nothing
534+
end
535+
function Jfunc(ΔŨtup::T...) where T<:Real
536+
ΔŨ1 = ΔŨtup[begin]
537+
ΔŨ = get_tmp(ΔŨ_cache, ΔŨ1)
538+
update_simulations!(ΔŨ, ΔŨtup)
539+
Ȳ, Ū = get_tmp(Ȳ_cache, ΔŨ1), get_tmp(Ū_cache, ΔŨ1)
540+
Ŷe, Ue = get_tmp(Ŷe_cache, ΔŨ1), get_tmp(Ue_cache, ΔŨ1)
541+
return obj_nonlinprog!(Ȳ, Ū, mpc, model, Ue, Ŷe, ΔŨ)::T
542+
end
543+
function gfunc_i(i, ΔŨtup::NTuple{N, T}) where {N, T<:Real}
544+
ΔŨ1 = ΔŨtup[begin]
545+
ΔŨ = get_tmp(ΔŨ_cache, ΔŨ1)
546+
update_simulations!(ΔŨ, ΔŨtup)
547+
g = get_tmp(g_cache, ΔŨ1)
552548
return g[i]::T
553549
end
554550
gfunc = [(ΔŨ...) -> gfunc_i(i, ΔŨ) for i in 1:ng]

0 commit comments

Comments
 (0)