@@ -19,19 +19,6 @@ julia> u = mpc([5]); round.(u, digits=3)
1919"""
2020abstract type PredictiveController end
2121
22- " Include the additional information about the optimum to ease troubleshooting."
23- mutable struct OptimInfo
24- ΔŨ:: Vector{Float64}
25- ϵ :: Float64
26- J :: Float64
27- u :: Vector{Float64}
28- U :: Vector{Float64}
29- ŷ :: Vector{Float64}
30- Ŷ :: Vector{Float64}
31- ŷs:: Vector{Float64}
32- Ŷs:: Vector{Float64}
33- end
34-
3522" Include all the data for the constraints of [`PredictiveController`](@ref)"
3623struct ControllerConstraint
3724 Umin :: Vector{Float64}
@@ -260,8 +247,7 @@ function moveinput!(
260247 ŷs, Ŷs = predictstoch! (mpc, mpc. estim, d, ym)
261248 p = initpred! (mpc, mpc. estim. model, d, D̂, Ŷs, R̂y)
262249 linconstaint! (mpc, mpc. estim. model)
263- ΔŨ, J = optim_objective! (mpc, p)
264- write_info! (mpc, ΔŨ, J, ŷs, Ŷs)
250+ ΔŨ, _ = optim_objective! (mpc, p)
265251 Δu = ΔŨ[1 : mpc. estim. model. nu] # receding horizon principle: only Δu(k) is used (1st one)
266252 u = mpc. estim. lastu0 + mpc. estim. model. uop + Δu
267253 return u
@@ -277,10 +263,10 @@ setstate!(mpc::PredictiveController, x̂) = (setstate!(mpc.estim, x̂); return m
277263"""
278264 initstate!(mpc::PredictiveController, u, ym, d=Float64[])
279265
280- Init `mpc.info` and the states of `mpc.estim` [`StateEstimator`](@ref).
266+ Init `mpc.ΔŨ` for warm-starting and the states of `mpc.estim` [`StateEstimator`](@ref).
281267"""
282268function initstate! (mpc:: PredictiveController , u, ym, d= Float64[])
283- mpc. info . ΔŨ .= 0
269+ mpc. ΔŨ .= 0
284270 return initstate! (mpc. estim, u, ym, d)
285271end
286272
390376@doc raw """
391377 linconstraint!(mpc::PredictiveController, model::LinModel)
392378
393- Calc `b` vector for the linear model inequality constraints (``\m athbf{A ΔŨ ≤ b}``).
379+ Set `b` vector for the linear model inequality constraints (``\m athbf{A ΔŨ ≤ b}``).
394380"""
395381function linconstaint! (mpc:: PredictiveController , model:: LinModel )
396382 mpc. con. b[:] = [
@@ -401,12 +387,13 @@ function linconstaint!(mpc::PredictiveController, model::LinModel)
401387 - mpc. con. Ŷmin + mpc. F
402388 + mpc. con. Ŷmax - mpc. F
403389 ]
390+ set_normalized_rhs .(mpc. optim[:linconstraint ], mpc. con. b[mpc. con. i_b])
404391end
405392
406393@doc raw """
407394 linconstraint!(mpc::PredictiveController, model::NonLinModel)
408395
409- Calc `b` without predicted output ``\m athbf{Ŷ}`` constraints for [`NonLinModel`](@ref).
396+ Set `b` that excludes predicted output ``\m athbf{Ŷ}`` constraints for [`NonLinModel`](@ref).
410397"""
411398function linconstaint! (mpc:: PredictiveController , model:: NonLinModel )
412399 mpc. con. b[:] = [
@@ -415,6 +402,7 @@ function linconstaint!(mpc::PredictiveController, model::NonLinModel)
415402 - mpc. con. ΔŨmin
416403 + mpc. con. ΔŨmax
417404 ]
405+ set_normalized_rhs .(mpc. optim[:linconstraint ], mpc. con. b[mpc. con. i_b])
418406end
419407
420408"""
@@ -426,8 +414,7 @@ function optim_objective!(mpc::PredictiveController, p)
426414 optim = mpc. optim
427415 model = mpc. estim. model
428416 ΔŨ:: Vector{VariableRef} = optim[:ΔŨ ]
429- lastΔŨ = mpc. info. ΔŨ
430- set_normalized_rhs .(optim[:linconstraint ], mpc. con. b[mpc. con. i_b])
417+ lastΔŨ = mpc. ΔŨ
431418 # initial ΔŨ (warm-start): [Δu_{k-1}(k); Δu_{k-1}(k+1); ... ; 0_{nu × 1}]
432419 ΔŨ0 = [lastΔŨ[(model. nu+ 1 ): (mpc. Hc* model. nu)]; zeros (model. nu)]
433420 # if soft constraints, append the last slack value ϵ_{k-1}:
@@ -450,9 +437,9 @@ function optim_objective!(mpc::PredictiveController, p)
450437 @warn " MPC termination status not OPTIMAL or LOCALLY_SOLVED ($status )"
451438 @debug solution_summary (optim)
452439 end
453- ΔŨ_val = isfatal (status) ? ΔŨ0 : value .(ΔŨ) # fatal status : use last value
454- J_val = objective_value (optim) + p # optimal objective value by adding constant p
455- return ΔŨ_val , J_val
440+ mpc . ΔŨ[:] = isfatal (status) ? ΔŨ0 : value .(ΔŨ) # fatal status : use last value
441+ J_val = objective_value (optim) + p # add LinModel p constant (p=0 for NonLinModel)
442+ return mpc . ΔŨ , J_val
456443end
457444
458445
0 commit comments