Issue with ModelingToolkit in Pluto

I get the following error for initialising variables. the code runs fine in the terminal and in vscode using the same Julia instance.

Any ideas what this could be?

2 Likes

I don’t know. I’ll need @fonsp to help. It should work in any standard REPL or IDE, so Pluto must be changing the semantics of the global scope a bit.

3 Likes

Is this related to the lack of the interactive scoping of Pluto? Ie it is not consistent with the REPL/vscode inline/jupyter.

1 Like

This is due to the way Pluto handles running new cells. I think your issue is similar to Not escaping in macro definition gives confusing error · Issue #1112 · fonsp/Pluto.jl · GitHub

Note: I am working on adding support for Macros inside Pluto, if you want to try it see https://github.com/fonsp/Pluto.jl/pull/1032

2 Likes

It will probably work if you don’t use the macros. Pluto can’t see the assignments that are accomplished by the macros.

EDIT: See https://github.com/fonsp/Pluto.jl/issues/196. It may be worth making a specific new issue to see if someone can do some special case code for these macros.

2 Likes

Ok, this makes sense. Can I define the variables without the macro and still use the ModellingToolkit features?

1 Like

Also, the code above works in Neptune (non-reactive version of Pluto). Good to know for these special cases. I am very fond of working in Pluto…

1 Like

I think it should work and I think the MTK docs have examples without macros (but they are a lot wordier). Pluto also can’t track mutation, so if you make an object and then mutate it, you probably want to do both of those operations in one cell. A task in which the mutation can’t be contained in that way probably isn’t a fit for Pluto.

1 Like

yes, actually just doing

begin 
	@parameters t σ ρ β
	@variables x(t) y(t) z(t)	
end

makes it work in Pluto. Nice!

But also, does anyone know how to get the Latex rendered?

1 Like

This solution worked for me:
https://www.reddit.com/r/Julia/comments/mu9far/pluto_doesnt_render_symbolicsjl_expressions/

1 Like

nice, thanks!

Hi all,

I am setting a simple optimization model using JuMP. The model is as simple as this:

begin
	m1 = Model(Ipopt.Optimizer)
	@variable(m1, z1)
	@variable(m1, z2)
	@variable(m1, z3)

	@objective(m1, Min, z1^2 + 0.5*z2^2 + 0.4*z3^2)

	@constraint(m1, cons1b, 0.5*z1 + z2 == 10)
	@constraint(m1, cons1c, 0.5*z1 + z3 == 20)
end

I can solve it in VScode, Atom, and Jupyter as well. In Pluto the following error pops up:

cannot assign a value to variable workspace5.z1 from module workspace11

	1. macro expansion    @   macros.jl:149[inlined]
	2. top-level scope    @   Local: 3

Is there a way around this problem? I have been migrating a significant amount of notebooks into Pluto, and they have to be ready in early September. I realize that the problem is related to the macros used in JuMP, but JuMP does not have an alternative syntax.

Help will be very much appreciated. Thanks.

I have been migrating a significant amount of notebooks into Pluto, and they have to be ready in early September.

I would not recommend this. JuMP’s workflow does not integrate well with Pluto. Even if the macro thing gets sorted (it may already be?), the mutation issue remains.

Previous discussions:

Hi @odow,

Thank you very much for providing help so quickly. I tried some tricks, looked around in the discussions linked to this subject, and guessed that I would be in trouble. Anyway, I expected someone to come up with a trick that solves the problem as it happens in many other situations.

Unfortunately, it is too late to stop the migration process. Eleven out of 12 notebooks have been finished. This was the last one: optimization. If we cannot use a macro inside Pluto, that is quite bad. We use macros everywhere. I will have a go and try Neptune. This is for undergraduate teaching (economics, not computation), and it is a bad idea to mix up Pluto and VSCode or some other IDE. Too much for them.

In the end, I managed to run the simple JuPM model described above inside Pluto.

With the help of @disberd, here, a solution like the one provided by VSCode, Atom, or Jupyter can be found in that link.

1 Like