# How to define constraints on a collection of vectors separately?

**URL:** <https://discourse.julialang.org/t/how-to-define-constraints-on-a-collection-of-vectors-separately/82926>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, vector, modelling\
**Created:** [June 17, 2022, 4:05pm UTC](https://discourse.julialang.org/t/how-to-define-constraints-on-a-collection-of-vectors-separately/82926 "2022-06-17T16:05:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_12](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kevin\_12](https://discourse.julialang.org/u/Kevin_12)\
**Post date:** [June 17, 2022, 4:05pm UTC](https://discourse.julialang.org/t/how-to-define-constraints-on-a-collection-of-vectors-separately/82926/1 "2022-06-17T16:05:49Z")

</div>

The original problem is so larg. But, Suppose we have this small set of vectors

```julia
A = [[ [13, 48,99] , [71, 58, 99]],
        [[91, 59, 22], [94, 86, 22], [71, 58, 22], [19,55,22]],
        [[41, 79, 12], [23, 16, 12], [53, 28, 12]] ]

B =[99, 22, 12] # the last member of each above vector has a meaning

```

as `length(A) = 3`, I’d like to have three constraints similar the following:

```julia
c1 := x[13, 48,99] + x[71, 58, 99] >= 2

c2: = x[91, 59, 22]+ x[94, 86, 22]+ x[71, 58, 22]+ x[19,55,22] >=2
c3:= x[41, 79, 12]+ x[23, 16, 12]+ x[53, 28, 12] >=2

```

My try

```julia

@constraint(m, [b in B], sum( x[i] for i in A) >=2)

```

---

<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:** [June 18, 2022, 1:20am UTC](https://discourse.julialang.org/t/how-to-define-constraints-on-a-collection-of-vectors-separately/82926/2 "2022-06-18T01:20:10Z")

</div>

I guess you want something like

```julia
A = [
    [[13, 48,99] , [71, 58, 99]],
    [[91, 59, 22], [94, 86, 22], [71, 58, 22], [19,55,22]],
    [[41, 79, 12], [23, 16, 12], [53, 28, 12]],
]
B = [99, 22, 12]

# The set of all unique indices in A
S = unique(vcat(A...))

model = Model()
@variable(model, x[S])
@constraint(model, [b in B], sum(x[j] for j in S if j[end] == b) >= 2)

# or

model = Model()
@variable(model, x[S])
@constraint(model, [i in 1:length(B)], sum(x[j] for j in A[i]) >= 2)

```

---

<div class="post-metadata">

**Author:** ![Kevin\_12](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kevin\_12](https://discourse.julialang.org/u/Kevin_12)\
**Post date:** [June 18, 2022, 9:28am UTC](https://discourse.julialang.org/t/how-to-define-constraints-on-a-collection-of-vectors-separately/82926/3 "2022-06-18T09:28:15Z")

</div>

> [@Kevin\_12](#):
>
> `x[13, 48,99] + x[71, 58, 99] >= 2`

Thanks @odow One question that I have is that:

I was wondering if there is any difference between `x[13, 48,99] + x[71, 58, 99] >= 2` and `x[(13, 48,99)] + x[(71, 58, 99)] >= 2` for Julia?  
If so, do you know of any method to convert `x[(13, 48,99)] + x[(71, 58, 99)] >= 2` to `x[13, 48,99] + x[71, 58, 99] >= 2` in general case (works for anything similar)
