Skip to content

Commit c217d01

Browse files
committed
changed: same cleanup in MovingHorizonEstimator
1 parent ed078b6 commit c217d01

3 files changed

Lines changed: 17 additions & 28 deletions

File tree

‎src/ModelPredictiveControl.jl‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@ include("plot_sim.jl")
6666

6767
include("precompile.jl")
6868

69-
7069
end

‎src/controller/nonlinmpc.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,8 @@ It is based on the splatting syntax. This method is really intricate and that's
831831
- The splatting syntax is required for [`add_nonlinear_operator`](@extref JuMP JuMP.add_nonlinear_operator)
832832
but automatic differentiation tools do not support splatting.
833833
- The internal of `MOI.jl` calls the 3 functions of `add_nonlinear_operator` with a runtime
834-
dispatch. It is thus a waste of time and resources to compile (a.k.a specialize) these
835-
functions. The `NoTypeFunction` wrapper hide their concrete type to disable compilation.
834+
dispatch. It is thus a waste of time and resources to compile (a.k.a specialize) these 3
835+
closures. The `NoTypeFunction` wrapper hide their concrete type to disable compilation.
836836
- The signature of gradient and hessian functions is not the same for univariate (`nZ̃ == 1`)
837837
and multivariate (`nZ̃ > 1`) operators in `JuMP`. Both must be defined.
838838
"""

‎src/estimator/mhe/construct.jl‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,9 +1445,11 @@ It is based on the splatting syntax. This method is really intricate and that's
14451445
- These functions are used inside the nonlinear optimization, so they must be type-stable
14461446
and as efficient as possible. All the function outputs and derivatives are cached and
14471447
updated in-place if required to use the efficient [`value_and_gradient!`](@extref DifferentiationInterface DifferentiationInterface.value_and_jacobian!).
1448-
- The splatting syntax for objective functions implies the use of `Vararg{T,N}` (see the [performance tip](@extref Julia Be-aware-of-when-Julia-avoids-specializing))
1449-
and memoization to avoid redundant computations. This is already complex, but it's even
1450-
worse knowing that the automatic differentiation tools do not support splatting.
1448+
- The splatting syntax is required for [`add_nonlinear_operator`](@extref JuMP JuMP.add_nonlinear_operator)
1449+
but automatic differentiation tools do not support splatting.
1450+
- The internal of `MOI.jl` calls the 3 functions of `add_nonlinear_operator` with a runtime
1451+
dispatch. It is thus a waste of time and resources to compile (a.k.a specialize) these 3
1452+
closures. The `NoTypeFunction` wrapper hide their concrete type to disable compilation.
14511453
"""
14521454
function get_nonlinobj_op(
14531455
estim::MovingHorizonEstimator, optim::JuMP.GenericModel{JNT}
@@ -1524,33 +1526,21 @@ function get_nonlinobj_op(
15241526
end
15251527
end
15261528
J_func = if !isnothing(hess)
1527-
function (Z̃_arg::Vararg{T, N}) where {N, T<:Real}
1528-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
1529-
return J[]::T
1530-
end
1529+
(Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); J[])
15311530
else
1532-
function (Z̃_arg::Vararg{T, N}) where {N, T<:Real}
1533-
update_objective!(J, ∇J, Z̃_J, Z̃_arg)
1534-
return J[]::T
1535-
end
1531+
(Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); J[])
15361532
end
1537-
# only the multivariate syntax of JuMP.@operator, univariate is impossible for MHE
1538-
# since Z̃ comprises the arrival state estimate AND the estimated process noise:
1533+
# only the multivariate syntax of JuMP.add_nonlinear_operator, univariate is impossible
1534+
# for MHE since Z̃ comprises the arrival state estimate AND the estimated process noise:
15391535
∇J_func! = if !isnothing(hess)
1540-
function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real}
1541-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
1542-
return ∇J_arg .= ∇J
1543-
end
1536+
(∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J)
15441537
else
1545-
function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real}
1546-
update_objective!(J, ∇J, Z̃_J, Z̃_arg)
1547-
return ∇J_arg .= ∇J
1548-
end
1549-
end
1550-
function ∇²J_func!(∇²J_arg::AbstractMatrix{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real}
1551-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
1552-
return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure)
1538+
(∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J)
15531539
end
1540+
∇²J_func! = (∇²J_arg, Z̃_arg...) -> (
1541+
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg);
1542+
fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure)
1543+
)
15541544
J_op = if !isnothing(hess)
15551545
JuMP.add_nonlinear_operator(
15561546
optim, nZ̃,

0 commit comments

Comments
 (0)