-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtranscription.jl
More file actions
1654 lines (1540 loc) · 70.8 KB
/
transcription.jl
File metadata and controls
1654 lines (1540 loc) · 70.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const COLLOCATION_NODE_TYPE = Float64
"""
Abstract supertype of all transcription methods of [`PredictiveController`](@ref).
The module currently supports [`SingleShooting`](@ref), [`MultipleShooting`](@ref),
[`TrapezoidalCollocation`](@ref) and [`OrthogonalCollocation`](@ref) transcription methods.
"""
abstract type TranscriptionMethod end
abstract type ShootingMethod <: TranscriptionMethod end
abstract type CollocationMethod <: TranscriptionMethod end
@doc raw"""
SingleShooting()
Construct a direct single shooting [`TranscriptionMethod`](@ref).
The decision variable in the optimization problem is (excluding the slack ``ϵ`` and without
any custom move blocking):
```math
\mathbf{Z} = \mathbf{ΔU} = \begin{bmatrix}
\mathbf{Δu}(k+0) \\
\mathbf{Δu}(k+1) \\
\vdots \\
\mathbf{Δu}(k+H_c-1) \end{bmatrix}
```
This method computes the predictions by calling the augmented discrete-time model
recursively over the prediction horizon ``H_p`` in the objective function, or by updating
the linear coefficients of the quadratic optimization for [`LinModel`](@ref). It is
generally more efficient for small control horizon ``H_c``, stable and mildly nonlinear
plant model/constraints.
"""
struct SingleShooting <: ShootingMethod end
@doc raw"""
MultipleShooting(; f_threads=false, h_threads=false)
Construct a direct multiple shooting [`TranscriptionMethod`](@ref).
The decision variable is (excluding ``ϵ``):
```math
\mathbf{Z} = \begin{bmatrix} \mathbf{ΔU} \\ \mathbf{X̂_0} \end{bmatrix}
```
thus it also includes the predicted states, expressed as deviation vectors from the
operating point ``\mathbf{x̂_{op}}`` (see [`augment_model`](@ref)):
```math
\mathbf{X̂_0} = \mathbf{X̂ - X̂_{op}} = \begin{bmatrix}
\mathbf{x̂}_i(k+1) - \mathbf{x̂_{op}} \\
\mathbf{x̂}_i(k+2) - \mathbf{x̂_{op}} \\
\vdots \\
\mathbf{x̂}_i(k+H_p) - \mathbf{x̂_{op}} \end{bmatrix}
```
where ``\mathbf{x̂}_i(k+j)`` is the state prediction for time ``k+j``, estimated by the
observer at time ``i=k`` or ``i=k-1`` depending on its `direct` flag. Note that
``\mathbf{X̂_0 = X̂}`` if the operating point is zero, which is typically the case in practice
for [`NonLinModel`](@ref).
This transcription computes the predictions by calling the augmented discrete-time model
in the equality constraint function recursively over ``H_p``, or by updating the linear
equality constraint vector for [`LinModel`](@ref). It is generally more efficient for large
control horizon ``H_c``, unstable or highly nonlinear models/constraints. Multithreading
with `f_threads` or `h_threads` keyword arguments can be advantageous if ``\mathbf{f}`` or
``\mathbf{h}`` in the [`NonLinModel`](@ref) is expensive to evaluate, respectively.
Sparse optimizers like `OSQP` or `Ipopt` and sparse Jacobian computations are recommended
for this transcription method.
"""
struct MultipleShooting <: ShootingMethod
f_threads::Bool
h_threads::Bool
function MultipleShooting(; f_threads=false, h_threads=false)
return new(f_threads, h_threads)
end
end
@doc raw"""
TrapezoidalCollocation(h::Int=0; f_threads=false, h_threads=false)
Construct an implicit trapezoidal [`TranscriptionMethod`](@ref) with `h`th order hold.
This is the simplest collocation method. It supports continuous-time [`NonLinModel`](@ref)s
only. The decision variables are the same as for [`MultipleShooting`](@ref), hence similar
computational costs. See the same docstring for descriptions of `f_threads` and `h_threads`
keywords. The `h` argument is `0` or `1`, for piecewise constant or linear manipulated
inputs ``\mathbf{u}`` (`h=1` is slightly less expensive). Note that the various [`DiffSolver`](@ref)
here assume zero-order hold, so `h=1` will induce a plant-model mismatch if the plant is
simulated with these solvers. Measured disturbances ``\mathbf{d}`` are piecewise linear.
This transcription computes the predictions by calling the continuous-time model in the
equality constraint function and by using the implicit trapezoidal rule. It can handle
moderately stiff systems and is A-stable. See Extended Help for more details.
!!! warning
The built-in [`StateEstimator`](@ref) will still use the `solver` provided at the
construction of the [`NonLinModel`](@ref) to estimate the plant states, not the
trapezoidal rule (see `supersample` option of [`RungeKutta`](@ref) for stiff systems).
Sparse optimizers like `Ipopt` and sparse Jacobian computations are recommended for this
transcription method.
# Extended Help
!!! details "Extended Help"
Note that the stochastic model of the unmeasured disturbances is strictly discrete-time,
as described in [`ModelPredictiveControl.init_estimstoch`](@ref). Collocation methods
require continuous-time dynamics. Because of this, the stochastic states are transcribed
separately using a [`MultipleShooting`](@ref) method. See [`con_nonlinprogeq!`](@ref)
for more details.
"""
struct TrapezoidalCollocation <: CollocationMethod
h::Int
no::Int
f_threads::Bool
h_threads::Bool
function TrapezoidalCollocation(h::Int=0; f_threads=false, h_threads=false)
if !(h == 0 || h == 1)
throw(ArgumentError("h argument must be 0 or 1 for TrapezoidalCollocation."))
end
no = 2 # 2 collocation points per intervals for trapezoidal rule
return new(h, no, f_threads, h_threads)
end
end
@doc raw"""
OrthogonalCollocation(
h::Int=0, no::Int=3; f_threads=false, h_threads=false, roots=:gaussradau
)
Construct an orthogonal collocation on finite elements [`TranscriptionMethod`](@ref).
Also known as pseudo-spectral method. It supports continuous-time [`NonLinModel`](@ref)s
only. The `h` argument is the hold order for ``\mathbf{u}`` (`0` or `1`), and the `no`
argument, the number of collocation points ``n_o``. The decision variable is similar to
[`MultipleShooting`](@ref), but it also includes the collocation points:
```math
\mathbf{Z} = \begin{bmatrix} \mathbf{ΔU} \\ \mathbf{X̂_0} \\ \mathbf{K} \end{bmatrix}
```
where ``\mathbf{K}`` encompasses all the intermediate stages of the deterministic states
(the first `nx` elements of ``\mathbf{x̂}``):
```math
\mathbf{K} = \begin{bmatrix}
\mathbf{k}_{1}(k+0) \\
\mathbf{k}_{2}(k+0) \\
\vdots \\
\mathbf{k}_{n_o}(k+0) \\
\mathbf{k}_{1}(k+1) \\
\mathbf{k}_{2}(k+1) \\
\vdots \\
\mathbf{k}_{n_o}(k+H_p-1) \end{bmatrix}
```
and ``\mathbf{k}_i(k+j)`` is the deterministic state prediction for the ``i``th collocation
point at the ``j``th stage/interval/finite element (details in Extended Help). The `roots`
keyword argument is either `:gaussradau` or `:gausslegendre`, for Gauss-Radau or
Gauss-Legendre quadrature, respectively. See [`MultipleShooting`](@ref) docstring for
descriptions of `f_threads` and `h_threads` keywords. This transcription computes the
predictions by enforcing the collocation and continuity constraints at the collocation
points. It is efficient for highly stiff systems, but generally more expensive than the
other methods for non-stiff systems. See Extended Help for more details.
!!! warning
The built-in [`StateEstimator`](@ref) will still use the `solver` provided at the
construction of the [`NonLinModel`](@ref) to estimate the plant states, not orthogonal
collocation (see `supersample` option of [`RungeKutta`](@ref) for stiff systems).
Sparse optimizers like `Ipopt` and sparse Jacobian computations are highly recommended for
this transcription method (sparser formulation than [`MultipleShooting`](@ref)).
# Extended Help
!!! details "Extended Help"
As explained in the Extended Help of [`TrapezoidalCollocation`](@ref), the stochastic
states are left out of the ``\mathbf{K}`` vector since collocation methods require
continuous-time dynamics and the stochastic model is discrete.
The collocation points are located at the roots of orthogonal polynomials, which is
"optimal" for approximating the state trajectories with polynomials of degree ``n_o``.
The method then enforces the system dynamics at these points. The Gauss-Legendre scheme
is more accurate than Gauss-Radau but only A-stable, while the latter being L-stable.
See [`con_nonlinprogeq!`](@ref) for implementation details.
"""
struct OrthogonalCollocation <: CollocationMethod
h::Int
no::Int
f_threads::Bool
h_threads::Bool
τ::Vector{COLLOCATION_NODE_TYPE}
function OrthogonalCollocation(
h::Int=0, no::Int=3; f_threads=false, h_threads=false, roots=:gaussradau
)
if !(h == 0 || h == 1)
throw(ArgumentError("h argument must be 0 or 1 for OrthogonalCollocation."))
end
if roots==:gaussradau
x, _ = FastGaussQuadrature.gaussradau(no, COLLOCATION_NODE_TYPE)
# we reverse the nodes to include the τ=1.0 node:
τ = (reverse(-x) .+ 1) ./ 2
elseif roots==:gausslegendre
x, _ = FastGaussQuadrature.gausslegendre(no)
# converting [-1, 1] to [0, 1] (see
# https://en.wikipedia.org/wiki/Gaussian_quadrature#Change_of_interval):
τ = (x .+ 1) ./ 2
else
throw(ArgumentError("roots argument must be :gaussradau or :gausslegendre."))
end
return new(h, no, f_threads, h_threads, τ)
end
end
@doc raw"""
init_orthocolloc(model::SimModel, transcription::OrthogonalCollocation) -> Mo, Co, λo
Init the differentiation and continuity matrices for [`OrthogonalCollocation`](@ref).
Introducing ``τ_i``, the ``i``th root of the orthogonal polynomial normalized to the
interval ``[0, 1]``, and ``τ_0=0``, each state trajectories are approximated by a distinct
polynomial of degree ``n_o``. The differentiation matrix ``\mathbf{M_o}``, continuity
matrix ``\mathbf{C_o}`` and continuity coefficient ``λ_o`` are pre-computed with:
```math
\begin{aligned}
\mathbf{P_o} &= \begin{bmatrix}
τ_1^1 \mathbf{I} & τ_1^2 \mathbf{I} & \cdots & τ_1^{n_o} \mathbf{I} \\
τ_2^1 \mathbf{I} & τ_2^2 \mathbf{I} & \cdots & τ_2^{n_o} \mathbf{I} \\
\vdots & \vdots & \ddots & \vdots \\
τ_{n_o}^1 \mathbf{I} & τ_{n_o}^2 \mathbf{I} & \cdots & τ_{n_o}^{n_o} \mathbf{I} \end{bmatrix} \\
\mathbf{Ṗ_o} &= \begin{bmatrix}
τ_1^0 \mathbf{I} & 2τ_1^1 \mathbf{I} & \cdots & n_o τ_1^{n_o-1} \mathbf{I} \\
τ_2^0 \mathbf{I} & 2τ_2^1 \mathbf{I} & \cdots & n_o τ_2^{n_o-1} \mathbf{I} \\
\vdots & \vdots & \ddots & \vdots \\
τ_{n_o}^0 \mathbf{I} & 2τ_{n_o}^1 \mathbf{I} & \cdots & n_o τ_{n_o}^{n_o-1} \mathbf{I} \end{bmatrix} \\
\mathbf{M_o} &= \frac{1}{T_s} \mathbf{Ṗ_o} \mathbf{P_o}^{-1} \\
\mathbf{C_o} &= \begin{bmatrix}
L_1(1) \mathbf{I} & L_2(1) \mathbf{I} & \cdots & L_{n_o}(1) \mathbf{I} \end{bmatrix} \\
λ_o &= L_0(1)
\end{aligned}
```
where ``\mathbf{P_o}`` is a matrix to evaluate the polynamial values w/o the coefficients
and Y-intercept, and ``\mathbf{Ṗ_o}``, to evaluate its derivatives. The Lagrange polynomial
``L_j(τ)`` bases are defined as:
```math
L_j(τ) = \prod_{i=0, i≠j}^{n_o} \frac{τ - τ_i}{τ_j - τ_i}
```
"""
function init_orthocolloc(
model::SimModel{NT}, transcription::OrthogonalCollocation
) where {NT<:Real}
nx, no = model.nx, transcription.no
τ = transcription.τ
Po = Matrix{NT}(undef, nx*no, nx*no) # polynomial matrix (w/o the Y-intercept term)
Ṗo = Matrix{NT}(undef, nx*no, nx*no) # polynomial derivative matrix
for j=1:no, i=1:no
iRows = (1:nx) .+ nx*(i-1)
iCols = (1:nx) .+ nx*(j-1)
Po[iRows, iCols] = (τ[i]^j)*I(nx)
Ṗo[iRows, iCols] = (j*τ[i]^(j-1))*I(nx)
end
Mo = sparse((Ṗo/Po)/model.Ts)
Co = Matrix{NT}(undef, nx, nx*no)
for j=1:no
iCols = (1:nx) .+ nx*(j-1)
Co[:, iCols] = lagrange_end(j, transcription)*I(nx)
end
Co = sparse(Co)
λo = lagrange_end(0, transcription)
return Mo, Co, λo
end
"Return empty sparse matrices and `NaN` for other [`TranscriptionMethod`](@ref)"
init_orthocolloc(::SimModel, ::TranscriptionMethod) = spzeros(0,0), spzeros(0,0), NaN
"Evaluate the Lagrange basis polynomial ``L_j`` at `τ=1`."
function lagrange_end(j, transcription::OrthogonalCollocation)
τ_val = 1
τ_values = [0; transcription.τ] # including the τ=0 node for the Lagrange polynomials
j_index = j + 1 # because of the τ=0 node
τj = τ_values[j_index]
Lj = 1
for i in eachindex(τ_values)
i == j_index && continue
τi = τ_values[i]
Lj *= (τ_val - τi)/(τj - τi)
end
return Lj
end
function validate_transcription(::LinModel, ::CollocationMethod)
throw(ArgumentError("Collocation methods are not supported for LinModel."))
return nothing
end
function validate_transcription(::NonLinModel{<:Real, <:EmptySolver}, ::CollocationMethod)
throw(ArgumentError("Collocation methods require continuous-time NonLinModel."))
return nothing
end
validate_transcription(::SimModel, ::TranscriptionMethod) = nothing
"Get the number of elements in the optimization decision vector `Z`."
function get_nZ(estim::StateEstimator, ::SingleShooting, Hp, Hc)
return estim.model.nu*Hc
end
function get_nZ(estim::StateEstimator, ::TranscriptionMethod, Hp, Hc)
return estim.model.nu*Hc + estim.nx̂*Hp
end
function get_nZ(estim::StateEstimator, transcription::OrthogonalCollocation, Hp, Hc)
return estim.model.nu*Hc + estim.nx̂*Hp + estim.model.nx*transcription.no*Hp
end
"Get length of the `k` vector with all the solver intermediate steps or all the collocation pts."
get_nk(model::SimModel, ::ShootingMethod) = model.nk
get_nk(model::SimModel, transcription::CollocationMethod) = model.nx*transcription.no
@doc raw"""
init_predmat(
model::LinModel, estim, transcription::SingleShooting, Hp, Hc, nb
) -> E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
Construct the prediction matrices for [`LinModel`](@ref) and [`SingleShooting`](@ref).
The model predictions are evaluated from the deviation vectors (see [`setop!`](@ref)), the
decision variable ``\mathbf{Z = ΔU}`` (with a [`SingleShooting`](@ref) transcription), and:
```math
\begin{aligned}
\mathbf{Ŷ_0} &= \mathbf{E Z} + \mathbf{G d_0}(k) + \mathbf{J D̂_0}
+ \mathbf{K x̂_0}(k) + \mathbf{V u_0}(k-1)
+ \mathbf{B} + \mathbf{Ŷ_s} \\
&= \mathbf{E Z} + \mathbf{F}
\end{aligned}
```
in which ``\mathbf{x̂_0}(k) = \mathbf{x̂}_i(k) - \mathbf{x̂_{op}}``, with ``i = k`` if
`estim.direct==true`, otherwise ``i = k - 1``. The predicted outputs ``\mathbf{Ŷ_0}`` and
measured disturbances ``\mathbf{D̂_0}`` respectively include ``\mathbf{ŷ_0}(k+j)`` and
``\mathbf{d̂_0}(k+j)`` values with ``j=1`` to ``H_p``, and input increments ``\mathbf{ΔU}``,
``\mathbf{Δu}(k+j_ℓ)`` from ``ℓ=0`` to ``H_c-1``. The vector ``\mathbf{B}`` contains the
contribution for non-zero state ``\mathbf{x̂_{op}}`` and state update ``\mathbf{f̂_{op}}``
operating points (for linearization at non-equilibrium point, see [`linearize`](@ref)). The
stochastic predictions ``\mathbf{Ŷ_s=0}`` if `estim` is not a [`InternalModel`](@ref), see
[`init_stochpred`](@ref). The method also computes similar matrices for the predicted
terminal state at ``k+H_p``:
```math
\begin{aligned}
\mathbf{x̂_0}(k+H_p) &= \mathbf{e_x̂ Z} + \mathbf{g_x̂ d_0}(k) + \mathbf{j_x̂ D̂_0}
+ \mathbf{k_x̂ x̂_0}(k) + \mathbf{v_x̂ u_0}(k-1)
+ \mathbf{b_x̂} \\
&= \mathbf{e_x̂ Z} + \mathbf{f_x̂}
\end{aligned}
```
The matrices ``\mathbf{E, G, J, K, V, B, e_x̂, g_x̂, j_x̂, k_x̂, v_x̂, b_x̂}`` are defined in the
Extended Help section. The ``\mathbf{F}`` and ``\mathbf{f_x̂}`` vectors are recalculated at
each control period ``k``, see [`initpred!`](@ref) and [`linconstraint!`](@ref).
# Extended Help
!!! details "Extended Help"
Using the augmented matrices ``\mathbf{Â, B̂_u, Ĉ, B̂_d, D̂_d}`` in `estim` (see
[`augment_model`](@ref)), and the following two functions with integer arguments:
```math
\begin{aligned}
\mathbf{Q}(i, m, b) &= \begin{bmatrix}
\mathbf{Ĉ S}(i-b+0)\mathbf{B̂_u} \\
\mathbf{Ĉ S}(i-b+1)\mathbf{B̂_u} \\
\vdots \\
\mathbf{Ĉ S}(m-b-1)\mathbf{B̂_u}
\end{bmatrix} \\
\mathbf{S}(m) &= ∑_{ℓ=0}^m \mathbf{Â}^ℓ
\end{aligned}
```
the prediction matrices are computed from the ``j_ℓ`` integers introduced in the
[`move_blocking`](@ref) documentation and the following equations:
```math
\begin{aligned}
\mathbf{E} &= \begin{bmatrix}
\mathbf{Q}(j_0, j_1, j_0) & \mathbf{0} & \cdots & \mathbf{0} \\
\mathbf{Q}(j_1, j_2, j_0) & \mathbf{Q}(j_1, j_2, j_1) & \cdots & \mathbf{0} \\
\vdots & \vdots & \ddots & \vdots \\
\mathbf{Q}(j_{H_c-1}, j_{H_c}, j_0) & \mathbf{Q}(j_{H_c-1}, j_{H_c}, j_1) & \cdots & \mathbf{Q}(j_{H_c-1}, j_{H_c}, j_{H_c-1}) \end{bmatrix} \\
\mathbf{G} &= \begin{bmatrix}
\mathbf{Ĉ}\mathbf{Â}^{0} \mathbf{B̂_d} \\
\mathbf{Ĉ}\mathbf{Â}^{1} \mathbf{B̂_d} \\
\vdots \\
\mathbf{Ĉ}\mathbf{Â}^{H_p-1} \mathbf{B̂_d} \end{bmatrix} \\
\mathbf{J} &= \begin{bmatrix}
\mathbf{D̂_d} & \mathbf{0} & \cdots & \mathbf{0} \\
\mathbf{Ĉ}\mathbf{Â}^{0} \mathbf{B̂_d} & \mathbf{D̂_d} & \cdots & \mathbf{0} \\
\vdots & \vdots & \ddots & \vdots \\
\mathbf{Ĉ}\mathbf{Â}^{H_p-2} \mathbf{B̂_d} & \mathbf{Ĉ}\mathbf{Â}^{H_p-3} \mathbf{B̂_d} & \cdots & \mathbf{D̂_d} \end{bmatrix} \\
\mathbf{K} &= \begin{bmatrix}
\mathbf{Ĉ}\mathbf{Â}^{1} \\
\mathbf{Ĉ}\mathbf{Â}^{2} \\
\vdots \\
\mathbf{Ĉ}\mathbf{Â}^{H_p} \end{bmatrix} \\
\mathbf{V} &= \mathbf{Q}(0, H_p, 0) \\
\mathbf{B} &= \begin{bmatrix}
\mathbf{Ĉ S}(0) \\
\mathbf{Ĉ S}(1) \\
\vdots \\
\mathbf{Ĉ S}(H_p-1) \end{bmatrix} \mathbf{\big(f̂_{op} - x̂_{op}\big)}
\end{aligned}
```
For the terminal constraints, the matrices are computed with:
```math
\begin{aligned}
\mathbf{e_x̂} &= \begin{bmatrix}
\mathbf{S}(H_p-j_0-1)\mathbf{B̂_u} & \mathbf{S}(H_p-j_1-1)\mathbf{B̂_u} & \cdots & \mathbf{S}(H_p-j_{H_c-1}-1)\mathbf{B̂_u} \end{bmatrix} \\
\mathbf{g_x̂} &= \mathbf{Â}^{H_p-1} \mathbf{B̂_d} \\
\mathbf{j_x̂} &= \begin{bmatrix}
\mathbf{Â}^{H_p-2}\mathbf{B̂_d} & \mathbf{Â}^{H_p-3}\mathbf{B̂_d} & \cdots & \mathbf{0} \end{bmatrix} \\
\mathbf{k_x̂} &= \mathbf{Â}^{H_p} \\
\mathbf{v_x̂} &= \mathbf{S}(H_p-1)\mathbf{B̂_u} \\
\mathbf{b_x̂} &= \mathbf{S}(H_p-1)\mathbf{\big(f̂_{op} - x̂_{op}\big)}
\end{aligned}
```
The complex structure of the ``\mathbf{E}`` and ``\mathbf{e_x̂}`` matrices is due to the
move blocking implementation: the decision variable ``\mathbf{Z}`` only contains the
input increment ``\mathbf{Δu}`` of the free moves (see [`move_blocking`](@ref)).
"""
function init_predmat(
model::LinModel, estim::StateEstimator{NT}, transcription::SingleShooting, Hp, Hc, nb
) where {NT<:Real}
Â, B̂u, Ĉ, B̂d, D̂d = estim.Â, estim.B̂u, estim.Ĉ, estim.B̂d, estim.D̂d
nu, nx̂, ny, nd = model.nu, estim.nx̂, model.ny, model.nd
# --- pre-compute matrix powers ---
# Apow 3D array : Apow[:,:,1] = A^0, Apow[:,:,2] = A^1, ... , Apow[:,:,Hp+1] = A^Hp
Âpow = Array{NT}(undef, nx̂, nx̂, Hp+1)
Âpow[:,:,1] = I(nx̂)
for j=2:Hp+1
Âpow[:,:,j] = @views Âpow[:,:,j-1]*Â
end
# Apow_csum 3D array : Apow_csum[:,:,1] = A^0, Apow_csum[:,:,2] = A^1 + A^0, ...
Âpow_csum = cumsum(Âpow, dims=3)
jℓ_data = [0; cumsum(nb)] # introduced in move_blocking docstring
# four helper functions to improve code clarity and be similar to eqs. in docstring:
getpower(array3D, power) = @views array3D[:,:, power+1]
S(m) = @views Âpow_csum[:,:, m+1]
jℓ(ℓ) = jℓ_data[ℓ+1]
function Q!(Q, i, m, b)
for ℓ=0:m-i-1
iRows = (1:ny) .+ ny*ℓ
Q[iRows, :] = Ĉ * S(i-b+ℓ) * B̂u
end
return Q
end
# --- current state estimates x̂0 ---
kx̂ = getpower(Âpow, Hp)
K = Matrix{NT}(undef, Hp*ny, nx̂)
for j=1:Hp
iRow = (1:ny) .+ ny*(j-1)
K[iRow,:] = Ĉ*getpower(Âpow, j)
end
# --- previous manipulated inputs lastu0 ---
vx̂ = S(Hp-1)*B̂u
V = Matrix{NT}(undef, Hp*ny, nu)
Q!(V, 0, Hp, 0)
# --- decision variables Z ---
nZ = get_nZ(estim, transcription, Hp, Hc)
ex̂ = Matrix{NT}(undef, nx̂, nZ)
E = zeros(NT, Hp*ny, nZ)
for j=0:Hc-1
iCol = (1:nu) .+ nu*j
for i=j:Hc-1
i_Q, m_Q, b_Q = jℓ(i), jℓ(i+1), jℓ(j)
iRow = (1:ny*nb[i+1]) .+ ny*i_Q
Q = @views E[iRow, iCol]
Q!(Q, i_Q, m_Q, b_Q)
end
ex̂[:, iCol] = S(Hp - jℓ(j) - 1)*B̂u
end
# --- current measured disturbances d0 and predictions D̂0 ---
gx̂ = getpower(Âpow, Hp-1)*B̂d
G = Matrix{NT}(undef, Hp*ny, nd)
jx̂ = Matrix{NT}(undef, nx̂, Hp*nd)
J = repeatdiag(D̂d, Hp)
if nd > 0
for j=1:Hp
iRow = (1:ny) .+ ny*(j-1)
G[iRow,:] = Ĉ*getpower(Âpow, j-1)*B̂d
end
for j=1:Hp
iRow = (ny*j+1):(ny*Hp)
iCol = (1:nd) .+ nd*(j-1)
J[iRow, iCol] = G[iRow .- ny*j,:]
jx̂[: , iCol] = j < Hp ? getpower(Âpow, Hp-j-1)*B̂d : zeros(NT, nx̂, nd)
end
end
# --- state x̂op and state update f̂op operating points ---
coef_bx̂ = S(Hp-1)
coef_B = Matrix{NT}(undef, ny*Hp, nx̂)
for j=1:Hp
iRow = (1:ny) .+ ny*(j-1)
coef_B[iRow,:] = Ĉ*S(j-1)
end
f̂op_n_x̂op = estim.f̂op - estim.x̂op
bx̂ = coef_bx̂ * f̂op_n_x̂op
B = coef_B * f̂op_n_x̂op
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
end
@doc raw"""
init_predmat(
model::LinModel, estim, transcription::MultipleShooting, Hp, Hc, nb
) -> E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
Construct the prediction matrices for [`LinModel`](@ref) and [`MultipleShooting`](@ref).
They are defined in the Extended Help section.
# Extended Help
!!! details "Extended Help"
They are all appropriately sized zero matrices ``\mathbf{0}``, except for:
```math
\begin{aligned}
\mathbf{E} &= [\begin{smallmatrix}\mathbf{0} & \mathbf{E^{x̂}} \end{smallmatrix}] \\
\mathbf{E^{x̂}} &= \text{diag}\mathbf{(Ĉ,Ĉ,...,Ĉ)} \\
\mathbf{J} &= \text{diag}\mathbf{(D̂_d,D̂_d,...,D̂_d)} \\
\mathbf{e_x̂} &= [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]
\end{aligned}
```
"""
function init_predmat(
model::LinModel, estim::StateEstimator{NT}, ::MultipleShooting, Hp, Hc, nb
) where {NT<:Real}
Ĉ, D̂d = estim.Ĉ, estim.D̂d
nu, nx̂, ny, nd = model.nu, estim.nx̂, model.ny, model.nd
# --- current state estimates x̂0 ---
K = zeros(NT, Hp*ny, nx̂)
kx̂ = zeros(NT, nx̂, nx̂)
# --- previous manipulated inputs lastu0 ---
V = zeros(NT, Hp*ny, nu)
vx̂ = zeros(NT, nx̂, nu)
# --- decision variables Z ---
E = [zeros(NT, Hp*ny, Hc*nu) repeatdiag(Ĉ, Hp)]
ex̂ = [zeros(NT, nx̂, Hc*nu + (Hp-1)*nx̂) I]
# --- current measured disturbances d0 and predictions D̂0 ---
G = zeros(NT, Hp*ny, nd)
gx̂ = zeros(NT, nx̂, nd)
J = repeatdiag(D̂d, Hp)
jx̂ = zeros(NT, nx̂, Hp*nd)
# --- state x̂op and state update f̂op operating points ---
B = zeros(NT, Hp*ny)
bx̂ = zeros(NT, nx̂)
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
end
"""
init_predmat(
model::NonLinModel, estim, transcription::SingleShooting, Hp, Hc, nb
) -> E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
Return empty matrices for [`SingleShooting`](@ref) of [`NonLinModel`](@ref)
"""
function init_predmat(
model::NonLinModel, estim::StateEstimator{NT}, transcription::SingleShooting, Hp, Hc, _
) where {NT<:Real}
nu, nx̂, nd = model.nu, estim.nx̂, model.nd
nZ = get_nZ(estim, transcription, Hp, Hc)
E = zeros(NT, 0, nZ)
G = zeros(NT, 0, nd)
J = zeros(NT, 0, nd*Hp)
K = zeros(NT, 0, nx̂)
V = zeros(NT, 0, nu)
B = zeros(NT, 0)
ex̂, gx̂, jx̂, kx̂, vx̂, bx̂ = E, G, J, K, V, B
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
end
@doc raw"""
init_predmat(
model::NonLinModel, estim, transcription::TranscriptionMethod, Hp, Hc, nb
) -> E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
Return the terminal state matrices for [`NonLinModel`](@ref) and other [`TranscriptionMethod`](@ref).
The output prediction matrices are all empty matrices. The terminal state matrices are
given in the Extended Help section.
# Extended Help
!!! details "Extended Help"
The terminal state matrices all appropriately sized zero matrices ``\mathbf{0}``, except
for ``\mathbf{e_x̂} = [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]``
if `transcription` is a [`MultipleShooting`](@ref), and ``\mathbf{e_x̂} =
[\begin{smallmatrix}\mathbf{0} & \mathbf{I} & \mathbf{0}\end{smallmatrix}]`` otherwise.
"""
function init_predmat(
model::NonLinModel, estim::StateEstimator{NT}, transcription::TranscriptionMethod, Hp, Hc, _
) where {NT<:Real}
nu, nx̂, nd = model.nu, estim.nx̂, model.nd
nΔU = nu*Hc
nX̂0 = nx̂*Hp
nZ = get_nZ(estim, transcription, Hp, Hc)
E = zeros(NT, 0, nZ)
G = zeros(NT, 0, nd)
J = zeros(NT, 0, nd*Hp)
K = zeros(NT, 0, nx̂)
V = zeros(NT, 0, nu)
B = zeros(NT, 0)
myzeros = zeros(nx̂, nZ - nΔU - nX̂0)
ex̂ = [zeros(NT, nx̂, nΔU + nX̂0 - nx̂) I myzeros]
gx̂ = zeros(NT, nx̂, nd)
jx̂ = zeros(NT, nx̂, nd*Hp)
kx̂ = zeros(NT, nx̂, nx̂)
vx̂ = zeros(NT, nx̂, nu)
bx̂ = zeros(NT, nx̂)
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
end
@doc raw"""
init_defectmat(
model::LinModel, estim::StateEstimator, transcription::MultipleShooting, Hp, Hc, nb
) -> ES, GS, JS, KS, VS, BS
Init the matrices for computing the defects over the predicted states.
Knowing that the decision vector ``\mathbf{Z}`` contains both ``\mathbf{ΔU}`` and
``\mathbf{X̂_0}`` vectors (with a [`MultipleShooting`](@ref) transcription), an equation
similar to the prediction matrices (see [`init_predmat`](@ref)) computes the defects of
the estimated states of ``H_p``:
```math
\begin{aligned}
\mathbf{Ŝ} &= \mathbf{E_S Z} + \mathbf{G_S d_0}(k) + \mathbf{J_S D̂_0}
+ \mathbf{K_S x̂_0}(k) + \mathbf{V_S u_0}(k-1)
+ \mathbf{B_S} \\
&= \mathbf{E_S Z} + \mathbf{F_S}
\end{aligned}
```
They are forced to be ``\mathbf{Ŝ = 0}`` using the optimization equality constraints. The
matrices ``\mathbf{E_S, G_S, J_S, K_S, V_S, B_S}`` are defined in the Extended Help section.
# Extended Help
!!! details "Extended Help"
Using the augmented matrices ``\mathbf{Â, B̂_u, Ĉ, B̂_d, D̂_d}`` in `estim` (see
[`augment_model`](@ref)), the [`move_blocking`](@ref) vector ``\mathbf{n_b}``, and the
following ``\mathbf{Q}(n_i)`` matrix of size `(nx̂*ni, nu)`:
```math
\mathbf{Q}(n_i) = \begin{bmatrix}
\mathbf{B̂_u} \\
\mathbf{B̂_u} \\
\vdots \\
\mathbf{B̂_u} \end{bmatrix}
```
The defect matrices are computed with:
```math
\begin{aligned}
\mathbf{E_S} &= \begin{bmatrix}
\mathbf{E_{S}^{Δu}} & \mathbf{E_{S}^{x̂}} \end{bmatrix} \\
\mathbf{E_{S}^{Δu}} &= \begin{bmatrix}
\mathbf{Q}(n_1) & \mathbf{0} & \cdots & \mathbf{0} \\
\mathbf{Q}(n_2) & \mathbf{Q}(n_2) & \cdots & \mathbf{0} \\
\vdots & \vdots & \ddots & \vdots \\
\mathbf{Q}(n_{H_c}) & \mathbf{Q}(n_{H_c}) & \cdots & \mathbf{Q}(n_{H_c}) \end{bmatrix} \\
\mathbf{E_{S}^{x̂}} &= \begin{bmatrix}
-\mathbf{I} & \mathbf{0} & \cdots & \mathbf{0} & \mathbf{0} \\
\mathbf{Â} & -\mathbf{I} & \cdots & \mathbf{0} & \mathbf{0} \\
\vdots & \vdots & \ddots & \vdots & \vdots \\
\mathbf{0} & \mathbf{0} & \cdots & \mathbf{Â} & -\mathbf{I} \end{bmatrix} \\
\mathbf{G_S} &= \begin{bmatrix}
\mathbf{B̂_d} \\ \mathbf{0} \\ \vdots \\ \mathbf{0} \end{bmatrix} \\
\mathbf{J_S} &= \begin{bmatrix}
\mathbf{0} & \mathbf{0} & \cdots & \mathbf{0} & \mathbf{0} \\
\mathbf{B̂_d} & \mathbf{0} & \cdots & \mathbf{0} & \mathbf{0} \\
\vdots & \vdots & \ddots & \vdots & \vdots \\
\mathbf{0} & \mathbf{0} & \cdots & \mathbf{B̂_d} & \mathbf{0} \end{bmatrix} \\
\mathbf{K_S} &= \begin{bmatrix}
\mathbf{Â} \\ \mathbf{0} \\ \vdots \\ \mathbf{0} \end{bmatrix} \\
\mathbf{V_S} &= \begin{bmatrix}
\mathbf{B̂_u} \\ \mathbf{B̂_u} \\ \vdots \\ \mathbf{B̂_u} \end{bmatrix} \\
\mathbf{B_S} &= \begin{bmatrix}
\mathbf{f̂_{op} - x̂_{op}} \\ \mathbf{f̂_{op} - x̂_{op}} \\ \vdots \\ \mathbf{f̂_{op} - x̂_{op}} \end{bmatrix}
\end{aligned}
```
The ``\mathbf{E_S^{Δu}}`` matrix structure is due to the move blocking implementation:
the ``\mathbf{ΔU}`` vector only contains the input increment of the free moves
(see [`move_blocking`](@ref)).
"""
function init_defectmat(
model::LinModel, estim::StateEstimator{NT}, ::MultipleShooting, Hp, Hc, nb
) where {NT<:Real}
nu, nx̂, nd = model.nu, estim.nx̂, model.nd
Â, B̂u, B̂d = estim.Â, estim.B̂u, estim.B̂d
# helper function to be similar to eqs. in docstring:
function Q!(Q, ni)
for ℓ=0:ni-1
iRows = (1:nx̂) .+ nx̂*ℓ
Q[iRows, :] = B̂u
end
return Q
end
# --- current state estimates x̂0 ---
KS = [Â; zeros(NT, nx̂*(Hp-1), nx̂)]
# --- previous manipulated inputs lastu0 ---
VS = repeat(B̂u, Hp)
# --- decision variables Z ---
nI_nx̂ = Matrix{NT}(-I, nx̂, nx̂)
ES = [zeros(NT, nx̂*Hp, nu*Hc) repeatdiag(nI_nx̂, Hp)]
for j=1:Hc
iCol = (1:nu) .+ nu*(j-1)
for i=j:Hc
ni = nb[i]
iRow = (1:nx̂*ni) .+ nx̂*sum(nb[1:i-1])
Q = @views ES[iRow, iCol]
Q!(Q, ni)
end
end
for j=1:Hp-1
iRow = (1:nx̂) .+ nx̂*j
iCol = (1:nx̂) .+ nx̂*(j-1) .+ nu*Hc
ES[iRow, iCol] = Â
end
# --- current measured disturbances d0 and predictions D̂0 ---
GS = [B̂d; zeros(NT, nx̂*(Hp-1), nd)]
JS = [zeros(NT, nx̂, nd*Hp); repeatdiag(B̂d, Hp-1) zeros(NT, nx̂*(Hp-1), nd)]
# --- state x̂op and state update f̂op operating points ---
BS = repeat(estim.f̂op - estim.x̂op, Hp)
return ES, GS, JS, KS, VS, BS
end
@doc raw"""
init_defectmat(
model::SimModel, ::StateEstimator, ::TranscriptionMethod, Hp, Hc, _
) -> ES, GS, JS, KS, VS, BS
Init the matrices for computing the defects of the stochastic states only.
The documentation of [`init_estimstoch`](@ref) shows that the stochastic model of the
unmeasured disturbances is linear and discrete-time. The defect of the stochastic states
over ``H_p`` is therefore:
```math
\begin{aligned}
\mathbf{Ŝ_s} &= \mathbf{E_S Z} + \mathbf{K_S x̂_0}(k) \\
&= \mathbf{E_S Z} + \mathbf{F_S}
\end{aligned}
```
The matrices ``\mathbf{E_S}`` and ``\mathbf{K_S}`` are defined in the Extended Help section.
# Extended Help
!!! details "Extended Help"
Using the stochastic matrix ``\mathbf{A_s}`` in `estim` (see [`init_estimstoch`](@ref)),
the defect matrices are computed with:
```math
\begin{aligned}
\mathbf{E_{S}^{Δu}} &= \mathbf{0} \\
\mathbf{E_{S}^{x̂}} &= \begin{bmatrix}
\mathbf{0} &-\mathbf{I} & \mathbf{0} & \mathbf{0} & \cdots & \mathbf{0} & \mathbf{0} & \mathbf{0} \\
\mathbf{0} & \mathbf{A_s} & \mathbf{0} &-\mathbf{I} & \cdots & \mathbf{0} & \mathbf{0} & \mathbf{0} \\
\vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots \\
\mathbf{0} & \mathbf{0} & \mathbf{0} & \mathbf{0} & \cdots & \mathbf{A_s} & \mathbf{0} &-\mathbf{I} \end{bmatrix} \\
\mathbf{E_{S}^{k}} &= \mathbf{0} \\
\mathbf{K_S} &= \begin{bmatrix}
\mathbf{0} & \mathbf{A_s} \\
\mathbf{0} & \mathbf{0} \\
\vdots & \vdots \\
\mathbf{0} & \mathbf{0} \end{bmatrix}
\end{aligned}
```
and:
- if `transcription` is an [`OrthogonalCollocation`](@ref), ``\mathbf{E_S} = [\begin{smallmatrix}
\mathbf{E_{S}^{Δu}} & \mathbf{E_{S}^{x̂}} & \mathbf{E_{S}^{k}} \end{smallmatrix}]``
- else ``\mathbf{E_S} = [\begin{smallmatrix} \mathbf{E_{S}^{Δu}} & \mathbf{E_{S}^{x̂}} \end{smallmatrix}]``
"""
function init_defectmat(
model::SimModel, estim::StateEstimator{NT}, transcription::TranscriptionMethod, Hp, Hc, _
) where {NT<:Real}
nu, nx, nd, nx̂, nxs = model.nu, model.nx, model.nd, estim.nx̂, estim.nxs
nZ = get_nZ(estim, transcription, Hp, Hc)
nK = nZ - nu*Hc - nx̂*Hp
As = estim.As
# --- current state estimates x̂0 ---
KS = zeros(NT, nxs*Hp, nx̂)
KS[1:nxs, nx+1:end] = As
# --- previous manipulated inputs lastu0 ---
VS = zeros(nxs*Hp, nu)
# --- decision variables Z ---
zeros_nI = [zeros(NT, nxs, nx) -I]
ES = [zeros(NT, nxs*Hp, nu*Hc) repeatdiag(zeros_nI, Hp) zeros(NT, nxs*Hp, nK)]
for j=1:Hp-1
iRow = (1:nxs) .+ nxs*j
iCol = (nx+1:nx̂) .+ nx̂*(j-1) .+ nu*Hc
ES[iRow, iCol] = As
end
# --- current measured disturbances d0 and predictions D̂0 ---
GS = zeros(NT, nxs*Hp, nd)
JS = zeros(NT, nxs*Hp, nd*Hp)
# --- state x̂op and state update f̂op operating points ---
BS = zeros(NT, nxs*Hp)
return ES, GS, JS, KS, VS, BS
end
"""
init_defectmat(
model::SimModel, estim::InternalModel, ::TranscriptionMethod, Hp, Hc, _
) -> ES, GS, JS, KS, VS, BS
Return empty matrices for [`InternalModel`](@ref) (the state vector is not augmented).
"""
function init_defectmat(
::SimModel, estim::InternalModel, transcription::TranscriptionMethod, Hp, Hc, _
)
return init_defectmat_empty(estim, transcription, Hp, Hc)
end
"""
init_defectmat(
model::SimModel, estim::StateEstimator, ::TranscriptionMethod, Hp, Hc, nb
) -> ES, GS, JS, KS, VS, BS
Return empty matrices for [`SingleShooting`](@ref) transcription (N/A).
"""
function init_defectmat(
::SimModel, estim::StateEstimator, transcription::SingleShooting, Hp, Hc, _
)
return init_defectmat_empty(estim, transcription, Hp, Hc)
end
function init_defectmat(
::SimModel, estim::InternalModel, transcription::SingleShooting, Hp, Hc, _
)
return init_defectmat_empty(estim, transcription, Hp, Hc)
end
function init_defectmat_empty(
estim::StateEstimator{NT}, transcription::TranscriptionMethod, Hp, Hc
) where {NT<:Real}
model = estim.model
nx̂, nu, nd = estim.nx̂, model.nu, model.nd
nZ = get_nZ(estim, transcription, Hp, Hc)
ES = zeros(NT, 0, nZ)
GS = zeros(NT, 0, nd)
JS = zeros(NT, 0, nd*Hp)
KS = zeros(NT, 0, nx̂)
VS = zeros(NT, 0, nu)
BS = zeros(NT, 0)
return ES, GS, JS, KS, VS, BS
end
@doc raw"""
init_matconstraint_mpc(
model::LinModel, transcription::TranscriptionMethod, nc::Int,
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_Wmin, i_Wmax, i_x̂min, i_x̂max,
args...
) -> i_b, i_g, A, Aeq, neq
Init `i_b`, `i_g`, `neq`, and `A` and `Aeq` matrices for the all the MPC constraints.
The linear and nonlinear constraints are respectively defined as:
```math
\begin{aligned}
\mathbf{A Z̃ } &≤ \mathbf{b} \\
\mathbf{A_{eq} Z̃} &= \mathbf{b_{eq}} \\
\mathbf{g(Z̃)} &≤ \mathbf{0} \\
\mathbf{g_{eq}(Z̃)} &= \mathbf{0} \\
\end{aligned}
```
The argument `nc` is the number of custom nonlinear inequality constraints in
``\mathbf{g_c}``. `i_b` is a `BitVector` including the indices of ``\mathbf{b}`` that are
finite numbers. `i_g` is a similar vector but for the indices of ``\mathbf{g}``. The method
also returns the ``\mathbf{A, A_{eq}}`` matrices and `neq` if `args` is provided. In such a
case, `args` needs to contain all the inequality and equality constraint matrices:
`A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_Wmin, A_Wmax, A_x̂min, A_x̂max, Aeq`.
The integer `neq` is the number of nonlinear equality constraints in ``\mathbf{g_{eq}}``.
"""
function init_matconstraint_mpc(
::LinModel{NT}, ::TranscriptionMethod, nc::Int,
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_Wmin, i_Wmax, i_x̂min, i_x̂max,
args...
) where {NT<:Real}
if isempty(args)
A, Aeq, neq = nothing, nothing, nothing
else
(
A_Umin, A_Umax,
A_ΔŨmin, A_ΔŨmax,
A_Ymin, A_Ymax,
A_Wmin, A_Wmax,
A_x̂min, A_x̂max,
Aeq
) = args
A = [
A_Umin; A_Umax;
A_ΔŨmin; A_ΔŨmax;
A_Ymin; A_Ymax;
A_Wmin; A_Wmax
A_x̂min; A_x̂max;
]
neq = 0 # number of nonlinear equality constraints
end
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_Ymin; i_Ymax; i_Wmin; i_Wmax; i_x̂min; i_x̂max]
i_g = trues(nc)
return i_b, i_g, A, Aeq, neq
end
"Init `i_b` without output & terminal constraints if `NonLinModel` and `SingleShooting`."
function init_matconstraint_mpc(
::NonLinModel{NT}, ::SingleShooting, nc::Int,
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_Wmin, i_Wmax, i_x̂min, i_x̂max,
args...
) where {NT<:Real}
if isempty(args)
A, Aeq, neq = nothing, nothing, nothing
else
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, _ , _ , A_Wmin, A_Wmax, _ , _ , Aeq = args
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Wmin; A_Wmax]
neq = 0 # number of nonlinear equality constraints
end
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_Wmin; i_Wmax]
i_g = [i_Ymin; i_Ymax; i_x̂min; i_x̂max; trues(nc)]
return i_b, i_g, A, Aeq, neq
end
"Init `i_b` without output constraints if `NonLinModel` and other `TranscriptionMethod`."
function init_matconstraint_mpc(
::NonLinModel{NT}, ::TranscriptionMethod, nc::Int,
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_Wmin, i_Wmax, i_x̂min, i_x̂max,
args...
) where {NT<:Real}
if isempty(args)
A, Aeq, neq = nothing, nothing, nothing
else
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, _ , _ , A_Wmin, A_Wmax, A_x̂min, A_x̂max, Aeq = args
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Wmin; A_Wmax; A_x̂min; A_x̂max]
nΔŨ, nZ̃ = size(A_ΔŨmin)
neq = nZ̃ - nΔŨ - size(Aeq, 1) # number of nonlinear equality constraints
end
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_Wmin; i_Wmax; i_x̂min; i_x̂max]
i_g = [i_Ymin; i_Ymax; trues(nc)]
return i_b, i_g, A, Aeq, neq
end
@doc raw"""
linconstraint!(mpc::PredictiveController, model::LinModel)
Set `b` vector for the linear model inequality constraints (``\mathbf{A Z̃ ≤ b}``).
Also init ``\mathbf{f_x̂} = \mathbf{g_x̂ d_0}(k) + \mathbf{j_x̂ D̂_0} + \mathbf{k_x̂ x̂_0}(k) +
\mathbf{v_x̂ u_0}(k-1) + \mathbf{b_x̂}`` vector for the terminal constraints, see
[`init_predmat`](@ref). The ``\mathbf{F_w}`` vector for the custom linear constraints is
also updated, see [`relaxW`](@ref).
"""
function linconstraint!(mpc::PredictiveController, model::LinModel, ::TranscriptionMethod)
nU, nΔŨ, nY = length(mpc.con.U0min), length(mpc.con.ΔŨmin), length(mpc.con.Y0min)
nW = length(mpc.con.Wmin)
nx̂, fx̂ = mpc.estim.nx̂, mpc.con.fx̂
fx̂ .= mpc.con.bx̂
mul!(fx̂, mpc.con.kx̂, mpc.estim.x̂0, 1, 1)
mul!(fx̂, mpc.con.vx̂, mpc.lastu0, 1, 1)
if model.nd > 0
mul!(fx̂, mpc.con.gx̂, mpc.d0, 1, 1)
mul!(fx̂, mpc.con.jx̂, mpc.D̂0, 1, 1)
end
linconstraint_custom!(mpc, model)
n = 0
mpc.con.b[(n+1):(n+nU)] .= @. -mpc.con.U0min + mpc.Tu_lastu0
n += nU
mpc.con.b[(n+1):(n+nU)] .= @. +mpc.con.U0max - mpc.Tu_lastu0
n += nU
mpc.con.b[(n+1):(n+nΔŨ)] .= @. -mpc.con.ΔŨmin
n += nΔŨ
mpc.con.b[(n+1):(n+nΔŨ)] .= @. +mpc.con.ΔŨmax
n += nΔŨ
mpc.con.b[(n+1):(n+nY)] .= @. -mpc.con.Y0min + mpc.F
n += nY
mpc.con.b[(n+1):(n+nY)] .= @. +mpc.con.Y0max - mpc.F
n += nY
mpc.con.b[(n+1):(n+nW)] .= @. -mpc.con.Wmin + mpc.con.Fw
n += nW
mpc.con.b[(n+1):(n+nW)] .= @. +mpc.con.Wmax - mpc.con.Fw
n += nW
mpc.con.b[(n+1):(n+nx̂)] .= @. -mpc.con.x̂0min + fx̂
n += nx̂
mpc.con.b[(n+1):(n+nx̂)] .= @. +mpc.con.x̂0max - fx̂
if any(mpc.con.i_b)
lincon = mpc.optim[:linconstraint]
JuMP.set_normalized_rhs(lincon, mpc.con.b[mpc.con.i_b])
end
return nothing
end
"Set `b` excluding predicted output constraints for `NonLinModel` and not `SingleShooting`."
function linconstraint!(mpc::PredictiveController, model::NonLinModel, ::TranscriptionMethod)
nU, nΔŨ = length(mpc.con.U0min), length(mpc.con.ΔŨmin)
nW = length(mpc.con.Wmin)
nx̂ = mpc.estim.nx̂
# here, updating fx̂ is not necessary since fx̂ = 0
linconstraint_custom!(mpc, model)
n = 0
mpc.con.b[(n+1):(n+nU)] .= @. -mpc.con.U0min + mpc.Tu_lastu0
n += nU
mpc.con.b[(n+1):(n+nU)] .= @. +mpc.con.U0max - mpc.Tu_lastu0
n += nU
mpc.con.b[(n+1):(n+nΔŨ)] .= @. -mpc.con.ΔŨmin
n += nΔŨ
mpc.con.b[(n+1):(n+nΔŨ)] .= @. +mpc.con.ΔŨmax
n += nΔŨ
mpc.con.b[(n+1):(n+nW)] .= @. -mpc.con.Wmin + mpc.con.Fw
n += nW
mpc.con.b[(n+1):(n+nW)] .= @. +mpc.con.Wmax - mpc.con.Fw
n += nW
mpc.con.b[(n+1):(n+nx̂)] .= @. -mpc.con.x̂0min
n += nx̂
mpc.con.b[(n+1):(n+nx̂)] .= @. +mpc.con.x̂0max
if any(mpc.con.i_b)
lincon = mpc.optim[:linconstraint]
JuMP.set_normalized_rhs(lincon, mpc.con.b[mpc.con.i_b])
end
end
"Also exclude terminal constraints for `NonLinModel` and `SingleShooting`."
function linconstraint!(mpc::PredictiveController, model::NonLinModel, ::SingleShooting)