Skip to content

Commit 9c6a596

Browse files
authored
Merge pull request #513 from JuliaControl/cleanup_obj_op
changed: cleaner and shorter code in `get_nonlinobj_op`
2 parents 9bb5cfe + c217d01 commit 9c6a596

3 files changed

Lines changed: 38 additions & 68 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: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -825,12 +825,14 @@ Return the nonlinear operator for the objective of `mpc` [`NonLinMPC`](@ref).
825825
826826
It is based on the splatting syntax. This method is really intricate and that's because of:
827827
828-
- These functions are used inside the nonlinear optimization, so they must be type-stable
829-
and as efficient as possible. All the function outputs and derivatives are cached and
830-
updated in-place if required to use the efficient [`value_and_gradient!`](@extref DifferentiationInterface DifferentiationInterface.value_and_jacobian!).
831-
- 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))
832-
and memoization to avoid redundant computations. This is already complex, but it's even
833-
worse knowing that the automatic differentiation tools do not support splatting.
828+
- These functions are used inside the nonlinear optimization, so as efficient as possible.
829+
All the function outputs and derivatives are cached and updated in-place if required to
830+
use the efficient [`value_and_gradient!`](@extref DifferentiationInterface DifferentiationInterface.value_and_jacobian!).
831+
- The splatting syntax is required for [`add_nonlinear_operator`](@extref JuMP JuMP.add_nonlinear_operator)
832+
but automatic differentiation tools do not support splatting.
833+
- 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 3
835+
closures. The `NoTypeFunction` wrapper hide their concrete type to disable compilation.
834836
- The signature of gradient and hessian functions is not the same for univariate (`nZ̃ == 1`)
835837
and multivariate (`nZ̃ > 1`) operators in `JuMP`. Both must be defined.
836838
"""
@@ -897,51 +899,30 @@ function get_nonlinobj_op(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where J
897899
end
898900
end
899901
J_func = if !isnothing(hess)
900-
function (Z̃_arg::Vararg{T, N}) where {N, T<:Real}
901-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
902-
return J[]::T
903-
end
902+
(Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); J[])
904903
else
905-
function (Z̃_arg::Vararg{T, N}) where {N, T<:Real}
906-
update_objective!(J, ∇J, Z̃_J, Z̃_arg)
907-
return J[]::T
908-
end
904+
(Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); J[])
909905
end
910-
∇J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.@operator doc):
906+
∇J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.add_nonlinear_operator doc):
911907
if !isnothing(hess)
912-
function (Z̃_arg)
913-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
914-
return ∇J[]
915-
end
908+
(Z̃_arg) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇J[])
916909
else
917-
function (Z̃_arg)
918-
update_objective!(J, ∇J, Z̃_J, Z̃_arg)
919-
return ∇J[]
920-
end
910+
(Z̃_arg) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); ∇J[])
921911
end
922-
else # multivariate syntax (see JuMP.@operator doc):
912+
else # multivariate syntax (see JuMP.add_nonlinear_operator doc):
923913
if !isnothing(hess)
924-
function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real}
925-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
926-
return ∇J_arg .= ∇J
927-
end
914+
(∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J)
928915
else
929-
function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real}
930-
update_objective!(J, ∇J, Z̃_J, Z̃_arg)
931-
return ∇J_arg .= ∇J
932-
end
916+
(∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J)
933917
end
934918
end
935-
∇²J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.@operator doc):
936-
function (Z̃_arg)
937-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
938-
return ∇²J[]
939-
end
940-
else # multivariate syntax (see JuMP.@operator doc):
941-
function (∇²J_arg::AbstractMatrix{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real}
942-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
943-
return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure)
944-
end
919+
∇²J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.add_nonlinear_operator doc):
920+
(Z̃_arg) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇²J[])
921+
else # multivariate syntax (see JuMP.add_nonlinear_operator doc):
922+
(∇²J_arg, Z̃_arg...) -> (
923+
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg);
924+
fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure)
925+
)
945926
end
946927
J_op = if !isnothing(hess)
947928
JuMP.add_nonlinear_operator(

‎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)