# Adding SOS1 constraint problem

**URL:** <https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [September 7, 2020, 5:20pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209 "2020-09-07T17:20:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 7, 2020, 5:20pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/1 "2020-09-07T17:20:57Z")

</div>

I am trying to add a simple SOS1 constraint, but getting the following error

MethodError: no method matching build\_constraint(::JuMP.var"#\_error#68"{Symbol}, ::Array{VariableRef,2}, ::SOS1)

for

```julia
            @constraint(
                premex,
                SOSa[t in TIME],
                [PRODAMOUNT[op_k, u_k, t] for u_k in ["BUHLER1", "BUHLER2"], (op_k, op) in _ORDER_PRODUCTs_ALL] in SOS1()
            )   

```

What I am doing wrong here?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 7, 2020, 5:45pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/2 "2020-09-07T17:45:27Z")

</div>

I am not sure.

The only official use I found of SOS1 is [this line inside the tests](https://github.com/jump-dev/JuMP.jl/blob/5023f145edd8f2dae6632494a403cf484d5b379d/test/constraint.jl#L546).

Could you post the full backtrace inside triple backticks here?

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 7, 2020, 6:35pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/3 "2020-09-07T18:35:31Z")

</div>

Let;'s forget SOS1(), the most important is to figure out how to create constraints for this:

X[i,j,k] , Int, \>= 0

Now: for every j and k, but if one i has value \> 0 then all others should be =0

How to write this constraint in julia / jump?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 7, 2020, 6:53pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/4 "2020-09-07T18:53:15Z")

</div>

If the variables are binary this is easy:

```julia
@constraint(model, sum(X[i, j, k] for i in I, j in J, k in K) <= 1)

```

If the variables are not binary then I do not know a simple and clean way to do it. Maybe SOS1 was your best choice.

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 7, 2020, 7:09pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/5 "2020-09-07T19:09:06Z")

</div>

Actually I solved it like this:

```julia
            @constraint(
                premex,
                BUHLER1_BUHLER2[op_k in keys(_ORDER_PRODUCTs_ALL), t in TIME],
                PRODAMOUNT[op_k, "BUHLER1", t] => {sum(PRODAMOUNT[op_k, "BUHLER2", t] for op_k in keys(_ORDER_PRODUCTs_ALL)) == 0}
            )   
            
            @constraint(
                premex,
                BUHLER2_BUHLER1[op_k in keys(_ORDER_PRODUCTs_ALL), t in TIME],
                PRODAMOUNT[op_k, "BUHLER2", t] => {sum(PRODAMOUNT[op_k, "BUHLER1", t] for op_k in keys(_ORDER_PRODUCTs_ALL)) == 0}
            )   

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 7, 2020, 7:20pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/6 "2020-09-07T19:20:46Z")

</div>

I… maybe I am wrong, but I am not entirely sure this works… what are the `{}` supposed to mean? Also, `=>` means pair creation, `1 => 2` gives a pair of 1 and 2, it does not mean that 1 should be greater-than-or-equal-to 2.

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 7, 2020, 7:36pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/7 "2020-09-07T19:36:08Z")

</div>

> [@sogrbilja](#):
>
> `=>`

these are Indicator-constraints: [Constraints · JuMP](https://jump.dev/JuMP.jl/v0.21.1/constraints/#Indicator-constraints-1)

So let’s analyse BUHLER1\_BUHLER2 constraint it says:

if PRODAMOUNT[op\_k, “BUHLER1”, t] \> 0 then every PRODAMOUNT[op\_k, “BUHLER2”, t] should be 0

that way we can manipulate (change) other values based on the value we want. Note that left from =\> should be Boolean, but luckily julia/jump treats all number != 0 as true (LUCKILY 😄 )

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 7, 2020, 7:47pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/8 "2020-09-07T19:47:27Z")

</div>

> [@sogrbilja](#):
>
> that way we can manipulate (change) other values based on the value we want. Note that left from =\> should be Boolean, but luckily julia/jump treats all number != 0 as true (LUCKILY 😄 )

Yes I was about to say this. The documentation says this is intended for binary variables. It would be good to confirm this works with the devs first.

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 7, 2020, 8:01pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/9 "2020-09-07T20:01:49Z")

</div>

Sure, but I am getting right results, when BUHLER1 is chosen there is no BUHLER2 at the same time. So I am happy (finally) with it.

---

<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:** [September 7, 2020, 9:40pm UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/10 "2020-09-07T21:40:05Z")

</div>

Your problem is that you are passing a matrix (the `::Array{VariableRef,2}`).

```nohighlight
julia> [i+j for i in 1:2, j in 1:2]
2×2 Array{Int64,2}:
 2 3
 3 4

julia> [i+j for i in 1:2 for j in 1:2]
4-element Array{Int64,1}:
 2
 3
 3
 4

```

You want something like

```nohighlight
@constraint(
    premex,
    SOSa[t in TIME],
    [
        PRODAMOUNT[op_k, u_k, t] 
        for u_k in ["BUHLER1", "BUHLER2"] 
        for (op_k, op) in _ORDER_PRODUCTs_ALL
    ] in SOS1()
)  

```

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 8, 2020, 5:57am UTC](https://discourse.julialang.org/t/adding-sos1-constraint-problem/46209/11 "2020-09-08T05:57:57Z")

</div>

Thanks, Oscar, that is exactly what I am looking for! Now I’ve learned that it is possible to nest for loops from your example.

Sorry, but I am learning Julia / Jump just to finish one project started in AMPL by my friend and we have to deliver the final product - a windows application with a user interface and this model and we are already late. So no time for learning, just google it and that is ti. 😄
