Skip to content

Commit ed078b6

Browse files
committed
changed: similar cleanup
1 parent 71cf027 commit ed078b6

1 file changed

Lines changed: 23 additions & 42 deletions

File tree

‎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
835+
functions. 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...)
901-
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
902-
return J[]
903-
end
902+
(Z̃_arg...) -> (update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg); J[])
904903
else
905-
function (Z̃_arg...)
906-
update_objective!(J, ∇J, Z̃_J, Z̃_arg)
907-
return J[]
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, Z̃_arg...)
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, Z̃_arg...)
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, Z̃_arg...)
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(

0 commit comments

Comments
 (0)