Has anyone tried using JuMP inside a Pluto.jl notebook? Are there any tricks one needs to be aware of?
I have a simple notebook with two cells:
“”"
using JuMP, GLPK
“”"
and
“”"
begin
M = Model(GLPK.Optimizer) @variable(M, test)
end
“”"
The first time I run the second cell, it works, but subsequently, I get an error:
“cannot assign a value to variable workspace767.test from module workspace768”
(Note that the numbers are different, and that the 767 stays the same, whereas the 768 increases with each run of the cell)
Any ideas as to how to work around this, or do JuMP and Pluto not play well with each other yet?
I’ve found the same. There are all kinds of reassingment errors: both in assigning to the variables within the model, and assigning to the julia variables.
The simplest way I’ve found around these errors is to wrap the whole model creation in one cell
begin
model = Model(Ipopt.Optimizer) # recreate/reset the model
x1, x2 = nothing, nothing # clear the julia variables
@variable(model, x1)
@variable(model, x2)
@NLobjective(model, Min, (1 - x1)^2 + 100*(x2 - x1^2)^2)
model
end
Similarly for querying results, because of the way that pluto orders cells, you should force that solutions are quieried immediately after optimising.
begin
optimize!(model)
value(x1), value(x2), termination_status(model)
end
I’m now just trying the same issues here with optimization within Pluto. I tried running what you’ve suggested but with constraints, and when I name the constraints like this @constraint(model, c1, 4x >= 3) I get “cannot assign a value to variable workspace32.c1 from module workspace52”. But when I add constraints like this @constraint(model, 4x >= 3), then the model runs and I get no errors. Have you found a better way to name constraints? Or should I just not bother because it works for now?
You cannot build a JuMP model step-by-step in Pluto because JuMP models use mutation. If you must use JuMP and Pluto, wrap the entire model construction in a begin ... end block.
Thanks for the clarification. I read some old discourse threads and it seemed like there was a solution coming soon. But this seems to be working fine.