JuMP: optimizing in loop with an updated parameter does not work as I expect from experience with AMPL/GAMS behavior

Hi,

When I update a parameter of a predefined model, JuMP does not seem to update the parameter, at least when I call to optimize!. My solution for now is to put the whole model definition in a function as in the first illustration below and and call the function each time I change a parameter. But I was wondering whether there is another solution by way of the second model illustration below?

Any ideas are appreciated.

First code illustration

function NLP(ϕ)
    nlp = Model(optimizer_with_attributes(Ipopt.Optimizer))
    set_silent(nlp)
    @variable(nlp, x>=0, start = 1)
    @NLobjective(nlp, Max, log(x) - ϕ * x)
    optimize!(nlp)
    display(value(x))
    display(termination_status(nlp))
end

function solve()
    cnt = 100
    ϕ = 1
    while cnt > 95
        NLP(ϕ)
        ϕ = ϕ / cnt
        cnt -= 1
    end
end

@time solve()

Second code illustration

function solve()
    cnt = 100
    ϕ = 1

    nlp = Model(optimizer_with_attributes(Ipopt.Optimizer))
    set_silent(nlp)
    @variable(nlp, x>=0, start = 1)
    @NLobjective(nlp, Max, log(x) - ϕ * x)

    while cnt > 95
        optimize!(nlp)
        display(value(x))
        display(termination_status(nlp))
        ϕ = ϕ / cnt
        cnt -= 1
    end
end

@time solve()

https://jump.dev/JuMP.jl/stable/nlp/#Nonlinear-Parameters

Thanks @odow. I see how this works.