# SOS use on multidimensional arrays

**URL:** <https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282>\
**Category:** Optimization (Mathematical)\
**Created:** [May 16, 2019, 1:31pm UTC](https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282 "2019-05-16T13:31:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iharjunk](https://avatars.discourse-cdn.com/v4/letter/i/e0b2c6/32.png) [@iharjunk](https://discourse.julialang.org/u/iharjunk)\
**Post date:** [May 16, 2019, 1:31pm UTC](https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282/1 "2019-05-16T13:31:11Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [May 16, 2019, 2:13pm UTC](https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282/2 "2019-05-16T14:13:19Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 16, 2019, 2:25pm UTC](https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282/3 "2019-05-16T14:25:40Z")

</div>

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:

```nohighlight
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, :]` 🙂

p.s. You might want to read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757). It has some advice of formatting the code in questions and making a minimum working example.

---

<div class="post-metadata">

**Author:** ![iharjunk](https://avatars.discourse-cdn.com/v4/letter/i/e0b2c6/32.png) [@iharjunk](https://discourse.julialang.org/u/iharjunk)\
**Post date:** [May 16, 2019, 2:42pm UTC](https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282/4 "2019-05-16T14:42:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 16, 2019, 3:29pm UTC](https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282/5 "2019-05-16T15:29:30Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![iharjunk](https://avatars.discourse-cdn.com/v4/letter/i/e0b2c6/32.png) [@iharjunk](https://discourse.julialang.org/u/iharjunk)\
**Post date:** [May 17, 2019, 4:48am UTC](https://discourse.julialang.org/t/sos-use-on-multidimensional-arrays/24282/6 "2019-05-17T04:48:23Z")

</div>

Great, now working! Thank you!!!
