Skip to content

Improve efficiency of set_normalized_rhs! with exact Hessians #377

Closed

Description

@franckgaga

Similarly to jump-dev/Ipopt.jl#515, calling set_normalized_rhs! to update the constant vector of linear constraints before re-solving is not efficient, since it invalidates the inner model thus requiring an expensive initialize calls.

Here's a minimum reproducible example with ModelPredictiveControl.jl:

using ModelPredictiveControl, JuMP
using KNITRO

N = 35 # number of JuMP.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]
transcription = MultipleShooting()
optim = Model(KNITRO.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])
@profview sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])

producing this profile:

Image

The initialize portion would completely disappear of the flame graph if the model was not invalidated. A solution similar to jump-dev/Ipopt.jl#516 could be re-implemented here.

Thanks!

Activity

  1. odow commented on Nov 11, 2025

    @odow
    Member

    Looks like we just need to call KN_set_con_xxbnd again:

    # Add bound to constraint.
    if isa(set, MOI.LessThan{Float64})
    val = _clamp_inf(set.upper)
    KNITRO.@_checked KNITRO.KN_set_con_upbnd(model, num_cons, val)
    elseif isa(set, MOI.GreaterThan{Float64})
    val = _clamp_inf(set.lower)
    KNITRO.@_checked KNITRO.KN_set_con_lobnd(model, num_cons, val)
    elseif isa(set, MOI.EqualTo{Float64})
    val = _clamp_inf(set.value)
    KNITRO.@_checked KNITRO.KN_set_con_eqbnd(model, num_cons, val)
    else
    @assert set isa MOI.Interval{Float64}
    # Add upper bound.
    lb, ub = _clamp_inf(set.lower), _clamp_inf(set.upper)
    KNITRO.@_checked KNITRO.KN_set_con_lobnd(model, num_cons, lb)
    KNITRO.@_checked KNITRO.KN_set_con_upbnd(model, num_cons, ub)
    end

    This is easier than Ipopt.

  2. odow commented on Nov 12, 2025

    @odow
    Member

    Thanks! Feel free to keep opening issues. Benchmarks and reproducible examples are always helpful.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions