Skip to content

Commit 434ed1d

Browse files
committed
changed: ΔU->U conversion with floats (faster)
1 parent f5ad896 commit 434ed1d

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

src/controller/explicitmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ struct ExplicitMPC{NT<:Real, SE<:StateEstimator} <: PredictiveController{NT}
1212
R̂u::Vector{NT}
1313
R̂y::Vector{NT}
1414
noR̂u::Bool
15-
::BitMatrix
16-
T::BitMatrix
15+
::Matrix{NT}
16+
T::Matrix{NT}
1717
::Matrix{NT}
1818
F::Vector{NT}
1919
G::Matrix{NT}
@@ -44,7 +44,7 @@ struct ExplicitMPC{NT<:Real, SE<:StateEstimator} <: PredictiveController{NT}
4444
# dummy vals (updated just before optimization):
4545
R̂y, R̂u = zeros(NT, ny*Hp), zeros(NT, nu*Hp)
4646
noR̂u = iszero(L_Hp)
47-
S, T = init_ΔUtoU(nu, Hp, Hc)
47+
S, T = init_ΔUtoU(model, Hp, Hc)
4848
E, F, G, J, K, V = init_predmat(estim, model, Hp, Hc)
4949
S̃, Ñ_Hc, Ẽ = S, N_Hc, E # no slack variable ϵ for ExplicitMPC
5050
P̃, q̃, p = init_quadprog(model, Ẽ, S̃, M_Hp, Ñ_Hc, L_Hp)

src/controller/linmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ struct LinMPC{
2222
R̂u::Vector{NT}
2323
R̂y::Vector{NT}
2424
noR̂u::Bool
25-
::BitMatrix
26-
T::BitMatrix
25+
::Matrix{NT}
26+
T::Matrix{NT}
2727
::Matrix{NT}
2828
F::Vector{NT}
2929
G::Matrix{NT}
@@ -52,7 +52,7 @@ struct LinMPC{
5252
# dummy vals (updated just before optimization):
5353
R̂y, R̂u = zeros(NT, ny*Hp), zeros(NT, nu*Hp)
5454
noR̂u = iszero(L_Hp)
55-
S, T = init_ΔUtoU(nu, Hp, Hc)
55+
S, T = init_ΔUtoU(model, Hp, Hc)
5656
E, F, G, J, K, V, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂ = init_predmat(estim, model, Hp, Hc)
5757
con, S̃, Ñ_Hc, Ẽ = init_defaultcon(estim, Hp, Hc, Cwt, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂)
5858
P̃, q̃, p = init_quadprog(model, Ẽ, S̃, M_Hp, Ñ_Hc, L_Hp)

src/controller/nonlinmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct NonLinMPC{
2424
R̂u::Vector{NT}
2525
R̂y::Vector{NT}
2626
noR̂u::Bool
27-
::BitMatrix
28-
T::BitMatrix
27+
::Matrix{NT}
28+
T::Matrix{NT}
2929
::Matrix{NT}
3030
F::Vector{NT}
3131
G::Matrix{NT}
@@ -53,7 +53,7 @@ struct NonLinMPC{
5353
# dummy vals (updated just before optimization):
5454
R̂y, R̂u = zeros(NT, ny*Hp), zeros(NT, nu*Hp)
5555
noR̂u = iszero(L_Hp)
56-
S, T = init_ΔUtoU(nu, Hp, Hc)
56+
S, T = init_ΔUtoU(model, Hp, Hc)
5757
E, F, G, J, K, V, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂ = init_predmat(estim, model, Hp, Hc)
5858
con, S̃, Ñ_Hc, Ẽ = init_defaultcon(estim, Hp, Hc, Cwt, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂)
5959
P̃, q̃, p = init_quadprog(model, Ẽ, S̃, M_Hp, Ñ_Hc, L_Hp)

src/predictive_control.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ end
742742
set_objective_linear_coef!(::PredictiveController, _ ) = nothing
743743

744744
@doc raw"""
745-
init_ΔUtoU(nu, Hp) -> S, T
745+
init_ΔUtoU(model, Hp, Hc) -> S, T
746746
747747
Init manipulated input increments to inputs conversion matrices.
748748
@@ -752,8 +752,9 @@ are calculated by:
752752
\mathbf{U} = \mathbf{S} \mathbf{ΔU} + \mathbf{T} \mathbf{u}(k-1) \\
753753
```
754754
"""
755-
function init_ΔUtoU(nu, Hp, Hc)
756-
I_nu = BitMatrix(I(nu))
755+
function init_ΔUtoU(model::SimModel{NT}, Hp, Hc) where {NT<:Real}
756+
# S and T are `Matrix{NT}` since conversion is faster than `Matrix{Bool}` or `BitMatrix`
757+
I_nu = Matrix{NT}(I, model.nu, model.nu)
757758
S_Hc = LowerTriangular(repeat(I_nu, Hc, Hc))
758759
S = [S_Hc; repeat(I_nu, Hp - Hc, Hc)]
759760
T = repeat(I_nu, Hp)
@@ -1082,14 +1083,13 @@ constraints:
10821083
```
10831084
"""
10841085
function relaxU(::SimModel{NT}, C, C_umin, C_umax, S) where {NT<:Real}
1085-
S_NT = convert(Matrix{NT}, S)
10861086
if !isinf(C) # ΔŨ = [ΔU; ϵ]
10871087
# ϵ impacts ΔU → U conversion for constraint calculations:
1088-
A_Umin, A_Umax = -[S_NT C_umin], [S_NT -C_umax]
1088+
A_Umin, A_Umax = -[S C_umin], [S -C_umax]
10891089
# ϵ has no impact on ΔU → U conversion for prediction calculations:
1090-
= [S falses(size(S, 1))]
1090+
= [S zeros(NT, size(S, 1))]
10911091
else # ΔŨ = ΔU (only hard constraints)
1092-
A_Umin, A_Umax = -S_NT, S_NT
1092+
A_Umin, A_Umax = -S, S
10931093
= S
10941094
end
10951095
return A_Umin, A_Umax, S̃

0 commit comments

Comments
 (0)