Skip to content

Commit 341f107

Browse files
committed
added: improve code reuse in MovingHorizonEstimator objective functions
1 parent 5820512 commit 341f107

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/estimator/mhe/construct.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,33 +1309,33 @@ function get_optim_functions(
13091309
x̄_cache::DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache(zeros(JNT, nx̂), Nc)
13101310
û0_cache::DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache(zeros(JNT, nu), Nc)
13111311
ŷ0_cache::DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache(zeros(JNT, nŷ), Nc)
1312-
function Jfunc(Z̃tup::T...)::T where {T <: Real}
1313-
Z̃1 = Z̃tup[begin]
1314-
Z̃, g = get_tmp(Z̃_cache, Z̃1), get_tmp(g_cache, Z̃1)
1315-
for i in eachindex(Z̃tup)
1316-
Z̃[i] = Z̃tup[i] # Z̃ .= Z̃tup seems to produce a type instability
1317-
end
1318-
V̂, X̂0 = get_tmp(V̂_cache, Z̃1), get_tmp(X̂0_cache, Z̃1)
1319-
û0, ŷ0 = get_tmp(û0_cache, Z̃1), get_tmp(ŷ0_cache, Z̃1)
1320-
V̂, X̂0 = predict!(V̂, X̂0, û0, ŷ0, estim, model, Z̃)
1321-
ϵ = (nϵ 0) ? Z̃[begin] : zero(T) # ϵ = 0 if Cwt=Inf (meaning: no relaxation)
1322-
g = con_nonlinprog!(g, estim, model, X̂0, V̂, ϵ)
1323-
= get_tmp(x̄_cache, Z̃1)
1324-
return obj_nonlinprog!(x̄, estim, model, V̂, Z̃)::T
1325-
end
1326-
function gfunc_i(i, Z̃tup::NTuple{N, T})::T where {N, T <:Real}
1327-
Z̃1 = Z̃tup[begin]
1328-
Z̃, g = get_tmp(Z̃_cache, Z̃1), get_tmp(g_cache, Z̃1)
1312+
function update_simulations!(Z̃, Z̃tup::NTuple{N, T}) where {N, T <:Real}
13291313
if any(new !== old for (new, old) in zip(Z̃tup, Z̃)) # new Z̃tup, update predictions:
1330-
V̂, X̂0 = get_tmp(V̂_cache, Z̃1), get_tmp(X̂0_cache, Z̃1)
1331-
û0, ŷ0 = get_tmp(û0_cache, Z̃1), get_tmp(ŷ0_cache, Z̃1)
1314+
Z̃1 = Z̃tup[begin]
13321315
for i in eachindex(Z̃tup)
13331316
Z̃[i] = Z̃tup[i] # Z̃ .= Z̃tup seems to produce a type instability
13341317
end
1335-
V̂, X̂0 = predict!(V̂, X̂0, û0, ŷ0, estim, model, Z̃)
1318+
V̂, X̂0 = get_tmp(V̂_cache, Z̃1), get_tmp(X̂0_cache, Z̃1)
1319+
û0, ŷ0 = get_tmp(û0_cache, Z̃1), get_tmp(ŷ0_cache, Z̃1)
1320+
g = get_tmp(g_cache, Z̃1)
1321+
V̂, X̂0 = predict!(V̂, X̂0, û0, ŷ0, estim, model, Z̃)
13361322
ϵ = (nϵ 0) ? Z̃[begin] : zero(T) # ϵ = 0 if Cwt=Inf (meaning: no relaxation)
13371323
g = con_nonlinprog!(g, estim, model, X̂0, V̂, ϵ)
13381324
end
1325+
return nothing
1326+
end
1327+
function Jfunc(Z̃tup::T...)::T where {T <: Real}
1328+
Z̃1 = Z̃tup[begin]
1329+
= get_tmp(Z̃_cache, Z̃1)
1330+
update_simulations!(Z̃, Z̃tup)
1331+
x̄, V̂ = get_tmp(x̄_cache, Z̃1), get_tmp(V̂_cache, Z̃1)
1332+
return obj_nonlinprog!(x̄, estim, model, V̂, Z̃)::T
1333+
end
1334+
function gfunc_i(i, Z̃tup::NTuple{N, T})::T where {N, T <:Real}
1335+
Z̃1 = Z̃tup[begin]
1336+
= get_tmp(Z̃_cache, Z̃1)
1337+
update_simulations!(Z̃, Z̃tup)
1338+
g = get_tmp(g_cache, Z̃1)
13391339
return g[i]
13401340
end
13411341
gfunc = [(Z̃...) -> gfunc_i(i, Z̃) for i in 1:ng]

0 commit comments

Comments
 (0)