Skip to content

Commit c713f61

Browse files
committed
added: init diff. matrix as dense or sparse as required by backend
1 parent 1ed36a8 commit c713f61

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

src/ModelPredictiveControl.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using DifferentiationInterface: ADTypes.AbstractADType, AutoForwardDiff, AutoSpa
1111
using DifferentiationInterface: gradient!, jacobian!, prepare_gradient, prepare_jacobian
1212
using DifferentiationInterface: Constant, Cache
1313
using SparseConnectivityTracer: TracerSparsityDetector
14-
using SparseMatrixColorings: GreedyColoringAlgorithm
14+
using SparseMatrixColorings: GreedyColoringAlgorithm, sparsity_pattern
1515

1616
import ForwardDiff #TODO: delete this after `linearize!` and `ExtendedKalmanFilter` are updated
1717

src/controller/nonlinmpc.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ function get_optim_functions(
590590
jac_backend ::AbstractADType
591591
) where JNT<:Real
592592
model, transcription = mpc.estim.model, mpc.transcription
593-
#TODO: initialize jacobian as sparsed if it's the case?
594593
#TODO: fix type of all cache to ::Vector{JNT} (verify performance difference with and w/o)
595594
#TODO: mêmes choses pour le MHE
596595
# --------------------- update simulation function ------------------------------------
@@ -631,14 +630,14 @@ function get_optim_functions(
631630
update_simulations!(Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq)
632631
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
633632
end
634-
Z̃_∇J = fill(myNaN, nZ̃)
635-
∇J = zeros(JNT, nZ̃) # gradient of objective J
633+
Z̃_∇J = fill(myNaN, nZ̃)
636634
∇J_context = (
637635
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
638636
Cache(Û0), Cache(X̂0),
639637
Cache(gc), Cache(g), Cache(geq)
640638
)
641639
∇J_prep = prepare_gradient(Jfunc!, grad_backend, Z̃_∇J, ∇J_context...)
640+
∇J = Vector{JNT}(undef, nZ̃)
642641
∇Jfunc! = if nZ̃ == 1
643642
function (Z̃arg)
644643
Z̃_∇J .= Z̃arg
@@ -668,7 +667,6 @@ function get_optim_functions(
668667
return update_simulations!(Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq)
669668
end
670669
Z̃_∇g = fill(myNaN, nZ̃)
671-
∇g = zeros(JNT, ng, nZ̃) # Jacobian of inequality constraints g
672670
∇g_context = (
673671
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
674672
Cache(Û0), Cache(X̂0),
@@ -679,6 +677,7 @@ function get_optim_functions(
679677
mpc.con.i_g .= true
680678
∇g_prep = prepare_jacobian(gfunc!, g, jac_backend, Z̃_∇g, ∇g_context...)
681679
mpc.con.i_g .= i_g_old
680+
∇g = init_diffmat(JNT, jac_backend, ∇g_prep, nZ̃, ng)
682681
∇gfuncs! = Vector{Function}(undef, ng)
683682
for i in eachindex(∇gfuncs!)
684683
∇gfuncs_i! = if nZ̃ == 1
@@ -716,13 +715,13 @@ function get_optim_functions(
716715
return update_simulations!(Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq)
717716
end
718717
Z̃_∇geq = fill(myNaN, nZ̃)
719-
∇geq = zeros(JNT, neq, nZ̃) # Jacobian of equality constraints geq
720718
∇geq_context = (
721719
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
722720
Cache(Û0), Cache(X̂0),
723721
Cache(gc), Cache(g)
724722
)
725723
∇geq_prep = prepare_jacobian(geqfunc!, geq, jac_backend, Z̃_∇geq, ∇geq_context...)
724+
∇geq = init_diffmat(JNT, jac_backend, ∇geq_prep, nZ̃, neq)
726725
∇geqfuncs! = Vector{Function}(undef, neq)
727726
for i in eachindex(∇geqfuncs!)
728727
# only multivariate syntax, univariate is impossible since nonlinear equality

src/estimator/mhe/construct.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,14 +1362,14 @@ function get_optim_functions(
13621362
return obj_nonlinprog!(x̄, estim, model, V̂, Z̃)
13631363
end
13641364
Z̃_∇J = fill(myNaN, nZ̃)
1365-
∇J = Vector{JNT}(undef, nZ̃) # gradient of objective J
13661365
∇J_context = (
13671366
Cache(V̂), Cache(X̂0),
13681367
Cache(û0), Cache(ŷ0),
13691368
Cache(g),
13701369
Cache(x̄),
13711370
)
13721371
∇J_prep = prepare_gradient(Jfunc!, grad_backend, Z̃_∇J, ∇J_context...)
1372+
∇J = Vector{JNT}(undef, nZ̃)
13731373
∇Jfunc! = if nZ̃ == 1
13741374
function (Z̃arg)
13751375
Z̃_∇J .= Z̃arg
@@ -1399,7 +1399,6 @@ function get_optim_functions(
13991399
return update_simulations!(Z̃, V̂, X̂0, û0, ŷ0, g)
14001400
end
14011401
Z̃_∇g = fill(myNaN, nZ̃)
1402-
∇g = Matrix{JNT}(undef, ng, nZ̃) # Jacobian of inequality constraints g
14031402
∇g_context = (
14041403
Cache(V̂), Cache(X̂0),
14051404
Cache(û0), Cache(ŷ0),
@@ -1409,6 +1408,7 @@ function get_optim_functions(
14091408
estim.con.i_g .= true
14101409
∇g_prep = prepare_jacobian(gfunc!, g, jac_backend, Z̃_∇g, ∇g_context...)
14111410
estim.con.i_g .= i_g_old
1411+
∇g = init_diffmat(JNT, jac_backend, ∇g_prep, nZ̃, ng)
14121412
∇gfuncs! = Vector{Function}(undef, ng)
14131413
for i in eachindex(∇gfuncs!)
14141414
∇gfuncs![i] = if nZ̃ == 1

src/general.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function get_jacobian!(A, buffer::JacobianBuffer, y, x)
2727
return ForwardDiff.jacobian!(A, buffer.f!, y, x, buffer.config)
2828
end
2929

30+
"Init a differentiation result matrix as dense or sparse matrix, as required by `backend`."
31+
init_diffmat(T, backend::AbstractADType, _ , nx , ny) = Matrix{T}(undef, ny, nx)
32+
init_diffmat(T, backend::AutoSparse ,prep , _ , _ ) = similar(sparsity_pattern(prep), T)
33+
3034
"Termination status that means 'no solution available'."
3135
const ERROR_STATUSES = (
3236
JuMP.INFEASIBLE, JuMP.DUAL_INFEASIBLE, JuMP.LOCALLY_INFEASIBLE,

0 commit comments

Comments
 (0)