How to pass a SymPy expression supported by JuMP

I would like to convert a SymPy expression in order to use as an objetive function in JuMP. Suppose my variable involves two variables p [1,1] and p [1,2].

using SymPy
using  JuMP, Ipopt


# expr is my SymPy expression
fn = lambdify( expr )

Now, my model

model = Model(Ipopt.Optimizer)
l = zeros(1,2)
@variable(model, p[ j = 1:S] >= 0 )

#obj function
@objective(model,Min,fn)
print(model)
The objective function `#99` is not supported by JuMP.

I’m guessing that your function is nonlinear, in which case you should use @NLobjective.

It might also be necessary to register the function.

1 Like

Hi, I do this

x = Sym("x")
y = Sym("y")
expr = x^2 + y
fn = lambdify( expr )

and the model

model = Model(Ipopt.Optimizer)
l = zeros(1,2)
@variable(model, x[ j = 1:2] >= 0 )

register(model, :fn,2, fn, autodiff = true)

#obj function
@NLobjective(model,Min,fn)
print(model)
Unexpected object #99 (of type SymPy.var"#99#100"{SymPy.var"###792"} in nonlinear expression.

Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] _parse_NL_expr_runtime(::Model, ::Function, ::Array{JuMP._Derivatives.NodeData,1}, ::Int64, ::Array{Float64,1}) at C:\Users\xxxxx\.julia\packages\JuMP\e0Uc2\src\parse_nlp.jl:223
 [3] top-level scope at C:\Users\xxxxx\.julia\packages\JuMP\e0Uc2\src\parse_nlp.jl:247
 [4] top-level scope at C:\Users\xxxxx\.julia\packages\JuMP\e0Uc2\src\macros.jl:1368
 [5] top-level scope at In[92]:8
 [6] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

Sorry, I don’t know what SymPy’s lambdify does in detail and expected that it would simply return a callable function.

It needs to be @NLobjective(model, Min, fn(x[1], x[2])).

using JuMP, SymPy, Ipopt
x = Sym("x")
y = Sym("y")
expr = x^2 + y
fn = lambdify(expr)
model = Model(Ipopt.Optimizer)
@variable(model, x[1:2] >= 0)
register(model, :fn, 2, fn; autodiff = true)
@NLobjective(model, Min, fn(x[1], x[2]))
optimize!(model)
2 Likes

I will often need to optimize a function with many variables. I wouldn’t want to have to write variable by variable. Is there any way to avoid this for automation issues?

model = Model(Ipopt.Optimizer)
@variable(model, p[1:S] >= 0)
register(model, :fn, 2, fn; autodiff = true)
@NLobjective(model, Min, fn( p[1:S]  ) )
Incorrect number of arguments for "fn" in nonlinear expression.

This is explained in the documentation:
https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-functions-with-vector-inputs

@NLobjective(model, Min, fn(p...))
3 Likes