tkoolen / Parametron.jl

Efficiently solving instances of a parameterized family of (possibly mixed-integer) linear/quadratic optimization problems in Julia
Other
73 stars 7 forks source link

Support for sparse matrix multiplication #108

Open tkoolen opened 4 years ago

tkoolen commented 4 years ago

I just noticed that in @expression and in @constraint sparse matrices are not accepted. For example, A=spzeros(n,m). If I use A_expr = @expression A or @constraint(model, A * X ==0) It gives me this error:

ERROR: LoadError: MethodError: no method matching one(::Type{Any})
Closest candidates are:
  one(::Type{Union{Missing, T}}) where T at missing.jl:87
  one(::Missing) at missing.jl:83
  one(::BitArray{2}) at bitarray.jl:400
  ...
Stacktrace:
 [1] one(::Type{Any}) at ./missing.jl:87
 [2] *(::SparseMatrixCSC{Float32,Int64}, ::Array{Variable,1})at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/SparseArrays/src/linalg.jl:53

Originally posted by @ssadat in https://github.com/tkoolen/Parametron.jl/issues/107#issuecomment-545127495

blegat commented 4 years ago

This might be related: We started a project to abstract all the code we have in JuMP/src/operator.jl to rewrite LinearAlgebra and SparseArrays code for JuMP expressions: https://github.com/JuliaOpt/MutableArithmetics.jl/blob/master/src/linear_algebra.jl The approach would be to create an abstract type so that if Parametron.Variable is a subtype of this abstract type, the call is dispatched to MutableArithmetics instead of LinearAlgebra/SparseArrays. Then the multiplication will exploit the mutability of the expressions created and will compute the element type of the resulting array correctly (usually, what is used in SparseArrays doesn't work as it start by promoting the type of both array to a common type so it the product of an array of Int and an array of Variable gives quadratic expressions).