I would like to convert a SymPy expression in order to use as an objetive function in JuMP. Suppose my variable involves two variables
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
(this question was also asked on another platform: https://discourse.julialang.org/t/how-to-pass-a-sympy-expression-supported-by-jump/62109/3)
My answer from Discourse:
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)