Skip to content

Commit 6fecbfe

Browse files
committed
add examples to setconstraint! doc
1 parent 365b34b commit 6fecbfe

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
33
authors = ["Francis Gagnon"]
4-
version = "0.6.1"
4+
version = "0.6.2"
55

66
[deps]
77
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"

src/predictive_control.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ and ``\mathbf{ŷ_{max}}`` are soft by default.
8585
- `c_Δumax=fill(0.0,nu)` : `Δumax` softness weights ``\mathbf{c_{Δu_{max}}}``
8686
- `c_ŷmin=fill(1.0,ny)` : `ŷmin` softness weights ``\mathbf{c_{ŷ_{min}}}``
8787
- `c_ŷmax=fill(1.0,ny)` : `ŷmax` softness weights ``\mathbf{c_{ŷ_{max}}}``
88+
89+
# Examples
90+
```jldoctest
91+
julia> mpc = LinMPC(setop!(LinModel(tf(3, [30, 1]), 4), uop=[50], yop=[25]));
92+
93+
julia> mpc = setconstraint!(mpc, umin=[0], umax=[100], c_umin=[0.0], c_umax=[0.0]);
94+
95+
julia> mpc = setconstraint!(mpc, Δumin=[-10], Δumax=[+10], c_Δumin=[1.0], c_Δumax=[1.0])
96+
97+
```
8898
"""
8999
function setconstraint!(
90100
mpc::PredictiveController;
@@ -239,7 +249,7 @@ See also [`LinMPC`](@ref), [`NonLinMPC`](@ref).
239249
```jldoctest
240250
julia> mpc = LinMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Hp=1000, Hc=1);
241251
242-
julia> u = moveinput!(mpc, [5]); round.(u, digits=3)
252+
julia> ry = [5]; u = moveinput!(mpc, ry); round.(u, digits=3)
243253
1-element Vector{Float64}:
244254
1.0
245255
```

test/test_predictive_control.jl

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ end
6565
@test u [1] atol=1e-2
6666
_ , info = getinfo(mpc1)
6767
@test info[:u] u
68-
@test info[:Ŷ][end] 5 atol=1e-2
68+
@test info[:Ŷ][end] r[1] atol=1e-2
6969
mpc2 = LinMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Cwt=Inf, Hp=1000, Hc=1)
7070
u = moveinput!(mpc2, [5])
7171
@test u [1] atol=1e-2
@@ -141,45 +141,44 @@ end
141141
end
142142

143143
@testset "NonLinMPC moves and getinfo" begin
144-
linmodel = LinModel(tf(5, [2, 1]), 3)
144+
linmodel = LinModel(tf(5, [2, 1]), 3.0)
145145
nmpc_lin = NonLinMPC(linmodel, Nwt=[0], Hp=1000, Hc=1)
146146
r = [5]
147147
u = moveinput!(nmpc_lin, r)
148-
@test u [1] atol=1e-3
148+
@test u [1] atol=5e-2
149149
u = nmpc_lin(r)
150-
@test u [1] atol=1e-3
150+
@test u [1] atol=5e-2
151151
_ , info = getinfo(nmpc_lin)
152152
@test info[:u] u
153-
@test info[:Ŷ][end] 5 atol=1e-2
153+
@test info[:Ŷ][end] r[1] atol=5e-2
154154
Hp = 1000
155155
R̂y = fill(5, Hp)
156156
JE = (_ , ŶE, _ ) -> sum((ŶE[2:end] - R̂y).^2)
157157
nmpc = NonLinMPC(linmodel, Mwt=[0], Nwt=[0], Cwt=Inf, Ewt=1, JE=JE, Hp=Hp, Hc=1)
158158
u = moveinput!(nmpc)
159-
@test u [1] atol=1e-2
160-
f(x,u,_) = linmodel.A*x + linmodel.Bu*u
161-
h(x,_) = linmodel.C*x
162-
nonlinmodel = NonLinModel(f, h, 3, 1, 1, 1)
159+
@test u [1] atol=5e-2
160+
linmodel = LinModel([tf(5, [2, 1]) tf(7, [8,1])], 3.0, i_d=[2])
161+
f(x,u,d) = linmodel.A*x + linmodel.Bu*u + linmodel.Bd*d
162+
h(x,d) = linmodel.C*x + linmodel.Dd*d
163+
nonlinmodel = NonLinModel(f, h, 3.0, 1, 2, 1, 1)
163164
nmpc2 = NonLinMPC(nonlinmodel, Nwt=[0], Hp=1000, Hc=1)
164-
r = [5]
165-
u = moveinput!(nmpc2, r)
166-
@test u [1] atol=1e-2
167-
u = nmpc2(r)
168-
@test u [1] atol=1e-2
165+
d = [0.1]
166+
r = 7*d
167+
u = moveinput!(nmpc2, r, d)
168+
@test u [0] atol=5e-2
169+
u = nmpc2(r, d)
170+
@test u [0] atol=5e-2
169171
_ , info = getinfo(nmpc2)
170172
@test info[:u] u
171-
@test info[:Ŷ][end] 5 atol=1e-2
173+
@test info[:Ŷ][end] r[1] atol=5e-2
172174
nmpc3 = NonLinMPC(nonlinmodel, Nwt=[0], Cwt=Inf, Hp=1000, Hc=1)
173-
u = moveinput!(nmpc3, [5])
174-
@test u [1] atol=1e-2
175+
u = moveinput!(nmpc3, r, d)
176+
@test u [0] atol=5e-2
175177
nmpc4 = NonLinMPC(nonlinmodel, Mwt=[0], Nwt=[0], Lwt=[1], ru=[12])
176-
u = moveinput!(nmpc4, [0])
177-
@test u [12] atol=1e-2
178-
nmpc5 = NonLinMPC(nonlinmodel, Mwt=[0], Nwt=[0], Cwt=Inf, Ewt=1, JE=JE, Hp=Hp, Hc=1)
179-
u = moveinput!(nmpc5)
180-
@test u [1] atol=1e-2
181-
nmpc6 = setconstraint!(NonLinMPC(nonlinmodel, Cwt=Inf), ŷmin=[-1])
182-
C_Ŷmax_end = nmpc6.optim.nlp_model.operators.registered_multivariate_operators[end].f
178+
u = moveinput!(nmpc4, [0], d)
179+
@test u [12] atol=5e-2
180+
nmpc5 = setconstraint!(NonLinMPC(nonlinmodel, Cwt=Inf), ŷmin=[-1])
181+
C_Ŷmax_end = nmpc5.optim.nlp_model.operators.registered_multivariate_operators[end].f
183182
@test C_Ŷmax_end(Float64.((1.0, 1.0))) 0.0 # test con_nonlinprog_i(i,::NTuple{N, Float64})
184183
@test C_Ŷmax_end(Float32.((1.0, 1.0))) 0.0 # test con_nonlinprog_i(i,::NTuple{N, Real})
185184
end

0 commit comments

Comments
 (0)