Using JuMP in Python Environment

I am trying to embed JuMP-based Julia script into a Python-based computing framework using PyCall. However, I am facing problems when I define JuMP variable as

from julia import Ipopt
from julia.JuMP import Model 
from julia.JuMP import add_constraint
from julia.JuMP import add_variable

m = Model(Ipopt.Optimizer)
add_variable(m, x[1:2]

results in following error message

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-61e874849be7> in <module>
      1 m = Model(Ipopt.Optimizer)
----> 2 add_variable(m, x[1:2])

NameError: name 'x' is not defined

Is there any document that I can refer to for troubleshooting?

add_variable and add_constraint are not the same as @variable and @constraint. You need figure out how to call Julia macros from Python (if it’s even possible).

A better question is: why do you want to call JuMP from Python? This is probably more hassle than it’s worth. Use https://www.pyomo.org instead.

1 Like

I managed this by wrapping JuMP model into a Julia function. It can also dump out the variable values in Python.

One reason, I initially developed JuMP models, and I want to focus to improve the JuMP model rather than recoding in Pyomo. The second reason, there are subprocedures dependent on Python libraries for other tasks, and the entire framework is intended to be directed in a Python environment. Thanks

3 Likes