@@ -578,15 +578,17 @@ Inspired from: [User-defined operators with vector outputs](@extref JuMP User-de
578578function get_optim_functions (mpc:: NonLinMPC , :: JuMP.GenericModel{JNT} ) where JNT<: Real
579579 # ----- common cache for Jfunc, gfuncs, geqfuncs called with floats -------------------
580580 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
582583 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
584585 nΔŨ, nUe, nŶe = nu* Hc + nϵ, nU + nu, nŶ + ny
585586 strict = Val (true )
586587 myNaN = convert (JNT, NaN ) # NaN to force update_simulations! at first call:
587588 Z̃ :: Vector{JNT} = fill (myNaN, nZ̃)
588589 ΔŨ:: Vector{JNT} = zeros (JNT, nΔŨ)
589590 x̂0end:: Vector{JNT} = zeros (JNT, nx̂)
591+ K0:: Vector{JNT} = zeros (JNT, nK)
590592 Ue:: Vector{JNT} , Ŷe:: Vector{JNT} = zeros (JNT, nUe), zeros (JNT, nŶe)
591593 U0:: Vector{JNT} , Ŷ0:: Vector{JNT} = zeros (JNT, nU), zeros (JNT, nŶ)
592594 Û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
596598 function Jfunc (Z̃arg:: Vararg{T, N} ) where {N, T<: Real }
597599 if isdifferent (Z̃arg, Z̃)
598600 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̃)
600602 end
601603 return obj_nonlinprog! (Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ):: T
602604 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̃)
605607 return obj_nonlinprog! (Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
606608 end
607609 Z̃_∇J = fill (myNaN, nZ̃)
608610 ∇J_context = (
609611 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),
611613 Cache (gc), Cache (g), Cache (geq),
612614 )
613615 ∇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
631633 gfunc_i = function (Z̃arg:: Vararg{T, N} ) where {N, T<: Real }
632634 if isdifferent (Z̃arg, Z̃)
633635 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+ )
635639 end
636640 return g[i]:: T
637641 end
638642 gfuncs[i] = gfunc_i
639643 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+ )
642648 end
643649 Z̃_∇g = fill (myNaN, nZ̃)
644650 ∇g_context = (
645651 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),
647653 Cache (gc), Cache (geq),
648654 )
649655 # temporarily enable all the inequality constraints for sparsity detection:
@@ -678,19 +684,23 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
678684 geqfunc_i = function (Z̃arg:: Vararg{T, N} ) where {N, T<: Real }
679685 if isdifferent (Z̃arg, Z̃)
680686 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+ )
682690 end
683691 return geq[i]:: T
684692 end
685693 geqfuncs[i] = geqfunc_i
686694 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+ )
689699 end
690700 Z̃_∇geq = fill (myNaN, nZ̃)
691701 ∇geq_context = (
692702 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),
694704 Cache (gc), Cache (g)
695705 )
696706 ∇geq_prep = prepare_jacobian (geqfunc!, geq, mpc. jacobian, Z̃_∇geq, ∇geq_context... ; strict)
716726
717727"""
718728 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,
720730 mpc::PredictiveController, Z̃
721731 ) -> nothing
722732
@@ -725,17 +735,17 @@ Update in-place all vectors for the predictions of `mpc` controller at decision
725735The method mutates all the arguments before the `mpc` argument.
726736"""
727737function 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̃
729739)
730740 model, transcription = mpc. estim. model, mpc. transcription
731741 U0 = getU0! (U0, mpc, Z̃)
732742 ΔŨ = 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̃)
734744 Ue, Ŷe = extended_vectors! (Ue, Ŷe, mpc, U0, Ŷ0)
735745 ϵ = getϵ (mpc, Z̃)
736746 gc = con_custom! (gc, mpc, Ue, Ŷe, ϵ)
737747 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̃)
739749 return nothing
740750end
741751
0 commit comments