JuMP @constraint with sparse matrix

I want to create PSD constraints with a sparse matrix of AffExpr as in the snippet below (rawdata is a Vector of tuples of the form (I, J, Va, Vs) containing row-col-values information)

model = let
    println("Constructing SDP model...")
    model = JuMP.Model()
    numvars = maximum([maximum(m[3]) for m in rawdata])
    println("Number of variables = $numvars")
    x = @variable(model, x[1:numvars])
    for m in rawdata
        mat = sparse(m[1], m[2], m[4].*x[m[3]])
        @constraint(model, mat >=0, PSDCone())
    end
    model
end

Is this error expected?

MethodError: no method matching -(::SparseMatrixCSC{AffExpr, Int64}, ::Int64)
Closest candidates are:
  -(!Matched::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:86
  -(!Matched::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}, ::T) where T at C:\Users\******\.julia\packages\MathOptInterface\YDdD3\src\Utilities\functions.jl:1725
  -(!Matched::LinearAlgebra.UniformScaling, ::Number) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\uniformscaling.jl:147
  ...
sub_mul(a::SparseMatrixCSC{AffExpr, Int64}, b::Int64) at MutableArithmetics.jl:33
operate(::typeof(MutableArithmetics.sub_mul), ::SparseMatrixCSC{AffExpr, Int64}, ::Int64) at interface.jl:131
operate_fallback!(::MutableArithmetics.NotMutable, ::Function, ::SparseMatrixCSC{AffExpr, Int64}, ::Int64) at interface.jl:428
operate!(op::typeof(MutableArithmetics.sub_mul), x::SparseMatrixCSC{AffExpr, Int64}, args::Int64) at rewrite.jl:83
macro expansion at rewrite.jl:279 [inlined]
macro expansion at macros.jl:676 [inlined]
top-level scope at readDavid.jl:25
eval at boot.jl:360 [inlined]

I can’t reproduce this. Please provide a reproducible example:

julia> using JuMP, SparseArrays

julia> model = Model();

julia> @variable(model, x[1:2]);

julia> X = sparse([1, 2], [1, 2], x .+ 1)
2×2 SparseMatrixCSC{AffExpr, Int64} with 2 stored entries:
 x[1] + 1  ⋅
 ⋅         x[2] + 1

julia> @constraint(model, X >= 0, PSDCone())
[x[1] + 1  0;
 0         x[2] + 1] ∈ PSDCone()

Also, ensure that you have updated your packages. JuMP should be version 0.22

julia> import Pkg

julia> Pkg.status("JuMP")
      Status `/private/tmp/Project.toml`
  [4076af6c] JuMP v0.22.0

I can reproduce this error with JuMP v0.21.10 using your MWE, and confirm that it is solved under version 0.22.

2 Likes

Note that this syntax didn’t exist in 0.21, so the error message is somewhat arbitrary.

1 Like

Sorry guys, I can’t reproduce it too! But the problem is fixed after upgrading my packages (and have a clean session).

1 Like