Skip to content

added: ExplicitMPC now supports automatic differentiation - #524

Open
franckgaga wants to merge 10 commits into
mainfrom
verify_obsv_option
Open

franckgaga wants to merge 10 commits into
mainfrom
verify_obsv_option

Conversation

@franckgaga

@franckgaga franckgaga commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Bugfix for #523. I only tested it with ForwardDiff.jl, for now.

I added a verify_obsv=true keyword argument to all LinModel state observers, that is:

  • SteadyKalmanFilter
  • KalmanFilter
  • Luenberger

The failed checkups for the Hessian condition number $\mathrm{cond}(\mathbf{\tilde{H}})$ and the prediction horizon $H_p$ will still emit a @warn, but it can be silenced locally with:

using Logging                                              
const ERROR_ONLY = ConsoleLogger(stderr, Logging.Error)
mpc = with_logger(ERROR_ONLY) do
    ExplicitMPC(estim; Hp=20, Hc=2)
end

@franckgaga franckgaga changed the title added: verif_obsv=true keyword argument to all LinModel state observers added: ExplicitMPC now support automatic differentiation Nov 18, 2026
@codecov-commenter

codecov-commenter commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.56098% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.27%. Comparing base (019ab4d) to head (ffa0ecc).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/estimator/construct.jl 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #524      +/-   ##
==========================================
- Coverage   97.32%   97.27%   -0.06%     
==========================================
  Files          28       28              
  Lines        7077     7087      +10     
==========================================
+ Hits         6888     6894       +6     
- Misses        189      193       +4     
Flag Coverage Δ
unittests 97.27% <97.56%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

It seems to crash on `ControlSystems.jl` side, but it's clean in this repo.
@franckgaga

Copy link
Copy Markdown
Member Author

@PTWaade

Now it's possible to autodiff this MRE with ForwardDiff.jl:

using ModelPredictiveControl, ForwardDiff
using Logging                                              # new line
const ERROR_ONLY = ConsoleLogger(stderr, Logging.Error)    # new line

A  = [0.9 0.08; -0.15 0.75]
Bu = reshape([0.005, 0.09], 2, 1)
C  = [1.0 0.0]

# The first move of an ExplicitMPC as a function of its input weight
function first_move(Nwt)
    T = typeof(Nwt)
    model = LinModel{T}(A, Bu, C, 0, 0, 0.1)
    estim = KalmanFilter(model; nint_ym=0, verify_obsv=false)   # added: verify_obsv=false
    mpc = ExplicitMPC(estim; Hp=20, Hc=2)
    preparestate!(mpc, [0.0])
    return moveinput!(mpc, [1.0])[1]
end

@show first_move(0.5)
@show ForwardDiff.derivative(first_move, 0.5)
with_logger(ERROR_ONLY) do
    @time ForwardDiff.derivative(first_move, 0.5)
end

Does it seems to fit your needs?

@franckgaga franckgaga changed the title added: ExplicitMPC now support automatic differentiation added: ExplicitMPC now supports automatic differentiation Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Building an ExplicitMPC from dual numbers fails in eigenvalue checks, so its weights can't be differentiated

2 participants