copied from JuMP Gitter channel
I have a question about solving a nonlinear JuMP model in a loop while updating some nonlinear parameters of the obj function:
using JuMP, Ipopt
function setup_model()
model = JuMP.Model(with_optimizer(Ipopt.Optimizer))
@variable(model, x[1:2])
# I want to change this parameter in an (MPC)-loop later
@NLparameter(model, x_ref[1:2] == 0)
function my_obj(x...)
return (x[1] - x_ref[1])^3 + (x[2] - x_ref[2])^2
end
function ∇my_obj(g, x...)
g[1] = 3 * (x[1] - x_ref[1])^2
g[2] = 2 * (x[2] - x_ref[2])
end
JuMP.register(model, :my_obj, 2, my_obj, my_obj)
@NLobjective(model, Min, my_obj(x...))
return model
end
function run_in_loop(model::JuMP.Model, new_ref)
# update NLparameter x_ref here, how?
# x_ref <-- new_ref
JuMP.optimize!(model)
return JuMP.value(x)
end
# MAIN LOOP
model = setup_model()
for i = 1:5
new_ref = rand(2)
x = run_in_loop(model, new_ref)
end
My problem looks similar to this simplified (stupid) example. Basically I want to solve the model in each iteration with updated parameters x_ref
. I have two questions about that:
- How do I access the NLparameter in anothter function having only the model?
model[:x_ref]
doesn’t work. - When trying to solve a problem with such an objective function I get the error:
ERROR: LoadError: MethodError: no method matching -(::Float64, ::NonlinearParameter)
Closest candidates are:
-(::Float64, ::Float64) at float.jl:403
-(::Float64) at float.jl:393
-(::Real, ::Complex{Bool}) at complex.jl:300
...
Stacktrace:
[1] (::var"#my_obj#501"{Array{NonlinearParameter,1}})(::Float64, ::Vararg{Float64,N} where N) at /Users/Micha/Dropbox/Research/4YP_Linh/question.jl:11
[2] (::JuMP.var"#96#99"{var"#my_obj#501"{Array{NonlinearParameter,1}}})(::SubArray{Float64,1,Array{Float64,1},Tuple{UnitRange{Int64}},true}) at /Users/Micha/.julia/packages/JuMP/MsUSY/src/nlp.jl:1177