Skip to content

added: ExplicitMPC now supports AD with ForwardDiff.jl - #524

Merged
franckgaga merged 19 commits into
mainfrom
verify_obsv_option
Nov 19, 2026
Merged

franckgaga merged 19 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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.29%. Comparing base (9101f09) to head (0337402).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #524      +/-   ##
==========================================
+ Coverage   97.25%   97.29%   +0.03%     
==========================================
  Files          28       28              
  Lines        7077     7089      +12     
==========================================
+ Hits         6883     6897      +14     
+ Misses        194      192       -2     
Flag Coverage Δ
unittests 97.29% <100.00%> (+0.03%) ⬆️

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
@franckgaga franckgaga changed the title added: ExplicitMPC now supports automatic differentiation added: ExplicitMPC now supports automatic differentiation with ForwardDiff.jl Nov 19, 2026
@franckgaga franckgaga changed the title added: ExplicitMPC now supports automatic differentiation with ForwardDiff.jl added: ExplicitMPC now supports AD with ForwardDiff.jl Nov 19, 2026
@franckgaga

Copy link
Copy Markdown
Member Author

@PTWaade I will merge this PR, release an update and keep the issue open to remind me to check for the other autodiff engines.

@franckgaga

Copy link
Copy Markdown
Member Author

By the way the default SteadyKalmanFilter now support ForwardDiff with the latest ControlSystemsBase patch. Note that it requires ImplicitDifferentiation and ComponentArray to be loaded, however, so the time-varying KalmanFilter might be simpler and faster to differentiate. Here's the example with the SteadyKalmanFilter:

using ModelPredictiveControl, ForwardDiff
using ImplicitDifferentiation, ComponentArrays # needed to support AD through `kalman`
using Logging                                             
const ERROR_ONLY = ConsoleLogger(stderr, Logging.Error)   

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)
    mpc = ExplicitMPC(model; Hp=20, Hc=2, nint_ym=0, verify_obsv=false)
    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

@franckgaga
franckgaga merged commit 760983f into main Nov 19, 2026
14 of 16 checks passed
@franckgaga
franckgaga deleted the verify_obsv_option branch November 19, 2026 16:42
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.

2 participants