Skip to content

Commit b818d7b

Browse files
committed
Revert "Make Hessian sparsity detection work with SCT (prototype)"
This reverts commit f7d7293.
1 parent f7d7293 commit b818d7b

File tree

6 files changed

+36
-53
lines changed

6 files changed

+36
-53
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1616
ProgressLogging = "33c8b6b6-d38a-422a-b730-caa89a2f386c"
1717
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1818
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
19-
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2019
SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5"
2120
SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"
2221

@@ -37,7 +36,6 @@ PrecompileTools = "1"
3736
ProgressLogging = "0.1"
3837
Random = "1.10"
3938
RecipesBase = "1"
40-
SparseArrays = "1.11.0"
4139
SparseConnectivityTracer = "0.6.13"
4240
SparseMatrixColorings = "0.4.14"
4341
TestItemRunner = "1"

src/ModelPredictiveControl.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module ModelPredictiveControl
33
using PrecompileTools
44
using LinearAlgebra
55
using Random: randn
6-
using SparseArrays
76

87
using RecipesBase
98
using ProgressLogging
@@ -48,7 +47,6 @@ include("state_estim.jl")
4847
include("predictive_control.jl")
4948
include("plot_sim.jl")
5049

51-
#=
5250
@setup_workload begin
5351
# Putting some things in `@setup_workload` instead of `@compile_workload` can reduce the
5452
# size of the precompile file and potentially make loading faster.
@@ -58,6 +56,5 @@ include("plot_sim.jl")
5856
include("precompile.jl")
5957
end
6058
end
61-
=#
6259

6360
end

src/controller/construct.jl

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
struct PredictiveControllerBuffer{NT<:Real,M<:AbstractMatrix{NT}}
1+
struct PredictiveControllerBuffer{NT<:Real}
22
u ::Vector{NT}
33
::Vector{NT}
44
::Vector{NT}
55
::Vector{NT}
66
U ::Vector{NT}
77
::Matrix{NT}
8-
P̃u::M
8+
P̃u::Matrix{NT}
99
empty::Vector{NT}
1010
end
1111

@@ -29,19 +29,14 @@ function PredictiveControllerBuffer(
2929
= Matrix{NT}(undef, ny*Hp, nZ̃)
3030
P̃u = Matrix{NT}(undef, nu*Hp, nZ̃)
3131
empty = Vector{NT}(undef, 0)
32-
return PredictiveControllerBuffer{NT,typeof(P̃u)}(u, Z̃, D̂, Ŷ, U, Ẽ, P̃u, empty)
32+
return PredictiveControllerBuffer{NT}(u, Z̃, D̂, Ŷ, U, Ẽ, P̃u, empty)
3333
end
3434

3535
"Include all the objective function weights of [`PredictiveController`](@ref)"
36-
struct ControllerWeights{
37-
NT<:Real,
38-
H1<:Hermitian{NT, <:AbstractMatrix{NT}},
39-
H2<:Hermitian{NT, <:AbstractMatrix{NT}},
40-
H3<:Hermitian{NT, <:AbstractMatrix{NT}},
41-
}
42-
M_Hp::H1
43-
Ñ_Hc::H2
44-
L_Hp::H3
36+
struct ControllerWeights{NT<:Real}
37+
M_Hp::Hermitian{NT, Matrix{NT}}
38+
Ñ_Hc::Hermitian{NT, Matrix{NT}}
39+
L_Hp::Hermitian{NT, Matrix{NT}}
4540
E ::NT
4641
iszero_M_Hp::Vector{Bool}
4742
iszero_Ñ_Hc::Vector{Bool}
@@ -51,15 +46,15 @@ struct ControllerWeights{
5146
model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt=Inf, Ewt=0
5247
) where NT<:Real
5348
validate_weights(model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt)
54-
M_Hp = Hermitian(convert.(NT, M_Hp), :L)
55-
N_Hc = Hermitian(convert.(NT, N_Hc), :L)
56-
L_Hp = Hermitian(convert.(NT, L_Hp), :L)
49+
# convert `Diagonal` to normal `Matrix` if required:
50+
M_Hp = Hermitian(convert(Matrix{NT}, M_Hp), :L)
51+
N_Hc = Hermitian(convert(Matrix{NT}, N_Hc), :L)
52+
L_Hp = Hermitian(convert(Matrix{NT}, L_Hp), :L)
5753
nΔU = size(N_Hc, 1)
5854
C = Cwt
5955
if !isinf(Cwt)
6056
# ΔŨ = [ΔU; ϵ] (ϵ is the slack variable)
61-
# Ñ_Hc = Hermitian([N_Hc zeros(NT, nΔU, 1); zeros(NT, 1, nΔU) C], :L)
62-
Ñ_Hc = Hermitian(blockdiag(sparse(N_Hc), sparse(Diagonal([C]))), :L)
57+
Ñ_Hc = Hermitian([N_Hc zeros(NT, nΔU, 1); zeros(NT, 1, nΔU) C], :L)
6358
else
6459
# ΔŨ = ΔU (only hard constraints)
6560
Ñ_Hc = N_Hc
@@ -69,7 +64,7 @@ struct ControllerWeights{
6964
iszero_Ñ_Hc = [iszero(Ñ_Hc)]
7065
iszero_L_Hp = [iszero(L_Hp)]
7166
iszero_E = iszero(E)
72-
return new{NT,typeof(M_Hp),typeof(Ñ_Hc),typeof(L_Hp)}(M_Hp, Ñ_Hc, L_Hp, E, iszero_M_Hp, iszero_Ñ_Hc, iszero_L_Hp, iszero_E)
67+
return new{NT}(M_Hp, Ñ_Hc, L_Hp, E, iszero_M_Hp, iszero_Ñ_Hc, iszero_L_Hp, iszero_E)
7368
end
7469
end
7570

@@ -589,10 +584,10 @@ function relaxU(Pu::Matrix{NT}, C_umin, C_umax, nϵ) where NT<:Real
589584
# ϵ impacts Z → U conversion for constraint calculations:
590585
A_Umin, A_Umax = -[Pu C_umin], [Pu -C_umax]
591586
# ϵ has no impact on Z → U conversion for prediction calculations:
592-
P̃u = sparse_hcat(sparse(Pu), spzeros(NT, size(Pu, 1)))
587+
P̃u = [Pu zeros(NT, size(Pu, 1))]
593588
else # Z̃ = Z (only hard constraints)
594589
A_Umin, A_Umax = -Pu, Pu
595-
P̃u = sparse(Pu)
590+
P̃u = Pu
596591
end
597592
return A_Umin, A_Umax, P̃u
598593
end
@@ -626,17 +621,17 @@ bound, which is more precise than a linear inequality constraint. However, it is
626621
convenient to treat it as a linear inequality constraint since the optimizer `OSQP.jl` does
627622
not support pure bounds on the decision variables.
628623
"""
629-
function relaxΔU(PΔu::AbstractMatrix{NT}, C_Δumin, C_Δumax, ΔUmin, ΔUmax, nϵ) where NT<:Real
624+
function relaxΔU(PΔu::Matrix{NT}, C_Δumin, C_Δumax, ΔUmin, ΔUmax, nϵ) where NT<:Real
630625
nZ = size(PΔu, 2)
631626
if== 1 # Z̃ = [Z; ϵ]
632627
ΔŨmin, ΔŨmax = [ΔUmin; NT[0.0]], [ΔUmax; NT[Inf]] # 0 ≤ ϵ ≤ ∞
633628
A_ϵ = [zeros(NT, 1, nZ) NT[1.0]]
634629
A_ΔŨmin, A_ΔŨmax = -[PΔu C_Δumin; A_ϵ], [PΔu -C_Δumax; A_ϵ]
635-
P̃Δu = blockdiag(sparse(PΔu), spdiagm([one(NT)]))
630+
P̃Δu = [PΔu zeros(NT, size(PΔu, 1), 1); zeros(NT, 1, size(PΔu, 2)) NT[1.0]]
636631
else # Z̃ = Z (only hard constraints)
637632
ΔŨmin, ΔŨmax = ΔUmin, ΔUmax
638633
A_ΔŨmin, A_ΔŨmax = -PΔu, PΔu
639-
P̃Δu = sparse(PΔu)
634+
P̃Δu = PΔu
640635
end
641636
return A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, P̃Δu
642637
end

src/controller/execute.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,7 @@ function obj_nonlinprog!(
370370
end
371371
# --- economic term ---
372372
E_JE = obj_econ(mpc, model, Ue, Ŷe)
373-
return (
374-
JR̂y +
375-
JΔŨ +
376-
JR̂u +
377-
E_JE
378-
)
373+
return JR̂y + JΔŨ + JR̂u + E_JE
379374
end
380375

381376
"No custom nonlinear constraints `gc` by default, return `gc` unchanged."

src/controller/nonlinmpc.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ struct NonLinMPC{
1717
JB<:AbstractADType,
1818
PT<:Any,
1919
JEfunc<:Function,
20-
GCfunc<:Function,
21-
M<:AbstractMatrix{NT}
20+
GCfunc<:Function
2221
} <: PredictiveController{NT}
2322
estim::SE
2423
transcription::TM
@@ -38,8 +37,8 @@ struct NonLinMPC{
3837
p::PT
3938
R̂u::Vector{NT}
4039
R̂y::Vector{NT}
41-
P̃Δu::M
42-
P̃u ::M
40+
P̃Δu::Matrix{NT}
41+
P̃u ::Matrix{NT}
4342
Tu ::Matrix{NT}
4443
Tu_lastu0::Vector{NT}
4544
::Matrix{NT}
@@ -108,7 +107,7 @@ struct NonLinMPC{
108107
nZ̃ = get_nZ(estim, transcription, Hp, Hc) +
109108
= zeros(NT, nZ̃)
110109
buffer = PredictiveControllerBuffer(estim, transcription, Hp, Hc, nϵ)
111-
mpc = new{NT, SE, TM, JM, GB, HB, JB, PT, JEfunc, GCfunc, typeof(P̃u)}(
110+
mpc = new{NT, SE, TM, JM, GB, JB, PT, JEfunc, GCfunc}(
112111
estim, transcription, optim, con,
113112
gradient, jacobian,
114113
Z̃, ŷ,
@@ -280,9 +279,9 @@ function NonLinMPC(
280279
Mwt = fill(DEFAULT_MWT, model.ny),
281280
Nwt = fill(DEFAULT_NWT, model.nu),
282281
Lwt = fill(DEFAULT_LWT, model.nu),
283-
M_Hp = Diagonal(repeat(Mwt, Hp)),
284-
N_Hc = Diagonal(repeat(Nwt, Hc)),
285-
L_Hp = Diagonal(repeat(Lwt, Hp)),
282+
M_Hp = diagm(repeat(Mwt, Hp)),
283+
N_Hc = diagm(repeat(Nwt, Hc)),
284+
L_Hp = diagm(repeat(Lwt, Hp)),
286285
Cwt = DEFAULT_CWT,
287286
Ewt = DEFAULT_EWT,
288287
JE ::Function = (_,_,_,_) -> 0.0,
@@ -311,9 +310,9 @@ function NonLinMPC(
311310
Mwt = fill(DEFAULT_MWT, model.ny),
312311
Nwt = fill(DEFAULT_NWT, model.nu),
313312
Lwt = fill(DEFAULT_LWT, model.nu),
314-
M_Hp = Diagonal(repeat(Mwt, Hp)),
315-
N_Hc = Diagonal(repeat(Nwt, Hc)),
316-
L_Hp = Diagonal(repeat(Lwt, Hp)),
313+
M_Hp = diagm(repeat(Mwt, Hp)),
314+
N_Hc = diagm(repeat(Nwt, Hc)),
315+
L_Hp = diagm(repeat(Lwt, Hp)),
317316
Cwt = DEFAULT_CWT,
318317
Ewt = DEFAULT_EWT,
319318
JE ::Function = (_,_,_,_) -> 0.0,
@@ -366,9 +365,9 @@ function NonLinMPC(
366365
Mwt = fill(DEFAULT_MWT, estim.model.ny),
367366
Nwt = fill(DEFAULT_NWT, estim.model.nu),
368367
Lwt = fill(DEFAULT_LWT, estim.model.nu),
369-
M_Hp = Diagonal(repeat(Mwt, Hp)),
370-
N_Hc = Diagonal(repeat(Nwt, Hc)),
371-
L_Hp = Diagonal(repeat(Lwt, Hp)),
368+
M_Hp = diagm(repeat(Mwt, Hp)),
369+
N_Hc = diagm(repeat(Nwt, Hc)),
370+
L_Hp = diagm(repeat(Lwt, Hp)),
372371
Cwt = DEFAULT_CWT,
373372
Ewt = DEFAULT_EWT,
374373
JE ::Function = (_,_,_,_) -> 0.0,

src/controller/transcription.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ function init_ZtoΔU end
8585
function init_ZtoΔU(
8686
estim::StateEstimator{NT}, transcription::SingleShooting, _ , Hc
8787
) where {NT<:Real}
88-
PΔu = Diagonal(fill(one(NT), estim.model.nu*Hc))
88+
PΔu = Matrix{NT}(I, estim.model.nu*Hc, estim.model.nu*Hc)
8989
return PΔu
9090
end
9191

9292
function init_ZtoΔU(
9393
estim::StateEstimator{NT}, transcription::MultipleShooting, Hp, Hc
9494
) where {NT<:Real}
95-
I_nu_Hc = Diagonal(fill(one(NT), estim.model.nu*Hc))
96-
PΔu = sparse_hcat(I_nu_Hc , spzeros(NT, estim.model.nu*Hc, estim.nx̂*Hp))
95+
I_nu_Hc = Matrix{NT}(I, estim.model.nu*Hc, estim.model.nu*Hc)
96+
PΔu = [I_nu_Hc zeros(NT, estim.model.nu*Hc, estim.nx̂*Hp)]
9797
return PΔu
9898
end
9999

@@ -145,8 +145,7 @@ function init_ZtoU(
145145
) where {NT<:Real}
146146
model = estim.model
147147
# Pu and Tu are `Matrix{NT}`, conversion is faster than `Matrix{Bool}` or `BitMatrix`
148-
I_nu = Diagonal(fill(one(NT), model.nu))
149-
# TODO: make PU and friends sparse
148+
I_nu = Matrix{NT}(I, model.nu, model.nu)
150149
PU_Hc = LowerTriangular(repeat(I_nu, Hc, Hc))
151150
PUdagger = [PU_Hc; repeat(I_nu, Hp - Hc, Hc)]
152151
Pu = init_PUmat(estim, transcription, Hp, Hc, PUdagger)

0 commit comments

Comments
 (0)