1
1
" Include all the data for the constraints of [`PredictiveController`](@ref)"
2
- struct ControllerConstraint{NT<: Real }
2
+ struct ControllerConstraint{NT<: Real , GCfunc <: Function }
3
3
ẽx̂ :: Matrix{NT}
4
4
fx̂ :: Vector{NT}
5
5
gx̂ :: Matrix{NT}
@@ -31,12 +31,14 @@ struct ControllerConstraint{NT<:Real}
31
31
c_x̂min :: Vector{NT}
32
32
c_x̂max :: Vector{NT}
33
33
i_g :: BitVector
34
+ gc! :: GCfunc
35
+ nc :: Int
34
36
end
35
37
36
38
@doc raw """
37
39
setconstraint!(mpc::PredictiveController; <keyword arguments>) -> mpc
38
40
39
- Set the constraint parameters of the [`PredictiveController`](@ref) `mpc`.
41
+ Set the bound constraint parameters of the [`PredictiveController`](@ref) `mpc`.
40
42
41
43
The predictive controllers support both soft and hard constraints, defined by:
42
44
```math
@@ -146,7 +148,8 @@ function setconstraint!(
146
148
C_Δumin = C_Deltaumin, C_Δumax = C_Deltaumax,
147
149
)
148
150
model, con, optim = mpc. estim. model, mpc. con, mpc. optim
149
- nu, ny, nx̂, Hp, Hc, nϵ = model. nu, model. ny, mpc. estim. nx̂, mpc. Hp, mpc. Hc, mpc. nϵ
151
+ nu, ny, nx̂, Hp, Hc = model. nu, model. ny, mpc. estim. nx̂, mpc. Hp, mpc. Hc
152
+ nϵ, nc = mpc. nϵ, con. nc
150
153
notSolvedYet = (JuMP. termination_status (optim) == JuMP. OPTIMIZE_NOT_CALLED)
151
154
if isnothing (Umin) && ! isnothing (umin)
152
155
size (umin) == (nu,) || throw (ArgumentError (" umin size must be $((nu,)) " ))
@@ -275,7 +278,8 @@ function setconstraint!(
275
278
i_Ymin, i_Ymax = .! isinf .(con. Y0min), .! isinf .(con. Y0max)
276
279
i_x̂min, i_x̂max = .! isinf .(con. x̂0min), .! isinf .(con. x̂0max)
277
280
if notSolvedYet
278
- con. i_b[:], con. i_g[:], con. A[:] = init_matconstraint_mpc (model,
281
+ con. i_b[:], con. i_g[:], con. A[:] = init_matconstraint_mpc (
282
+ model, nc,
279
283
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax,
280
284
i_Ymin, i_Ymax, i_x̂min, i_x̂max,
281
285
con. A_Umin, con. A_Umax, con. A_ΔŨmin, con. A_ΔŨmax,
@@ -287,9 +291,10 @@ function setconstraint!(
287
291
JuMP. delete (optim, optim[:linconstraint ])
288
292
JuMP. unregister (optim, :linconstraint )
289
293
@constraint (optim, linconstraint, A* ΔŨvar .≤ b)
290
- setnonlincon ! (mpc, model, optim)
294
+ set_nonlincon ! (mpc, model, optim)
291
295
else
292
- i_b, i_g = init_matconstraint_mpc (model,
296
+ i_b, i_g = init_matconstraint_mpc (
297
+ model, nc,
293
298
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax,
294
299
i_Ymin, i_Ymax, i_x̂min, i_x̂max
295
300
)
302
307
303
308
304
309
@doc raw """
305
- init_matconstraint_mpc(model::LinModel,
306
- i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max, args...
310
+ init_matconstraint_mpc(
311
+ model::LinModel, nc::Int,
312
+ i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
313
+ args...
307
314
) -> i_b, i_g, A
308
315
309
316
Init `i_b`, `i_g` and `A` matrices for the linear and nonlinear inequality constraints.
@@ -315,19 +322,22 @@ The linear and nonlinear inequality constraints are respectively defined as:
315
322
\m athbf{g(ΔŨ)} &≤ \m athbf{0}
316
323
\e nd{aligned}
317
324
```
318
- `i_b` is a `BitVector` including the indices of ``\m athbf{b}`` that are finite numbers.
319
- `i_g` is a similar vector but for the indices of ``\m athbf{g}`` (empty if `model` is a
320
- [`LinModel`](@ref)). The method also returns the ``\m athbf{A}`` matrix if `args` is
321
- provided. In such a case, `args` needs to contain all the inequality constraint matrices:
325
+ The argument `nc` is the number of custom nonlinear constraints in ``\m athbf{g_c}``. `i_b`
326
+ is a `BitVector` including the indices of ``\m athbf{b}`` that are finite numbers. `i_g` is a
327
+ similar vector but for the indices of ``\m athbf{g}``. The method also returns the
328
+ ``\m athbf{A}`` matrix if `args` is provided. In such a case, `args` needs to contain all
329
+ the inequality constraint matrices:
322
330
`A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max`.
323
331
"""
324
- function init_matconstraint_mpc (:: LinModel{NT} ,
325
- i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max, args...
332
+ function init_matconstraint_mpc (
333
+ :: LinModel{NT} , nc:: Int ,
334
+ i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
335
+ args...
326
336
) where {NT<: Real }
327
337
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_Ymin; i_Ymax; i_x̂min; i_x̂max]
328
- i_g = BitVector ( )
338
+ i_g = trues (nc )
329
339
if isempty (args)
330
- A = zeros (NT, length (i_b), 0 )
340
+ A = nothing
331
341
else
332
342
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max = args
333
343
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
@@ -336,13 +346,15 @@ function init_matconstraint_mpc(::LinModel{NT},
336
346
end
337
347
338
348
" Init `i_b, A` without outputs and terminal constraints if `model` is not a [`LinModel`](@ref)."
339
- function init_matconstraint_mpc (:: SimModel{NT} ,
340
- i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max, args...
349
+ function init_matconstraint_mpc (
350
+ :: SimModel{NT} , nc:: Int ,
351
+ i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
352
+ args...
341
353
) where {NT<: Real }
342
354
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax]
343
- i_g = [i_Ymin; i_Ymax; i_x̂min; i_x̂max]
355
+ i_g = [i_Ymin; i_Ymax; i_x̂min; i_x̂max; trues (nc) ]
344
356
if isempty (args)
345
- A = zeros (NT, length (i_b), 0 )
357
+ A = nothing
346
358
else
347
359
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, _ , _ , _ , _ = args
348
360
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax]
@@ -351,7 +363,7 @@ function init_matconstraint_mpc(::SimModel{NT},
351
363
end
352
364
353
365
" By default, there is no nonlinear constraint, thus do nothing."
354
- setnonlincon ! (:: PredictiveController , :: SimModel , :: JuMP.GenericModel ) = nothing
366
+ set_nonlincon ! (:: PredictiveController , :: SimModel , :: JuMP.GenericModel ) = nothing
355
367
356
368
"""
357
369
default_Hp(model::LinModel)
@@ -625,16 +637,20 @@ function init_quadprog(::SimModel{NT}, Ẽ, S̃, M_Hp, Ñ_Hc, L_Hp) where {NT<:
625
637
end
626
638
627
639
"""
628
- init_defaultcon_mpc(estim, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂) -> con, S̃, Ñ_Hc, Ẽ
640
+ init_defaultcon_mpc(
641
+ estim, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂,
642
+ gc!=(_,_,_,_,_,_)->nothing, nc=0
643
+ ) -> con, S̃, Ñ_Hc, Ẽ
629
644
630
645
Init `ControllerConstraint` struct with default parameters based on estimator `estim`.
631
646
632
647
Also return `S̃`, `Ñ_Hc` and `Ẽ` matrices for the the augmented decision vector `ΔŨ`.
633
648
"""
634
649
function init_defaultcon_mpc (
635
650
estim:: StateEstimator{NT} ,
636
- Hp, Hc, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂, bx̂
637
- ) where {NT<: Real }
651
+ Hp, Hc, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂, bx̂,
652
+ gc!:: GCfunc = (_,_,_,_,_,_)-> nothing , nc= 0
653
+ ) where {NT<: Real , GCfunc<: Function }
638
654
model = estim. model
639
655
nu, ny, nx̂ = model. nu, model. ny, estim. nx̂
640
656
nϵ = isinf (C) ? 0 : 1
@@ -661,16 +677,17 @@ function init_defaultcon_mpc(
661
677
i_Ymin, i_Ymax = .! isinf .(Y0min), .! isinf .(Y0max)
662
678
i_x̂min, i_x̂max = .! isinf .(x̂0min), .! isinf .(x̂0max)
663
679
i_b, i_g, A = init_matconstraint_mpc (
664
- model,
680
+ model, nc,
665
681
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
666
682
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂max, A_x̂min
667
683
)
668
684
b = zeros (NT, size (A, 1 )) # dummy b vector (updated just before optimization)
669
- con = ControllerConstraint {NT} (
685
+ con = ControllerConstraint {NT, GCfunc } (
670
686
ẽx̂ , fx̂ , gx̂ , jx̂ , kx̂ , vx̂ , bx̂ ,
671
687
U0min , U0max , ΔŨmin , ΔŨmax , Y0min , Y0max , x̂0min , x̂0max,
672
688
A_Umin , A_Umax, A_ΔŨmin, A_ΔŨmax , A_Ymin , A_Ymax , A_x̂min , A_x̂max,
673
- A , b , i_b , C_ymin , C_ymax , c_x̂min , c_x̂max , i_g
689
+ A , b , i_b , C_ymin , C_ymax , c_x̂min , c_x̂max , i_g,
690
+ gc! , nc
674
691
)
675
692
return con, nϵ, S̃, Ñ_Hc, Ẽ
676
693
end
0 commit comments