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

Sparse Matrix multiplied by variable vector with Julia v1.0.5 and 1.3 #2163

Closed
kaarthiksundar opened this issue Feb 15, 2020 · 4 comments
Closed
Labels
Category: SparseArrays Issue with sparse arrays and JuMP

Comments

@kaarthiksundar
Copy link

kaarthiksundar commented Feb 15, 2020

The code that worked with Julia 1.0.5 does not work with Julia 1.3.

# in julia 1.0.5
julia> using JuMP, GLPK, SparseArrays
julia> glpk_optimizer = JuMP.with_optimizer(GLPK.Optimizer, tm_lim = 100.0, msg_lev = GLPK.OFF)
julia> m = JuMP.Model(glpk_optimizer)
julia> num_variables = 8; 
julia> JuMP.@variable(m, y[i=1:num_variables])
julia> I = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]; 
julia> J = [1, 2, 3, 3, 4, 4, 5, 5, 6, 6]; 
julia> V = [1, 1, -0.333333, -1.0, -0.666667, 0.0, -1.0, -1.0, -1.0, -1.0];
julia> A = sparse(I, J, V, 8, 2)
julia> A * y
2-element Array{GenericAffExpr{Float64,VariableRef},1}:
 y[1] - 0.33333333333333337 y[3] - 0.6666666666666666 y[4] - y[5] - y[6]
 y[2] - y[3] - y[5] - y[6]   

# in julia 1.3
julia> A * y
ERROR: MethodError: no method matching one(::Type{GenericAffExpr{C,VariableRef} where C})
Closest candidates are:
  one(::Type{Missing}) at missing.jl:103
  one(::BitArray{2}) at bitarray.jl:400
  one(::Missing) at missing.jl:100
  ...
Stacktrace:
 [1] *(::SparseMatrixCSC{Real,Int64}, ::Array{VariableRef,1}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/SparseArrays/src/linalg.jl:54
 [2] top-level scope at REPL[23]:1

Any help is appreciated.

@odow
Copy link
Member

odow commented Feb 16, 2020

You likely need to do A * y in a macro, i.e.,

@expression(model, A * y)

I can't reproduce this on Julia 1.3 and JuMP#master.

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

julia> @variable(model, y[i=1:8])
8-element Array{VariableRef,1}:
 y[1]
 y[2]
 y[3]
 y[4]
 y[5]
 y[6]
 y[7]
 y[8]

julia> I = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2];

julia> J = [1, 2, 3, 3, 4, 4, 5, 5, 6, 6];

julia> V = [1, 1, -0.333333, -1.0, -0.666667, 0.0, -1.0, -1.0, -1.0, -1.0];  

julia> A = sparse(I, J, V, 2, 8)  # Is your code correct for this line? I reversed 8, 2.
2×8 SparseMatrixCSC{Float64,Int64} with 10 stored entries:
  [1, 1]  =  1.0
  [2, 2]  =  1.0
  [1, 3]  =  -0.333333
  [2, 3]  =  -1.0
  [1, 4]  =  -0.666667
  [2, 4]  =  0.0
  [1, 5]  =  -1.0
  [2, 5]  =  -1.0
  [1, 6]  =  -1.0
  [2, 6]  =  -1.0

julia> A * y
2-element Array{GenericAffExpr{Float64,VariableRef},1}:
 y[1] - 0.333333 y[3] - 0.666667 y[4] - y[5] - y[6]
 y[2] - y[3] - y[5] - y[6]                         

Much of the default sparse fallbacks don't work well with JuMP because we don't define one() and zero() for GenericAffExpr.

See: #1151

@blegat
Copy link
Member

blegat commented Feb 17, 2020

This might be because the type of the sparse matrix is SparseMatrixCSC{Real,Int64} which is not concrete, try SparseMatrixCSC{Float64,Int64}.

@odow odow added the Category: SparseArrays Issue with sparse arrays and JuMP label Feb 19, 2020
@odow
Copy link
Member

odow commented Apr 3, 2020

Still can't reproduce this on JuMP 0.21.2, even with V = Real[ ... ].

julia> A * y
2-element Array{GenericAffExpr{Float64,VariableRef},1}:
 y[1] - 0.333333 y[3] - 0.666667 y[4] - y[5] - y[6]
 y[2] - y[3] - y[5] - y[6]                         

julia> typeof(A)
SparseMatrixCSC{Real,Int64}

julia> typeof(y)
Array{VariableRef,1}

Can you reproduce? If not, I will close.

@odow
Copy link
Member

odow commented Dec 6, 2020

Closing as not reproducible. Please re-open if you can find a reproducible example on JuMP#master.

@odow odow closed this as completed Dec 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: SparseArrays Issue with sparse arrays and JuMP
Development

No branches or pull requests

3 participants