Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug x_noise argument in sim! and deprecate preparestate!(::SimModel, _ , _ ) #173

Merged
merged 3 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/controller/nonlinmpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ Also return vectors with the nonlinear inequality constraint functions `gfuncs`,
`∇gfuncs!`, for the associated gradients. Lastly, also return vectors with the nonlinear
equality constraint functions `geqfuncs` and gradients `∇geqfuncs!`.

This method is really indicated and I'm not proud of it. That's because of 3 elements:
This method is really intricate and I'm not proud of it. That's because of 3 elements:

- These functions are used inside the nonlinear optimization, so they must be type-stable
and as efficient as possible.
Expand Down
2 changes: 1 addition & 1 deletion src/estimator/mhe/construct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ Return the nonlinear objective `Jfunc` function, and `∇Jfunc!`, to compute its
Also return vectors with the nonlinear inequality constraint functions `gfuncs`, and
`∇gfuncs!`, for the associated gradients.

This method is really indicated and I'm not proud of it. That's because of 3 elements:
This method is really intricate and I'm not proud of it. That's because of 3 elements:

- These functions are used inside the nonlinear optimization, so they must be type-stable
and as efficient as possible.
Expand Down
11 changes: 6 additions & 5 deletions src/plot_sim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ function sim_closedloop!(
d = lastd + d_step + d_noise.*randn(plant.nd)
y = evaloutput(plant, d) + y_step + y_noise.*randn(plant.ny)
ym = y[estim.i_ym]
preparestate!(est_mpc, ym, d)
x = preparestate!(plant)
x̂ = preparestate!(est_mpc, ym, d)
u = sim_getu!(est_mpc, u_ry, d, ru)
ud = u + u_step + u_noise.*randn(plant.nu)
Y_data[:, i] .= y
Expand All @@ -296,10 +297,10 @@ function sim_closedloop!(
Ud_data[:, i] .= ud
Ru_data[:, i] .= ru
D_data[:, i] .= d
X_data[:, i] .= plant.x0 .+ plant.xop
X̂_data[:, i] .= estim.x̂0 .+ estim.x̂op
x = updatestate!(plant, ud, d);
x[:] += x_noise.*randn(plant.nx)
X_data[:, i] .= x
X̂_data[:, i] .=
updatestate!(plant, ud, d);
plant.x0 .+= x_noise.*randn(plant.nx)
updatestate!(est_mpc, u, ym, d)
end
res = SimResult(
Expand Down
13 changes: 11 additions & 2 deletions src/sim_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,25 @@ function initstate!(model::SimModel, u, d=model.buffer.empty)
end

@doc raw"""
preparestate!(model::SimModel, _ , _ ) -> x
preparestate!(model::SimModel) -> x

Do nothing for [`SimModel`](@ref) and return the current model state ``\mathbf{x}(k)``.
"""
function preparestate!(model::SimModel, ::Any , ::Any=model.buffer.empty)
function preparestate!(model::SimModel)
x = model.buffer.x
x .= model.x0 .+ model.xop
return x
end

function preparestate!(model::SimModel, ::Any , ::Any=model.buffer.empty)
Base.depwarn(
"The method preparestate!(model::SimModel, u, d=[]) is deprecated. Use "*
" preparestate!(model::SimModel) instead.",
:preparestate!,
)
return preparestate!(model)
end

@doc raw"""
updatestate!(model::SimModel, u, d=[]) -> xnext

Expand Down
3 changes: 2 additions & 1 deletion test/1_test_sim_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ end
@test evaloutput(linmodel1, Float64[]) ≈ linmodel1(Float64[]) ≈ [50,30]
x = initstate!(linmodel1, [10, 60])
@test evaloutput(linmodel1) ≈ [50 + 19.0, 30 + 7.4]
@test preparestate!(linmodel1, [10, 60]) ≈ x
@test preparestate!(linmodel1) ≈ x # new method
@test preparestate!(linmodel1, [10, 60]) ≈ x # deprecated method
@test updatestate!(linmodel1, [10, 60]) ≈ x
linmodel2 = LinModel(append(tf(1, [1, 0]), tf(2, [10, 1])), 1.0)
x = initstate!(linmodel2, [10, 3])
Expand Down
3 changes: 3 additions & 0 deletions test/4_test_plot_sim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ end
@test res_man.D_data ≈ res.D_data
@test res_man.X_data ≈ res.X_data
@test res_man.X̂_data ≈ res.X̂_data

res2 = sim!(estim, 15, x_noise=[0.1, 0.2, 0.3, 0.4])
@test !(res2.X_data ≈ res.X_data)
end

@testitem "StateEstimator Plots" setup=[SetupMPCtests] begin
Expand Down