@@ -578,15 +578,17 @@ Inspired from: [User-defined operators with vector outputs](@extref JuMP User-de
578
578
function get_optim_functions (mpc:: NonLinMPC , :: JuMP.GenericModel{JNT} ) where JNT<: Real
579
579
# ----- common cache for Jfunc, gfuncs, geqfuncs called with floats -------------------
580
580
model = mpc. estim. model
581
- nu, ny, nx̂, nϵ, Hp, Hc = model. nu, model. ny, mpc. estim. nx̂, mpc. nϵ, mpc. Hp, mpc. Hc
581
+ nu, ny, nx̂, nϵ, nk = model. nu, model. ny, mpc. estim. nx̂, mpc. nϵ, model. nk
582
+ Hp, Hc = mpc. Hp, mpc. Hc
582
583
ng, nc, neq = length (mpc. con. i_g), mpc. con. nc, mpc. con. neq
583
- nZ̃, nU, nŶ, nX̂ = length (mpc. Z̃), Hp* nu, Hp* ny, Hp* nx̂
584
+ nZ̃, nU, nŶ, nX̂, nK = length (mpc. Z̃), Hp* nu, Hp* ny, Hp* nx̂, Hp * nk
584
585
nΔŨ, nUe, nŶe = nu* Hc + nϵ, nU + nu, nŶ + ny
585
586
strict = Val (true )
586
587
myNaN = convert (JNT, NaN ) # NaN to force update_simulations! at first call:
587
588
Z̃ :: Vector{JNT} = fill (myNaN, nZ̃)
588
589
ΔŨ:: Vector{JNT} = zeros (JNT, nΔŨ)
589
590
x̂0end:: Vector{JNT} = zeros (JNT, nx̂)
591
+ K0:: Vector{JNT} = zeros (JNT, nK)
590
592
Ue:: Vector{JNT} , Ŷe:: Vector{JNT} = zeros (JNT, nUe), zeros (JNT, nŶe)
591
593
U0:: Vector{JNT} , Ŷ0:: Vector{JNT} = zeros (JNT, nU), zeros (JNT, nŶ)
592
594
Û0:: Vector{JNT} , X̂0:: Vector{JNT} = zeros (JNT, nU), zeros (JNT, nX̂)
@@ -596,18 +598,18 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
596
598
function Jfunc (Z̃arg:: Vararg{T, N} ) where {N, T<: Real }
597
599
if isdifferent (Z̃arg, Z̃)
598
600
Z̃ .= Z̃arg
599
- update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq, mpc, Z̃)
601
+ update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
600
602
end
601
603
return obj_nonlinprog! (Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ):: T
602
604
end
603
- function Jfunc! (Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq)
604
- update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq, mpc, Z̃)
605
+ function Jfunc! (Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq)
606
+ update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
605
607
return obj_nonlinprog! (Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
606
608
end
607
609
Z̃_∇J = fill (myNaN, nZ̃)
608
610
∇J_context = (
609
611
Cache (ΔŨ), Cache (x̂0end), Cache (Ue), Cache (Ŷe), Cache (U0), Cache (Ŷ0),
610
- Cache (Û0), Cache (X̂0),
612
+ Cache (Û0), Cache (K0), Cache ( X̂0),
611
613
Cache (gc), Cache (g), Cache (geq),
612
614
)
613
615
∇J_prep = prepare_gradient (Jfunc!, mpc. gradient, Z̃_∇J, ∇J_context... ; strict)
@@ -631,19 +633,23 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
631
633
gfunc_i = function (Z̃arg:: Vararg{T, N} ) where {N, T<: Real }
632
634
if isdifferent (Z̃arg, Z̃)
633
635
Z̃ .= Z̃arg
634
- update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq, mpc, Z̃)
636
+ update_predictions! (
637
+ ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃
638
+ )
635
639
end
636
640
return g[i]:: T
637
641
end
638
642
gfuncs[i] = gfunc_i
639
643
end
640
- function gfunc! (g, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, geq)
641
- return update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq, mpc, Z̃)
644
+ function gfunc! (g, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, geq)
645
+ return update_predictions! (
646
+ ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃
647
+ )
642
648
end
643
649
Z̃_∇g = fill (myNaN, nZ̃)
644
650
∇g_context = (
645
651
Cache (ΔŨ), Cache (x̂0end), Cache (Ue), Cache (Ŷe), Cache (U0), Cache (Ŷ0),
646
- Cache (Û0), Cache (X̂0),
652
+ Cache (Û0), Cache (K0), Cache ( X̂0),
647
653
Cache (gc), Cache (geq),
648
654
)
649
655
# temporarily enable all the inequality constraints for sparsity detection:
@@ -678,19 +684,23 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
678
684
geqfunc_i = function (Z̃arg:: Vararg{T, N} ) where {N, T<: Real }
679
685
if isdifferent (Z̃arg, Z̃)
680
686
Z̃ .= Z̃arg
681
- update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq, mpc, Z̃)
687
+ update_predictions! (
688
+ ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃
689
+ )
682
690
end
683
691
return geq[i]:: T
684
692
end
685
693
geqfuncs[i] = geqfunc_i
686
694
end
687
- function geqfunc! (geq, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g)
688
- return update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq, mpc, Z̃)
695
+ function geqfunc! (geq, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g)
696
+ return update_predictions! (
697
+ ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃
698
+ )
689
699
end
690
700
Z̃_∇geq = fill (myNaN, nZ̃)
691
701
∇geq_context = (
692
702
Cache (ΔŨ), Cache (x̂0end), Cache (Ue), Cache (Ŷe), Cache (U0), Cache (Ŷ0),
693
- Cache (Û0), Cache (X̂0),
703
+ Cache (Û0), Cache (K0), Cache ( X̂0),
694
704
Cache (gc), Cache (g)
695
705
)
696
706
∇geq_prep = prepare_jacobian (geqfunc!, geq, mpc. jacobian, Z̃_∇geq, ∇geq_context... ; strict)
716
726
717
727
"""
718
728
update_predictions!(
719
- ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq,
729
+ ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq,
720
730
mpc::PredictiveController, Z̃
721
731
) -> nothing
722
732
@@ -725,17 +735,17 @@ Update in-place all vectors for the predictions of `mpc` controller at decision
725
735
The method mutates all the arguments before the `mpc` argument.
726
736
"""
727
737
function update_predictions! (
728
- ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, X̂0, gc, g, geq, mpc:: PredictiveController , Z̃
738
+ ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc:: PredictiveController , Z̃
729
739
)
730
740
model, transcription = mpc. estim. model, mpc. transcription
731
741
U0 = getU0! (U0, mpc, Z̃)
732
742
ΔŨ = getΔŨ! (ΔŨ, mpc, transcription, Z̃)
733
- Ŷ0, x̂0end = predict! (Ŷ0, x̂0end, X̂0, Û0, mpc, model, transcription, U0, Z̃)
743
+ Ŷ0, x̂0end = predict! (Ŷ0, x̂0end, X̂0, Û0, K0, mpc, model, transcription, U0, Z̃)
734
744
Ue, Ŷe = extended_vectors! (Ue, Ŷe, mpc, U0, Ŷ0)
735
745
ϵ = getϵ (mpc, Z̃)
736
746
gc = con_custom! (gc, mpc, Ue, Ŷe, ϵ)
737
747
g = con_nonlinprog! (g, mpc, model, transcription, x̂0end, Ŷ0, gc, ϵ)
738
- geq = con_nonlinprogeq! (geq, X̂0, Û0, mpc, model, transcription, U0, Z̃)
748
+ geq = con_nonlinprogeq! (geq, X̂0, Û0, K0, mpc, model, transcription, U0, Z̃)
739
749
return nothing
740
750
end
741
751
0 commit comments