Skip to content

Commit d18ac95

Browse files
committed
added: pretty-print diff. backends in NonLinModel, NonLinMPC and MovingHorizonEstimator
1 parent 7dfd65a commit d18ac95

File tree

6 files changed

+39
-10
lines changed

6 files changed

+39
-10
lines changed

src/controller/nonlinmpc.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,14 @@ end
748748

749749
"Evaluate the economic term `E*JE` of the objective function for [`NonLinMPC`](@ref)."
750750
function obj_econ(
751-
mpc::NonLinMPC, model::SimModel, Ue, Ŷe::AbstractVector{NT}
751+
mpc::NonLinMPC, ::SimModel, Ue, Ŷe::AbstractVector{NT}
752752
) where NT<:Real
753753
E_JE = mpc.weights.iszero_E ? zero(NT) : mpc.weights.E*mpc.JE(Ue, Ŷe, mpc.D̂e, mpc.p)
754754
return E_JE
755-
end
755+
end
756+
757+
"Print the differentiation backends of a [`NonLinMPC`](@ref) controller."
758+
function print_backends(io::IO, mpc::NonLinMPC)
759+
println(io, "├ gradient: $(backend_str(mpc.gradient))")
760+
println(io, "├ jacobian: $(backend_str(mpc.jacobian))")
761+
end

src/estimator/mhe.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ function Base.show(io::IO, estim::MovingHorizonEstimator)
99
println(io, "$(nameof(typeof(estim))) estimator with a sample time Ts = $(model.Ts) s:")
1010
println(io, "├ model: $(nameof(typeof(model)))")
1111
println(io, "├ optimizer: $(JuMP.solver_name(estim.optim)) ")
12+
print_backends(io, estim, model)
1213
println(io, "└ dimensions:")
1314
print_estim_dim(io, estim, n)
1415
end
1516

17+
function print_backends(io::IO, estim::MovingHorizonEstimator, ::SimModel)
18+
println(io, "├ gradient: $(backend_str(estim.gradient))")
19+
println(io, "├ jacobian: $(backend_str(estim.jacobian))")
20+
end
21+
print_backends(::IO, ::MovingHorizonEstimator, ::LinModel) = nothing
22+
23+
1624
"Print the overall dimensions of the MHE `estim` with left padding `n`."
1725
function print_estim_dim(io::IO, estim::MovingHorizonEstimator, n)
1826
nu, nd = estim.model.nu, estim.model.nd

src/general.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ end
6161
init_diffmat(T, backend::AbstractADType, _ , nx , ny) = Matrix{T}(undef, ny, nx)
6262
init_diffmat(T, backend::AutoSparse ,prep , _ , _ ) = similar(sparsity_pattern(prep), T)
6363

64+
backend_str(backend::AbstractADType) = string(nameof(typeof(backend)))
65+
function backend_str(backend::AutoSparse)
66+
str = "AutoSparse ($(nameof(typeof(backend.dense_ad))),"*
67+
" $(nameof(typeof(backend.sparsity_detector))),"*
68+
" $(nameof(typeof(backend.coloring_algorithm))))"
69+
return str
70+
end
71+
6472
"Verify that x and y elements are different using `!==`."
6573
isdifferent(x, y) = any(xi !== yi for (xi, yi) in zip(x, y))
6674

src/model/nonlinmodel.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,11 @@ h!(y0, model::NonLinModel, x0, d0, p) = model.h!(y0, x0, d0, p)
311311

312312
include("solver.jl")
313313

314-
function detailstr(model::NonLinModel{<:Real, <:RungeKutta{N}}) where N
315-
return "solver: $(nameof(typeof(model.solver)))($N)"
314+
function print_details(io::IO, model::NonLinModel{<:Real, <:RungeKutta{N}}) where N
315+
println(io, "├ solver: $(nameof(typeof(model.solver)))($N)")
316+
println(io, "├ linearization: $(backend_str(model.jacobian))")
317+
end
318+
function print_details(io::IO, model::NonLinModel)
319+
println(io, "├ solver: $(nameof(typeof(model.solver)))")
320+
println(io, "├ linearization: $(backend_str(model.jacobian))")
316321
end
317-
detailstr(::NonLinModel{<:Real, <:EmptySolver}) = "solver: empty"
318-
detailstr(::NonLinModel) = ""

src/predictive_control.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ function Base.show(io::IO, mpc::PredictiveController)
3737
println(io, "├ model: $(nameof(typeof(model)))")
3838
println(io, "├ optimizer: $(JuMP.solver_name(mpc.optim)) ")
3939
println(io, "├ transcription: $(nameof(typeof(mpc.transcription)))")
40+
print_backends(io, mpc)
4041
println(io, "└ dimensions:")
4142
println(io, "$(lpad(Hp, n)) prediction steps Hp")
4243
println(io, "$(lpad(Hc, n)) control steps Hc")
4344
println(io, "$(lpad(nϵ, n)) slack variable ϵ (control constraints)")
4445
print_estim_dim(io, mpc.estim, n)
4546
end
4647

48+
"No differentiation backends to print for a `PredictiveController` by default."
49+
print_backends(::IO, ::PredictiveController) = nothing
50+
4751
"Functor allowing callable `PredictiveController` object as an alias for `moveinput!`."
4852
function (mpc::PredictiveController)(
4953
ry::Vector = mpc.estim.model.yop,

src/sim_model.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ function setstate!(model::SimModel, x)
174174
return model
175175
end
176176

177-
detailstr(model::SimModel) = ""
178-
179177
@doc raw"""
180178
initstate!(model::SimModel, u, d=[]) -> x
181179
@@ -374,15 +372,17 @@ function Base.show(io::IO, model::SimModel)
374372
nu, nd = model.nu, model.nd
375373
nx, ny = model.nx, model.ny
376374
n = maximum(ndigits.((nu, nx, ny, nd))) + 1
377-
details = detailstr(model)
378375
println(io, "$(nameof(typeof(model))) with a sample time Ts = $(model.Ts) s:")
379-
!isempty(details) && println(io, "$details")
376+
print_details(io, model)
380377
println(io, "└ dimensions:")
381378
println(io, "$(lpad(nu, n)) manipulated inputs u")
382379
println(io, "$(lpad(nx, n)) states x")
383380
println(io, "$(lpad(ny, n)) outputs y")
384381
print(io, "$(lpad(nd, n)) measured disturbances d")
385382
end
386383

384+
"Print additional details of `model` if any (no details by default)."
385+
print_details(::IO, ::SimModel) = nothing
386+
387387
"Functor allowing callable `SimModel` object as an alias for `evaloutput`."
388388
(model::SimModel)(d=model.buffer.empty) = evaloutput(model::SimModel, d)

0 commit comments

Comments
 (0)