Skip to content

added: ExplicitMPC now support AD with ReverseDiff.jl - #528

Merged
franckgaga merged 5 commits into
mainfrom
reverse_diff_empc
Nov 20, 2026
Merged

franckgaga merged 5 commits into
mainfrom
reverse_diff_empc

Conversation

@franckgaga

@franckgaga franckgaga commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Related to #523. The change needed to also support ReverseDiff.jl was quite small. The solution suggested by Claude worked well.

@PTWaade Here's a MRE with ReverseDiff.jl:

using ModelPredictiveControl
using ReverseDiff
using Logging                                          
const ERROR_ONLY = ConsoleLogger(stderr, Logging.Error)   


A  = [0.9 0.08; -0.15 0.75]
Bu = [0.005, 0.09]
C  = [1.0 0.0]

function first_move(Nwt)
    T = eltype(Nwt)
    model = LinModel{T}(A, Bu, C, 0, 0, 0.1)
    estim = KalmanFilter(model, nint_ym=0, verify_obsv=false)
    mpc = ExplicitMPC(estim; Hp=20, Hc=2, Nwt=Nwt)
    preparestate!(mpc, [0.0])
    return moveinput!(mpc, [1.0])[1]
end

@show first_move([0.5])
@show ReverseDiff.gradient(first_move, [0.5])
with_logger(ERROR_ONLY) do
    @time ReverseDiff.gradient(first_move, [0.5])
end

giving:

first_move([0.5]) = 2.0023741965935966
┌ Warning: could not estimate the model delay, Hp (20) might be too small.
└ @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/explicitmpc.jl:179
┌ Warning: could not validate the condition number of the QP Hessian.
└ @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/construct.jl:870
ReverseDiff.gradient(first_move, [0.5]) = [-1.5058894886897185]
  0.000453 seconds (28.78 k allocations: 1.179 MiB)

@PTWaade

PTWaade commented Nov 20, 2026

Copy link
Copy Markdown

Great news! :)

@codecov-commenter

codecov-commenter commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.36%. Comparing base (760983f) to head (e4cbef3).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #528      +/-   ##
==========================================
+ Coverage   97.31%   97.36%   +0.04%     
==========================================
  Files          28       28              
  Lines        7089     7099      +10     
==========================================
+ Hits         6899     6912      +13     
+ Misses        190      187       -3     
Flag Coverage Δ
unittests 97.36% <100.00%> (+0.04%) ⬆️

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.

@franckgaga
franckgaga merged commit dd67ee0 into main Nov 20, 2026
14 of 15 checks passed
@franckgaga
franckgaga deleted the reverse_diff_empc branch November 20, 2026 21:01
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.

3 participants