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

Bug in vectorized constraints with SparseAxisArrays #2919

Closed
hdavid16 opened this issue Mar 12, 2022 · 1 comment · Fixed by #2929
Closed

Bug in vectorized constraints with SparseAxisArrays #2919

hdavid16 opened this issue Mar 12, 2022 · 1 comment · Fixed by #2929
Labels
Category: Containers Related to the Containers submodule Type: Bug
Milestone

Comments

@hdavid16
Copy link
Contributor

I think the following should work (but is not currently supported):

@variable(model, u[i = 1:2, j = i:3])
Containers.@container(c[i = 1:2, j = i:3],rand())
@constraint(model, u .<= c)

ERROR: MethodError: no method matching _indices()
Closest candidates are:
  _indices(::JuMP.Containers.SparseAxisArray, ::Any...) at C:\Users\HD\.julia\packages\JuMP\lnUbA\src\Containers\SparseAxisArray.jl:217
  _indices(::Any, ::Any...) at C:\Users\HD\.julia\packages\JuMP\lnUbA\src\Containers\SparseAxisArray.jl:215

If the indices match, shouldn't this be allowed?

@odow odow added Type: Bug Category: Containers Related to the Containers submodule labels Mar 12, 2022
@odow
Copy link
Member

odow commented Mar 12, 2022

I think it's intended that this works, because broadcasting two SparseAxisArrays over a function works. I'd have to dig in further to see what the issue is:

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, u[i = 1:2, j = i:3])
JuMP.Containers.SparseAxisArray{VariableRef, 2, Tuple{Int64, Int64}} with 5 entries:
  [1, 1]  =  u[1,1]
  [1, 2]  =  u[1,2]
  [1, 3]  =  u[1,3]
  [2, 2]  =  u[2,2]
  [2, 3]  =  u[2,3]

julia> Containers.@container(c[i = 1:2, j = i:3],rand())
JuMP.Containers.SparseAxisArray{Float64, 2, Tuple{Int64, Int64}} with 5 entries:
  [1, 1]  =  0.527184
  [1, 2]  =  0.557751
  [1, 3]  =  0.118786
  [2, 2]  =  0.683632
  [2, 3]  =  0.828271

julia> @constraint(model, u .<= c)
ERROR: MethodError: no method matching _indices()
Closest candidates are:
  _indices(::JuMP.Containers.SparseAxisArray, ::Any...) at /Users/oscar/.julia/dev/JuMP/src/Containers/SparseAxisArray.jl:225
  _indices(::Any, ::Any...) at /Users/oscar/.julia/dev/JuMP/src/Containers/SparseAxisArray.jl:223
Stacktrace:
 [1] _indices(::Base.RefValue{String}) (repeats 3 times)
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/SparseAxisArray.jl:223
 [2] copy(bc::Base.Broadcast.Broadcasted{JuMP.Containers.BroadcastStyle{2, Tuple{Int64, Int64}}, Nothing, typeof(add_constraint), Tuple{Base.RefValue{Model}, Base.Broadcast.Broadcasted{JuMP.Containers.BroadcastStyle{2, Tuple{Int64, Int64}}, Nothing, typeof(build_constraint), Tuple{Base.RefValue{JuMP.var"#_error#85"{Tuple{Symbol, Expr}, Symbol, LineNumberNode}}, JuMP.Containers.SparseAxisArray{AffExpr, 2, Tuple{Int64, Int64}}, Base.RefValue{MathOptInterface.LessThan{Float64}}}}, Base.RefValue{String}}})
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/SparseAxisArray.jl:182
 [3] materialize(bc::Base.Broadcast.Broadcasted{JuMP.Containers.BroadcastStyle{2, Tuple{Int64, Int64}}, Nothing, typeof(add_constraint), Tuple{Base.RefValue{Model}, Base.Broadcast.Broadcasted{JuMP.Containers.BroadcastStyle{2, Tuple{Int64, Int64}}, Nothing, typeof(build_constraint), Tuple{Base.RefValue{JuMP.var"#_error#85"{Tuple{Symbol, Expr}, Symbol, LineNumberNode}}, JuMP.Containers.SparseAxisArray{AffExpr, 2, Tuple{Int64, Int64}}, Base.RefValue{MathOptInterface.LessThan{Float64}}}}, Base.RefValue{String}}})
   @ Base.Broadcast ./broadcast.jl:883
 [4] macro expansion
   @ ~/.julia/dev/JuMP/src/macros.jl:816 [inlined]
 [5] top-level scope
   @ REPL[5]:1

julia> foo(a, b) = a * b
foo (generic function with 1 method)

julia> foo.(u, c)
JuMP.Containers.SparseAxisArray{AffExpr, 2, Tuple{Int64, Int64}} with 5 entries:
  [1, 1]  =  0.527184280519112 u[1,1]
  [1, 2]  =  0.5577508885495441 u[1,2]
  [1, 3]  =  0.11878560090114121 u[1,3]
  [2, 2]  =  0.6836319966831192 u[2,2]
  [2, 3]  =  0.8282711576813722 u[2,3]

@odow odow changed the title Should JuMP support vectorized operations on SparseAxisArrays Bug in vectorized constraints with SparseAxisArrays Mar 12, 2022
@odow odow added this to the 1.x milestone Mar 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Containers Related to the Containers submodule Type: Bug
Development

Successfully merging a pull request may close this issue.

2 participants