# \[JuMP\] Updating NLparameter of a model in a loop

**URL:** <https://discourse.julialang.org/t/jump-updating-nlparameter-of-a-model-in-a-loop/35081>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 24, 2020, 6:02pm UTC](https://discourse.julialang.org/t/jump-updating-nlparameter-of-a-model-in-a-loop/35081 "2020-02-24T18:02:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)\
**Post date:** [February 24, 2020, 6:02pm UTC](https://discourse.julialang.org/t/jump-updating-nlparameter-of-a-model-in-a-loop/35081/1 "2020-02-24T18:02:05Z")

</div>

_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:

```julia
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:

1. How do I access the NLparameter in anothter function having only the model? `model[:x_ref]` doesn’t work.
2. When trying to solve a problem with such an objective function I get the error:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 25, 2020, 4:26am UTC](https://discourse.julialang.org/t/jump-updating-nlparameter-of-a-model-in-a-loop/35081/2 "2020-02-25T04:26:49Z")

</div>

Hmm. I don’t know if NL parameters work in user-defined functions. It would be useful to update the docs for this.

If you can build the objective programmatically, you might be better off using [Nonlinear Modeling · JuMP](https://www.juliaopt.org/JuMP.jl/stable/nlp/#Raw-expression-input-1)

---

<div class="post-metadata">

**Author:** ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)\
**Post date:** [February 25, 2020, 11:41am UTC](https://discourse.julialang.org/t/jump-updating-nlparameter-of-a-model-in-a-loop/35081/3 "2020-02-25T11:41:33Z")

</div>

Thanks @odow.  
I think building it like this would be quite tricky as the real objective function that I am using has a lot of terms (a model predictive control objective).

1. Could I abuse a JuMP variable as a parameter? Define `@variable(model, p)` and then constraining `p == parameter` at each iteration?
2. Even if the NLparameter is not used in a user-defined function, how would I reference the NLparameter if I am given only the model?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 25, 2020, 2:40pm UTC](https://discourse.julialang.org/t/jump-updating-nlparameter-of-a-model-in-a-loop/35081/4 "2020-02-25T14:40:18Z")

</div>

1. Sure. Use `fix(p, parameter)`. This is what I do, for example, in SDDP.

2. Store it in the model:

```nohighlight
model = Model()
@NLparameter(model, my_param == 1)
model[:my_param] = my_param
model[:my_param]

```

This is likely an oversight. PRs to fix appreciated.
