Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JuMP interface with register: AssertionError: d.want_hess error #318

Closed
franckgaga opened this issue Apr 7, 2024 · 3 comments
Closed

JuMP interface with register: AssertionError: d.want_hess error #318

franckgaga opened this issue Apr 7, 2024 · 3 comments

Comments

@franckgaga
Copy link

Related to #115

The JuMP.jl interface does not work with user-registered nonlinear operators. Here is a simple example running JuMP v1.21.0 and MadNLP v0.8.0:

using JuMP, MadNLP
optim = Model(MadNLP.Optimizer)
nvar = 2
function Jfunc(ΔŨtup...)
    ΔŨ = collect(ΔŨtup)
    return sum(ΔŨ.^2)
end
@variable(optim, ΔŨvar[1:nvar])
register(optim, :Jfunc, nvar, Jfunc, autodiff=true)
@NLobjective(optim, Min, Jfunc(ΔŨvar...))
optimize!(optim)
value.(ΔŨvar)

giving:

ERROR: AssertionError: d.want_hess
Stacktrace:
  [1] hessian_lagrangian_structure
    @ ~/.julia/packages/MathOptInterface/mz9FK/src/Nonlinear/ReverseAD/mathoptinterface_api.jl:296 [inlined]
  [2] hessian_lagrangian_structure(evaluator::MathOptInterface.Nonlinear.Evaluator{…})
    @ MathOptInterface.Nonlinear ~/.julia/packages/MathOptInterface/mz9FK/src/Nonlinear/evaluator.jl:179
  [3] hessian_lagrangian_structure(model::MadNLPMOI.Optimizer)
    @ MadNLPMOI ~/.julia/packages/MadNLP/waaOd/ext/MadNLPMOI/MadNLPMOI.jl:733
  [4] MadNLPMOI.MOIModel(model::MadNLPMOI.Optimizer)
    @ MadNLPMOI ~/.julia/packages/MadNLP/waaOd/ext/MadNLPMOI/MadNLPMOI.jl:839
  [5] optimize!(model::MadNLPMOI.Optimizer)
    @ MadNLPMOI ~/.julia/packages/MadNLP/waaOd/ext/MadNLPMOI/MadNLPMOI.jl:893
  [6] optimize!
    @ ~/.julia/packages/MathOptInterface/mz9FK/src/Bridges/bridge_optimizer.jl:380 [inlined]
  [7] optimize!
    @ ~/.julia/packages/MathOptInterface/mz9FK/src/MathOptInterface.jl:85 [inlined]
  [8] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{…})
    @ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/mz9FK/src/Utilities/cachingoptimizer.jl:316
  [9] optimize!(model::Model; ignore_optimize_hook::Bool, _differentiation_backend::MathOptInterface.Nonlinear.SparseReverseMode, kwargs::@Kwargs{})
    @ JuMP ~/.julia/packages/JuMP/glJ0u/src/optimizer_interface.jl:457
 [10] optimize!(model::Model)
    @ JuMP ~/.julia/packages/JuMP/glJ0u/src/optimizer_interface.jl:409
 [11] top-level scope
    @ ~/.julia/dev/ModelPredictiveControl/src/controller/test_yo.jl:11
Some type information was truncated. Use `show(err)` to see complete types.
@frapac
Copy link
Collaborator

frapac commented Apr 10, 2024

Thank you for providing a MWE for your issue. I confirm that MadNLP is not supporting user-defined operator in MadNLP. A solution is to fallback to the LBFGS algorithm implemented inside MadNLP if no Hessian is detected (as it is done in Ipopt). This is addressed in this PR, which should solve your issue:
#322

@frapac
Copy link
Collaborator

frapac commented May 3, 2024

Solved by #322

@frapac frapac closed this as completed May 3, 2024
@franckgaga
Copy link
Author

franckgaga commented May 3, 2024

@frapac Thanks for your work!

The current MadNLP.jl dev version does solve this issue indeed on this MWE. It may or may not be related, but on the specific case of ModelPredictiveControl.jl (that internally uses user-registered nonlinear operator):

using ModelPredictiveControl, JuMP, MadNLP
f(x,u,_) = 0.1x
h(x,_) = x
model = NonLinModel(f, h, 1, 1, 1, 1, solver=nothing)
nmpc = NonLinMPC(model, Hp=10, optim=Model(MadNLP.Optimizer))
nmpc([1])

there is no longer the want_hess error, but a new one:

ERROR: UndefVarError: `∇f` not defined
Stacktrace:
  [1] eval_constraint_jacobian_transpose_product(block::MadNLPMOI.QPBlockData{…}, Jtv::Vector{…}, x::Vector{…}, v::Vector{…})
    @ MadNLPMOI ~/.julia/dev/MadNLP/ext/MadNLPMOI/utils.jl:528
  [2] eval_constraint_jacobian_transpose_product(model::MadNLPMOI.Optimizer, Jtv::Vector{…}, x::Vector{…}, v::Vector{…})
    @ MadNLPMOI ~/.julia/dev/MadNLP/ext/MadNLPMOI/MadNLPMOI.jl:768
  [3] jtprod!
    @ ~/.julia/dev/MadNLP/ext/MadNLPMOI/MadNLPMOI.jl:810 [inlined]
  [4] _eval_jtprod_wrapper!(cb::MadNLP.SparseCallback{…}, x::Vector{…}, v::Vector{…}, jvt::Vector{…})
    @ MadNLP ~/.julia/dev/MadNLP/src/nlpmodels.jl:620
  [5] eval_lag_hess_wrapper!(solver::MadNLPSolver{…}, kkt::MadNLP.SparseKKTSystem{…}, x::MadNLP.PrimalVector{…}, l::Vector{…}; is_resto::Bool)
    @ MadNLP ~/.julia/dev/MadNLP/src/IPM/callbacks.jl:169
  [6] eval_lag_hess_wrapper!
    @ ~/.julia/dev/MadNLP/src/IPM/callbacks.jl:142 [inlined]
  [7] regular!(solver::MadNLPSolver{…})
    @ MadNLP ~/.julia/dev/MadNLP/src/IPM/solver.jl:262
  [8] solve!(nlp::MadNLPMOI.MOIModel{…}, solver::MadNLPSolver{…}, stats::MadNLP.MadNLPExecutionStats{…}; x::Nothing, y::Nothing, zl::Nothing, zu::Nothing, kwargs::@Kwargs{})
    @ MadNLP ~/.julia/dev/MadNLP/src/IPM/solver.jl:165
  [9] solve!
    @ ~/.julia/dev/MadNLP/src/IPM/solver.jl:128 [inlined]
 [10] solve!
    @ ~/.julia/dev/MadNLP/src/IPM/solver.jl:14 [inlined]
 [11] solve!(solver::MadNLPSolver{…})
    @ MadNLP ~/.julia/dev/MadNLP/src/IPM/solver.jl:17
 [12] optimize!(model::MadNLPMOI.Optimizer)
    @ MadNLPMOI ~/.julia/dev/MadNLP/ext/MadNLPMOI/MadNLPMOI.jl:956
 [13] optimize!
    @ ~/.julia/packages/MathOptInterface/2rAFb/src/Bridges/bridge_optimizer.jl:380 [inlined]
 [14] optimize!
    @ ~/.julia/packages/MathOptInterface/2rAFb/src/MathOptInterface.jl:85 [inlined]
 [15] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{…})
    @ MathOptInterface.Utilities ~/.julia/packages/MathOptInterface/2rAFb/src/Utilities/cachingoptimizer.jl:316
 [16] optimize!(model::Model; ignore_optimize_hook::Bool, _differentiation_backend::MathOptInterface.Nonlinear.SparseReverseMode, kwargs::@Kwargs{})
    @ JuMP ~/.julia/packages/JuMP/as6Ji/src/optimizer_interface.jl:457
 [17] optimize!
    @ ~/.julia/packages/JuMP/as6Ji/src/optimizer_interface.jl:409 [inlined]
 [18] optim_objective!(mpc::NonLinMPC{Float64, UnscentedKalmanFilter{…}, Model, ModelPredictiveControl.var"#98#100"})
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/execute.jl:476
 [19] moveinput!(mpc::NonLinMPC{…}, ry::Vector{…}, d::Vector{…}; Dhat::Vector{…}, Rhaty::Vector{…}, Rhatu::Vector{…}, ym::Nothing, D̂::Vector{…}, R̂y::Vector{…}, R̂u::Vector{…})
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/execute.jl:67
 [20] moveinput!(mpc::NonLinMPC{…}, ry::Vector{…}, d::Vector{…})
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/controller/execute.jl:52
 [21] #_#123
    @ ~/.julia/dev/ModelPredictiveControl/src/predictive_control.jl:49 [inlined]
 [22] PredictiveController
    @ ~/.julia/dev/ModelPredictiveControl/src/predictive_control.jl:44 [inlined]
 [23] (::NonLinMPC{Float64, UnscentedKalmanFilter{…}, Model, ModelPredictiveControl.var"#98#100"})(ry::Vector{Int64})
    @ ModelPredictiveControl ~/.julia/dev/ModelPredictiveControl/src/predictive_control.jl:44
 [24] top-level scope
    @ ~/Dropbox/Programmation/Julia/TestMPC/src/test_yo.jl:67
Some type information was truncated. Use `show(err)` to see complete types.

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

No branches or pull requests

2 participants