@@ -162,7 +162,7 @@ controller minimizes the following objective function at each discrete time ``k`
162162 + \m athbf{(ΔU)}' \m athbf{N}_{H_c} \m athbf{(ΔU)} \\ &
163163 + \m athbf{(R̂_u - U)}' \m athbf{L}_{H_p} \m athbf{(R̂_u - U)}
164164 + C ϵ^2
165- + E J_E(\m athbf{U_e}, \m athbf{Ŷ_e}, \m athbf{D̂_e}, \m athbf{p})
165+ + E J_E(\m athbf{U_e}, \m athbf{Ŷ_e}, \m athbf{D̂_e}, \m athbf{p}, ϵ )
166166\e nd{aligned}
167167```
168168subject to [`setconstraint!`](@ref) bounds, and the custom inequality constraints:
@@ -219,8 +219,8 @@ This controller allocates memory at each time step for the optimization.
219219- `Wd=nothing` : custom linear constraint matrix for meas. disturbance (see Extended Help).
220220- `Wr=nothing` : custom linear constraint matrix for output setpoint (see Extended Help).
221221- `Ewt=0.0` : economic costs weight ``E`` (scalar).
222- - `JE=(_,_,_,_)->0.0` : economic or custom cost function ``J_E(\m athbf{U_e}, \m athbf{Ŷ_e},
223- \m athbf{D̂_e}, \m athbf{p})``.
222+ - `JE=(_,_,_,_,_ )->0.0` : economic or custom cost function ``J_E(\m athbf{U_e}, \m athbf{Ŷ_e},
223+ \m athbf{D̂_e}, \m athbf{p}, ϵ )``.
224224- `gc=(_,_,_,_,_,_)->nothing` or `gc!` : custom nonlinear inequality constraint function
225225 ``\m athbf{g_c}(\m athbf{U_e}, \m athbf{Ŷ_e}, \m athbf{D̂_e}, \m athbf{p}, ϵ)``, mutating or
226226 not (details in Extended Help).
@@ -275,8 +275,8 @@ NonLinMPC controller with a sample time Ts = 10.0 s:
275275 The economic cost ``J_E`` and custom constraint ``\m athbf{g_c}`` functions receive the
276276 extended vectors ``\m athbf{U_e}`` (`nu*Hp+nu` elements), ``\m athbf{Ŷ_e}`` (`ny+ny*Hp`
277277 elements) and ``\m athbf{D̂_e}`` (`nd+nd*Hp` elements) as arguments. They all include the
278- values from ``k`` to ``k + H_p`` (inclusively). The custom constraint also receives the
279- slack ``ϵ`` (scalar), which is always zero if `Cwt=Inf`.
278+ values from ``k`` to ``k + H_p`` (inclusively). They also receives the slack ``ϵ``
279+ (scalar), which is always zero if `Cwt=Inf`.
280280
281281 More precisely, the last two time steps in ``\m athbf{U_e}`` are forced to be equal, i.e.
282282 ``\m athbf{u}(k+H_p) = \m athbf{u}(k+H_p-1)``, since ``H_c ≤ H_p`` implies that
@@ -343,7 +343,7 @@ function NonLinMPC(
343343 Wr = nothing ,
344344 Cwt = DEFAULT_CWT,
345345 Ewt = DEFAULT_EWT,
346- JE :: Function = (_,_,_,_) -> 0.0 ,
346+ JE :: Function = (_,_,_,_,_ ) -> 0.0 ,
347347 gc!:: Function = (_,_,_,_,_,_) -> nothing ,
348348 gc :: Function = gc!,
349349 nc:: Int = 0 ,
@@ -414,7 +414,7 @@ function NonLinMPC(
414414 Wr = nothing ,
415415 Cwt = DEFAULT_CWT,
416416 Ewt = DEFAULT_EWT,
417- JE :: Function = (_,_,_,_) -> 0.0 ,
417+ JE :: Function = (_,_,_,_,_ ) -> 0.0 ,
418418 gc!:: Function = (_,_,_,_,_,_) -> nothing ,
419419 gc :: Function = gc!,
420420 nc = 0 ,
@@ -454,11 +454,11 @@ default_jacobian(::TranscriptionMethod) = DEFAULT_NONLINMPC_JACSPARSE
454454Validate `JE` function argument signature.
455455"""
456456function validate_JE (NT, JE)
457- # Ue, Ŷe, D̂e, p
458- if ! hasmethod (JE, Tuple{Vector{NT}, Vector{NT}, Vector{NT}, Any})
457+ # Ue, Ŷe, D̂e, p, ϵ
458+ if ! hasmethod (JE, Tuple{Vector{NT}, Vector{NT}, Vector{NT}, Any, NT })
459459 error (
460460 " the economic cost function has no method with type signature " *
461- " JE(Ue::Vector{$(NT) }, Ŷe::Vector{$(NT) }, D̂e::Vector{$(NT) }, p::Any)"
461+ " JE(Ue::Vector{$(NT) }, Ŷe::Vector{$(NT) }, D̂e::Vector{$(NT) }, p::Any, ϵ:: $(NT) )"
462462 )
463463 end
464464 return nothing
@@ -513,19 +513,20 @@ should ease troubleshooting of simple bugs e.g.: the user forgets to set the `nc
513513function test_custom_functions (NT, model:: SimModel , JE, gc!, nc, Uop, Yop, Dop, p)
514514 uop, dop, yop = model. uop, model. dop, model. yop
515515 Ue, Ŷe, D̂e = [Uop; uop], [yop; Yop], [dop; Dop]
516+ ϵ = zero (NT)
516517 try
517- val:: NT = JE (Ue, Ŷe, D̂e, p)
518+ val:: NT = JE (Ue, Ŷe, D̂e, p, ϵ )
518519 catch err
519520 @warn (
520521 """
521- Calling the JE function with Ue, Ŷe, D̂e arguments fixed at uop=$uop ,
522- yop=$yop , dop=$dop failed with the following stacktrace. Did you forget
522+ Calling the JE function with Ue, Ŷe, D̂e, ϵ arguments fixed at uop=$uop ,
523+ yop=$yop , dop=$dop , ϵ=0 failed with the following stacktrace. Did you forget
523524 to set the keyword argument p?
524525 """ ,
525526 exception= (err, catch_backtrace ())
526527 )
527528 end
528- ϵ, gc = zero (NT), Vector {NT} (undef, nc)
529+ gc = Vector {NT} (undef, nc)
529530 try
530531 gc! (gc, Ue, Ŷe, D̂e, p, ϵ)
531532 catch err
@@ -553,7 +554,7 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
553554 Ue = [U; U[(end - mpc. estim. model. nu + 1 ): end ]]
554555 Ŷe = [ŷ; Ŷ]
555556 D̂e = [d; D̂]
556- JE = mpc. JE (Ue, Ŷe, D̂e, mpc. p)
557+ JE = mpc. JE (Ue, Ŷe, D̂e, mpc. p, ϵ )
557558 LHS = Vector {NT} (undef, mpc. con. nc)
558559 mpc. con. gc! (LHS, Ue, Ŷe, D̂e, mpc. p, ϵ)
559560 info[:JE ] = JE
@@ -1092,9 +1093,9 @@ end
10921093
10931094" Evaluate the economic term `E*JE` of the objective function for [`NonLinMPC`](@ref)."
10941095function obj_econ (
1095- mpc:: NonLinMPC , :: SimModel , Ue, Ŷe:: AbstractVector{NT}
1096+ mpc:: NonLinMPC , :: SimModel , Ue, Ŷe:: AbstractVector{NT} , ϵ
10961097) where NT<: Real
1097- E_JE = mpc. weights. iszero_E ? zero (NT) : mpc. weights. E* mpc. JE (Ue, Ŷe, mpc. D̂e, mpc. p)
1098+ E_JE = mpc. weights. iszero_E ? zero (NT) : mpc. weights. E* mpc. JE (Ue, Ŷe, mpc. D̂e, mpc. p, ϵ )
10981099 return E_JE
10991100end
11001101
0 commit comments