Skip to content

Reuse existing data structures on re-solve - #516

Merged
odow merged 8 commits into
masterfrom
od/resolve
Nov 9, 2025
Merged

odow merged 8 commits into
masterfrom
od/resolve

Conversation

@odow

@odow odow commented Nov 7, 2025

Copy link
Copy Markdown
Member

Closes #515

@codecov

codecov Bot commented Nov 7, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3a7aed2) to head (e888e71).
⚠ïļ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #516   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1154      1164   +10     
=========================================
+ Hits          1154      1164   +10     

☔ View full report in Codecov by Sentry.
ðŸ“Ē 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.

@odow

odow commented Nov 7, 2025

Copy link
Copy Markdown
Member Author

Before

julia> @time sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x˂_0=[0, 0, 0])
  0.250533 seconds (1.72 M allocations: 119.051 MiB, 3.97% gc time)
Simulation results of NonLinMPC with 35 time steps.

after

julia> @time sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x˂_0=[0, 0, 0])
  0.192342 seconds (586.57 k allocations: 49.896 MiB, 2.61% gc time)
Simulation results of NonLinMPC with 35 time steps.

Code

using ModelPredictiveControl, JuMP
using Ipopt

N = 35 # number of solves/optimize! calls

function f!(ṋ, x, u, _ , p)
    g, L, K, m = p          # [m/sÂē], [m], [kg/s], [kg]
    Îļ, ω = x[1], x[2]       # [rad], [rad/s]
    τ  = u[1]               # [Nm]
    ṋ[1] = ω
    ṋ[2] = -g/L*sin(Îļ) - K/m*ω + τ/m/L^2
    return nothing
end
h!(y, x, _ , _ ) = (y[1] = 180/π*x[1]; nothing) # [°]
p = [9.8, 0.4, 1.2, 0.3]
nu, nx, ny, Ts = 1, 2, 1, 0.1
model = NonLinModel(f!, h!, Ts, nu, nx, ny; p)
p_plant = copy(p); p_plant[3] = p[3]*1.25
plant = NonLinModel(f!, h!, Ts, nu, nx, ny; p=p_plant)
Hp, Hc, Mwt, Nwt = 20, 2, [0.5], [2.5]
Îą=0.01; σQ=[0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1]
σQint_ym = zeros(0)
umin, umax = [-1.5], [+1.5]
nc = Hp+1
transcription = MultipleShooting()
optim = Model(Ipopt.Optimizer); 
oracle = true
hessian = true
nmpc = NonLinMPC(model; 
    Hp, Hc, Mwt, Nwt, Cwt=Inf, transcription, oracle, hessian, optim,
    Îą, σQ, σR, nint_u, σQint_u, σQint_ym
)
nmpc = setconstraint!(nmpc; umin, umax)
unset_time_limit_sec(nmpc.optim)
sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x˂_0=[0, 0, 0])
@time sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x˂_0=[0, 0, 0])

@odow
odow marked this pull request as ready for review November 7, 2025 01:04
@odow odow changed the title WIP: try to reuse existing data structures on re-solve Reuse existing data structures on re-solve Nov 7, 2025
@franckgaga

Copy link
Copy Markdown

Damn that was fast! Did you sleep, like, sometimes ? XP

Good job, seems to solve the "useless" initialize call at each iteration. It's cool that I will be able to keep my polymorphism behavior in linconstraint! and linconstrainteq! methods.

@odow

odow commented Nov 7, 2025

Copy link
Copy Markdown
Member Author

Did you sleep, like, sometimes

I live in New Zealand. Quiet Friday for me

if !has_nlp_constraints && !has_quadratic_constraints
Ipopt.AddIpoptStrOption(model.inner, "jac_c_constant", "yes")
Ipopt.AddIpoptStrOption(model.inner, "jac_d_constant", "yes")
if model.is_linear

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Jacobian can be constant, even if the objective is nonlinear ;)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But can we detect these cases easily ? The linear case is just an obvious case, so no additional code except maybe a Bool test. That would not be the case with nonlinear constraints.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_linear referred only to the constraints. I renamed to has_only_linear_constraints

@franckgaga

Copy link
Copy Markdown

Nice profile, good job!

image

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Re-use initialised AD if bounds change

3 participants