Skip to content

Commit ed9088f

Browse files
committed
improve performance with @views for fhat and hhat functions
1 parent 661d5f1 commit ed9088f

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

example/juMPC.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ updatestate!(ssKalmanFilter2,[1, 1],[1,1])
6262
initstate!(ssKalmanFilter1,[0,0],[2,1])
6363

6464
uscKalmanFilter1 = UnscentedKalmanFilter(linModel1)
65+
6566
updatestate!(uscKalmanFilter1,[0,0],[2,1])
6667

6768
initstate!(uscKalmanFilter1,[0,0],[2,1])
@@ -130,7 +131,7 @@ function test_mpc(model, mpc)
130131
end
131132

132133
@time u_data, y_data, r_data, d_data = test_mpc(linModel4, mpc)
133-
#@profview u_data, y_data, r_data, d_data = test_mpc(linModel4, mpc)
134+
@profview u_data, y_data, r_data, d_data = test_mpc(linModel4, mpc)
134135
#=
135136
using PlotThemes, Plots
136137
#theme(:default)

src/predictive_control.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ end
782782
783783
Init `b` vector for the linear model inequality constraints (``\mathbf{A ΔŨ ≤ b}``).
784784
"""
785-
function init_constraint(mpc, ::LinModel, F, lastu)
785+
function init_constraint(mpc::C, ::LinModel, F, lastu) where {C<:PredictiveController}
786786
b = [
787787
-mpc.Umin + mpc.T_Hc*lastu
788788
+mpc.Umax - mpc.T_Hc*lastu

src/sim_model.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ function LinModel(
161161
C = sys_dis.C
162162
Dd = sys_dis.D[:,nu+1:end]
163163
# the `let` block captures and fixes A, Bu, Bd, C, Dd values (faster computations):
164-
f(x, u, d) = A*x + Bu*u + Bd*d
165-
h(x, d) = C*x + Dd*d
164+
f = let A=A, Bu=Bu, Bd=Bd
165+
(x, u, d) -> A*x + Bu*u + Bd*d
166+
end
167+
h = let C=C, Dd=Dd
168+
(x, d) -> C*x + Dd*d
169+
end
166170
return LinModel_ssfunc(A, Bu, C, Bd, Dd, f, h, Ts, nu, nx, ny, nd)
167171
end
168172

src/state_estim.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function stoch_ym2y(model::SimModel, i_ym, Asm, Bsm, Csm, Dsm)
7373
end
7474

7575
@doc raw"""
76-
init_estimstoch(model::SimModel, i_ym, nint_ym::Vector{Int})
76+
init_estimstoch(i_ym, nint_ym::Vector{Int})
7777
7878
Calc stochastic model matrices from output integrators specifications for state estimation.
7979
@@ -150,10 +150,10 @@ end
150150
@doc raw"""
151151
f̂(estim::StateEstimator, x̂, u, d)
152152
153-
Update the augmented model state for estimation.
153+
State function ``\mathbf{f̂}`` of the augmented model.
154154
155-
By introducing an augmented state vector ``\mathbf{x}`` like in [`augment_model`](@ref) doc,
156-
the ``\mathbf{f̂}`` method updates it from the augmented model, defined as :
155+
By introducing an augmented state vector ``\mathbf{x}`` like in [`augment_model`](@ref), the
156+
function returns the next state of the augmented model, defined as:
157157
```math
158158
\begin{aligned}
159159
\mathbf{x}(k+1) &= \mathbf{f̂}\Big(\mathbf{x}(k), \mathbf{u}(k), \mathbf{d}(k)\Big) \\
@@ -162,8 +162,9 @@ the ``\mathbf{f̂}`` method updates it from the augmented model, defined as :
162162
```
163163
"""
164164
function (estim::E, x̂, u, d) where {E<:StateEstimator}
165-
# TODO: consider using views : https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-views
166-
return [estim.model.f(x̂[1:estim.model.nx], u, d); estim.As*x̂[estim.model.nx+1:end]]
165+
# `@views` macro avoid copies with matrix slice operator e.g. [a:b]
166+
nx = estim.model.nx
167+
@views return [estim.model.f(x̂[1:nx], u, d); estim.As*x̂[nx+1:end]]
167168
end
168169

169170
@doc raw"""
@@ -172,7 +173,9 @@ end
172173
Output function ``\mathbf{ĥ}`` of the augmented model, see [`f̂`](@ref) for details.
173174
"""
174175
function (estim::E, x̂, d) where {E<:StateEstimator}
175-
return estim.model.h(x̂[1:estim.model.nx], d) + estim.Cs*x̂[estim.model.nx+1:end]
176+
# `@views` macro avoid copies with matrix slice operator e.g. [a:b]
177+
nx = estim.model.nx
178+
@views return estim.model.h(x̂[1:nx], d) + estim.Cs*x̂[nx+1:end]
176179
end
177180

178181

0 commit comments

Comments
 (0)