Error with building NLexpression in JuMP 0.19.2

I ran into this unexpected error when trying to build a nonlinear expression.

julia> mean_vs = @NLexpression(model, [i = 1:num_buses], sum(prod2.get([m-1, m-1]) *
                               (vre_coeffs[i, m]^2 + vim_coeffs[i, m]^2) for m = 1:num_basis))
ERROR: LoadError: MethodError: no method matching _is_sum(::Expr)
Closest candidates are:
  _is_sum(::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\macros.jl:8
Stacktrace:
 [1] _parse_NL_expr(::Symbol, ::Expr, ::Symbol, ::Symbol, ::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:40
 [2] _parse_NL_expr(::Symbol, ::Expr, ::Symbol, ::Symbol, ::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:124
 [3] (::getfield(JuMP, Symbol("##80#81")){Symbol,Symbol,Symbol})(::Expr) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:34
 [4] _parse_gen(::Expr, ::getfield(JuMP, Symbol("##80#81")){Symbol,Symbol,Symbol}) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_expr.jl:325
 [5] _parse_gen(::Expr, ::getfield(JuMP, Symbol("##80#81")){Symbol,Symbol,Symbol}) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_expr.jl:347
 [6] _parse_NL_expr(::Symbol, ::Expr, ::Symbol, ::Int64, ::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:34
 [7] _process_NL_expr(::Symbol, ::Expr) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:232
 [8] @NLexpression(::LineNumberNode, ::Module, ::Vararg{Any,N} where N) at [user-dir]\.julia\packages\JuMP\ibcEh\src\macros.jl:1639
in expression starting at REPL[15]:1

vre_coeffs and vim_coeffs are variables and prod2.get simply returns a float. Am I doing something wrong or is this some sort of bug? Thanks!

Hi there!

Please read PSA: make it easier to help you and provide a minimal working example that reproduces the same error.

However, that looks like a bug because you should probably get a nicer error message. It might be because of the prod2.get. What happens if you try:

prod2_get = [prod2.get([m-1, m-1]) for m in 1:num_basis]
@NLexpression(model, [i = 1:num_buses], sum(
    prod2_get[m] * (vre_coeffs[i, m]^2 + vim_coeffs[i, m]^2) 
    for m = 1:num_basis)
)

Thanks for the link, I am new to using discourse.

prod2.get is indeed the problem. I wasn’t aware scalar functions weren’t allowed in JuMP NLexpressions. In this particular case a meaningful error message is not provided since the function is stored in a struct. A minimal example is provided below:

Input

using JuMP
myfunc(t) = 1
struct MyStruct
   func::Function
end
my_object = MyStruct(myfunc)
m = Model()
@variable(m, x[1:2])
@NLexpression(m, sum(my_object.func(i) * x[i] for i = 1:2))

Error Output

ERROR: LoadError: MethodError: no method matching _is_sum(::Expr)
Closest candidates are:
  _is_sum(::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\macros.jl:8
Stacktrace:
 [1] _parse_NL_expr(::Symbol, ::Expr, ::Symbol, ::Symbol, ::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:40
 [2] _parse_NL_expr(::Symbol, ::Expr, ::Symbol, ::Symbol, ::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:124
 [3] (::getfield(JuMP, Symbol("##80#81")){Symbol,Symbol,Symbol})(::Expr) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:34
 [4] _parse_gen(::Expr, ::getfield(JuMP, Symbol("##80#81")){Symbol,Symbol,Symbol}) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_expr.jl:325
 [5] _parse_gen(::Expr, ::getfield(JuMP, Symbol("##80#81")){Symbol,Symbol,Symbol}) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_expr.jl:347
 [6] _parse_NL_expr(::Symbol, ::Expr, ::Symbol, ::Int64, ::Symbol) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:34
 [7] _process_NL_expr(::Symbol, ::Expr) at [user-dir]\.julia\packages\JuMP\ibcEh\src\parse_nlp.jl:232
 [8] @NLexpression(::LineNumberNode, ::Module, ::Vararg{Any,N} where N) at [user-dir]\.julia\packages\JuMP\ibcEh\src\macros.jl:1639
in expression starting at REPL[7]:1

Thanks for finding this bug and providing a simple example! You’re correct that we don’t detect that it is a function because of the struct.

I’ve opened an issue: https://github.com/JuliaOpt/JuMP.jl/issues/2016

Thanks for the report. We have a fix incoming for the bad error message: https://github.com/JuliaOpt/JuMP.jl/pull/2017.