# JuMP and Pluto.jl?

**URL:** <https://discourse.julialang.org/t/jump-and-pluto-jl/49210>\
**Category:** Optimization (Mathematical)\
**Tags:** pluto\
**Created:** [October 28, 2020, 11:58pm UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210 "2020-10-28T23:58:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [October 28, 2020, 11:58pm UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/1 "2020-10-28T23:58:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![timquelch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timquelch/32/18278_2.png) [@timquelch](https://discourse.julialang.org/u/timquelch)\
**Post date:** [October 29, 2020, 1:21am UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/2 "2020-10-29T01:21:54Z")

</div>

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

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

```julia
begin
	optimize!(model)
	value(x1), value(x2), termination_status(model)
end

```

---

<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:** [October 29, 2020, 1:30am UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/3 "2020-10-29T01:30:25Z")

</div>

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](https://github.com/fonsp/Pluto.jl/issues/564)

---

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [October 29, 2020, 3:54am UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/4 "2020-10-29T03:54:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![twelvespot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/twelvespot/32/26397_2.png) [@twelvespot](https://discourse.julialang.org/u/twelvespot)\
**Post date:** [July 2, 2021, 3:20pm UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/5 "2021-07-02T15:20:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 2, 2021, 3:28pm UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/6 "2021-07-02T15:28:24Z")

</div>

Pluto cannot detect variable dependencies defined in macros yet, but this will change soon, see [https://github.com/fonsp/Pluto.jl/pull/1032](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?

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [February 2, 2022, 12:50pm UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/7 "2022-02-02T12:50:02Z")

</div>

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

---

<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 2, 2022, 7:09pm UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/8 "2022-02-02T19:09:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [February 4, 2022, 5:16pm UTC](https://discourse.julialang.org/t/jump-and-pluto-jl/49210/9 "2022-02-04T17:16:04Z")

</div>

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.
