-
-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing error message in nonlinear #3235
Comments
MWE: julia> using JuMP
julia> model = Model();
julia> @variable(model, x[1:2]);
julia> @NLobjective(model, Min, sum(sin.(x)))
ERROR: sin is not defined for type AbstractVariableRef. Are you trying to build a nonlinear problem? Make sure you use @NLconstraint/@NLobjective.
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] sin(#unused#::VariableRef)
@ JuMP ~/.julia/dev/JuMP/src/operators.jl:472
[3] _broadcast_getindex_evalf
@ ./broadcast.jl:648 [inlined]
[4] _broadcast_getindex
@ ./broadcast.jl:621 [inlined]
[5] getindex
@ ./broadcast.jl:575 [inlined]
[6] copy
@ ./broadcast.jl:922 [inlined]
[7] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(sin), Tuple{Vector{VariableRef}}})
@ Base.Broadcast ./broadcast.jl:883
[8] macro expansion
@ ~/.julia/dev/JuMP/src/macros.jl:1867 [inlined]
[9] top-level scope
@ REPL[24]:1 |
So the issue is the broadcast (jmp) pkg> st
Status `/private/tmp/jmp/Project.toml`
[4076af6c] JuMP v1.0.0
julia> using JuMP
julia> model = Model();
julia> @variable(model, x[1:2]);
julia> @NLobjective(model, Min, sum(sin.(x)))
ERROR: sum() can appear in nonlinear expressions only if the argument is a generator statement, for example, sum(x[i] for i in 1:N).
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] macro expansion
@ ~/.julia/packages/JuMP/0C6kd/src/parse_nlp.jl:544 [inlined]
[3] macro expansion
@ ~/.julia/packages/JuMP/0C6kd/src/macros.jl:2057 [inlined]
[4] top-level scope
@ REPL[17]:1
julia> @NLobjective(model, Min, sin.(x)' * (1:2))
ERROR: sin is not defined for type AbstractVariableRef. Are you trying to build a nonlinear problem? Make sure you use @NLconstraint/@NLobjective.
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] sin(#unused#::VariableRef)
@ JuMP ~/.julia/packages/JuMP/0C6kd/src/operators.jl:409
[3] _broadcast_getindex_evalf
@ ./broadcast.jl:648 [inlined]
[4] _broadcast_getindex
@ ./broadcast.jl:621 [inlined]
[5] getindex
@ ./broadcast.jl:575 [inlined]
[6] copy
@ ./broadcast.jl:922 [inlined]
[7] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(sin), Tuple{Vector{VariableRef}}})
@ Base.Broadcast ./broadcast.jl:883
[8] macro expansion
@ ~/.julia/packages/JuMP/0C6kd/src/parse_nlp.jl:544 [inlined]
[9] macro expansion
@ ~/.julia/packages/JuMP/0C6kd/src/macros.jl:2057 [inlined]
[10] top-level scope
@ REPL[18]:1 |
This is actually quite hard to fix, because we currently support things like: using JuMP
model = Model();
@variable(model, x);
a = 1
@NLobjective(model, Min, x * sin.(a)) In this case, we eagerly evaluate julia> @macroexpand @NLobjective(model, Min, x * sin.(a))
quote
#= REPL[51]:1 =#
JuMP._valid_model(model, :model)
begin
#= /Users/oscar/.julia/dev/JuMP/src/macros.jl:2390 =#
begin
#= /Users/oscar/.julia/dev/JuMP/src/macros.jl:1867 =#
JuMP._init_NLP(model)
var"#121###337" = JuMP.Expr(:call, :*, x, sin.(a))
end
#= /Users/oscar/.julia/dev/JuMP/src/macros.jl:2391 =#
JuMP.set_nonlinear_objective(model, JuMP._throw_error_for_invalid_sense(JuMP.var"#_error#97"{LineNumberNode, Symbol, Symbol, Expr}(:(#= REPL[51]:1 =#), :model, :Min, :(x * sin.(a))), MathOptInterface.MIN_SENSE), var"#121###337")
end
end So it's breaking to now disallow all broadcasts. We could potentially check if any of the arguments to a broadcast are variables. But that might also cause issues. Perhaps we just need to improve the error message. |
Reported on slack:
The issue is that we try to eagerly evaluate the contents inside
sum
:The text was updated successfully, but these errors were encountered: