SOS use on multidimensional arrays

Hello all!
I am trying to use the new way of SOS2 constraints (MOI.SOS2) but since I have a 3-dimensional variable (the last dimension should be part of the SOS2 set), existing examples are not sufficient. Any hints how to expand? Using the following code does not work

P = 10
Q = 5
@variable(m,0<=test[1:P,1:Q]<=1)
for p = 1:P
@constraint(m, test[p] in MOI.SOS2(collect(1:Q)))
end

and results in the error: ERROR: In @constraint(m, test[p] in MOI.SOS2(collect(1:Q))): unable to add the constraint because we don’t recognize test[1,1] as a valid JuMP function.

I would have expected that since in the example x[1:3] is referred to as x in the SOS2-constraint this would have been sufficient. I would not like to step into more inconvenient modeling approaches but think this should be straightforward and supported by MOI!

Thankful for all solutions!

This question needs more context, please provide a complete example.

In the meantime, you could try test[p, :] in ....

In your code, test is an Array{VariableRef, 2}. But test[p] just selects one element, not a vector. This isn’t specific to JuMP. See:

julia> x = rand(3, 3)
3×3 Array{Float64,2}:
 0.0576085  0.285097  0.125166
 0.701127   0.35993   0.462062
 0.727124   0.182186  0.378751

julia> x[1]
0.05760854662389847

As @leethargo says, you need a slice: test[p, :] :slight_smile:

p.s. You might want to read Please read: make it easier to help you. It has some advice of formatting the code in questions and making a minimum working example.

Thank you!

Unfortunately using test[p,:] results in …

Constraints of type MathOptInterface.VectorOfVariables-in-MathOptInterface.SOS2{Int64} are not supported by the solver and there are no bridges that can reformulate it into supported constraints.

Here the entire “dummy” problem…

model = Model(with_optimizer(Gurobi.Optimizer, env, OutputFlag=1))
P = 10
Q = 5
@variable(model,0<=test[1:P,1:Q]<=1)
for p = 1:P
@constraint(model, test[p,:] in MOI.SOS2(collect(1:Q)))
end
@objective(model, Max, sum(i*test[i,j] for i=1:P, j=1:Q))
optimize!(model)

My real problem is a very large MILP where I try to represent a working point of a unit i at each time t with a piecewise linear function using some lambda-variables, i.e.
level[i,t] = sum(lambda[i,t,p]*coeff[p] for p=1:pc)
This is needed to handle some nonlinearities and is possible to do via GAMS/AMPL but I would like to be able to formulate these more complex PWL in Julia.

This bug will be fixed in the next release of JuMP. For now, you need

@constraint(model, test[p,:] in MOI.SOS2(collect(1.0:Q)))
                                                  ^^ Note this

Great, now working! Thank you!!!