JuMP and Pluto.jl?

Hi,

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?

Thanks!
Geoff

3 Likes

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
3 Likes

I had a play and ran into similar issues. It seems to not like the macros creating new variables and modifying the state of the model.

Underlying issue is this: https://github.com/fonsp/Pluto.jl/issues/564

1 Like

Thanks Tim and Oscar,

Your solution works Tim, so that’s great. It would be nice if it was neater, but it’s not critical right now.

Good work on finding the issue Oscar, I had a look but didn’t know enough to know what to look for.

Cheers,
Geoff

2 Likes

Hi Tim,

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?

Thanks,
Jarad

Pluto cannot detect variable dependencies defined in macros yet, but this will change soon, see https://github.com/fonsp/Pluto.jl/pull/1032

Can you try the Pluto branch of this PR and check if you still get the same error?

This #1032 did not seem to fix the JuMP Pluto problems. It would be nice to see it step by step.

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.

3 Likes

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.