Skip to content
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

Provide better error message for Vector+VariableRef #3554

Closed
odow opened this issue Nov 1, 2023 · 2 comments · Fixed by #3558
Closed

Provide better error message for Vector+VariableRef #3554

odow opened this issue Nov 1, 2023 · 2 comments · Fixed by #3558
Labels
Type: Error Messages Can be fixed with better error message

Comments

@odow
Copy link
Member

odow commented Nov 1, 2023

https://discourse.julialang.org/t/methoderror-no-method-matching-zero-type-vector-affexpr/105639

julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[1:2])
2-element Vector{VariableRef}:
 x[1]
 x[2]

julia> @variable(model, y)
y

julia> @constraint(model, x + y .>= 0)
ERROR: MethodError: no method matching zero(::Type{Vector{VariableRef}})

Closest candidates are:
  zero(::Union{Type{P}, P}) where P<:Dates.Period
   @ Dates ~/.julia/juliaup/julia-1.9.3+0.x64.apple.darwin14/share/julia/stdlib/v1.9/Dates/src/periods.jl:51
  zero(::GenericQuadExpr)
   @ JuMP ~/.julia/dev/JuMP/src/quad_expr.jl:126
  zero(::AbstractVariableRef)
   @ JuMP ~/.julia/dev/JuMP/src/variables.jl:348
  ...

Stacktrace:
  [1] _instantiate_zero(#unused#::Type{Vector{VariableRef}})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:28
  [2] promote_operation_fallback(op::typeof(+), #unused#::Type{Vector{VariableRef}}, #unused#::Type{VariableRef})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:51
  [3] promote_operation(::typeof(+), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:113
  [4] promote_operation_fallback(op::typeof(MutableArithmetics.add_mul), T::Type, args::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:99
  [5] promote_operation(::typeof(MutableArithmetics.add_mul), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:113
  [6] mutability(::Type, ::Function, ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:266
  [7] mutability(::Vector{VariableRef}, ::Function, ::Vector{VariableRef}, ::VariableRef)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:274
  [8] operate!!(op::typeof(MutableArithmetics.add_mul), x::Vector{VariableRef}, args::VariableRef)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/rewrite.jl:93
  [9] macro expansion
    @ ~/.julia/dev/MutableArithmetics/src/rewrite.jl:321 [inlined]
 [10] macro expansion
    @ ~/.julia/dev/JuMP/src/macros.jl:717 [inlined]
 [11] top-level scope
    @ REPL[5]:1
@odow
Copy link
Member Author

odow commented Nov 1, 2023

I don't really know a good way of going about this. Perhaps we should just add a method to operate!!(op::typeof(MutableArithmetics.add_mul), x::Vector{VariableRef}, args::VariableRef)? We can't do it at the MA level, because there might be types that do support vector + scalar.

@odow odow added the Type: Error Messages Can be fixed with better error message label Nov 2, 2023
@blegat
Copy link
Member

blegat commented Nov 2, 2023

I think the issue is promote_operation. We could try using promote_op and only resort to the fallback in case promote_op does not return something concrete. I was discussing this with @vchuravy at the JuliaCon and it seems the takeaway was that promote_op is undecidable (like the HALT problem) and promote_op wants to always terminate so it has to give away something. So it guarantees to always return a supertype but it's not always the tightest possible supertype. For MA, we always want a concrete type so that's why we cannot completely rely on promote_op but in the case where promote_op does return a concrete type then we can just use it.
I also discussed this with @schlichtanders after the IsDef.jl talk as IsDef.Out is trying to achieve the same goal as promote_operation, see:
https://www.youtube.com/watch?v=2QLJOQNiZhI&t=1765s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Error Messages Can be fixed with better error message
Development

Successfully merging a pull request may close this issue.

2 participants