Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/ModelPredictiveControl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ include("plot_sim.jl")

include("precompile.jl")


end
65 changes: 23 additions & 42 deletions src/controller/nonlinmpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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(
Expand Down
40 changes: 15 additions & 25 deletions src/estimator/mhe/construct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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̃,
Expand Down
Loading