JuMP constraint MethodError: no method matching isless(::Int64, ::VariableRef)

Hey, I need your help!

What I am basically trying is to add a constraint to a JuMP matrix variable that no more than one element of a row can be a value larger than 0.

In other words only 1 element in the row can be a non Zero value.

@variable(ExModel, matrixVaribale[1:nSize,1:nSize] >= 0, Int)

for n = 1:nSize
    @constraint(ExModel, sum((matrixVariable[n,s] > 0) for s=1:nSize) <= 1)
end

However when I try to run my code I get the following error:

LoadError: MethodError: no method matching isless(::Int64, ::VariableRef)
...
in expression starting at ..\caseStudy.jl:48
<(::Int64, ::VariableRef) at operators.jl:268
>(::VariableRef, ::Int64) at operators.jl:294
macro expansion at rewrite.jl:227 [inlined]
macro expansion at macros.jl:416 [inlined]
top-level scope at caseStudy.jl:49

Any ideas on other ways to acomplish this?

It must have to do something with the way Julia interprets the JuMP variabale as a VariableRef instead of as an Int or Float

Thanks in advance

valenmgama

Note: I also get this error when trying to perform other operations on the variable such as %

I managed to find a work around by adding another variable:

@variable(ExModel, matrixVariable[1:nSize,1:nSize] >= 0, Int)
@variable(ExModel, relationsVariable[1:nSize,1:nSize], Bin)

for n = 1:nSize
    @constraint(ExModel,sum(relationsVariable[n,:] <= 1)
end

@constraint(ExModel, matrixVariable[:,:]  .<=  relationsVariable[:,:]*typemax(Int)

Still would love to hear if anybody out there has any other ideas.
valenmgama

See https://jump.dev/JuMP.jl/dev/constraints/#Special-Ordered-Sets-(SOS1-and-SOS2)-1

1 Like

Thanks! Just what was needed! For anyone else checking this thread my code in the end was:

@variable(ExModel, matrixVaribale[1:nSize,1:nSize] >= 0, Int)

for n = 1:nSize
    @constraint(ExModel, matrixVariable[n,:] in SOS1())
end
1 Like