diff --git a/src/ModelPredictiveControl.jl b/src/ModelPredictiveControl.jl index 723281bc6..cbbce5e6a 100644 --- a/src/ModelPredictiveControl.jl +++ b/src/ModelPredictiveControl.jl @@ -66,5 +66,4 @@ include("plot_sim.jl") include("precompile.jl") - end \ No newline at end of file diff --git a/src/controller/nonlinmpc.jl b/src/controller/nonlinmpc.jl index d53a0dc08..f6609a4a0 100644 --- a/src/controller/nonlinmpc.jl +++ b/src/controller/nonlinmpc.jl @@ -825,12 +825,14 @@ Return the nonlinear operator for the objective of `mpc` [`NonLinMPC`](@ref). It is based on the splatting syntax. This method is really intricate and that's because of: -- These functions are used inside the nonlinear optimization, so they must be type-stable - and as efficient as possible. All the function outputs and derivatives are cached and - updated in-place if required to use the efficient [`value_and_gradient!`](@extref DifferentiationInterface DifferentiationInterface.value_and_jacobian!). -- 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)) - and memoization to avoid redundant computations. This is already complex, but it's even - worse knowing that the automatic differentiation tools do not support splatting. +- These functions are used inside the nonlinear optimization, so as efficient as possible. + All the function outputs and derivatives are cached and updated in-place if required to + use the efficient [`value_and_gradient!`](@extref DifferentiationInterface DifferentiationInterface.value_and_jacobian!). +- The splatting syntax is required for [`add_nonlinear_operator`](@extref JuMP JuMP.add_nonlinear_operator) + but automatic differentiation tools do not support splatting. +- The internal of `MOI.jl` calls the 3 functions of `add_nonlinear_operator` with a runtime + dispatch. It is thus a waste of time and resources to compile (a.k.a specialize) these 3 + closures. The `NoTypeFunction` wrapper hide their concrete type to disable compilation. - The signature of gradient and hessian functions is not the same for univariate (`nZ̃ == 1`) and multivariate (`nZ̃ > 1`) operators in `JuMP`. Both must be defined. """ @@ -897,51 +899,30 @@ function get_nonlinobj_op(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where J end end J_func = if !isnothing(hess) - function (Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return J[]::T - end + (Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); J[]) else - function (Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, Z̃_J, Z̃_arg) - return J[]::T - end + (Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); J[]) end - ∇J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.@operator doc): + ∇J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.add_nonlinear_operator doc): if !isnothing(hess) - function (Z̃_arg) - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return ∇J[] - end + (Z̃_arg) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇J[]) else - function (Z̃_arg) - update_objective!(J, ∇J, Z̃_J, Z̃_arg) - return ∇J[] - end + (Z̃_arg) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); ∇J[]) end - else # multivariate syntax (see JuMP.@operator doc): + else # multivariate syntax (see JuMP.add_nonlinear_operator doc): if !isnothing(hess) - function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return ∇J_arg .= ∇J - end + (∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J) else - function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, Z̃_J, Z̃_arg) - return ∇J_arg .= ∇J - end + (∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J) end end - ∇²J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.@operator doc): - function (Z̃_arg) - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return ∇²J[] - end - else # multivariate syntax (see JuMP.@operator doc): - function (∇²J_arg::AbstractMatrix{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure) - end + ∇²J_func! = if nZ̃ == 1 # univariate syntax (see JuMP.add_nonlinear_operator doc): + (Z̃_arg) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇²J[]) + else # multivariate syntax (see JuMP.add_nonlinear_operator doc): + (∇²J_arg, Z̃_arg...) -> ( + update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); + fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure) + ) end J_op = if !isnothing(hess) JuMP.add_nonlinear_operator( diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index 1b6fe0ef6..ed5321c34 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -1445,9 +1445,11 @@ It is based on the splatting syntax. This method is really intricate and that's - These functions are used inside the nonlinear optimization, so they must be type-stable and as efficient as possible. All the function outputs and derivatives are cached and updated in-place if required to use the efficient [`value_and_gradient!`](@extref DifferentiationInterface DifferentiationInterface.value_and_jacobian!). -- 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)) - and memoization to avoid redundant computations. This is already complex, but it's even - worse knowing that the automatic differentiation tools do not support splatting. +- The splatting syntax is required for [`add_nonlinear_operator`](@extref JuMP JuMP.add_nonlinear_operator) + but automatic differentiation tools do not support splatting. +- The internal of `MOI.jl` calls the 3 functions of `add_nonlinear_operator` with a runtime + dispatch. It is thus a waste of time and resources to compile (a.k.a specialize) these 3 + closures. The `NoTypeFunction` wrapper hide their concrete type to disable compilation. """ function get_nonlinobj_op( estim::MovingHorizonEstimator, optim::JuMP.GenericModel{JNT} @@ -1524,33 +1526,21 @@ function get_nonlinobj_op( end end J_func = if !isnothing(hess) - function (Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return J[]::T - end + (Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); J[]) else - function (Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, Z̃_J, Z̃_arg) - return J[]::T - end + (Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); J[]) end - # only the multivariate syntax of JuMP.@operator, univariate is impossible for MHE - # since Z̃ comprises the arrival state estimate AND the estimated process noise: + # only the multivariate syntax of JuMP.add_nonlinear_operator, univariate is impossible + # for MHE since Z̃ comprises the arrival state estimate AND the estimated process noise: ∇J_func! = if !isnothing(hess) - function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return ∇J_arg .= ∇J - end + (∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J) else - function (∇J_arg::AbstractVector{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, Z̃_J, Z̃_arg) - return ∇J_arg .= ∇J - end - end - function ∇²J_func!(∇²J_arg::AbstractMatrix{T}, Z̃_arg::Vararg{T, N}) where {N, T<:Real} - update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg) - return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure) + (∇J_arg, Z̃_arg...) -> (update_objective!(J, ∇J, Z̃_J, Z̃_arg); ∇J_arg .= ∇J) end + ∇²J_func! = (∇²J_arg, Z̃_arg...) -> ( + update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); + fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure) + ) J_op = if !isnothing(hess) JuMP.add_nonlinear_operator( optim, nZ̃,