@@ -43,7 +43,7 @@ struct NonLinMPC{
4343 Ps:: Matrix{NT}
4444 d0:: Vector{NT}
4545 D̂0:: Vector{NT}
46- D̂E :: Vector{NT}
46+ D̂e :: Vector{NT}
4747 Uop:: Vector{NT}
4848 Yop:: Vector{NT}
4949 Dop:: Vector{NT}
@@ -76,7 +76,7 @@ struct NonLinMPC{
7676 q̃, r = zeros (NT, size (H̃, 1 )), zeros (NT, 1 )
7777 Ks, Ps = init_stochpred (estim, Hp)
7878 # dummy vals (updated just before optimization):
79- d0, D̂0, D̂E = zeros (NT, nd), zeros (NT, nd* Hp), zeros (NT, nd + nd* Hp)
79+ d0, D̂0, D̂e = zeros (NT, nd), zeros (NT, nd* Hp), zeros (NT, nd + nd* Hp)
8080 Uop, Yop, Dop = repeat (model. uop, Hp), repeat (model. yop, Hp), repeat (model. dop, Hp)
8181 nΔŨ = size (Ẽ, 2 )
8282 ΔŨ = zeros (NT, nΔŨ)
@@ -91,7 +91,7 @@ struct NonLinMPC{
9191 Ẽ, F, G, J, K, V, B,
9292 H̃, q̃, r,
9393 Ks, Ps,
94- d0, D̂0, D̂E ,
94+ d0, D̂0, D̂e ,
9595 Uop, Yop, Dop,
9696 buffer
9797 )
@@ -165,8 +165,8 @@ state estimator :
165165- `JE=(_,_,_,_)->0.0` : economic or custom cost function ``J_E(\m athbf{U_e}, \m athbf{Ŷ_e},
166166 \m athbf{D̂_e}, \m athbf{p})``.
167167- `gc=(_,_,_,_,_,_)->nothing` or `gc!` : custom inequality constraint function
168- ``\m athbf{g_c}(\m athbf{U_e}, \m athbf{Ŷ_e}, \m athbf{D̂_e}, \m athbf{p}, ϵ)`` ( mutating or
169- not, details in Extended Help).
168+ ``\m athbf{g_c}(\m athbf{U_e}, \m athbf{Ŷ_e}, \m athbf{D̂_e}, \m athbf{p}, ϵ)``, mutating or
169+ not ( details in Extended Help).
170170- `nc=0` : number of custom inequality constraints.
171171- `p=model.p` : ``J_E`` and ``\m athbf{g_c}`` functions parameter ``\m athbf{p}`` (any type).
172172- `optim=JuMP.Model(Ipopt.Optimizer)` : nonlinear optimizer used in the predictive
@@ -346,20 +346,6 @@ function validate_JE(NT, JE)
346346 return nothing
347347end
348348
349- " Get mutating custom constraint function `gc!` from the provided function in argument."
350- function get_mutating_gc (NT, gc)
351- ismutating_gc = validate_gc (NT, gc)
352- gc! = if ismutating_gc
353- gc
354- else
355- function gc! (LHS, Ue, Ŷe, D̂e, p, ϵ)
356- LHS .= gc (Ue, Ŷe, D̂e, p, ϵ)
357- return nothing
358- end
359- end
360- return gc!
361- end
362-
363349"""
364350 validate_gc(NT, gc) -> ismutating
365351
@@ -371,30 +357,55 @@ function validate_gc(NT, gc)
371357 # LHS, Ue, Ŷe, D̂e, p, ϵ
372358 Tuple{Vector{NT}, Vector{NT}, Vector{NT}, Vector{NT}, Any, NT}
373359 )
374- println (ismutating)
375360 # Ue, Ŷe, D̂e, p, ϵ
376361 if ! (ismutating || hasmethod (gc, Tuple{Vector{NT}, Vector{NT}, Vector{NT}, Any, NT}))
377362 error (
378363 " the custom constraint function has no method with type signature " *
379- " gc(UE ::Vector{$(NT) }, ŶE ::Vector{$(NT) }, D̂E ::Vector{$(NT) }, p::Any, ϵ::$(NT) ) " *
364+ " gc(Ue ::Vector{$(NT) }, Ŷe ::Vector{$(NT) }, D̂e ::Vector{$(NT) }, p::Any, ϵ::$(NT) ) " *
380365 " or mutating form gc!(LHS::Vector{$(NT) }, Ue::Vector{$(NT) }, Ŷe::Vector{$(NT) }, " *
381366 " D̂e::Vector{$(NT) }, p::Any, ϵ::$(NT) )"
382367 )
383368 end
384369 return ismutating
385370end
386371
372+ " Get mutating custom constraint function `gc!` from the provided function in argument."
373+ function get_mutating_gc (NT, gc)
374+ ismutating_gc = validate_gc (NT, gc)
375+ gc! = if ismutating_gc
376+ gc
377+ else
378+ println (" YO!" )
379+ function gc! (LHS, Ue, Ŷe, D̂e, p, ϵ)
380+ LHS .= gc (Ue, Ŷe, D̂e, p, ϵ)
381+ return nothing
382+ end
383+ end
384+ return gc!
385+ end
386+
387+ function test_custom_functions (JE, gc!, uop; Uop, dop, Dop, ΔŨ, p)
388+ Ue = [Uop; uop]
389+ D̂e = [dop; Dop]
390+ Ŷ0, x̂0next =
391+ Ŷ0, x̂0end = predict! (Ŷ0, x̂0, x̂0next, u0, û0, mpc, model, mpc. ΔŨ)
392+
393+
394+ JE = JE (Uop, Uop, Dop, p)
395+
396+ end
397+
387398"""
388399 addinfo!(info, mpc::NonLinMPC) -> info
389400
390401For [`NonLinMPC`](@ref), add `:sol` and the optimal economic cost `:JE`.
391402"""
392403function addinfo! (info, mpc:: NonLinMPC )
393404 U, Ŷ, D̂, ŷ, d = info[:U ], info[:Ŷ ], info[:D̂ ], info[:ŷ ], info[:d ]
394- UE = [U; U[(end - mpc. estim. model. nu + 1 ): end ]]
395- ŶE = [ŷ; Ŷ]
396- D̂E = [d; D̂]
397- info[:JE ] = mpc. JE (UE, ŶE, D̂E , mpc. p)
405+ Ue = [U; U[(end - mpc. estim. model. nu + 1 ): end ]]
406+ Ŷe = [ŷ; Ŷ]
407+ D̂e = [d; D̂]
408+ info[:JE ] = mpc. JE (Ue, Ŷe, D̂e , mpc. p)
398409 info[:sol ] = JuMP. solution_summary (mpc. optim, verbose= true )
399410 return info
400411end
@@ -571,15 +582,15 @@ con_nonlinprog!(g, ::NonLinMPC, ::LinModel, _ , _ , _ ) = g
571582" Evaluate the economic term of the objective function for [`NonLinMPC`](@ref)."
572583function obj_econ! (U0, Ȳ, mpc:: NonLinMPC , model:: SimModel , Ŷ0, ΔŨ)
573584 if ! iszero (mpc. E)
574- ny, Hp, ŷ, D̂E = model. ny, mpc. Hp, mpc. ŷ, mpc. D̂E
585+ ny, Hp, ŷ, D̂e = model. ny, mpc. Hp, mpc. ŷ, mpc. D̂e
575586 U = U0
576587 U .+ = mpc. Uop
577588 uend = @views U[(end - model. nu+ 1 ): end ]
578589 Ŷ = Ȳ
579590 Ŷ .= Ŷ0 .+ mpc. Yop
580- UE = [U; uend]
581- ŶE = [ŷ; Ŷ]
582- E_JE = mpc. E* mpc. JE (UE, ŶE, D̂E , mpc. p)
591+ Ue = [U; uend]
592+ Ŷe = [ŷ; Ŷ]
593+ E_JE = mpc. E* mpc. JE (Ue, Ŷe, D̂e , mpc. p)
583594 else
584595 E_JE = 0.0
585596 end
0 commit comments