Skip to content

Commit 3c225a0

Browse files
authored
Merge pull request #91 from JuliaControl/no_empty_default_d
Reduce allocations by avoiding `empty` for default `d` argument
2 parents e98514c + bf2a920 commit 3c225a0

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

src/controller/execute.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
@doc raw"""
2-
initstate!(mpc::PredictiveController, u, ym, d=[]) -> x̂
2+
initstate!(mpc::PredictiveController, u, ym, d=mpc.estim.model.dop) -> x̂
33
44
Init the states of `mpc.estim` [`StateEstimator`](@ref) and warm start `mpc.ΔŨ` at zero.
55
"""
6-
function initstate!(mpc::PredictiveController, u, ym, d=empty(mpc.estim.x̂0))
6+
function initstate!(mpc::PredictiveController, u, ym, d=mpc.estim.model.dop)
77
mpc.ΔŨ .= 0
88
return initstate!(mpc.estim, u, ym, d)
99
end
1010

1111
@doc raw"""
12-
moveinput!(mpc::PredictiveController, ry=mpc.estim.model.yop, d=[]; <keyword args>) -> u
12+
moveinput!(
13+
mpc::PredictiveController, ry=mpc.estim.model.yop, d=mpc.estim.model.dop;
14+
<keyword arguments>
15+
) -> u
1316
1417
Compute the optimal manipulated input value `u` for the current control period.
1518
@@ -29,14 +32,13 @@ See also [`LinMPC`](@ref), [`ExplicitMPC`](@ref), [`NonLinMPC`](@ref).
2932
3033
- `mpc::PredictiveController` : solve optimization problem of `mpc`.
3134
- `ry=mpc.estim.model.yop` : current output setpoints ``\mathbf{r_y}(k)``.
32-
- `d=[]` : current measured disturbances ``\mathbf{d}(k)``.
35+
- `d=mpc.estim.model.dop` : current measured disturbances ``\mathbf{d}(k)``.
3336
- `D̂=repeat(d, mpc.Hp)` or *`Dhat`* : predicted measured disturbances ``\mathbf{D̂}``, constant
3437
in the future by default or ``\mathbf{d̂}(k+j)=\mathbf{d}(k)`` for ``j=1`` to ``H_p``.
3538
- `R̂y=repeat(ry, mpc.Hp)` or *`Rhaty`* : predicted output setpoints ``\mathbf{R̂_y}``, constant
3639
in the future by default or ``\mathbf{r̂_y}(k+j)=\mathbf{r_y}(k)`` for ``j=1`` to ``H_p``.
37-
- `R̂u=repeat(mpc.estim.model.uop, mpc.Hp)` or *`Rhatu`* : predicted manipulated input
38-
setpoints, constant in the future by default or ``\mathbf{r̂_u}(k+j)=\mathbf{u_{op}}`` for
39-
``j=0`` to ``H_p-1``.
40+
- `R̂u=mpc.Uop` or *`Rhatu`* : predicted manipulated input setpoints, constant in the future
41+
by default or ``\mathbf{r̂_u}(k+j)=\mathbf{u_{op}}`` for ``j=0`` to ``H_p-1``.
4042
- `ym=nothing` : current measured outputs ``\mathbf{y^m}(k)``, only required if `mpc.estim`
4143
is an [`InternalModel`](@ref).
4244
@@ -52,10 +54,10 @@ julia> ry = [5]; u = moveinput!(mpc, ry); round.(u, digits=3)
5254
function moveinput!(
5355
mpc::PredictiveController,
5456
ry::Vector = mpc.estim.model.yop,
55-
d ::Vector = empty(mpc.estim.x̂0);
57+
d ::Vector = mpc.estim.model.dop;
5658
Dhat ::Vector = repeat(d, mpc.Hp),
5759
Rhaty::Vector = repeat(ry, mpc.Hp),
58-
Rhatu::Vector = mpc.noR̂u ? empty(mpc.estim.x̂0) : repeat(mpc.estim.model.uop, mpc.Hp),
60+
Rhatu::Vector = mpc.Uop,
5961
ym::Union{Vector, Nothing} = nothing,
6062
= Dhat,
6163
R̂y = Rhaty,
@@ -506,11 +508,11 @@ end
506508
set_objective_linear_coef!(::PredictiveController, _ ) = nothing
507509

508510
"""
509-
updatestate!(mpc::PredictiveController, u, ym, d=[]) -> x̂
511+
updatestate!(mpc::PredictiveController, u, ym, d=mpc.estim.model.dop) -> x̂
510512
511513
Call [`updatestate!`](@ref) on `mpc.estim` [`StateEstimator`](@ref).
512514
"""
513-
function updatestate!(mpc::PredictiveController, u, ym, d=empty(mpc.estim.x̂0))
515+
function updatestate!(mpc::PredictiveController, u, ym, d=mpc.estim.model.dop)
514516
return updatestate!(mpc.estim, u, ym, d)
515517
end
516518
updatestate!(::PredictiveController, _ ) = throw(ArgumentError("missing measured outputs ym"))

src/estimator/execute.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ end
7979

8080

8181
@doc raw"""
82-
initstate!(estim::StateEstimator, u, ym, d=[]) -> x̂
82+
initstate!(estim::StateEstimator, u, ym, d=estim.model.dop) -> x̂
8383
8484
Init `estim.x̂0` states from current inputs `u`, measured outputs `ym` and disturbances `d`.
8585
@@ -111,7 +111,7 @@ julia> evaloutput(estim) ≈ y
111111
true
112112
```
113113
"""
114-
function initstate!(estim::StateEstimator, u, ym, d=empty(estim.x̂0))
114+
function initstate!(estim::StateEstimator, u, ym, d=estim.model.dop)
115115
# --- validate arguments ---
116116
validate_args(estim, u, ym, d)
117117
# --- init state estimate ----
@@ -161,7 +161,7 @@ Left `estim.x̂0` estimate unchanged if `model` is not a [`LinModel`](@ref).
161161
init_estimate!(::StateEstimator, ::SimModel, _ , _ , _ ) = nothing
162162

163163
@doc raw"""
164-
evaloutput(estim::StateEstimator, d=[]) -> ŷ
164+
evaloutput(estim::StateEstimator, d=estim.model.dop) -> ŷ
165165
166166
Evaluate `StateEstimator` outputs `ŷ` from `estim.x̂0` states and disturbances `d`.
167167
@@ -176,7 +176,7 @@ julia> ŷ = evaloutput(kf)
176176
20.0
177177
```
178178
"""
179-
function evaloutput(estim::StateEstimator{NT}, d=empty(estim.x̂0)) where NT <: Real
179+
function evaloutput(estim::StateEstimator{NT}, d=estim.model.dop) where NT <: Real
180180
validate_args(estim.model, d)
181181
ŷ0 = Vector{NT}(undef, estim.model.ny)
182182
d0 = d - estim.model.dop
@@ -187,10 +187,10 @@ function evaloutput(estim::StateEstimator{NT}, d=empty(estim.x̂0)) where NT <:
187187
end
188188

189189
"Functor allowing callable `StateEstimator` object as an alias for `evaloutput`."
190-
(estim::StateEstimator)(d=empty(estim.x̂0)) = evaloutput(estim, d)
190+
(estim::StateEstimator)(d=estim.model.dop) = evaloutput(estim, d)
191191

192192
@doc raw"""
193-
updatestate!(estim::StateEstimator, u, ym, d=[]) -> x̂
193+
updatestate!(estim::StateEstimator, u, ym, d=estim.model.dop) -> x̂
194194
195195
Update `estim.x̂0` estimate with current inputs `u`, measured outputs `ym` and dist. `d`.
196196
@@ -207,7 +207,7 @@ julia> x̂ = updatestate!(kf, [1], [0]) # x̂[2] is the integrator state (nint_y
207207
0.0
208208
```
209209
"""
210-
function updatestate!(estim::StateEstimator, u, ym, d=empty(estim.x̂0))
210+
function updatestate!(estim::StateEstimator, u, ym, d=estim.model.dop)
211211
validate_args(estim, u, ym, d)
212212
u0, ym0, d0 = remove_op!(estim, u, ym, d)
213213
x̂0next = update_estimate!(estim, u0, ym0, d0)

src/predictive_control.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Abstract supertype of all predictive controllers.
33
44
---
55
6-
(mpc::PredictiveController)(ry, d=[]; kwargs...) -> u
6+
(mpc::PredictiveController)(ry, d=mpc.estim.model.dop; kwargs...) -> u
77
88
Functor allowing callable `PredictiveController` object as an alias for [`moveinput!`](@ref).
99
@@ -42,7 +42,7 @@ end
4242
"Functor allowing callable `PredictiveController` object as an alias for `moveinput!`."
4343
function (mpc::PredictiveController)(
4444
ry::Vector = mpc.estim.model.yop,
45-
d ::Vector = empty(mpc.estim.x̂0);
45+
d ::Vector = mpc.estim.model.dop;
4646
kwargs...
4747
)
4848
return moveinput!(mpc, ry, d; kwargs...)

src/sim_model.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Abstract supertype of [`LinModel`](@ref) and [`NonLinModel`](@ref) types.
55
66
---
77
8-
(model::SimModel)(d=[]) -> y
8+
(model::SimModel)(d=model.dop) -> y
99
1010
Functor allowing callable `SimModel` object as an alias for [`evaloutput`](@ref).
1111
@@ -160,7 +160,7 @@ end
160160
detailstr(model::SimModel) = ""
161161

162162
@doc raw"""
163-
initstate!(model::SimModel, u, d=[]) -> x
163+
initstate!(model::SimModel, u, d=model.dop) -> x
164164
165165
Init `model.x0` with manipulated inputs `u` and measured disturbances `d` steady-state.
166166
@@ -182,7 +182,7 @@ julia> x ≈ updatestate!(model, u)
182182
true
183183
```
184184
"""
185-
function initstate!(model::SimModel, u, d=empty(model.x0))
185+
function initstate!(model::SimModel, u, d=model.dop)
186186
validate_args(model::SimModel, d, u)
187187
u0, d0 = u - model.uop, d - model.dop
188188
steadystate!(model, u0, d0)
@@ -191,7 +191,7 @@ function initstate!(model::SimModel, u, d=empty(model.x0))
191191
end
192192

193193
"""
194-
updatestate!(model::SimModel, u, d=[]) -> x
194+
updatestate!(model::SimModel, u, d=model.dop) -> x
195195
196196
Update `model.x0` states with current inputs `u` and measured disturbances `d`.
197197
@@ -204,7 +204,7 @@ julia> x = updatestate!(model, [1])
204204
1.0
205205
```
206206
"""
207-
function updatestate!(model::SimModel{NT}, u, d=empty(model.x0)) where NT <: Real
207+
function updatestate!(model::SimModel{NT}, u, d=model.dop) where NT <: Real
208208
validate_args(model::SimModel, d, u)
209209
xnext0 = Vector{NT}(undef, model.nx)
210210
u0, d0 = u - model.uop, d - model.dop
@@ -217,7 +217,7 @@ function updatestate!(model::SimModel{NT}, u, d=empty(model.x0)) where NT <: Rea
217217
end
218218

219219
"""
220-
evaloutput(model::SimModel, d=[]) -> y
220+
evaloutput(model::SimModel, d=model.dop) -> y
221221
222222
Evaluate `SimModel` outputs `y` from `model.x0` states and measured disturbances `d`.
223223
@@ -232,7 +232,7 @@ julia> y = evaloutput(model)
232232
20.0
233233
```
234234
"""
235-
function evaloutput(model::SimModel{NT}, d=empty(model.x0)) where NT <: Real
235+
function evaloutput(model::SimModel{NT}, d=model.dop) where NT <: Real
236236
validate_args(model, d)
237237
y0 = Vector{NT}(undef, model.ny)
238238
d0 = d - model.dop
@@ -262,7 +262,7 @@ to_mat(A::Real, dims...) = fill(A, dims)
262262

263263

264264
"Functor allowing callable `SimModel` object as an alias for `evaloutput`."
265-
(model::SimModel)(d=empty(model.x0)) = evaloutput(model::SimModel, d)
265+
(model::SimModel)(d=model.dop) = evaloutput(model::SimModel, d)
266266

267267
include("model/linmodel.jl")
268268
include("model/solver.jl")

src/state_estim.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Abstract supertype of all state estimators.
33
44
---
55
6-
(estim::StateEstimator)(d=[]) -> ŷ
6+
(estim::StateEstimator)(d=estim.model.dop) -> ŷ
77
88
Functor allowing callable `StateEstimator` object as an alias for [`evaloutput`](@ref).
99

0 commit comments

Comments
 (0)